Skip to content

Commit

Permalink
Version 1.3.0 (#7)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
janhartigan authored Nov 8, 2018
1 parent 3955687 commit fa8fb8d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
4 changes: 3 additions & 1 deletion checkers/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
2 changes: 0 additions & 2 deletions checkers/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class Game:

def __init__(self):
self.board = Board()
self.boards = [self.board];
self.moves = []
self.move_limit = 50

Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions checkers/piece.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = []

Expand All @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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], ...]
```

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="imparaai-checkers",
version="1.2.2",
version="1.3.0",
license='MIT',
author="ImparaAI",
author_email="[email protected]",
Expand Down

0 comments on commit fa8fb8d

Please sign in to comment.