Implementing Game Rules

From a rules standpoint, checkers is not all that complex of a game. All 12 of your pieces start out with the same movement capabilities, and the only special pieces ever on the board are those that have been crowned.

The goal of this chapter isn’t to produce a game of checkers that might be ready to plug into a WebGL site so you can ship it tomorrow. You’re building a foundation for such a thing, but the nitty-gritty details of checkers edge-case rules are beyond the scope of this chapter.

This checkers game implements some basic rules like crowning a piece and moving. It doesn’t implement jumps, multi-jumps, or detect a winner. If you would like to do that as a reader exercise, I would love to see your game on GitHub!

The first function in the following code listing, $shouldCrown, determines if a piece should be crowned. A piece is eligible for upgrade if it resides in its opponent’s kings row. The $crownPiece function uses a few functions you’ve already written to add a crown to a piece and then store it in the gameboard:

 ;; Should this piece get crowned?
 ;; We crown black pieces in row 0, white pieces in row 7
 (func $shouldCrown (param $pieceY i32) (param $piece i32) (result i32)
  (i32.or
  (i32.and
  (i32.eq
  (get_local $pieceY)
  (i32.const 0)
  )
  (call $isBlack (get_local $piece))
  )
 
  (i32.and
  (i32.eq
  (get_local $pieceY)
  (i32.const 7)
  )
  (call $isWhite (get_local $piece))
  )
  )
 )
 
 ;; Converts a piece into a crowned piece and invokes
 ;; a host notifier
 (func $crownPiece (param $x i32) (param $y i32)
  (local $piece i32)
  (set_local $piece (call $getPiece (get_local $x)(get_local $y)))
 
  (call $setPiece (get_local $x) (get_local $y)
  (call $withCrown (get_local $piece)))
 
  (call $notify_piececrowned (get_local $x)(get_local $y))
 )
 
 (func $distance (param $x i32)(param $y i32)(result i32)
  (i32.sub (get_local $x) (get_local $y))
 )

The local keyword declares a temporary variable that will only be visible within its enclosing scope. In the case of this code listing, all the locals are scoped to their containing function. The set_local instruction sets the value of a local variable. Sadly, you can’t declare and set a local in the same line of code. High-level languages that allow this sort of thing are just generating the separate declaration and assignment operations on your behalf.

Another thing worth pointing out in the preceding code listing is the call to $notify_piececrowned. This function will be imported by this checkers module and implemented by the host. Whenever a piece is crowned, the module calls this to let the host react to the change. This could include modifying the sprite on display for the newly crowned piece, playing a victorious sound, or both.

The $distance function is just a simple subtraction. In the next section on movement, you’ll see how that value is used to determine if that distance is valid.

Nearly all of the (naive) fundamentals of checkers are done at this point: the only thing left is to code player movement.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.19.63.106