From fa8fb8d781feca156eb6c7aff6d9e2a84e93c388 Mon Sep 17 00:00:00 2001 From: Jan Hartigan Date: Wed, 7 Nov 2018 17:20:16 -0800 Subject: [PATCH] Version 1.3.0 (#7) * adding spaces around assignment operators * Removing the boards property on the game class * Removing boards property from docs * Avoiding recalculating possible moves for pieces that have already done the work * version bump --- checkers/board.py | 4 +++- checkers/game.py | 2 -- checkers/piece.py | 17 +++++++++++++++++ readme.md | 5 ++--- setup.py | 2 +- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/checkers/board.py b/checkers/board.py index 8a7c3f6..e9d0264 100644 --- a/checkers/board.py +++ b/checkers/board.py @@ -66,7 +66,7 @@ def switch_turn(self): def move_piece(self, move): self.searcher.get_piece_by_position(move[0]).move(move[1]) - self.pieces = sorted(self.pieces, key=lambda piece: piece.position if piece.position else 0) + self.pieces = sorted(self.pieces, key = lambda piece: piece.position if piece.position else 0) def is_valid_row_and_column(self, row, column): if row < 0 or row >= self.height: @@ -81,4 +81,6 @@ def __setattr__(self, name, value): super(Board, self).__setattr__(name, value) if name == 'pieces': + [piece.reset_for_new_board() for piece in self.pieces] + self.searcher.build(self) \ No newline at end of file diff --git a/checkers/game.py b/checkers/game.py index 685f157..c6140a3 100644 --- a/checkers/game.py +++ b/checkers/game.py @@ -4,7 +4,6 @@ class Game: def __init__(self): self.board = Board() - self.boards = [self.board]; self.moves = [] self.move_limit = 50 @@ -13,7 +12,6 @@ def move(self, move): raise ValueError('The provided move is not possible') self.board = self.board.create_new_board_from_move(move) - self.boards.append(self.board) self.moves.append(move) return self diff --git a/checkers/piece.py b/checkers/piece.py index ed6868c..4ef1a13 100644 --- a/checkers/piece.py +++ b/checkers/piece.py @@ -10,6 +10,11 @@ def __init__(self): self.position = None self.board = None self.capture_move_enemies = {} + self.reset_for_new_board() + + def reset_for_new_board(self): + self.possible_capture_moves = None + self.possible_positional_moves = None def is_movable(self): return (self.get_possible_capture_moves() or self.get_possible_positional_moves()) and not self.captured @@ -23,6 +28,12 @@ def move(self, new_position): self.king = self.king or self.is_on_enemy_home_row() def get_possible_capture_moves(self): + if self.possible_capture_moves == None: + self.possible_capture_moves = self.build_possible_capture_moves() + + return self.possible_capture_moves + + def build_possible_capture_moves(self): adjacent_enemy_positions = list(filter((lambda position: position in self.board.searcher.get_positions_by_player(self.other_player)), self.get_adjacent_positions())) capture_move_positions = [] @@ -48,6 +59,12 @@ def get_position_behind_enemy(self, enemy_piece): return self.board.position_layout.get(row_behind_enemy, {}).get(column_behind_enemy) def get_possible_positional_moves(self): + if self.possible_positional_moves == None: + self.possible_positional_moves = self.build_possible_positional_moves() + + return self.possible_positional_moves + + def build_possible_positional_moves(self): new_positions = list(filter((lambda position: self.board.position_is_open(position)), self.get_adjacent_positions())) return self.create_moves_from_new_positions(new_positions) diff --git a/readme.md b/readme.md index 3ae2781..6b14c1e 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ A Python3 library that you can use to play a game of checkers/draughts. This is just a set of classes that you can use in your code, it's not an interactive shell checkersgame. -- **Version:** 1.2.2 +- **Version:** 1.3.0 [![Build Status](https://travis-ci.org/ImparaAI/checkers.png?branch=master)](https://travis-ci.org/ImparaAI/checkers) @@ -52,10 +52,9 @@ Find out who won: game.get_winner() #None or 1 or 2 ``` -Review the board and move history: +Review the move history: ```python -game.boards #[Board, Board, ...] game.moves #[[int, int], [int, int], ...] ``` diff --git a/setup.py b/setup.py index 94922a3..5ddc5bb 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="imparaai-checkers", - version="1.2.2", + version="1.3.0", license='MIT', author="ImparaAI", author_email="author@example.com",