-
Notifications
You must be signed in to change notification settings - Fork 3
/
tic_tac_toe.hpp
128 lines (118 loc) · 3.63 KB
/
tic_tac_toe.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* @file
* @copyright defined in eos/LICENSE.txt
*/
#include <eoslib/eos.hpp>
#include <eoslib/db.hpp>
/**
* @defgroup tictactoecontract Tic Tac Toe Contract
* @brief Defines the PvP tic tac toe contract example
* @ingroup examplecontract
*
* @details
*
* For the following tic-tac-toe game:
* - Each pair of player can have 2 unique game, one where player_1 become host and player_2 become challenger and vice versa
* - The game data is stored in the "host" scope and use the "challenger" as the key
*
* (0,0) coordinate is on the top left corner of the board
* @code
* (0,2)
* (0,0) - | o | x where - = empty cell
* - | x | - x = move by host
* (2,0) x | o | o o = move by challenger
* @endcode
*
* Board is represented with number:
* - 0 represents empty cell
* - 1 represents cell filled by host
* - 2 represents cell filled by challenger
* Therefore, assuming x is host, the above board will have the following representation: [0, 2, 1, 0, 1, 0, 1, 2, 2] inside the game object
*
* In order to deploy this contract:
* - Create an account called tic.tac.toe
* - Add tic.tac.toe key to your wallet
* - Set the contract on the tic.tac.toe account
*
* How to play the game:
* - Create a game using `create` action, with you as the host and other account as the challenger.
* - The first move needs to be done by the host, use the `move` action to make a move by specifying which row and column to fill.
* - Then ask the challenger to make a move, after that it's back to the host turn again, repeat until the winner is determined.
* - If you want to restart the game, use the `restart` action
* - If you want to clear the game from the database to save up some space after the game has ended, use the `close` action
* @{
*/
using namespace eos;
namespace tic_tac_toe {
/**
* @brief Data structure to hold game information
*/
struct PACKED(Game) {
Game() {};
Game(AccountName challenger, AccountName host):challenger(challenger), host(host), turn(host) {
// Initialize board
initialize_board();
};
AccountName challenger; // this also acts as key of the table
AccountName host;
AccountName turn; // = account name of host/ challenger
AccountName winner = N(none); // = none/ draw/ account name of host/ challenger
uint8_t board_len = 9;
uint8_t board[9]; //
// Initialize board with empty cell
void initialize_board() {
for (uint8_t i = 0; i < board_len ; i++) {
board[i] = 0;
}
}
// Reset game
void reset_game() {
initialize_board();
turn = host;
winner = N(none);
}
};
/**
* @brief Action to create new game
*/
struct Create {
AccountName challenger;
AccountName host;
};
/**
* @brief Action to restart new game
*/
struct Restart {
AccountName challenger;
AccountName host;
AccountName by; // the account who wants to restart the game
};
/**
* @brief Action to close new game
*/
struct Close {
AccountName challenger;
AccountName host;
};
/**
* @brief Data structure for movement
*/
struct Movement {
uint32_t row;
uint32_t column;
};
/**
* @brief Action to make movement
*/
struct Move {
AccountName challenger;
AccountName host;
AccountName by; // the account who wants to make the move
Movement movement;
};
/**
* @brief Table to store list of games
*/
using Games = Table<N(tic.tac.toe),N(tic.tac.toe),N(games),Game,uint64_t>;
}
/// @}