-
-
Save qwadratic/365f722497d498b36b8bacb8a7a80992 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import view | |
| from model import XOState | |
| def get_new_game_status(xostate): | |
| """TODO::убрать дублирующийся код""" | |
| for i in range(3): | |
| row = xostate.get_row(i) | |
| if row[0] == row[1] == row[2] and row[0] != 0: | |
| return row[0] | |
| for i in range(3): | |
| col = xostate.get_col(i) | |
| if col[0] == col[1] == col[2] and col[0] != 0: | |
| return col[0] | |
| for i in range(2): | |
| diag = xostate.get_diag(i) | |
| if diag[0] == diag[1] == diag[2] and diag[0] != 0: | |
| return diag[0] | |
| for i in range(3): | |
| row = xostate.get_row(i) | |
| for cell in row: | |
| if cell == 0: | |
| return 0 | |
| return 3 | |
| def game_loop(): | |
| """ | |
| TODO::* добавить проверки на корректность ввода | |
| TODO::* если ввод некорректен, повторить его не меняя игрока | |
| TODO::** спросить, хотят ли игроки сыграть еще раз (правильно структурировать код) | |
| TODO::*** переписать так чтобы можно было играть с компьютером + выбирать режим | |
| """ | |
| key_to_pos = { | |
| 'q': (0, 0), 'w': (0, 1), 'e': (0, 2), | |
| 'a': (1, 0), 's': (1, 1), 'd': (1, 2), | |
| 'z': (2, 0), 'x': (2, 1), 'c': (2, 2), | |
| } | |
| state = XOState() | |
| game_status = 0 | |
| while game_status == 0: | |
| view.clear_screen() | |
| view.print_field(state) | |
| turn = view.get_input(state) | |
| pos_row, pos_col = key_to_pos[turn] | |
| state.set_cell(pos_row, pos_col, state.current_player) | |
| state.switch_player() | |
| game_status = get_new_game_status(state) | |
| view.clear_screen() | |
| view.print_field(state) | |
| view.print_gameover(game_status) | |
| if __name__ == '__main__': | |
| game_loop() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class XOState: | |
| def __init__(self): | |
| self.field = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] | |
| self.current_player = 1 | |
| def set_cell(self, row, col, value): | |
| self.field[row][col] = value | |
| def get_row(self, n): | |
| return self.field[n] | |
| def get_col(self, n): | |
| return [self.field[i][n] for i in range(3)] | |
| def get_diag(self, n): | |
| if n == 0: | |
| return [self.field[i][i] for i in range(3)] | |
| if n == 1: | |
| return [self.field[i][2 - i] for i in range(3)] | |
| def switch_player(self): | |
| if self.current_player == 1: | |
| self.current_player = 2 | |
| elif self.current_player == 2: | |
| self.current_player = 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| вывести на экран: | |
| игровое поле | |
| состояние игры | |
| запросить ввод текущего игрока | |
| если надо, сообщение об ошибке | |
| o . . | |
| . . x | |
| . . . | |
| x|o > q|w|e|a|s|d|z|x|c | |
| x|o победил! | |
| ничья! | |
| """ | |
| import os | |
| import sys | |
| def print_field(xostate): | |
| for i in range(3): | |
| row = xostate.get_row(i) | |
| for cell in row: | |
| if cell == 0: | |
| print('.', end=' ') | |
| if cell == 1: | |
| print('x', end=' ') | |
| if cell == 2: | |
| print('o', end=' ') | |
| print() | |
| def get_input(xostate): | |
| if xostate.current_player == 1: | |
| print('x > ', end='') | |
| if xostate.current_player == 2: | |
| print('o > ', end='') | |
| return input() | |
| def print_gameover(game_result): | |
| if game_result == 1: | |
| print('x победил!') | |
| if game_result == 2: | |
| print('o победил!') | |
| if game_result == 3: | |
| print('ничья!') | |
| input() | |
| def clear_screen(): | |
| if sys.platform in ['linux', 'darwin']: | |
| os.system('clear') | |
| if sys.platform == 'win32': | |
| os.system('cls') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment