06-Queen-Safety

06-Queen-Safety - CS106X Winter 2008 Handout 06 January 11,...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106X Handout 06 Winter 2008 January 11, 2008 Two-Dimensional Grids and Queen Safety This handout was written by Jerry. Today's larger example demonstrates the use of a grid of Boolean values—that is, a single declaration of a Grid<bool> —to maintain information on a chessboard and the placement of queens. The code relevant to the demonstration is presented here in this handout. We want to use a two-dimensional Grid template to maintain information for all of the squares of a chessboard. Because our particular example only deals with the queen piece, we only need to store a true or false for each board location— true means a queen is present, false means there’s no queen. Ultimately, we are going to be interested in looking at an arbitrary position of the board and determine whether or not any of the queens placed elsewhere can attack that position. The following function initQueensBoard takes a Grid<bool> by reference and initializes every entry of the grid to be false . Note the use of a double- for loop to generate every pair of row and col that corresponds to a legal index. After this function gets called, the state of the board is very easily represented.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 static const int kNumRows = 8; static const int kNumColumns = 8; { for (int row = 0; row < kNumRows; row++) { for (int col = 0; col < kNumColumns; col++) { board(row, col) = false; } } } Next, we want a function placeRandomQueens that places some number of queens on the board at random locations. We enter the function with an idea of exactly how many queens need to be placed, and then repeatedly generate random coordinates on the board and placing queens there. Note the care placeRandomQueens takes to assign at most one queen to each location—otherwise, the if (!board(row, col)) wouldn't be necessary.
Background image of page 2
3 { Randomize(); int numQueensPlaced = 0; while (numQueensPlaced < numQueensToPlace) { int row = RandomInteger(0, kNumRows - 1); int col = RandomInteger(0, kNumColumns - 1); if (!board(row, col)) { board(row, col) = true; MarkLocation(Occupied, row, col); // assume it draws a 'Q' numQueensPlaced++; } } } If we are dealing with an eight by eight board, then a call to placeRandomQueens(board, 5) might produce the following configuration:
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/18/2008 for the course CS 106X taught by Professor Cain,g during the Winter '08 term at Stanford.

Page1 / 8

06-Queen-Safety - CS106X Winter 2008 Handout 06 January 11,...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online