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.
master
TiynGER 4 years ago
parent 4e69bd3977
commit 7fb821254c

@ -8,8 +8,8 @@ ychess is a chess implementation and engine written in nim.
## Usage ## Usage
Simply download the code and run `nim c -r game.nim`. To play chess in the commandline simply download the code and run `nim c -r game.nim`.
You can now play a 1v1 hotseat game of chess in the commandline. You can either play the 1v1 hotseat mode or a single player mode vs the engine.
## Testing ## Testing

@ -1,7 +1,10 @@
import ./chess import parseutils
proc runGame*(): void = import ./chess.nim
## Initializes and runs a game of chess. import ./engine.nim
proc runGameHotseat*(): void =
## Initializes and runs a game of chess as hotseat.
var game = initGame() var game = initGame()
var draw: string var draw: string
game.echoBoard(game.toMove) game.echoBoard(game.toMove)
@ -23,4 +26,66 @@ proc runGame*(): void =
if game.isStalemate(game.toMove): if game.isStalemate(game.toMove):
echo "Stalemate" 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()

Loading…
Cancel
Save