|
| 1 | +''' |
| 2 | +Exercise 27: Tic Tac Toe Draw |
| 3 | +
|
| 4 | +In a previous exercise we explored the idea of using a list of lists |
| 5 | +as a “data structure” to store information about a tic tac toe game. |
| 6 | +In a tic tac toe game, the “game server” needs to know where the Xs |
| 7 | +and Os are in the board, to know whether player 1 or player 2 (or |
| 8 | +whoever is X and O won). |
| 9 | +
|
| 10 | +There has also been an exercise about drawing the actual tic tac toe |
| 11 | +gameboard using text characters. |
| 12 | +
|
| 13 | +The next logical step is to deal with handling user input. When a |
| 14 | +player (say player 1, who is X) wants to place an X on the screen, |
| 15 | +they can’t just click on a terminal. So we are going to approximate |
| 16 | +this clicking simply by asking the user for a coordinate of where |
| 17 | +they want to place their piece. |
| 18 | +
|
| 19 | +As a reminder, our tic tac toe game is really a list of lists. The |
| 20 | +game starts out with an empty game board like this: |
| 21 | +
|
| 22 | +game = [[0, 0, 0], |
| 23 | + [0, 0, 0], |
| 24 | + [0, 0, 0]] |
| 25 | +The computer asks Player 1 (X) what their move is (in the format row, |
| 26 | +col), and say they type 1,3. Then the game would print out |
| 27 | +
|
| 28 | +game = [[0, 0, X], |
| 29 | + [0, 0, 0], |
| 30 | + [0, 0, 0]] |
| 31 | +And ask Player 2 for their move, printing an O in that place. |
| 32 | +
|
| 33 | +Things to note: |
| 34 | +
|
| 35 | +For this exercise, assume that player 1 (the first player to move) will |
| 36 | +always be X and player 2 (the second player) will always be O. |
| 37 | +Notice how in the example I gave coordinates for where I want to move |
| 38 | +starting from (1, 1) instead of (0, 0). To people who don’t program, |
| 39 | +starting to count at 0 is a strange concept, so it is better for the |
| 40 | +user experience if the row counts and column counts start at 1. This is |
| 41 | +not required, but whichever way you choose to implement this, it should |
| 42 | +be explained to the player. |
| 43 | +Ask the user to enter coordinates in the form “row,col” - a number, then |
| 44 | +a comma, then a number. Then you can use your Python skills to figure out |
| 45 | +which row and column they want their piece to be in. |
| 46 | +Don’t worry about checking whether someone won the game, but if a player |
| 47 | +tries to put a piece in a game position where there already is another |
| 48 | +piece, do not allow the piece to go there. |
| 49 | +
|
| 50 | +Bonus: |
| 51 | +For the “standard” exercise, don’t worry about “ending” the game - no |
| 52 | +need to keep track of how many squares are full. In a bonus version, keep |
| 53 | +track of how many squares are full and automatically stop asking for |
| 54 | +moves when there are no more valid moves. |
| 55 | +
|
| 56 | +''' |
| 57 | + |
| 58 | +# Solution |
| 59 | +def check_winner(input_list, size): |
| 60 | + """ |
| 61 | + Check the winner number in row, column, or diagonal direction. |
| 62 | +
|
| 63 | + Arguments: |
| 64 | + input_list -- a two dimensional list for checking. |
| 65 | + size -- the length for winning. |
| 66 | + |
| 67 | + Returns: |
| 68 | + winner -- the winner player number, if no winner return None. |
| 69 | + |
| 70 | + """ |
| 71 | + # Check row |
| 72 | + winner = check_row_winner(input_list, size) |
| 73 | + if winner == None: |
| 74 | + # Transpose matrix |
| 75 | + input_list = transpose(input_list) |
| 76 | + # Check column |
| 77 | + winner = check_row_winner(input_list, size) |
| 78 | + if winner == None: |
| 79 | + # Check diagnal |
| 80 | + winner = check_diagonal_winner(input_list, size) |
| 81 | + if winner == None: |
| 82 | + winner = check_diagonal_winner(list(zip(*reversed(input_list))), size) |
| 83 | + return winner |
| 84 | + |
| 85 | +def transpose(input_list): |
| 86 | + """ |
| 87 | + Transpose a two dimensinal list. |
| 88 | +
|
| 89 | + Arguments: |
| 90 | + input_list -- a two dimensional list for transposing. |
| 91 | + |
| 92 | + Returns: |
| 93 | + result -- transposed two dimensinal list. |
| 94 | + |
| 95 | + """ |
| 96 | + result = [] |
| 97 | + for i in range(len(input_list[0])): |
| 98 | + new_line = [new_list[i] for new_list in input_list] |
| 99 | + result.append(new_line) |
| 100 | + return result |
| 101 | + |
| 102 | +def check_row_winner(input_list, size): |
| 103 | + """ |
| 104 | + Check the winner number in row direction. |
| 105 | +
|
| 106 | + Arguments: |
| 107 | + input_list -- a two dimensional list for checking. |
| 108 | + size -- the length for winning. |
| 109 | + |
| 110 | + Returns: |
| 111 | + winner -- the winner player number, if no winner return None. |
| 112 | + |
| 113 | + """ |
| 114 | + for line in input_list: |
| 115 | + count = 1 |
| 116 | + for idx, value in enumerate(line): |
| 117 | + if line[idx] == line[idx+1]: |
| 118 | + count += 1 |
| 119 | + else: |
| 120 | + count = 1 |
| 121 | + if count == size and value != 0: |
| 122 | + return value |
| 123 | + if idx == len(line)-size+1: |
| 124 | + break |
| 125 | + |
| 126 | +def check_diagonal_winner(input_list, size): |
| 127 | + """ |
| 128 | + Check the winner number in diagonal direction. |
| 129 | +
|
| 130 | + Arguments: |
| 131 | + input_list -- a two dimensional list for checking. |
| 132 | + size -- the length for winning. |
| 133 | + |
| 134 | + Returns: |
| 135 | + winner -- the winner player number, if no winner return None. |
| 136 | + |
| 137 | + """ |
| 138 | + for row_idx, line in enumerate(input_list): |
| 139 | + winner = 0 |
| 140 | + try: |
| 141 | + list_for_check = [] |
| 142 | + for i in range(size): |
| 143 | + list_for_check.append(input_list[row_idx+i][i]) |
| 144 | + if list_for_check.count(list_for_check[0]) == size: |
| 145 | + if list_for_check[0] != 0: |
| 146 | + return list_for_check[0] |
| 147 | + except IndexError: |
| 148 | + winner = 0 |
| 149 | + |
| 150 | +def draw_board(size): |
| 151 | + """ |
| 152 | + Draw game boards in size of 'size'. |
| 153 | +
|
| 154 | + Arguments: |
| 155 | + size -- the size of the board. |
| 156 | + |
| 157 | + """ |
| 158 | + h_element = ' ---' |
| 159 | + v_element = '| ' |
| 160 | + for i in range(size): |
| 161 | + print(h_element * (size)) |
| 162 | + print(v_element * (size+1)) |
| 163 | + print(h_element * (size)) |
| 164 | + |
| 165 | +def draw_turn(row, column, input_list, user): |
| 166 | + """ |
| 167 | + Draw the game board after user typing a choice. |
| 168 | +
|
| 169 | + Arguments: |
| 170 | + row -- the row index. |
| 171 | + column -- the column index. |
| 172 | + input_list -- a two dimensional list for game board. |
| 173 | + user -- the user who type the choice |
| 174 | + |
| 175 | + Returns: |
| 176 | + input_list -- a two dimensional list for game board after changed. |
| 177 | + |
| 178 | + """ |
| 179 | + mark_dict = {'player1':'X', 'player2':'O'} |
| 180 | + input_list[row-1][column-1] = mark_dict[user] |
| 181 | + return input_list |
| 182 | + |
| 183 | +def main(): |
| 184 | + print('Welcome to the game!') |
| 185 | + user1 = input("Player 1's name:") |
| 186 | + user2 = input("Player 2's name:") |
| 187 | + info_dict = {user1:'player1', user2:'player2', 'round':1} |
| 188 | + input_list = [['0','0','0'],['0','0','0'],['0','0','0']] |
| 189 | + while True: |
| 190 | + row, column = input("Round {}, {}'s turn:".format(info_dict['round'], user1)).split() |
| 191 | + input_list = draw_turn(int(row), int(column), input_list, info_dict[user1]) |
| 192 | + print(input_list) |
| 193 | + row, column = input("Round {}, {}'s turn:".format(info_dict['round'], user2)).split() |
| 194 | + input_list = draw_turn(int(row), int(column), input_list, info_dict[user2]) |
| 195 | + print(input_list) |
| 196 | + info_dict['round'] += 1 |
| 197 | + |
| 198 | +if __name__ == "__main__": |
| 199 | + main() |
0 commit comments