Skip to content

Commit

Permalink
Merge branch 'test' of github.com:VincentAuriau/pyalapin into test
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentAuriau committed Oct 31, 2023
2 parents c43d5b5 + 4fb15f6 commit a78ea91
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 38 deletions.
2 changes: 1 addition & 1 deletion pyalapin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"""

__version__ = "0.0.1"
__author__ = 'Vincent Auriau'
__author__ = "Vincent Auriau"
1 change: 0 additions & 1 deletion pyalapin/engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,6 @@ def __init__(self, player1=None, player2=None, automatic_draw=True, ai=False):
self.player1 = player1

elif player1 is None:

if ai:
self.player1 = EasyAIPlayer(True)
else:
Expand Down
3 changes: 0 additions & 3 deletions pyalapin/engine/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,6 @@ def get_potential_moves(self, x, y):

return possible_moves


@abstractmethod
def get_threatened_cells_on_board(self, board):
"""
Expand All @@ -436,7 +435,6 @@ def get_threatened_cells_on_board(self, board):

cells_threatened = []
if self.is_white():

if x < 7:
# Diagonal cells
if y - 1 >= 0:
Expand All @@ -446,7 +444,6 @@ def get_threatened_cells_on_board(self, board):

# Symmetric for black pawns
else:

if x > 0:
if y - 1 >= 0:
cells_threatened.append((x - 1, y - 1))
Expand Down
20 changes: 14 additions & 6 deletions pyalapin/interface/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __init__(self, **kwargs):

class DisplayableCell(Button):
"""Base class to represent a Cell as Button"""

def __init__(self, row, column, **kwargs):
"""
Initialization of the representation of the cell.
Expand Down Expand Up @@ -78,6 +79,7 @@ class TableScreen(GridLayout):
cells: list of DisplayableCells
List of cells constituting the board
"""

def __init__(self, game, **kwargs):
"""
Initialization of the board display.
Expand Down Expand Up @@ -125,7 +127,7 @@ def __init__(self, game, **kwargs):
path_to_img = c_img

if piece.is_white():
piece_color = (1, 1, 1, 1) # For text color, could be removed
piece_color = (1, 1, 1, 1) # For text color, could be removed
path_to_img += "w"
else:
piece_color = (0, 0, 0, 1)
Expand All @@ -152,7 +154,7 @@ def __init__(self, game, **kwargs):
else:
# No piece to display
piece = ""
piece_color = (1, 1, 1, 1) # For text color could be removed
piece_color = (1, 1, 1, 1) # For text color could be removed
path_to_img = c_img + ".png"
# Unclicked
path_to_down_img = "down_" + path_to_img
Expand Down Expand Up @@ -269,7 +271,7 @@ def click_cell(self, event):
self.cells[event.row][event.column].background_down,
self.cells[event.row][event.column].background_normal,
)
# If no previous cell has been clicked, then it's the start cell that has been clicked,
# If no previous cell has been clicked, then it's the start cell that has been clicked,
# In this case it is store, waiting fot the click on the landinc cell.
if self.first_cell_clicked is None:
self.first_cell_clicked = (event.row, event.column)
Expand Down Expand Up @@ -325,9 +327,14 @@ def click_cell(self, event):
# In this case, game was not possible, reset last clicks so that the player can restart
# and redefine its move.
else:
popup = Popup(title='Unable Move',
content=Label(text='Your selected move is not possible, please, select another one.'),
size_hint=(None, None), size=(15, 15))
popup = Popup(
title="Unable Move",
content=Label(
text="Your selected move is not possible, please, select another one."
),
size_hint=(None, None),
size=(15, 15),
)
popup.open()

# Resets values befor next move
Expand All @@ -353,6 +360,7 @@ class MyApp(App):
"""
Main app to use to play game, by calling MyApp().buil() and then player.
"""

def __init__(self, play_with_ai=False, **kwargs):
"""
Initialization, with precision whether or not playing with AI.
Expand Down
13 changes: 7 additions & 6 deletions pyalapin/player/ai_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class EasyAIPlayer(Player):
pieces_positions_weights: dict
Values for each piece to be on a certain position.
"""

piece_weights = {
"pawn": 10,
"knight": 30,
Expand Down Expand Up @@ -337,11 +338,11 @@ def _alpha_beta(

best_move = [best_move, p_mv][np.argmax([best_score, score])]
best_score = np.max([best_score, score])

if best_score >= beta:
return best_score, best_move
alpha = np.max((alpha, best_score))

return best_score, best_move

else:
Expand All @@ -363,7 +364,7 @@ def _alpha_beta(

best_move = [best_move, p_mv][np.argmin([best_score, score])]
best_score = np.min([best_score, score])

if best_score <= alpha:
return best_score, best_move
beta = np.min([beta, best_score])
Expand Down Expand Up @@ -423,7 +424,7 @@ def time_to_play(self, board, depth=3, draw_board=False):
board.draw()
# current_score = self._score_board(board)
sel_score, sel_move = self._alpha_beta(board, depth=depth)

return sel_move

def _score_board(self, board):
Expand All @@ -447,7 +448,7 @@ def _score_board(self, board):
score += self.piece_positions_weights[piece_type][piece.x][piece.y]
own_king = board.all_material[self.color]["alive"]["king"]
if len(own_king) == 0:
score -= 1000
score -= 1000
else:
own_king = own_king[0]
if board.get_cell(own_king.x, own_king.y).is_threatened(
Expand All @@ -464,7 +465,7 @@ def _score_board(self, board):
adv_king = board.all_material[adv_color]["alive"]["king"]

if len(adv_king) == 0:
score -= 1000
score -= 1000
else:
adv_king = adv_king[0]
if board.get_cell(adv_king.x, adv_king.y).is_threatened(
Expand Down
38 changes: 18 additions & 20 deletions pyalapin/setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from setuptools import setup, find_packages

VERSION = "0.0.1"
Expand All @@ -11,22 +10,21 @@

# Setting up
setup(
name="pyalapin",
version=VERSION,
author="Vincent Auriau",
author_email="",
description=DESCRIPTION,
long_description=LONG_DESCRIPTION,
packages=find_packages(),
install_requires=["numpy"],

keywords=['python', 'first package'],
classifiers= [
"Development Status :: 3 - Alpha",
"Intended Audience :: Anyone",
"Programming Language :: Python :: 3",
"Operating System :: MacOS :: MacOS X",
"Operating System :: Microsoft :: Windows",
"Operating System :: Linux :: Linux",
]
)
name="pyalapin",
version=VERSION,
author="Vincent Auriau",
author_email="",
description=DESCRIPTION,
long_description=LONG_DESCRIPTION,
packages=find_packages(),
install_requires=["numpy"],
keywords=["python", "first package"],
classifiers=[
"Development Status :: 3 - Alpha",
"Intended Audience :: Anyone",
"Programming Language :: Python :: 3",
"Operating System :: MacOS :: MacOS X",
"Operating System :: Microsoft :: Windows",
"Operating System :: Linux :: Linux",
],
)
2 changes: 1 addition & 1 deletion utils/profile_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
game_is_on = game.move(ai_move, game.player2)
score = my_player._score_board(game.board)
print(my_player.model.summary())
"""
"""

0 comments on commit a78ea91

Please sign in to comment.