|
| 1 | +''' |
| 2 | +Exercise 26: Check Tic Tac Toe |
| 3 | + |
| 4 | +This exercise is Part 2 of 4 of the Tic Tac Toe exercise series. |
| 5 | +The other exercises are: Part 1, Part 3, and Part 4. |
| 6 | + |
| 7 | +As you may have guessed, we are trying to build up to a full |
| 8 | +tic-tac-toe board. However, this is significantly more than half |
| 9 | +an hour of coding, so weβre doing it in pieces. |
| 10 | + |
| 11 | +Today, we will simply focus on checking whether someone has WON |
| 12 | +a game of Tic Tac Toe, not worrying about how the moves were made. |
| 13 | + |
| 14 | +If a game of Tic Tac Toe is represented as a list of lists, like |
| 15 | +so: |
| 16 | + |
| 17 | +game = [[1, 2, 0], |
| 18 | + [2, 1, 0], |
| 19 | + [2, 1, 1]] |
| 20 | +where a 0 means an empty square, a 1 means that player 1 put their |
| 21 | +token in that space, and a 2 means that player 2 put their token |
| 22 | +in that space. |
| 23 | + |
| 24 | +Your task this week: given a 3 by 3 list of lists that represents |
| 25 | +a Tic Tac Toe game board, tell me whether anyone has won, and tell |
| 26 | +me which player won, if any. A Tic Tac Toe win is 3 in a row - |
| 27 | +either in a row, a column, or a diagonal. Donβt worry about the |
| 28 | +case where TWO people have won - assume that in every board there |
| 29 | +will only be one winner. |
| 30 | + |
| 31 | +''' |
| 32 | + |
| 33 | +# Solution |
| 34 | +def check_winner(input_list, size): |
| 35 | + """ |
| 36 | + Check the winner number in row, column, or diagonal direction. |
| 37 | + |
| 38 | + Arguments: |
| 39 | + input_list -- a two dimensional list for checking. |
| 40 | + size -- the length for winning. |
| 41 | + |
| 42 | + Returns: |
| 43 | + winner -- the winner player number, if no winner return None. |
| 44 | + |
| 45 | + """ |
| 46 | + # Check row |
| 47 | + winner = check_row_winner(input_list, size) |
| 48 | + if winner == None: |
| 49 | + # Transpose matrix |
| 50 | + input_list = transpose(input_list) |
| 51 | + # Check column |
| 52 | + winner = check_row_winner(input_list, size) |
| 53 | + if winner == None: |
| 54 | + # Check diagnal |
| 55 | + winner = check_diagonal_winner(input_list, size) |
| 56 | + if winner == None: |
| 57 | + winner = check_diagonal_winner(list(zip(*reversed(input_list))), size) |
| 58 | + return winner |
| 59 | + |
| 60 | +def transpose(input_list): |
| 61 | + """ |
| 62 | + Transpose a two dimensinal list. |
| 63 | + |
| 64 | + Arguments: |
| 65 | + input_list -- a two dimensional list for transposing. |
| 66 | + |
| 67 | + Returns: |
| 68 | + result -- transposed two dimensinal list. |
| 69 | + |
| 70 | + """ |
| 71 | + result = [] |
| 72 | + for i in range(len(input_list[0])): |
| 73 | + new_line = [new_list[i] for new_list in input_list] |
| 74 | + result.append(new_line) |
| 75 | + return result |
| 76 | + |
| 77 | +def check_row_winner(input_list, size): |
| 78 | + """ |
| 79 | + Check the winner number in row direction. |
| 80 | + |
| 81 | + Arguments: |
| 82 | + input_list -- a two dimensional list for checking. |
| 83 | + size -- the length for winning. |
| 84 | + |
| 85 | + Returns: |
| 86 | + winner -- the winner player number, if no winner return None. |
| 87 | + |
| 88 | + """ |
| 89 | + for line in input_list: |
| 90 | + count = 1 |
| 91 | + for idx, value in enumerate(line): |
| 92 | + if line[idx] == line[idx+1]: |
| 93 | + count += 1 |
| 94 | + else: |
| 95 | + count = 1 |
| 96 | + if count == size and value != 0: |
| 97 | + return value |
| 98 | + if idx == len(line)-size+1: |
| 99 | + break |
| 100 | + |
| 101 | +def check_diagonal_winner(input_list, size): |
| 102 | + """ |
| 103 | + Check the winner number in diagonal direction. |
| 104 | + |
| 105 | + Arguments: |
| 106 | + input_list -- a two dimensional list for checking. |
| 107 | + size -- the length for winning. |
| 108 | + |
| 109 | + Returns: |
| 110 | + winner -- the winner player number, if no winner return None. |
| 111 | + |
| 112 | + """ |
| 113 | + for row_idx, line in enumerate(input_list): |
| 114 | + winner = 0 |
| 115 | + try: |
| 116 | + list_for_check = [] |
| 117 | + for i in range(size): |
| 118 | + list_for_check.append(input_list[row_idx+i][i]) |
| 119 | + if list_for_check.count(list_for_check[0]) == size: |
| 120 | + if list_for_check[0] != 0: |
| 121 | + return list_for_check[0] |
| 122 | + except IndexError: |
| 123 | + winner = 0 |
| 124 | + |
| 125 | +def draw_board(size): |
| 126 | + """ |
| 127 | + Draw game boards in size of 'size'. |
| 128 | + |
| 129 | + Arguments: |
| 130 | + size -- the size of the board. |
| 131 | + |
| 132 | + """ |
| 133 | + h_element = ' ---' |
| 134 | + v_element = '| ' |
| 135 | + for i in range(size): |
| 136 | + print(h_element * (size)) |
| 137 | + print(v_element * (size+1)) |
| 138 | + print(h_element * (size)) |
| 139 | + |
| 140 | + |
| 141 | +def main(): |
| 142 | + #draw_board(int(input('Please input the size of board:'))) |
| 143 | + winner_is_2 = [[2, 2, 0], |
| 144 | + [2, 1, 0], |
| 145 | + [2, 1, 1]] |
| 146 | + |
| 147 | + winner_is_1 = [[1, 2, 0], |
| 148 | + [2, 1, 0], |
| 149 | + [2, 1, 1]] |
| 150 | + |
| 151 | + winner_is_also_1 = [[0, 1, 0], |
| 152 | + [2, 1, 0], |
| 153 | + [2, 1, 1]] |
| 154 | + |
| 155 | + no_winner = [[1, 2, 0], |
| 156 | + [2, 1, 0], |
| 157 | + [2, 1, 2]] |
| 158 | + |
| 159 | + also_no_winner = [[1, 2, 0], |
| 160 | + [2, 1, 0], |
| 161 | + [2, 1, 0]] |
| 162 | + |
| 163 | + also_no_winner1 = [[2, 1, 0, 1], |
| 164 | + [2, 1, 0, 2], |
| 165 | + [1, 0, 0, 1], |
| 166 | + [2, 1, 0, 2]] |
| 167 | + |
| 168 | + winner_is_2 = [[2, 1, 0, 0], |
| 169 | + [2, 1, 0, 2], |
| 170 | + [1, 0, 2, 1], |
| 171 | + [2, 2, 0, 2]] |
| 172 | + |
| 173 | + print(check_winner(winner_is_2, 3)) |
| 174 | + print(check_winner(winner_is_1, 3)) |
| 175 | + print(check_winner(winner_is_also_1, 3)) |
| 176 | + print(check_winner(no_winner, 3)) |
| 177 | + print(check_winner(also_no_winner, 3)) |
| 178 | + print(check_winner(also_no_winner1, 3)) |
| 179 | + print(check_winner(winner_is_2, 3)) |
| 180 | + |
| 181 | +if __name__ == "__main__": |
| 182 | + main() |
| 183 | + |
| 184 | +# Test Part |
| 185 | +# >>> %Run test.py |
| 186 | +# 2 |
| 187 | +# 1 |
| 188 | +# 1 |
| 189 | +# None |
| 190 | +# None |
| 191 | +# None |
| 192 | +# 2 |
0 commit comments