Thoughts and Profiling avatar

Python : writing codes: A Tic Tac Game

diodio

Published: 31 Jan 2020 โ€บ Updated: 31 Jan 2020Python : writing codes: A Tic Tac Game

Python : writing codes: A Tic Tac Game

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.

ml90t8.png
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..")

#EXCERCISE8

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.

Leave Python : writing codes: A Tic Tac Game to:

Written by

Art | Photography | Business | Startup Bootstrap Firms | Entrepreneurship | Cryptocurency | News

Read more #esteem posts


Best Posts From Thoughts and Profiling

We have not curated any of diodio's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Thoughts and Profiling