5) Games computation

The flowchart on the codes running on mc2 has an initialization process that is basically consists of the few components below.

After initialized the mouse and variables, mc2 initialize the Board[ ][ ] array, generate random mines in the board, and compute the number of mines in the 8 neighbors for each cell.

Each of the 8 bits in the “board” array carries the following meaning.

Bit #

Function

0-2

3 bits to store the number of mines in the surrounding neighbors.

3

Cell is uncovered when set, covered when cleared.

4

Cell is marked when set, unmarked when cleared

5-6

Not used

7

This cell has a mine if this bit is set. No mine if cleared

All cells are initialized to zero when the games is first started. Board[i][j]=0. This means

·  All cells are covered because bit 3 is cleared for all cells.

·  All cells are NOT marked because bit 4 is cleared for all cells.

·  All cells have no mine, and also has no mines in its neighbors because bit 7, and bit 0-2 are cleared for all cells.

A fixed number of random mines are generated in the Board by setting bit 7 of those cell that have mine. ie. Board[i][j]=Board[i][j] | 0b10000000.

To count the surrounding mines for each cell, we just go through every cell in Board[i][j] and count the 8 neighbors as shown in the picture below.

The calculated number for each cell Board[i][j] is stored in bit 0-2 of Board[i][j]. We put the flowchart of mc2 here again to give a better overview of how the games is being handled.

In the flowchart above,

·  When a cell is uncovered, it mean bit 3 of Board[cursor_row][cursor_col] is set, and the a request to update the screen at this location is sent to mc1. As explained before, the old cursor location is also asked to be refreshed at mc1.

·  When a cell is marked, it mean bit 4 of Board[cursor_row][cursor_col] is set, and the a request to update the screen at this location is sent to mc1.

When an empty cell is uncovered, the program will check if its surrounding cells are ALL empty. If its surrounding cells are all empty, these cells will also be uncovered. Whenever an empty cell is uncovered, the same check is performed. until no cells with these requirement are found. A recursive function is written to perform this task. It is written as a recursive function because it is easier to write. Not because it is more efficient. In fact, it is less efficient, but it worked quite well in our mc2. We also need to set the data stack to about 1500 bytes.

This feature of the games is the same as that implemented in the Windows' minesweeper.