hi,so it happens that i studied physics back at the university many years ago,but i am beginning to fall in love with "python","HtML", "JAVA script",
"SQL" and finally "PYTHON".**😁 😁
Please understand that i am new at this,and trust me i am learning very fast, i will share my codes here with my steemit friends and we can all learn together.
FROM COMMAND PROMPT
Today my code is written in python language,and ill try as much as possible to write them in a simple code format.The code is about a "GAME"
We all played when we were kids, called "tic tac game"below is the corresponding codes, feel free to ask questions.
from excercise8_functions import check_winning_combinations, render_play_pos
*#above implies that we shall be importing another file into the present file called excercise8
fpl = input("Pick a player, X or O? :-> ")
if fpl == 'x':
spl = 'o'
else:
spl = 'x'
current_player = fpl
print("First to play is ", current_player)
fpl_pos = [] # frist player play positions bank
spl_pos = [] # second player play positions bank
while True:
play = input("Pick a position between 1 - 9 :-> ")
if not play: # check for empty positions
print("Please enter a correct value")
continue
if not play.isdigit():
print("Please enter a correct value")
continue
if int(play) > 9 or int(play) < 1:
print("Invalid position specified.")
continue
if play in fpl_pos or play in spl_pos:
print("Position is already taken.")
continue
if current_player == fpl: # to confirm if current player is the first player
fpl_pos.append(play)
if len(fpl_pos) >= 3:
win_resp = check_winning_combinations(fpl_pos)
if win_resp == True:
print("Player", fpl, "wins the game...")
break
current_player = spl # switch to the next player
elif current_player == spl: # to confirm if current player is the second player
spl_pos.append(play)
if len(spl_pos) >= 3:
win_resp = check_winning_combinations(spl_pos)
if win_resp == True:
print("Player", spl, "wins the game...")
break
current_player = fpl # switch to the next player
if len(fpl_pos) + len(spl_pos) == 9 :
print("game is a tie.")
break
render_play_pos([fpl, fpl_pos], [spl, spl_pos])
print("next to play is", current_player)
print("Game over, good bye..")
def check_winning_combinations(selected):
# ['1', '5', '9', '4']
WIN_COMBINATIONS = [
(1, 2, 3),
(4, 5, 6),
(7, 8, 9),
(1, 4, 7),
(2, 5, 8),
(3, 6, 9),
(1, 5, 9),
(3, 5, 7),
]
counter = 0
for wins in WIN_COMBINATIONS:
for plays in selected:
if int(plays) in wins:
counter += 1
if counter == 3:
break
if counter == 3:
break
else:
counter = 0
return counter == 3
def box_point(value):
tmp = str(value)
return tmp.center(5)
def horizontal_line():
return "" * 5 + "+" + ""*5 + "+" + "_" * 5
def render_play_pos(fpos, spos):
# ['o', ["2", '9']]
# ['x', ['1', '8']]
row = ''
for x in range(1, 10):
if str(x) in fpos[1]:
row += box_point(fpos[0])
elif str(x) in spos[1]:
row += box_point(spos[0])
else:
row += box_point(x)
if x % 3 != 0:
row += "|"
else:
row += '\n'
if x == 9:
row += box_point("") + "+" + box_point("") + "+"
else:
row += horizontal_line()
row += "\n"
print(row)
# + "|"
X | 8 | 7
_____+_____+______
9 | O | 7
_____+_____+______
9 | 8 | 7
+ +
if name == 'main':
render_play_pos([],[])
So did you find the code simple? learn to master string manipulation it makes it easy for you.
dont forget to drop your feedback.
Thanks for using eSteem!
Your post has been voted as a part of eSteem encouragement program. Keep up the good work!
Dear reader, Install Android, iOS Mobile app or Windows, Mac, Linux Surfer app, if you haven't already!
Learn more: https://esteem.app
Join our discord: https://discord.me/esteem
This post has been voted on by the SteemSTEM curation team and voting trail. It is elligible for support from @minnowbooster.
If you appreciate the work we are doing, then consider supporting our witness @stem.witness!
For additional information please join us on the SteemSTEM discord and to get to know the rest of the community!
Please consider using the steemstem.io app and/or including @steemstem in the list of beneficiaries of this post. This could yield a stronger support from SteemSTEM.