30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

726 Graphics and Multimedia Chapter 16<br />

Fig. 16.26 Chess-game code (part 7 of 9).<br />

The FrmChessSurface Load event (lines 33–45) loads each tile image in<strong>to</strong><br />

mChessTile. It then calls method ResetBoard <strong>to</strong> refresh the Form and begin the<br />

game. Method ResetBoard (lines 48–136) assigns mChessPieces <strong>to</strong> a new Array-<br />

List, loading images for both the black and white chess-piece sets, and creates Bitmap<br />

selected <strong>to</strong> define the currently selected Bitmap set. Lines 72–134 loop through 64<br />

positions on the chess board, setting the tile color and piece for each tile. Lines 75–77 cause<br />

the currently selected image <strong>to</strong> switch <strong>to</strong> the blackPieces after the fifth row. If the row<br />

counter is on the first or last row, lines 83–109 add a new piece <strong>to</strong> mChessPieces. The<br />

type of the piece is based on the current column we are initializing. Pieces in chess are positioned<br />

in the following order, from left <strong>to</strong> right: Rook, knight, bishop, queen, king, bishop,<br />

knight and rook. Lines 112–117 add a new pawn at the current location if the current row<br />

is second or seventh.<br />

A chess board is defined by alternating light and dark tiles across a row in a pattern<br />

where the color that starts each row is equal <strong>to</strong> the color of the last tile of the previous row.<br />

Lines 122–128 assign the current board-tile color as an index in the mBoard array. Based<br />

on the alternating value of Boolean variable light and the results of the random operation<br />

on line 120, 0 and 1 are light tiles, whereas 2 and 3 are dark tiles. Line 133 inverts<br />

the value of light at the end of each row <strong>to</strong> maintain the staggered effect of a chess board.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!