-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.sol
204 lines (169 loc) · 5.3 KB
/
TicTacToe.sol
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// solc TicTacToe.sol --bin --abi --optimize --overwrite -o Compilado/
pragma solidity ^0.4.23;
contract TicTacToe {
//uint constant public gameCost = 0.1 ether;
uint constant public gameCost = 10;
uint8 public boardSize = 3;
uint8 movesCounter;
bool gameActive;
address[3][3] board;
address public player1;
address public player2;
uint balanceToWithdrawPlayer1;
uint balanceToWithdrawPlayer2;
uint timeToReact = 30 minutes;
uint gameValidUntil;
address activePlayer;
event PlayerJoined(address player);
event NextPlayer(address player);
event GameOverWithWin(address winner);
event GameOverWithDraw();
event PayoutSuccess(address receiver, uint amountInWei);
constructor() public payable {
player1 = msg.sender;
require(msg.value == gameCost);
gameValidUntil = now+timeToReact;
}
function joinGame() public payable {
assert(player2 == address(0));
gameActive = true;
require(msg.value == gameCost);
player2 = msg.sender;
emit PlayerJoined(player2);
if(block.number % 2 == 0) {
activePlayer = player2;
} else {
activePlayer = player1;
}
gameValidUntil = now + timeToReact;
emit NextPlayer(activePlayer);
}
function getBoard() public view returns(address[3][3]) {
return board;
}
function setWinner(address player) private {
gameActive = false;
//emit an event
emit GameOverWithWin(player);
uint balanceToPayOut = address(this).balance;
if(player.send(balanceToPayOut) != true) {
if(player == player1) {
balanceToWithdrawPlayer1 = balanceToPayOut;
} else {
balanceToWithdrawPlayer2 = balanceToPayOut;
}
} else {
emit PayoutSuccess(player, balanceToPayOut);
}
//transfer money to the winner
}
function withdrawWin() public {
uint balanceToTransfer;
if(msg.sender == player1) {
require(balanceToWithdrawPlayer1 > 0);
balanceToTransfer = balanceToWithdrawPlayer1;
balanceToWithdrawPlayer1 = 0;
player1.transfer(balanceToTransfer);
emit PayoutSuccess(player1, balanceToTransfer);
} else {
require(balanceToWithdrawPlayer2 > 0);
balanceToTransfer = balanceToWithdrawPlayer2;
balanceToWithdrawPlayer2 = 0;
player2.transfer(balanceToTransfer);
emit PayoutSuccess(player2, balanceToTransfer);
}
}
function setDraw() private {
gameActive = false;
emit GameOverWithDraw();
uint balanceToPayOut = address(this).balance/2;
if(player1.send(balanceToPayOut) == false) {
balanceToWithdrawPlayer1 += balanceToPayOut;
} else {
emit PayoutSuccess(player1, balanceToPayOut);
}
if(player2.send(balanceToPayOut) == false) {
balanceToWithdrawPlayer2 += balanceToPayOut;
} else {
emit PayoutSuccess(player2, balanceToPayOut);
}
}
function emergecyCashout() public {
require(gameValidUntil < now);
require(gameActive);
setDraw();
}
function setStone(uint8 x, uint8 y) public {
require(board[x][y] == address(0));
require(gameValidUntil > now);
assert(gameActive);
assert(x < boardSize);
assert(y < boardSize);
require(msg.sender == activePlayer);
board[x][y] = msg.sender;
movesCounter++;
gameValidUntil = now + timeToReact;
for(uint8 i = 0; i < boardSize; i++) {
if(board[i][y] != activePlayer) {
break;
}
//win
if(i == boardSize - 1) {
//winner
setWinner(activePlayer);
return;
}
}
for(i = 0; i < boardSize; i++) {
if(board[x][i] != activePlayer) {
break;
}
//win
if(i == boardSize - 1) {
//winner
setWinner(activePlayer);
return;
}
}
//diagonale
if(x == y) {
for(i = 0; i < boardSize; i++) {
if(board[i][i] != activePlayer) {
break;
}
//win
if(i == boardSize - 1) {
//winner
setWinner(activePlayer);
return;
}
}
}
//anti-diagonale
if((x+y) == boardSize-1) {
for(i = 0; i < boardSize; i++) {
if(board[i][(boardSize-1)-i] != activePlayer) {
break;
}
//win
if(i == boardSize - 1) {
//winner
setWinner(activePlayer);
return;
}
}
}
//draw
if(movesCounter == (boardSize**2)) {
//draw
setDraw();
return;
}
if(activePlayer == player1) {
activePlayer = player2;
} else {
activePlayer = player1;
}
emit NextPlayer(activePlayer);
}
}