Skip to content

Commit

Permalink
Merge pull request #2 from crnnr/dev
Browse files Browse the repository at this point in the history
merge to stable
  • Loading branch information
xXUnique31Xx authored Jul 2, 2024
2 parents 8eea927 + 797348d commit 5f360fc
Show file tree
Hide file tree
Showing 11 changed files with 1,109 additions and 229 deletions.
423 changes: 423 additions & 0 deletions SavedGames/02072024_073340_Gamestate.json

Large diffs are not rendered by default.

423 changes: 423 additions & 0 deletions SavedGames/02072024_075916_Gamestate.json

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@

class GameAI:

def __init__(self, model, view, color, enemy):
def __init__(self, board, view, color, enemy):

self.model = model
self.board = board
self.view = view
self.color = color
self.enemy = enemy

def alpha_beta(self, state, depth, alpha, beta, ai_playing):

# calcs the score of the current board
if depth == 0 or not self.model.check_for_king():
if depth == 0 or not self.board.check_for_king():
return self.calculate_board_value(state)

if ai_playing:
ai_value = -math.inf
self.model.currently_playing = "White"
self.board.currently_playing = "White"
# calcs the score of every possible move
for next_move in self.get_possible_moves(self.enemy, state):

x_move, y_move = next_move

temp = self.model.get_copy_board_state(state)
temp = self.board.get_copy_board_state(state)

change_position = None

Expand All @@ -47,7 +47,7 @@ def alpha_beta(self, state, depth, alpha, beta, ai_playing):
if change_position is not None:
temp[y_move].position = change_position

self.model.currently_playing = "Black"
self.board.currently_playing = "Black"

# White want the score as high as possible
ai_value = max(ai_value, value)
Expand All @@ -58,12 +58,12 @@ def alpha_beta(self, state, depth, alpha, beta, ai_playing):
return ai_value

player_value = math.inf
self.model.currently_playing = "Black"
self.board.currently_playing = "Black"
for next_move in self.get_possible_moves(self.color, state):

x_move, y_move = next_move

temp = self.model.get_copy_board_state(state)
temp = self.board.get_copy_board_state(state)

change_position = None

Expand Down Expand Up @@ -188,7 +188,7 @@ def calc_best_move(self, moves, queue, state):

for next_move in moves:

temp = self.model.get_copy_board_state(state)
temp = self.board.get_copy_board_state(state)

x_move, y_move = next_move
change_position = None
Expand All @@ -215,7 +215,7 @@ def calc_best_move(self, moves, queue, state):

def move(self):

state = self.model.get_copy_board_state()
state = self.board.get_copy_board_state()
possible_moves = self.get_possible_moves(self.color, state)
result = []

Expand Down Expand Up @@ -248,4 +248,4 @@ def move(self):
same_score.sort()
x_move, y_move = same_score[0][0]
output.close()
self.model.move_piece(x_move, y_move)
return x_move, y_move
18 changes: 17 additions & 1 deletion board.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
from player import HumanPlayer, ComputerPlayer
from pieces import Rook, Knight, Bishop, Queen, King, Pawn
from view import GameView
from controller import GameManager

class GameBoard:
"""Class that handles the game board"""

def __init__(self):
self.board_state = [None] * 64
self.show_symbols = True
self.view = GameView()
self.game_manager = GameManager(self.view)
self.correlation = {f"{chr(65 + col)}{8 - row}": row * 8 + col for row in range(8) for col in range(8)}
self.pieces = []
self.currently_playing = 'White'
self.show_symbols = True
self.set_initial_pieces()

def set_initial_pieces(self):
"""Set the initial pieces on the board"""
Expand All @@ -22,7 +32,7 @@ def set_initial_pieces(self):
self.board_state[pos] = None
self.pieces = [piece for piece in self.board_state if piece is not None]

def _update_positions(self, piece, start_pos, goal_pos, update):
def update_positions(self, piece, start_pos, goal_pos, update):
"""Update the positions of the pieces on the board"""
killed_piece = self.board_state[goal_pos]
self.board_state[goal_pos], self.board_state[start_pos] = piece, None
Expand All @@ -33,6 +43,12 @@ def _update_positions(self, piece, start_pos, goal_pos, update):
self.pieces.remove(killed_piece)
piece.moved = True

def toggle_player(self):
if self.currently_playing == 'White':
self.currently_playing = 'Black'
else:
self.currently_playing = 'White'

def check_for_king(self):
"""Check if the king is alive on the board"""
king_alive = False
Expand Down
Loading

0 comments on commit 5f360fc

Please sign in to comment.