Skip to content

Commit

Permalink
adjust controller, player and board
Browse files Browse the repository at this point in the history
  • Loading branch information
xXUnique31Xx committed Jul 1, 2024
1 parent 24bf6e7 commit abac621
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 82 deletions.
5 changes: 5 additions & 0 deletions board.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
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):
Expand Down
23 changes: 16 additions & 7 deletions controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
import os
from pathlib import Path
import datetime
import json
from view import GameView
from pieces import *
import json
from player import HumanPlayer, ComputerPlayer


class GameManager:

def __init__(self, view):
self.board = None
self.view = view
self.ai = None
self.player_white = None
self.player_black = None
self.user_ai = None
self.load_game = False

Expand All @@ -34,22 +38,27 @@ def get_menu_choice(self):
selection = input('Please enter the number that corresponds to your desired menu: ')

if selection == '1':
num_player = input('Enter number of players [1-2]: ')
num_player = GameView.input_prompt('Enter number of players [1-2]: ')
if num_player == '1':
self.board.ai = True
self.user_ai = GameAI(self.model, self.view, "Black", "White")
self.player_white = HumanPlayer('White')
self.player_black = ComputerPlayer('Black')
# self.user_ai = GameAI(self.model, self.view, "Black", "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.board.show_symbols = self.get_symbol_preference()
self.start_game()
else:
print('Your choice is not valid! Please try again!')
GameView.display_message('Your choice is not valid! Please try again!')
self.get_menu_choice()

elif selection == '2':
pass # game loading stuff goes here i guess
self.load_gamestate()
self.start_game()

elif selection == '3':
self.view.clear_console()
Expand Down Expand Up @@ -123,7 +132,7 @@ def get_input(self):
start_pos = choice[:2]
goal_pos = choice[-2:]
if start_pos[0] in lines and goal_pos[0] in lines and start_pos[1] in columns and goal_pos[1] in columns:
self.board.move_piece(self.board.correlation[start_pos], self.board.correlation[goal_pos])
self.player_white.make_move(self.board.correlation[start_pos], self.board.correlation[goal_pos])
else:
GameView.display_message('Invalid Choice')
self.get_input()
Expand Down Expand Up @@ -200,7 +209,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.model, self.view, "Black", "White")
self.user_ai = GameAI(self.board, self.view, "Black", "White")

if 'Ai' in GameSave:
self.ai = True
Expand Down
11 changes: 5 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

from model import Model
from board import GameBoard

if __name__ == "__main__":
model = Model()
model.game_manager.model = model
model.view.model = model
model.view.print_menu()
board = GameBoard()
board.game_manager.board = board
board.view.model = board
board.view.print_menu()
132 changes: 66 additions & 66 deletions model.py
Original file line number Diff line number Diff line change
@@ -1,74 +1,74 @@
from view import GameView
from controller import GameManager
from pieces import Rook, Knight, Bishop, Pawn, King, Queen
# from view import GameView
# from controller import GameManager
# from pieces import Rook, Knight, Bishop, Pawn, King, Queen

class Model:
# class Model:

def __init__(self):
self.board_state = [None] * 64
self.view = GameView()
self.game_manager = GameManager(self.view)
self.show_symbols = True
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.ai = None
self.set_initial_pieces()
# def __init__(self):
# self.board_state = [None] * 64
# self.view = GameView()
# self.game_manager = GameManager(self.view)
# self.show_symbols = True
# 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.ai = None
# self.set_initial_pieces()

