Python Forum
Please review and suggest some modifications about my TIC-TAC-TOE programme - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: Code Review (https://python-forum.io/forum-46.html)
+--- Thread: Please review and suggest some modifications about my TIC-TAC-TOE programme (/thread-26515.html)



Please review and suggest some modifications about my TIC-TAC-TOE programme - MohammedSohail - May-04-2020

#describing the board
board = [" ", " ", " ",
         " ", " ", " ",
         " ", " ", " "]

#making the board
def print_board():
    print(board[0] + '|' + board[1] + '|' + board[2])
    print('-+-+-')
    print(board[3] + '|' + board[4] + '|' + board[5])
    print('-+-+-')
    print(board[6] + '|' + board[7] + '|' + board[8])

#A counter to track the player's moves
count = 0
#main loop
while True:
    print_board()
    #PLAYER 1
    p1 = input('YOU ARE "X", Pick a number from 0-8 ')
    p1 = int(p1)
    board[p1] = 'X'
    count+=1
    print(count)
    #Rules of the game
    if board[0] == board[1] == board[2] != " ":
        print('player 1 wins the game')
        break
    elif board[3] == board[4] == board[5] != " ":
        print('player 1 wins the game')
        break
    elif board[6] == board[7] == board[8] != " ":
        print('player 1 wins the game')
        break
    elif board[0] == board[3] == board[6] != " ":
        print('player 1 wins the game')
        break
    elif board[1] == board[4] == board[7] != " ":
        print('player 1 wins the game')
        break
    elif board[2] == board[5] == board[8] != " ":
        print('player 1 wins the game')
        break
    elif board[0] == board[4] == board[8] != " ":
        print('player 1 wins the game')
        break
    elif board[2] == board[4] == board[6] != " ":
        print('player 1 wins the game')
        break
    print_board()
    #PLAYER 2
    p2 = input('YOU ARE "O", please choose a position 0-8 ')
    p2 = int(p2)
    board[p2] = 'O'
    count+=1
    print(count)
    if board[0] == board[1] == board[2] != " ":
        print('player 2 wins the game')
        break
    elif board[3] == board[4] == board[5] != " ":
        print('player 2 wins the game')
        break
    elif board[6] == board[7] == board[8] != " ":
        print('player 2 wins the game')
        break
    elif board[0] == board[3] == board[6] != " ":
        print('player 2 wins the game')
        break
    elif board[1] == board[4] == board[7] != " ":
        print('player 2 wins the game')
        break
    elif board[2] == board[5] == board[8] != " ":
        print('player 2 wins the game')
        break
    elif board[0] == board[4] == board[8] != " ":
        print('player 2 wins the game')
        break
    elif board[2] == board[4] == board[6] != " ":
        print('player 2 wins the game')
        break   
    #checking for ties
    if count >= 8:
        print('THE GAME ENDS WITH A TIE')
        break



RE: Please review and suggest some modifications about my TIC-TAC-TOE programme - michael1789 - May-04-2020

It didn't run for me. It printed the game board and that was it.


RE: Please review and suggest some modifications about my TIC-TAC-TOE programme - MohammedSohail - May-05-2020

No, It works i have tested it more than 10 times By the way this is my first game program and I have posted on a forum for the first time so, If there are some mistakes Please let me know.

Thank you.


RE: Please review and suggest some modifications about my TIC-TAC-TOE programme - pyzyx3qwerty - May-05-2020

(May-04-2020, 05:09 PM)michael1789 Wrote: It didn't run for me. It printed the game board and that was it.
It runs for me :/ Are you sure have ran it completely?
(May-05-2020, 04:45 AM)MohammedSohail Wrote: No, It works i have tested it more than 10 times By the way this is my first game program and I have posted on a forum for the first time so, If there are some mistakes Please let me know.

Thank you.
It's a fine code. I ran it and didn't come across any mistakes. I would have gone for it using built in functions, but your approach is fine