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


Messages In This Thread
Please review and suggest some modifications about my TIC-TAC-TOE programme - by MohammedSohail - May-04-2020, 03:22 PM

Forum Jump:

User Panel Messages

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