Skip to content

Commit

Permalink
remove all model stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
xXUnique31Xx committed Jul 1, 2024
1 parent 2815c22 commit 5cd52b4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 32 deletions.
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)
self.board.move_piece(x_move, y_move)
15 changes: 6 additions & 9 deletions controller.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from algorithm import GameAI
import sys
import os
from pathlib import Path
Expand All @@ -17,7 +16,6 @@ def __init__(self, view):
self.ai = None
self.player_white = None
self.player_black = None
self.user_ai = None
self.load_game = False

def get_after_game_choice(self):
Expand All @@ -41,15 +39,14 @@ def get_menu_choice(self):
num_player = GameView.input_prompt('Enter number of players [1-2]: ')
if num_player == '1':
self.board.ai = True
self.player_white = HumanPlayer('White')
self.player_black = ComputerPlayer('Black')
# self.user_ai = GameAI(self.model, self.view, "Black", "White")
self.player_white = HumanPlayer('White', self.view)
self.player_black = ComputerPlayer('Black', self.board, self.view, 'White')
self.board.show_symbols = self.get_symbol_preference()
self.start_game()
elif num_player == '2':
self.board.ai = False
self.player_white = HumanPlayer('White')
self.player_black = HumanPlayer('Black')
self.player_white = HumanPlayer('White', self.view)
self.player_black = HumanPlayer('Black', self.view)
self.board.show_symbols = self.get_symbol_preference()
self.start_game()
else:
Expand Down Expand Up @@ -83,7 +80,7 @@ def start_game(self):
if self.board.ai:
while self.board.check_for_king():
if self.board.currently_playing == 'Black':
self.user_ai.move()
self.player_black.make_move()
else:
self.get_input()
else:
Expand Down Expand Up @@ -209,7 +206,7 @@ def load_gamestate(self):
self.board.currently_playing = GameSave['currently_playing']
self.board.show_symbols = GameSave['show_symbols']
self.load_game = True
self.user_ai = GameAI(self.board, self.view, "Black", "White")
self.player_black = ComputerPlayer('Black', self.board, self.view, 'White')

if 'Ai' in GameSave:
self.ai = True
Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
if __name__ == "__main__":
board = GameBoard()
board.game_manager.board = board
board.view.model = board
board.view.board = board
board.view.print_menu()
16 changes: 10 additions & 6 deletions player.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from view import GameView
from algorithm import GameAI


class Player():
Expand All @@ -16,14 +17,14 @@ def make_move(self, board):

class HumanPlayer(Player):

def __init__(self, color):
def __init__(self, color, view):
super().__init__(color)
self.is_currently_playing = False
self.view = view

def make_move(self, start_pos, goal_pos, board, update=True):
"""Method to make a move."""
piece = board.board_state[start_pos]
if piece and piece.colour == self.currently_playing:
if piece and piece.colour == self.color:
if piece.check_legal_move(goal_pos):
board.update_positions(piece, start_pos, goal_pos, update)
board.toggle_player()
Expand All @@ -39,10 +40,13 @@ def make_move(self, start_pos, goal_pos, board, update=True):

class ComputerPlayer(Player):

def __init__(self, color):
def __init__(self, color, board, view, enemy):
super().__init__(color)
self.is_currently_playing = False
self.game_ai = GameAI(board, view, color, enemy)
self.view = view

def make_move(self, board):
def make_move(self):
"""Method to make a move."""
pass
self.game_ai.move()
self.view.update_board()
10 changes: 5 additions & 5 deletions view.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
class GameView:

def __init__(self):
self.model = None
self.board = None
self.last_board = None

def update_board(self, state=None):
self.clear_console()
state = state or self.model.board_state
print(f'Current turn: {self.model.currently_playing}\n')
state = state or self.board.board_state
print(f'Current turn: {self.board.currently_playing}\n')
self.print_board(state)
self.last_board = self.model.get_copy_board_state()
self.last_board = self.board.get_copy_board_state()

def print_board(self, state):
header = ' ' + ' '.join(' A B C D E F G H'.split())
Expand Down Expand Up @@ -87,4 +87,4 @@ def print_menu(self):
2) Load Game
3) Exit
""")
self.model.game_manager.get_menu_choice()
self.board.game_manager.get_menu_choice()

0 comments on commit 5cd52b4

Please sign in to comment.