A basic chess game on command line.
- Run
bundle install
- Run
ruby main.rb
- Choose human vs human, human vs AI, or replay a game from Apple's native chess game
I designed a 2D coordinate to facilitate accessing the 2D chess board. I overloaded operators so that we can calculate distances between two squares.
The board's access []
method is also overloaded. The []
(gettor) and []=
(settor) both check if the coordinate is within the limit of a chess board. I designed a hard raise
error if it's out of boundry, but the methods in the game would use rescue
. The user tries again instead of terminating the program.
- Testing ruby code
- use rubocup
- structuring larger projects
- persistence (i basically deleted the whole project and restarted)
- don't overengineer things, define your MVP first
- the coordinate and board system
- find a way to use Ruby's namespace systems more elegantly
- Break up
lib/game.rb
- Break up
lib/piece.rb
lib/piece.rb
is kept as a large file because of circular import problemslib/game.rb
is a long file because all the functions need the@board
parameter
- Break up
-
Click save
-
Choose game format 'Chess Game' (
.game
) -
Move this
.game
to this project'sdata/game_db
-
When prompted, choose " 2) Load a game from DB"
For the life of me, I can't figure out how to load this into my board.
PGN only records the destination of a piece for each player. A human player can easily tell which piece moved there because you can quickly sift through the rules and constraints. My parser isn't that smart, unfortunately.
Apple's .game file is an XML, which I parse with a ruby library. The XML file records the coordinates of the piece, and the coordinates of its destination. It's also the approach I take in prompting users & saving my game. You need to run install Gemfile
.
- white goes first
- each player inputs column, row separately using the standrad algebraic notation
- the player must input a valid piece and a valid destination. if not, the prompt repeats
- all basic movements (en passant not supported)
- king castle (without explicit commands), just input the coordinates
- simple AI player (choose available pieces and random moves)