From 7fb821254cc7f54fb18c456e4677e18a3d4b48aa Mon Sep 17 00:00:00 2001 From: TiynGER Date: Tue, 27 Apr 2021 23:13:41 +0200 Subject: [PATCH] game: added single player Integrated the engine into the playable game. Created a menu to choose between single player and hotseat. Added a way to choose difficulty and color in single player. --- README.md | 4 +-- src/game.nim | 73 +++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 71 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 7b4edb5..a169966 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,8 @@ ychess is a chess implementation and engine written in nim. ## Usage -Simply download the code and run `nim c -r game.nim`. -You can now play a 1v1 hotseat game of chess in the commandline. +To play chess in the commandline simply download the code and run `nim c -r game.nim`. +You can either play the 1v1 hotseat mode or a single player mode vs the engine. ## Testing diff --git a/src/game.nim b/src/game.nim index 3d10636..0d7afd8 100644 --- a/src/game.nim +++ b/src/game.nim @@ -1,7 +1,10 @@ -import ./chess +import parseutils -proc runGame*(): void = - ## Initializes and runs a game of chess. +import ./chess.nim +import ./engine.nim + +proc runGameHotseat*(): void = + ## Initializes and runs a game of chess as hotseat. var game = initGame() var draw: string game.echoBoard(game.toMove) @@ -23,4 +26,66 @@ proc runGame*(): void = if game.isStalemate(game.toMove): echo "Stalemate" -runGame() +proc runGameSolo*(color: Color, difficulty: int): void = + ## Initializes and runs a solo game of chess. + ## The player plays as `color`. + var game = initGame() + var draw: string + while not game.isCheckmate(game.toMove) and not game.isStalemate(game.toMove): + if (game.toMove == color): + game.echoBoard(color) + echo "Make a move" + var hMove = readLine(stdin) + while not game.checkedMove(notationToMove(hMove, game.toMove)): + hMove = readLine(stdin) + game.echoBoard(color) + if (game.isDrawClaimable): + echo "Do you want to claim a draw? (y/N)" + draw = readLine(stdin) + if (draw == "y"): + echo "Draw claimed" + break + else: + var cMove = bestMove(game, 3) + game.checkedMove(cMove) + if game.isCheckmate(game.toMove): + echo $game.toMove & " was checkmated" + if game.isStalemate(game.toMove): + echo "Stalemate" + +proc menu(): void = + ## Presents choices on what to play. + echo("\nWelcome to YchESs!\n\n\n") + var input: string + var playerCount: int + while true: + echo("How many players? (1/2)") + input = readLine(stdin) + discard parseInt(input, playerCount, 0) + if (playerCount == 1 or playerCount == 2): + break + if playerCount == 1: + var color: string + var difficulty: int + while true: + echo("Choose the difficulty for the engine (1-10)") + input = readLine(stdin) + discard parseInt(input, difficulty, 0) + if (difficulty >= 1 and difficulty <= 10): + break + while true: + echo("Do you want to play Black or White? (B/W)") + color = readLine(stdin) + if (color == "B"): + echo("\n\n\n") + runGameSolo(Color.Black, difficulty) + break + elif (color == "W"): + echo("\n\n\n") + runGameSolo(Color.White, difficulty) + break + else: + echo("\n\n\n") + runGameHotseat() + +menu()