Skip to content

Commit

Permalink
Merge pull request #14 from whiletruelearn/black
Browse files Browse the repository at this point in the history
allow to play as black or random
  • Loading branch information
whiletruelearn authored Oct 11, 2024
2 parents 56d13fa + 1dc5250 commit 4f771ef
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions termichess/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ def __init__(self):
super().__init__()
self.engine = chess.engine.SimpleEngine.popen_uci("stockfish")
self.move_sound = sa.WaveObject.from_wave_file(f"{ASSETS_PATH}/sound/move.wav")

self.player_color = "white"

def compose(self) -> ComposeResult:
yield ConfigScreen()
yield ChessBoard(classes="hidden")
Expand Down Expand Up @@ -405,7 +406,10 @@ def toggle_screen(self):
def apply_config(self):

ChessSquare.piece_set = CONF["piece-type"]
player_color = CONF["player_color"]
self.player_color = CONF["player_color"]

if self.player_color == "random":
self.player_color = choice(["white","black"])

difficulty_mapping = {
"beginner": {"depth": 1, "randomness": 0.3},
Expand All @@ -416,6 +420,7 @@ def apply_config(self):
}
self.engine_settings = difficulty_mapping.get(CONF["difficulty"], {"depth": 1, "randomness": 0.3})
self.notify(f"Difficulty set to: {CONF['difficulty']}")
self.notify(f"Player color: {self.player_color}")
self.chess_board.render_board()


Expand Down Expand Up @@ -458,7 +463,7 @@ def make_computer_move(self):
move = result.move
self.chess_board.board.push(move)
self.chess_board.last_move = move
self.move_history.add_move(f"Computer: {move}")
self.move_history.add_move(f"Computer ({('white' if self.player_color == 'black' else 'black')}): {move}")
self.chess_board.render_board()
self.play_move_sound()
self.update_info()
Expand All @@ -475,9 +480,10 @@ def update_info(self):
status = "Game Over - Draw!"
turn = "Game Ended"
else:
turn = "White" if self.chess_board.board.turn == chess.WHITE else "Black"
current_turn = "White" if self.chess_board.board.turn == chess.WHITE else "Black"
turn = f"{current_turn} ({'Player' if current_turn.lower() == self.player_color else 'Computer'})"
status = "Check!" if self.chess_board.board.is_check() else "Normal"
self.info_panel.update_info(turn, status,CONF["difficulty"])
self.info_panel.update_info(turn, status, CONF["difficulty"])

def play_move_sound(self):
self.move_sound.play()
Expand All @@ -504,6 +510,10 @@ def start_game(self):
self.current_screen = "game"
self.toggle_screen()
self.chess_board.render_board()
self.update_info()

if self.player_color == "black":
self.set_timer(1,self.make_computer_move)


def on_button_pressed(self, event: Button.Pressed) -> None:
Expand Down

0 comments on commit 4f771ef

Please sign in to comment.