Skip to content

Commit

Permalink
Merge pull request #29 from VincentAuriau/test
Browse files Browse the repository at this point in the history
DOC: Add +/- completed documentation
  • Loading branch information
VincentAuriau authored Nov 1, 2023
2 parents 9f52561 + 12e3466 commit ea8d168
Show file tree
Hide file tree
Showing 20 changed files with 590 additions and 153 deletions.
6 changes: 6 additions & 0 deletions pyalapin/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""
pyalapin, chess python package.
"""

__version__ = "0.0.1"
__author__ = "Vincent Auriau"
Empty file added pyalapin/engine/__init__.py
Empty file.
File renamed without changes.
124 changes: 102 additions & 22 deletions python/engine/engine.py → pyalapin/engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

import copy

from engine.move import Move
from player.player import Player, AIRandomPlayer
from player.ai_player import EasyAIPlayer
from player.my_player import MyPlayer
import engine.material as material
from pyalapin.engine.move import Move
from pyalapin.player.player import Player, AIRandomPlayer
from pyalapin.player.ai_player import EasyAIPlayer
from pyalapin.player.my_player import MyPlayer
import pyalapin.engine.material as material


class Color:
Expand Down Expand Up @@ -292,7 +292,6 @@ def is_threatened(
return True

elif piece_to_check is None:
print("def")
keep_going = True
x_to_check += i
y_to_check += j
Expand Down Expand Up @@ -414,10 +413,9 @@ def __init__(self, empty_init=False):
True if you want to start from an existing board.
"""
if not empty_init:
self.board = None
self.white_king, self.black_king, self.all_material = self._reset_board()

def deepcopy(self, memodict={}):
def deepcopy(self, light=True):
"""Method to create an uncorrelated clone of the board.
Returns
Expand All @@ -428,12 +426,21 @@ def deepcopy(self, memodict={}):
copied_object = Board(empty_init=True)
board = [[Cell(i, j, None) for j in range(8)] for i in range(8)]
copied_object.board = board
copied_material = self.deep_copy_material()
white_king = copied_material["white"]["alive"]["king"][0]
black_king = copied_material["black"]["alive"]["king"][0]
if light:
copied_material = self.light_deep_copy_material()
else:
copied_material = self.deep_copy_material()

assert (
len(copied_material["black"]["alive"]["king"]) > 0
), "Black king is dead ?"
assert (
len(copied_material["white"]["alive"]["king"]) > 0
), "White king is dead ?"
copied_object.white_king = copied_material["white"]["alive"]["king"][0]
copied_object.black_king = copied_material["black"]["alive"]["king"][0]
copied_object.all_material = copied_material
copied_object.white_king = white_king
copied_object.black_king = black_king

for piece_list in copied_material["white"]["alive"].values():
for piece in piece_list:
copied_object.get_cell(piece.x, piece.y).set_piece(piece)
Expand All @@ -443,6 +450,63 @@ def deepcopy(self, memodict={}):

return copied_object

def light_deep_copy_material(self):
"""Method to create an uncorrelated clone of all the pieces on the board. Light version
where only alive pieces are returned.
Returns
-------
dict of Pieces
Exact copy of self.all_material.
"""
material = {
"white": {
"alive": {
"pawn": [],
"knight": [],
"bishop": [],
"rook": [],
"queen": [],
"king": [],
},
"killed": {
"pawn": [],
"knight": [],
"bishop": [],
"rook": [],
"queen": [],
"king": [],
},
},
"black": {
"alive": {
"pawn": [],
"knight": [],
"bishop": [],
"rook": [],
"queen": [],
"king": [],
},
"killed": {
"pawn": [],
"knight": [],
"bishop": [],
"rook": [],
"queen": [],
"king": [],
},
},
}

for color in ["white", "black"]:
for status in ["alive"]:
for piece_type in ["pawn", "knight", "bishop", "rook", "queen", "king"]:
for piece in self.all_material[color][status][piece_type]:
material[color][status][piece_type].append(
piece.piece_deepcopy()
)
return material

def deep_copy_material(self):
"""Method to create an uncorrelated clone of all the pieces on the board, killed and not killed.
Expand Down Expand Up @@ -499,7 +563,7 @@ def deep_copy_material(self):
)
return material

