Python Forum
Please review and suggest some modifications about my TIC-TAC-TOE programme
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please review and suggest some modifications about my TIC-TAC-TOE programme
#1
#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
Reply
#2
It didn't run for me. It printed the game board and that was it.
Reply
#3
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.
Reply
#4
(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
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020