def _reached_end_black(self, player): """Check to see if black player has moved a piece to end of the board.""" if self.board.get(Point(3, 1)) == player or \ self.board.get(Point(3, 2)) == player or \ self.board.get(Point(3, 3)) == player: return True return False
def _reached_end_white(self, player): """Check to see if white player has moved a piece to end of the board.""" if self.board.get(Point(1, 1)) == player or \ self.board.get(Point(1, 2)) == player or \ self.board.get(Point(1, 3)) == player: return True return False
def legal_moves(self): moves = [] for row_1 in ROWS: for col_1 in COLS: #For each point, also loop through every other point as a possible move for row_2 in ROWS: for col_2 in COLS: move = Move(Point(row_1, col_1), Point(row_2, col_2)) if self.is_valid_move(move): moves.append(move) return moves
def point_from_coords(text): # The col name is the first character col_name = text[0] # The row number is the second character row = int(text[1]) # Return point where the col number is defined from the column names set return Point(row, COL_NAMES.index(col_name) + 1)
def _has_3_in_a_row(self, player): # Vertical for col in COLS: if all(self.board.get(Point(row, col)) == player for row in ROWS): return True # Horizontal for row in ROWS: if all(self.board.get(Point(row, col)) == player for col in COLS): return True # Diagonal UL to LR if all(self.board.get(p) == player for p in DIAG_1): return True # Diagonal UR to LL if all(self.board.get(p) == player for p in DIAG_2): return True return False
def legal_moves(self): moves = [] for row in ROWS: for col in COLS: move = Move(Point(row, col)) if self.is_valid_move(move): moves.append(move) return moves
def print_board(board): """ 盤面全体の状況を表示 """ for row in range(board.num_rows, 0, -1): bump = " " if row <= 9 else "" # 二桁の行番号になったときのための空白 line = [] for col in range(1, board.num_cols + 1): stone = board.get(Point(row=row, col=col)) # 全ての点の打石状況を確認 line.append(STONE_TO_CHAR[stone]) print('%s%d %s' % (bump, row, ''.join(line))) # 空白,行番号,行の石 print(' ' + ''.join(COLS[:board.num_cols])) # 列記号,ABCD...
def is_over(self): if self._has_3_in_a_row(Player.x): return True if self._has_3_in_a_row(Player.o): return True if all( self.board.get(Point(row, col)) is not None for row in ROWS for col in COLS): return True return False
def _has_3_in_a_row(self, player): for col in COLS: if all(self.board.get(Point(row, col)) == player for row in ROWS): return True for row in ROWS: if all(self.board.get(Point(row, col)) == player for col in COLS): return True if self.board.get(Point(1, 1)) == player and \ self.board.get(Point(2, 2)) == player and \ self.board.get(Point(3, 3)) == player: return True if self.board.get(Point(1, 3)) == player and \ self.board.get(Point(2, 2)) == player and \ self.board.get(Point(3, 1)) == player: return True
def is_over(self): # If Player x has 3 in a row, the game ends if self._has_3_in_a_row(Player.x): return True # If Player o has 3 in a row, the game ends if self._has_3_in_a_row(Player.o): return True # If no player has won but all the points are occupied, the game ends if all( self.board.get(Point(row, col)) is not None for row in ROWS for col in COLS): return True # False if we can keep playing return False
def new_game(cls): board = Board() # Initialize starting Hexapawn pieces: board.place(Player.o, Point(1, 1)) board.place(Player.o, Point(1, 2)) board.place(Player.o, Point(1, 3)) board.place(Player.x, Point(3, 2)) board.place(Player.x, Point(3, 3)) board.place(Player.x, Point(3, 1)) return GameState(board, Player.x, None)
def _has_3_in_a_row(self, player): # Vertical for col in COLS: if all(self.board.get(Point(row, col)) == player for row in ROWS): return True # Horizontal for row in ROWS: if all(self.board.get(Point(row, col)) == player for col in COLS): return True # Diagonal UL to LR if self.board.get(Point(1, 1)) == player and \ self.board.get(Point(2, 2)) == player and \ self.board.get(Point(3, 3)) == player: return True # Diagonal UR to LL if self.board.get(Point(1, 3)) == player and \ self.board.get(Point(2, 2)) == player and \ self.board.get(Point(3, 1)) == player: return True return False
def print_board(board): # Print first line print(' A B C') # Fer each row for row in (1, 2, 3): # Store pieces of this row in a set pieces = [] # For each column for col in (1, 2, 3): # Get the piece in this position if any piece = board.get(Point(row, col)) # Set a different piece depending on the player that holds this # position if piece == Player.x: pieces.append('X') elif piece == Player.o: pieces.append('O') else: pieces.append(' ') # Print the row joining positions with '|' print('%d %s' % (row, ' | '.join(pieces)))
def point_from_coords(coords): """ 人間の入力をBoardの座標に変換 ex. C3 -> (3, 3) """ col = COLS.index(coords[0]) + 1 row = int(coords[1:]) return Point(row=row, col=col)
__all__ = [ 'Board', 'GameState', 'Move', ] class IllegalMoveError(Exception): pass BOARD_SIZE = 3 ROWS = tuple(range(1, BOARD_SIZE + 1)) COLS = tuple(range(1, BOARD_SIZE + 1)) # Top left to lower right diagonal DIAG1 = (Point(1, 1), Point(2, 2), Point(3, 3)) # Top right to lower left diagonal DIAG2 = (Point(1, 3), Point(2, 2), Point(3, 1)) class Board: def __init__(self): self._grid = {} def place(self, player, point): assert self.is_on_grid(point) assert self._grid.get(point) is None self._grid[point] = player @staticmethod def is_on_grid(point):
import copy from dlgo.ttt.ttttypes import Player, Point __all__ = [ "Board", "GameState", "Move", ] BOARD_SIZE = 3 ROWS = tuple(range(1, BOARD_SIZE + 1)) COLS = tuple(range(1, BOARD_SIZE + 1)) # top left to bottom right diagonal DIAG_1 = (Point(1, 1), Point(2, 2), Point(3, 3)) # top right to bottom left diagonal DIAG_2 = (Point(1, 3), Point(2, 2), Point(3, 1)) class Move: def __init__(self, point: Point) -> None: self.point = point class Board: def __init__(self) -> None: self._grid = {} def place(self, player: Player, point: Point): assert self.is_on_grid(point) assert self._grid.get(point) is None self._grid[point] = player
def is_lose(self, player): if self.board.get(Point(1,1)) == player.other and \ self.board.get(Point(1,2)) == player: print('1,1-1,2') return True elif self.board.get(Point(1,1)) == player.other and \ self.board.get(Point(2,1)) == player: print('1,1-2,1') return True elif self.board.get(Point(1,3)) == player.other and \ self.board.get(Point(1,2)) == player: print('1,3-1,2') return True elif self.board.get(Point(1,3)) == player.other and \ self.board.get(Point(2,3)) == player: print('1,3-2,3') return True elif self.board.get(Point(3,1)) == player.other and \ self.board.get(Point(2,1)) == player: print('3,1-2,1') return True elif self.board.get(Point(3,1)) == player.other and \ self.board.get(Point(3,2)) == player: print('3,1-3,2') return True elif self.board.get(Point(3,3)) == player.other and \ self.board.get(Point(2,3)) == player: print('3,3-2,3') return True elif self.board.get(Point(3,3)) == player.other and \ self.board.get(Point(3,2)) == player: print('3,3-3,2') return True