In a previous section you made it possible to determine the outcome of a game by declaring a winner. You also emitted it.
The next logical step is to also record this information in storage. That will assist you in discerning between games that are current and those that have reached an end by winning. As you will see in subsequent sections about forfeit, there are other ways for a game to finish.
Therefore, a reasonable field to add is for the winner. It needs to contain:
The winner of a game that reaches completion.
Or the winner by forfeit when a game is expired.
Or a neutral value when the game is active.
In this section a draw is not handled and it would perhaps require yet another value to be saved in winner.
It is time to introduce another consideration. When a game has been won, no one else is going to play it. Its board will no longer be updated and is no longer used for any further on-chain decisions. In effect, the board becomes redundant. With a view to keeping a node's storage requirement low, you should delete the board's content but keep the rest of the game's information.
To keep a trace of the last state of the board, you emit it with an event.
Update the winner field, which remains neutral(opens new window) if there is no winner yet, and adjust the board depending on whether the game is finished or not:
This "*" means that in your tests no games have reached a conclusion with a winner. Time to fix that. In a dedicated full_game_helpers.go file, prepare all the moves that will be played in the test. For convenience, a move will be written as:
Copy
type GameMoveTest struct{
player string
fromX uint64
fromY uint64
toX uint64
toY uint64} x checkers testutil full_game_helpers.go View source
If you do not want to create a complete game yourself, you can choose this one:
You may want to add a small function that converts "b" and "r" into their respective player addresses:
Copy
funcGetPlayer(color string, black string, red string)string{if color =="b"{return black
}return red
} x checkers testutil full_game_helpers.go View source
And another that applies all the moves. This could become handy if you have multiple games in the future:
If you have created games in an earlier version of the code, you are now in a broken state. You cannot even play the old games because they have .Winner == "" and this will be caught by the if storedGame.Winner != rules.PieceStrings[rules.NO_PLAYER] test. At some point, look at migrations to avoid falling into such a situation with a blockchain in production.
Testing with the CLI up to the point where the game is resolved with a rightful winner is better covered by unit tests (as done here) or with a nice GUI. You will be able to partially test this with the CLI in the next section, via a forfeit.
synopsis
To summarize, this section has explored:
How to prepare for terminating games by defining a winner field that differentiates between the outright winner of a completed game, the winner by forfeit when a game is expired, or a game which is still active.
What new information and functions to add and where, including the winner field, helper functions to get any winner's address, a new error for games already finished, and checks for various application actions.
How to update your tests to check the functionality of your code.
How interacting via the CLI is partially impeded by any existing test games now being in a broken state due to the absence of a value in the winner field, with recommendations for next actions to take.