def set_initial_pieces(self):
positions = {'Rook': [0, 7, 56, 63], 'Knight': [1, 6, 57, 62], 'Bishop': [2, 5, 58, 61],
'Queen': [3, 59], 'King': [4, 60]}
for piece, places in positions.items():
for position in places:
color = 'White' if position > 7 else 'Black'
self.board_state[position] = globals()[piece](color, position, self)
for i in range(8):
self.board_state[8 + i] = Pawn('Black', 8 + i, self)
self.board_state[48 + i] = Pawn('White', 48 + i, self)
for pos in range(16, 48):
self.board_state[pos] = None
self.pieces = [piece for piece in self.board_state if piece is not None]
# def set_initial_pieces(self):
# positions = {'Rook': [0, 7, 56, 63], 'Knight': [1, 6, 57, 62], 'Bishop': [2, 5, 58, 61],
# 'Queen': [3, 59], 'King': [4, 60]}
# for piece, places in positions.items():
# for position in places:
# color = 'White' if position > 7 else 'Black'
# self.board_state[position] = globals()[piece](color, position, self)
# for i in range(8):
# self.board_state[8 + i] = Pawn('Black', 8 + i, self)
# self.board_state[48 + i] = Pawn('White', 48 + i, self)
# for pos in range(16, 48):
# self.board_state[pos] = None
# self.pieces = [piece for piece in self.board_state if piece is not None]

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

def move_piece(self, start_pos, goal_pos, update=True):
piece = self.board_state[start_pos]
if piece and piece.colour == self.currently_playing:
if piece.check_legal_move(goal_pos):
self._update_positions(piece, start_pos, goal_pos, update)
self.toggle_player()
else:
print('Illegal move! Please try again!')
self.game_manager.get_input()
else:
print('There is no piece of your color on this space. Please try again!')
self.game_manager.get_input()
if update:
self.view.update_board()
# def move_piece(self, start_pos, goal_pos, update=True):
# piece = self.board_state[start_pos]
# if piece and piece.colour == self.currently_playing:
# if piece.check_legal_move(goal_pos):
# self._update_positions(piece, start_pos, goal_pos, update)
# self.toggle_player()
# else:
# print('Illegal move! Please try again!')
# self.game_manager.get_input()
# else:
# print('There is no piece of your color on this space. Please try again!')
# self.game_manager.get_input()
# if update:
# self.view.update_board()

def _update_positions(self, piece, start_pos, goal_pos, update):
killed_piece = self.board_state[goal_pos]
self.board_state[goal_pos], self.board_state[start_pos] = piece, None
piece.position = goal_pos
if isinstance(piece, Pawn) and piece.upgrade():
self.board_state[goal_pos] = Queen(self.currently_playing, goal_pos, self)
if killed_piece:
self.pieces.remove(killed_piece)
piece.moved = True
# def _update_positions(self, piece, start_pos, goal_pos, update):
# killed_piece = self.board_state[goal_pos]
# self.board_state[goal_pos], self.board_state[start_pos] = piece, None
# piece.position = goal_pos
# if isinstance(piece, Pawn) and piece.upgrade():
# self.board_state[goal_pos] = Queen(self.currently_playing, goal_pos, self)
# if killed_piece:
# self.pieces.remove(killed_piece)
# piece.moved = True

def check_for_king(self):
king_alive = False
for i in self.pieces:
if type(i) == King and i.colour == self.currently_playing:
king_alive = True
break
return king_alive
# def check_for_king(self):
# king_alive = False
# for i in self.pieces:
# if type(i) == King and i.colour == self.currently_playing:
# king_alive = True
# break
# return king_alive

def get_copy_board_state(self, state=None):
if state is None:
state = self.board_state
return state.copy()
# def get_copy_board_state(self, state=None):
# if state is None:
# state = self.board_state
# return state.copy()
30 changes: 27 additions & 3 deletions player.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,48 @@
from view import GameView


class Player():

def __init__(self, color):
self.color = color
self.pieces = []
self.captured_pieces = []
self.check = False
self.checkmate = False
self.stalemate = False
self.is_currently_playing = False

def make_move(self, board):
"""Abstract method to be implemented by subclasses to make a move."""
raise NotImplementedError("Subclasses must implement this method")


class HumanPlayer(Player):

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

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.check_legal_move(goal_pos):
board._update_positions(piece, start_pos, goal_pos, update)
board.toggle_player()
else:
GameView.display_message('Illegal move! Please try again!')
self.game_manager.get_input()
else:
GameView.display_message('There is no piece of your color on this space. Please try again!')
self.game_manager.get_input()
if update:
self.view.update_board()


class ComputerPlayer(Player):

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

def make_move(self, board):
"""Method to make a move."""
pass

0 comments on commit abac621

Please sign in to comment.