def __deepcopy__(self, memodict={}):
def _deepcopy__(self, memodict={}):
"""Method to create an uncorrelated clone of the board.
Returns
Expand Down Expand Up @@ -904,7 +968,7 @@ class Game:

game_status = []

def __init__(self, automatic_draw=True, ai=False):
def __init__(self, player1=None, player2=None, automatic_draw=True, ai=False):
"""Initialization of the cell.
Parameters
Expand All @@ -914,14 +978,30 @@ def __init__(self, automatic_draw=True, ai=False):
ai : bool
Whether or not to play with AI. Is set to True, AI will play black pieces.
"""
self.player1 = Player(True)
self.ai = ai
if ai:
# self.player2 = AIRandomPlayer(False)
self.player2 = EasyAIPlayer(False)
# self.player2 = MyPlayer(white_side=False, path_to_model="./test1")

# If ai = True and both players are None, AI plays by default black pieces
if player2 is None:
if ai:
self.player2 = EasyAIPlayer(False)
else:
self.player2 = Player(False)

if player1 is None:
self.player1 = Player(True)
else:
self.player1 = player1

elif player1 is None:
if ai:
self.player1 = EasyAIPlayer(True)
else:
self.player1 = Player(True)
self.player2 = player2
else:
self.player2 = Player(False)
self.player1 = player1
self.player2 = player2

self.ai = ai
self.to_play_player = self.player1

self.board = Board()
Expand Down
80 changes: 68 additions & 12 deletions python/engine/material.py → pyalapin/engine/material.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import abstractmethod

from engine.color import Color
from pyalapin.engine.color import Color


class Piece(object):
Expand Down Expand Up @@ -158,6 +158,24 @@ def get_potential_moves(self, x, y):
"""
return None

@abstractmethod
def get_threatened_cells_on_board(self, board):
"""
Method to list which cells are threatened by the piece (authorized movement + conditioned on other pieces on board).
Parameters
----------
board: Board
game board self belong to
Returns
-------
list
List of threatened cells
"""
return []

@abstractmethod
def get_str(self):
"""Method to represent the piece as a string.
Expand Down Expand Up @@ -371,31 +389,69 @@ def get_potential_moves(self, x, y):
possible_moves = []
if self.is_white():
# Front cell
possible_moves.append((x + 1, y))
if x < 7:
possible_moves.append((x + 1, y))

# Diagonal cells
if y - 1 >= 0:
possible_moves.append((x + 1, y - 1))
if y + 1 <= 7:
possible_moves.append((x + 1, y + 1))
# Diagonal cells
if y - 1 >= 0:
possible_moves.append((x + 1, y - 1))
if y + 1 <= 7:
possible_moves.append((x + 1, y + 1))

# Double front cell
if x == 1:
possible_moves.append((x + 2, y))

# Symmetric for black pawns
else:
possible_moves.append((x - 1, y))
if x > 0:
possible_moves.append((x - 1, y))

if y - 1 >= 0:
possible_moves.append((x - 1, y - 1))
if y + 1 <= 7:
possible_moves.append((x - 1, y + 1))
if y - 1 >= 0:
possible_moves.append((x - 1, y - 1))
if y + 1 <= 7:
possible_moves.append((x - 1, y + 1))
if x == 6:
possible_moves.append((x - 2, y))

return possible_moves

@abstractmethod
def get_threatened_cells_on_board(self, board):
"""
Method to list which cells are threatened by the piece (authorized movement + conditioned on other pieces on board).
Parameters
----------
board: Board
game board self belong to
Returns
-------
list
List of threatened cells
"""

cells_threatened = []
if self.is_white():
if x < 7:
# Diagonal cells
if y - 1 >= 0:
cells_threatened.append((x + 1, y - 1))
if y + 1 <= 7:
cells_threatened.append((x + 1, y + 1))

# Symmetric for black pawns
else:
if x > 0:
if y - 1 >= 0:
cells_threatened.append((x - 1, y - 1))
if y + 1 <= 7:
cells_threatened.append((x - 1, y + 1))

return cells_threatened

def promote(self, promote_into="Queen"):
"""Method to promote a pawn to other material type. Only happens if the pawn reaches the other side of the board.
The player can choose which type of material he wants its pawn to be promoted into.
Expand Down
11 changes: 6 additions & 5 deletions python/engine/move.py → pyalapin/engine/move.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import copy
import pickle

import engine.material as material
import pyalapin.engine.material as material


class Move:
Expand Down Expand Up @@ -337,7 +337,7 @@ def move_pieces(self):
# Sets the different movement related attributes of Pieces
self._set_moved_attribute()

def is_possible_move(self):
def is_possible_move(self, check_chess=True):
# REFONDRE, particulièrement, faire en sorte qu'on ne vérifie chaque condition qu'une seule fois
# Why castling is checked here ?
"""
Expand Down Expand Up @@ -439,9 +439,10 @@ def work_future(curr_move, curr_board):
return future_board.get_cell(king.x, king.y).is_threatened(future_board, king.is_white())
"""
# Checks if the player's King is threatened after the move.
is_king_threatened_in_future = self._work_future_to_check_chess()
if is_king_threatened_in_future:
return False
if check_chess:
is_king_threatened_in_future = self._work_future_to_check_chess()
if is_king_threatened_in_future:
return False

return True

Expand Down
Loading

0 comments on commit ea8d168

Please sign in to comment.