|
| 1 | +from IPython.display import clear_output |
| 2 | + |
| 3 | +def display_board(board): |
| 4 | + clear_output() # Remember, this only works in jupyter! |
| 5 | + |
| 6 | + print(' | |') |
| 7 | + print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9]) |
| 8 | + print(' | |') |
| 9 | + print('-----------') |
| 10 | + print(' | |') |
| 11 | + print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6]) |
| 12 | + print(' | |') |
| 13 | + print('-----------') |
| 14 | + print(' | |') |
| 15 | + print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3]) |
| 16 | + print(' | |') |
| 17 | + |
| 18 | +test_board = ['#','X','O','X','O','X','O','X','O','X'] |
| 19 | +display_board(test_board) |
| 20 | + |
| 21 | +def player_input(): |
| 22 | + marker = '' |
| 23 | + |
| 24 | + while not (marker == 'X' or marker == 'O'): |
| 25 | + marker = input('Player 1: Do you want to be X or O? ').upper() |
| 26 | + |
| 27 | + if marker == 'X': |
| 28 | + return ('X', 'O') |
| 29 | + else: |
| 30 | + return ('O', 'X') |
| 31 | + |
| 32 | +player_input() |
| 33 | +def place_marker(board, marker, position): |
| 34 | + board[position] = marker |
| 35 | + |
| 36 | +place_marker(test_board,'$',8) |
| 37 | +display_board(test_board) |
| 38 | + |
| 39 | +def win_check(board,mark): |
| 40 | + |
| 41 | + return ((board[7] == mark and board[8] == mark and board[9] == mark) or # across the top |
| 42 | + (board[4] == mark and board[5] == mark and board[6] == mark) or # across the middle |
| 43 | + (board[1] == mark and board[2] == mark and board[3] == mark) or # across the bottom |
| 44 | + (board[7] == mark and board[4] == mark and board[1] == mark) or # down the middle |
| 45 | + (board[8] == mark and board[5] == mark and board[2] == mark) or # down the middle |
| 46 | + (board[9] == mark and board[6] == mark and board[3] == mark) or # down the right side |
| 47 | + (board[7] == mark and board[5] == mark and board[3] == mark) or # diagonal |
| 48 | + (board[9] == mark and board[5] == mark and board[1] == mark)) # diagonal |
| 49 | + |
| 50 | +win_check(test_board,'X') |
| 51 | + |
| 52 | +import random |
| 53 | + |
| 54 | +def choose_first(): |
| 55 | + if random.randint(0, 1) == 0: |
| 56 | + return 'Player 2' |
| 57 | + else: |
| 58 | + return 'Player 1' |
| 59 | + |
| 60 | +def space_check(board, position): |
| 61 | + |
| 62 | + return board[position] == ' ' |
| 63 | + |
| 64 | +def full_board_check(board): |
| 65 | + for i in range(1,10): |
| 66 | + if space_check(board, i): |
| 67 | + return False |
| 68 | + return True |
| 69 | + |
| 70 | +def player_choice(board): |
| 71 | + position = 0 |
| 72 | + |
| 73 | + while position not in [1,2,3,4,5,6,7,8,9] or not space_check(board, position): |
| 74 | + position = int(input('Choose your next position: (1-9) ')) |
| 75 | + |
| 76 | + return position |
| 77 | + |
| 78 | +def replay(): |
| 79 | + |
| 80 | + return input('Do you want to play again? Enter Yes or No: ').lower().startswith('y') |
| 81 | + |
| 82 | +print('Welcome to Tic Tac Toe!') |
| 83 | + |
| 84 | +while True: |
| 85 | + # Reset the board |
| 86 | + theBoard = [' '] * 10 |
| 87 | + player1_marker, player2_marker = player_input() |
| 88 | + turn = choose_first() |
| 89 | + print(turn + ' will go first.') |
| 90 | + |
| 91 | + play_game = input('Are you ready to play? Enter Yes or No.') |
| 92 | + |
| 93 | + if play_game.lower()[0] == 'y': |
| 94 | + game_on = True |
| 95 | + else: |
| 96 | + game_on = False |
| 97 | + |
| 98 | + while game_on: |
| 99 | + if turn == 'Player 1': |
| 100 | + # Player1's turn. |
| 101 | + |
| 102 | + display_board(theBoard) |
| 103 | + position = player_choice(theBoard) |
| 104 | + place_marker(theBoard, player1_marker, position) |
| 105 | + |
| 106 | + if win_check(theBoard, player1_marker): |
| 107 | + display_board(theBoard) |
| 108 | + print('Congratulations! You have won the game!') |
| 109 | + game_on = False |
| 110 | + else: |
| 111 | + if full_board_check(theBoard): |
| 112 | + display_board(theBoard) |
| 113 | + print('The game is a draw!') |
| 114 | + break |
| 115 | + else: |
| 116 | + turn = 'Player 2' |
| 117 | + |
| 118 | + else: |
| 119 | + # Player2's turn. |
| 120 | + |
| 121 | + display_board(theBoard) |
| 122 | + position = player_choice(theBoard) |
| 123 | + place_marker(theBoard, player2_marker, position) |
| 124 | + |
| 125 | + if win_check(theBoard, player2_marker): |
| 126 | + display_board(theBoard) |
| 127 | + print('Player 2 has won!') |
| 128 | + game_on = False |
| 129 | + else: |
| 130 | + if full_board_check(theBoard): |
| 131 | + display_board(theBoard) |
| 132 | + print('The game is a draw!') |
| 133 | + break |
| 134 | + else: |
| 135 | + turn = 'Player 1' |
| 136 | + |
| 137 | + if not replay(): |
| 138 | + break |
| 139 | + |
0 commit comments