Check All the Moves

The game is over when there are no more moves for the player to make. Sometimes this is hard for players to figure out, and it’s a nice touch to do it for them. Our strategy is to check every possible move until we find a legal one. If we don’t find one, the game is over. You can create two new methods in the Game class to help you figure this out. The first one, legal_move_for?(start_square), will return true if start_square has any possible legal moves and false if it does not. First, it checks to see whether start_square is empty and returns false in this case. Then it checks the moves from start_square to every square on the board. If it finds a legal move, it returns true. If it does not, it returns false:

Twelve/Twelve_1/game.rb ...

Get Learn Game Programming with Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.