Mission Objective To Aim For

Winning & Losing

Now that we have all of the components required for an almost complete game of TK-MineSweeper, it's time to introduce some variables which define the state of the game in terms of success and failure. We lose in TK-MineSweeper by stepping on mines. We will be able to win (when we have implemented the ability to defuse mines) in TK-MineSweeper by successfully locating all of the mines in the mineField without having trod on any of them.

Why not, then, define a state lost, which is 0 when lost is false, and 1 when lost is true ?

In a similar fashion, we define a variable, won to be 0 when we have not won, and 1 when we have won.

We may include them into the TK-MineSweeper proceedings as follows :

won is 0;
lost is 0;

These variables, however, have no meaning unless we give them some meaning. In the play function described in the previous section, we have a condition whereby we lose the game (we 'try' a cell which contains a mine). We currently receive a message saying something like : "BANG ! Oops, mine here"

With our new conditions, however, we can add the modification of lost to this part of the script, making lost equal 1.

The following reproduction of the play function includes the modification of lost, and can be copy & pasted at will :

So, we now have some definitions which relay the state of the player's success. However, whilst we have these variables, there is nothing to respond to them.

Game Results

However, we can soon remedy this by defining a function which is a triggered action in response to modifications to either of the game success variables : won or lost.

We can have a simple routine, which is triggered by modifications to won or lost. Depending upon the values of these success variables, this routine can output either a success message, or a failure message.

Selecting a cell containing a mine now results in a little message informing you that you have lost the game. At the current time, there is still no way of winning (not fair !) But, a redress is about to appear. The next section of this TK-MineSweeper demonstration looks at some more complex, but still pretty essential aspects, or the mineSweeper game. Namely, we are looking into how the player can select suspected mine cells, and try to win the game.