Sunday, October 19, 2008

Step 1 - The Minesweeper Algorithm

This algorithm is written in a quite high language and is meant to be a basis for the program itself.

The principle of minesweeper is as follows:
You are in front of a table of invisible content. Each field contains either a mine or no mine. If the user clicks on a field and it contains a mine, the game is over. if he clicks on an empty field, it reveals a number that states how many mines all the adjacent fields contain. If there are no mines around, all adjacent fields are automatically revolved. Hidden fields, which are supposed to contain a mine can be marked with a flag.



member variables:
mine count (how many mines are in the field)
queue (a stack of fields, which are to revolve one after the other)
minefield array (2d)
flag array (2d)

beginning, length, height and mine count given:
-make mine count accessible from anywhere inside the minesweeper class
-create a 2 dimensional array with the given length and height
-throw an event indicating the empty minefield is complete by now




M:revolve field, x and y given:
<-condition: If it is the first revolving{
->method: fill array randomly with (mine count) amount of mines, ignoring field x,y
-throw an event indication the minefield is filled by now
}
<-condition: if the field x,y is a mine{
-throw an event indicating field x, y has the value 9
-return the value 9
} else {
->method: get adjacent mine count
<-condition: if the adjacent mine count is larger than 0{
-throw an event indicating field x, y has the value (adjacent mine count)
-return the value (adjacent mine count)
} else {
-throw an event indicating field x, y has the value 0
->method: get the array of adjacent fields for field (x, y)
->method: add necessary items to the queue for (adjacent array)
->method: revolve next queue item
-return the value 0
}
}

M:get adjacent mine count, x and y given:
-remember a number
()loop: from x - 1 to x + 1{
()loop: from y- 1 to y + 1{
<-condition: if the x-loop is NOT at -1
AND if the y-loop is not at -1
AND if the x-loop is not at the length of the x-array
AND if the y-loop is not at the length of the y-array{
<-condition: if the x-loop is NOT 0 OR the y-loop is NOT 0{
<-condition: if the field is a mine{
-add 1 to the remembered number
}
}
}
}
}
-return the remembered number
M:get adjacent flag count
the same as above, just with the flag array

M:revolve next queue item
<-condition: if the queue is not empty{
-remember the item
-remove the item from the queue
->method: revolve field (x of item, y of item)
}

M:add necessary items to the queue, adjacent array given
()loop: for each item in the adjacent array{
<-condition: if the item is not yet in the queue
AND if the item is not revolved yet
AND if the item is not marked{
-add the item to the queue
}
}





M:mark item, x and y given:
<-condition: if the item is not revolved{
-swap the boolean value of the flag array at x, y
}
-return the boolean value of the field

M:fill array randomly, mine count, x and y given
-remember the amount of added mines.
()loop: as long as the added mines are less than the mine count{
-create a random number of the x position, maximum is the length of the x array of the minefield array
-create a random number of the y position, maximum is the length of the y array of the minefield array
<- condition: if the field at the random numbers contains NO mine{
-turn the random field into a mine
-add 1 to the amount of added mines in mind
}
}
-return the value 0

M:get the array of adjacent fields, x and y given
-remember a stack of fields
()loop: from x - 1 to x + 1{
()loop: from y- 1 to y + 1{
<-condition: if the x-loop is NOT at -1
AND if the y-loop is not at -1
AND if the x-loop is not at the length of the x-array
AND if the y-loop is not at the length of the y-array{
<-condition: if the x-loop is NOT 0 OR the y-loop is NOT 0{
-add the field to the stack of remembered fields
}
}
}
}
-return the stack of remembered fields

M:revolve adjacent fields, x and y given
->method: get adjacent flag count (x, y)
->method: get adjacent mine count(x, y)
<-condition: if the adjacent flag amount = the adjacent mine count{
->method: get the array of adjacent fields for field (x, y)
->method: add necessary items to the queue for (adjacent array)
->method: revolve next queue item
return the value 0
}
return the value 1

No comments: