|
| 1 | +# https://www.facebook.com/hitesh.vishnoi.146/posts/342179847062773 |
| 2 | +# subscribe by Hitesh Vishnoi |
| 3 | +#Implementation of Two Player Tic-Tac-Toe game in Python. |
| 4 | + |
| 5 | +''' We will make the board using dictionary |
| 6 | + in which keys will be the location(i.e : top-left,mid-right,etc.) |
| 7 | + and initialliy it's values will be empty space and then after every move |
| 8 | + we will change the value according to player's choice of move. ''' |
| 9 | + |
| 10 | +theBoard = {'7': ' ' , '8': ' ' , '9': ' ' , |
| 11 | + '4': ' ' , '5': ' ' , '6': ' ' , |
| 12 | + '1': ' ' , '2': ' ' , '3': ' ' } |
| 13 | + |
| 14 | +board_keys = [] |
| 15 | + |
| 16 | +for key in theBoard: |
| 17 | + board_keys.append(key) |
| 18 | + |
| 19 | +''' We will have to print the updated board after every move in the game and |
| 20 | + thus we will make a function in which we'll define the printBoard function |
| 21 | + so that we can easily print the board everytime by calling this function. ''' |
| 22 | + |
| 23 | +def printBoard(board): |
| 24 | + print(board['7'] + '|' + board['8'] + '|' + board['9']) |
| 25 | + print('-+-+-') |
| 26 | + print(board['4'] + '|' + board['5'] + '|' + board['6']) |
| 27 | + print('-+-+-') |
| 28 | + print(board['1'] + '|' + board['2'] + '|' + board['3']) |
| 29 | + |
| 30 | +# Now we'll write the main function which has all the gameplay functionality. |
| 31 | +def game(): |
| 32 | + |
| 33 | + turn = 'X' |
| 34 | + count = 0 |
| 35 | + |
| 36 | + |
| 37 | + for i in range(10): |
| 38 | + printBoard(theBoard) |
| 39 | + print("It's your turn," + turn + ".Move to which place?") |
| 40 | + |
| 41 | + move = input() |
| 42 | + |
| 43 | + if theBoard[move] == ' ': |
| 44 | + theBoard[move] = turn |
| 45 | + count += 1 |
| 46 | + else: |
| 47 | + print("That place is already filled.\nMove to which place?") |
| 48 | + continue |
| 49 | + |
| 50 | + # Now we will check if player X or O has won,for every move after 5 moves. |
| 51 | + if count >= 5: |
| 52 | + if theBoard['7'] == theBoard['8'] == theBoard['9'] != ' ': # across the top |
| 53 | + printBoard(theBoard) |
| 54 | + print("\nGame Over.\n") |
| 55 | + print(" **** " +turn + " won. ****") |
| 56 | + break |
| 57 | + elif theBoard['4'] == theBoard['5'] == theBoard['6'] != ' ': # across the middle |
| 58 | + printBoard(theBoard) |
| 59 | + print("\nGame Over.\n") |
| 60 | + print(" **** " +turn + " won. ****") |
| 61 | + break |
| 62 | + elif theBoard['1'] == theBoard['2'] == theBoard['3'] != ' ': # across the bottom |
| 63 | + printBoard(theBoard) |
| 64 | + print("\nGame Over.\n") |
| 65 | + print(" **** " +turn + " won. ****") |
| 66 | + break |
| 67 | + elif theBoard['1'] == theBoard['4'] == theBoard['7'] != ' ': # down the left side |
| 68 | + printBoard(theBoard) |
| 69 | + print("\nGame Over.\n") |
| 70 | + print(" **** " +turn + " won. ****") |
| 71 | + break |
| 72 | + elif theBoard['2'] == theBoard['5'] == theBoard['8'] != ' ': # down the middle |
| 73 | + printBoard(theBoard) |
| 74 | + print("\nGame Over.\n") |
| 75 | + print(" **** " +turn + " won. ****") |
| 76 | + break |
| 77 | + elif theBoard['3'] == theBoard['6'] == theBoard['9'] != ' ': # down the right side |
| 78 | + printBoard(theBoard) |
| 79 | + print("\nGame Over.\n") |
| 80 | + print(" **** " +turn + " won. ****") |
| 81 | + break |
| 82 | + elif theBoard['7'] == theBoard['5'] == theBoard['3'] != ' ': # diagonal |
| 83 | + printBoard(theBoard) |
| 84 | + print("\nGame Over.\n") |
| 85 | + print(" **** " +turn + " won. ****") |
| 86 | + break |
| 87 | + elif theBoard['1'] == theBoard['5'] == theBoard['9'] != ' ': # diagonal |
| 88 | + printBoard(theBoard) |
| 89 | + print("\nGame Over.\n") |
| 90 | + print(" **** " +turn + " won. ****") |
| 91 | + break |
| 92 | + |
| 93 | + # If neither X nor O wins and the board is full, we'll declare the result as 'tie'. |
| 94 | + if count == 9: |
| 95 | + print("\nGame Over.\n") |
| 96 | + print("It's a Tie!!") |
| 97 | + |
| 98 | + # Now we have to change the player after every move. |
| 99 | + if turn =='X': |
| 100 | + turn = 'O' |
| 101 | + else: |
| 102 | + turn = 'X' |
| 103 | + |
| 104 | + # Now we will ask if player wants to restart the game or not. |
| 105 | + restart = input("Do want to play Again?(y/n)") |
| 106 | + if restart == "y" or restart == "Y": |
| 107 | + for key in board_keys: |
| 108 | + theBoard[key] = " " |
| 109 | + |
| 110 | + game() |
| 111 | + |
| 112 | +if __name__ == "__main__": |
| 113 | + game() |
0 commit comments