It is the end of a chess game (human vs human), and only kings and pawns are left. Write a program that takes in a CSV file (from local file system) with the current positions of the pieces, e.g.:
king, white, d3
pawn, white, d4
king, black, b5
pawn, black, a4
pawn, black, c4
and then allows the players to legally move their pieces. You are only implementing the logic, not the UI. The only requirements are that there needs to be one exposed function that lets a player move his/her piece and another function to get the current state of the board.
Please don’t spend more than a couple hours on this. If there are features or edge cases that you don’t have time to include, take note of them, and we can discuss how you would tackle them afterward. Let me know if you have any questions.
- Given the time constraints, I aimed to make problem (listed above) smaller by first solving for the "black pawn" located at "c4". While solving for the "black pawn" first, moved quickly with well tested code. As I begin working on other pieces, I will aim to continue a test driven approach while enhancing the structure, descriptive nature and dynamic pattern of the source code.
- read file
- create Pieces
- convert position to something that can be represented on x & y coordinates
- add ability for pawn to move
- [x, y] = [column, row]
- black: move forward [x, y] + [0, -1]
- black: move diagonally
- condition: if there is a piece in diagonal position
- diagonal left: [x, y] + [-1, -1]
- diagonal right: [x, y] + [1, -1]
- white: move forward [x, y] + [0, 1]
- white: move diagonally
- condition: if there is a piece in diagonal position
- diagonal left: [x, y] + [-1, 1]
- diagonal right: [x, y] + [1, 1]
- cannot move back or horizontally
- cannot move forward if piece is in that position
- clone project
- assuming you have bundler installed, run
bundle install
from root directory of project- run tests:
bundle exec rspec
- You can run the code without bundler but you may not be able to run the tests.
- run tests:
- run program:
ruby ./main.rb
- see
Spec
directory for tests
- To move a piece: call
move
on an instance ofBoard
and pass in the current board position of the piece (eg 'c4') and the new preferred board position (eg 'c3').
- finish implementing logic described in "Initial Steps"
- add logic for white pawn, black king and white king
- re-factor method for placing pieces now that a method for calculating the list location from given coordinates exists. (See
find_list_position
) - remove excessive
if
else
statements and create new methods for logic - add tests for entering a destination that is off the board (logic already exists)
- make code descriptive enough where notes would not be (or very minimally) needed
- make
add_piece
andremove_piece
private methods, per instructions above - Thoughts: 'Piece' could inherit from 'Square'
- running the program with
ruby main.rb
and the tests withbundle exec rspec
should result in the following:
- how to tell if a king is about to be in check?
- if a king is in check, what are the available moves?
Setup: [black, pawn, c5], [white, king, d4]
-
black pawn move to c4
- for squares located at diagonal left and diagonal right
- if white king is in diagonal left or diagonal right
- print "check"
- else
- add white king to a property called
invalid pieces
- add white king to a property called
- if white king is in diagonal left or diagonal right
- for squares located at diagonal left and diagonal right
-
after successful move
- remove invalid pieces that were placed on squares by black pawn based on the previous location of black pawn
-
prepare to move, remove invalid pieces that were placed by white king, from surrounding squares
- white king move to d3
- for surrounding squares if black king is
- if the square located at d3 contains white king in the
invalid pieces
list- return "invalid move, king would be in check"
- white king move to d3
-
after successful move
- remove invalid pieces that were placed on squares by white king based on the previous location of white king
- track turns to ensure one person doesn't move twice
- piece should be able to kill another piece
- after a king is killed, game should end