-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiSOS.SOL
284 lines (237 loc) · 10.9 KB
/
MultiSOS.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//Δρούγκα Σοφία f3312105
//Αθανασίου Λυδία f3312102
pragma solidity >=0.7.0;
contract CryptoSOS {
//players data, define player as data structure
uint constant START_PRICE = 1 ether;
enum BoardSquareState {Empty, S, O}
uint8 private constant EMPTY = 0;
address game_owner;
uint32 private counter;
uint32 gameID = 1;
//mapping to games
mapping (uint32 => Game) private games;
constructor()
{
game_owner = msg.sender;
}
struct Game {
// player 1 plays first
uint8 current_move;
BoardSquareState[3][3] squareBoard;
// Player 1
gamePlayer p1;
// Player 2
gamePlayer p2;
//player's turn
uint8 active_player;
//game is in progress
bool gameIsActive;
//game has finished
bool gameIsOver ;
bool cancel_request;
bool game_move;
bool ur2slow_request ;
bool gameIsRunning;
uint start_time;
uint game_duration;
uint move;
uint time_to_request_cancel;
uint time_start_of_ur2slow;
uint latest_move;
uint8 reservedFunds;
uint256 deposit;
bool collected;
}
//requested events
event StartGame(address,address, uint32);
event Move(address,uint8, uint8, uint32);
event Winner(address);
struct gamePlayer
{
uint256 gamePlayerId;
address gamePlayerAddess;
bool gamePlayerIsPresent;
}
function play() public payable {
Game storage game = games[counter];
//1 Ether required to play
require (msg.value == START_PRICE);
require (!game.p1.gamePlayerIsPresent || !game.p2.gamePlayerIsPresent, "no one has in place yet");
if (games[gameID].p1.gamePlayerAddess == address(0x0)){
games[gameID].p1.gamePlayerAddess = msg.sender;
games[gameID].p1.gamePlayerIsPresent = true;
games[gameID].start_time = block.timestamp;
games[gameID].move = 1;
games[gameID].gameIsOver = false;
games[gameID].collected = false;
emit StartGame(games[gameID].p1.gamePlayerAddess, address(0x0), gameID);
} else if (games[gameID].p2.gamePlayerAddess == address(0x0) && !games[gameID].gameIsOver ) {
require(games[gameID].p1.gamePlayerAddess != msg.sender, "You ve been already registered as player 1");
games[gameID].p2.gamePlayerAddess = msg.sender;
games[gameID].p2.gamePlayerIsPresent = true;
games[gameID].gameIsRunning = true;
games[gameID].deposit = msg.value * 2 ether;
games[gameID].game_move = false;
emit StartGame(games[gameID].p1.gamePlayerAddess, games[gameID].p2.gamePlayerAddess,gameID);
gameID = gameID + 1;
} else {
// if last game was cancelled because no player2 entered, start a new game
gameID = gameID + 1;
games[gameID].p1.gamePlayerAddess = msg.sender;
games[gameID].start_time = block.timestamp;
games[gameID].move = 1;
games[gameID].gameIsOver = false;
games[gameID].collected = false;
games[gameID].game_move = false;
emit StartGame(games[gameID].p1.gamePlayerAddess, address(0x0), gameID);
}
}
function reserve_a_game(address addr) internal view returns (uint32){
uint32 runningGameID = 1;
while (runningGameID <= gameID) {
if(games[runningGameID].p1.gamePlayerAddess == addr || games[runningGameID].p2.gamePlayerAddess == addr){
return runningGameID;
}
runningGameID++;
}
return 0;
}
function placeO(uint8 position) public{
uint32 myId = reserve_a_game(msg.sender);
uint8 xpos;
uint8 ypos;
require(myId != 0, "You are not in any game" );
require(games[myId].gameIsRunning, "Wait for player 2");
require(games[myId].p1.gamePlayerIsPresent && games[myId].p2.gamePlayerIsPresent, "The game must be active.");
require(!games[myId].gameIsOver, "Game has finished!");
require (msg.sender == games[myId].p1.gamePlayerAddess || msg.sender == games[myId].p2.gamePlayerAddess );
require(games[myId].squareBoard[xpos][ypos]== BoardSquareState.Empty, "square");
require(position>=1 && position<=9);
xpos = (position-1)/3;
ypos = (position-1)%3;
games[myId].squareBoard[xpos][ypos] =BoardSquareState.O;
games[myId].game_move = true;
emit Move(msg.sender, position, 2, gameID);
games[myId].move = games[myId].move+1;
if (isWinner()) {
games[myId].gameIsOver = true;
//msg.sender.transfer(address(this).balance-0.2 ether);
(bool success, ) = msg.sender.call{value: 1.9 ether}("");
emit Winner(msg.sender);
} else if( games[myId].move>9){
games[myId].gameIsOver = true;
emit Winner(address(0x0));
}
}
function placeS(uint8 position) public{
uint32 myId = reserve_a_game(msg.sender);
require(myId != 0, "You are not in any game" );
uint8 xpos;
uint8 ypos;
require( games[myId].p1.gamePlayerIsPresent && games[myId].p2.gamePlayerIsPresent, "The game must be active.");
require(! games[myId].gameIsOver, "Game has finished!");
require (msg.sender == games[myId].p1.gamePlayerAddess || msg.sender == games[myId].p2.gamePlayerAddess );
require(position>=1 && position<=9);
xpos = (position-1)/3;
ypos = (position-1)%3;
require(games[myId].squareBoard[xpos][ypos]== BoardSquareState.Empty, "square");
games[myId].squareBoard[xpos][ypos] = BoardSquareState.S;
games[myId].game_move = true;
emit Move(msg.sender, position, 2, gameID);
games[myId].move = games[myId].move+1;
if (isWinner()) {
games[myId].gameIsOver = true;
(bool success, ) = msg.sender.call{value: 1.9 ether}("");
}else if(games[myId].move>9){
games[myId].gameIsOver = true;
emit Winner(address(0x0));
}
}
function collectProfit() public payable {
require(msg.sender == game_owner, "You are not the owner of the contract");
require(games[gameID].gameIsOver, "Game has not ended.");
// msg.sender.transfer(address(this).balance);
(bool success, ) = msg.sender.call{value: address(this).balance - games[gameID].reservedFunds*10**18}(""); //- reserve*10**18
}
function cancel() public payable {
require(games[gameID].time_to_request_cancel - games[gameID].start_time> 120 seconds, "Not time yet to request cancel");
require(!games[gameID].gameIsOver, "Game has alreadey completed.");
require(games[gameID].p2. gamePlayerAddess == address(0x0), "Game has already started");
require(address(this).balance > games[gameID].reservedFunds*10**18, "Not enough funds.");
games[gameID].gameIsOver = true;
//msg.sender.transfer(address(this).balance);
(bool success, ) = msg.sender.call{value: 1.8 ether}("");
}
function ur2slow() public payable{
games[gameID].time_start_of_ur2slow = block.timestamp;
require(! games[gameID].gameIsOver, "Game has ended.");
require( games[gameID].gameIsActive, "game is not active");
// require(msg.sender == opponentPlayer(getCurrentPlayer(move)), "it's your turn to play");
require( games[gameID].game_move,"There is no player yet");
require( games[gameID].time_start_of_ur2slow - games[gameID].latest_move > 60 seconds, "Not yet time to request slow");
games[gameID].gameIsOver = true;
// msg.sender.transfer(address(this).balance-0.1 ether);
(bool success, ) = msg.sender.call{value: 1.8 ether}("");
//(bool success, ) = msg.sender.call.ballance;
emit Move(msg.sender,1,1, gameID);
}
function opponentPlayer(address current_player) internal view returns (address) {
require( games[gameID].gameIsActive, "Wait for player 2");
if (current_player == games[gameID].p1.gamePlayerAddess) {
return games[gameID].p2.gamePlayerAddess;
} else if (current_player == games[gameID].p2.gamePlayerAddess) {
return games[gameID].p1.gamePlayerAddess;
} else {
revert("Invalid player.");
}
}
// returns the current player
function getCurrentPlayer(uint8 _current_move) internal view returns (address) {
if (_current_move % 2 == 0) {
return games[gameID].p2.gamePlayerAddess;
}
else {
return games[gameID].p1.gamePlayerAddess;
}
}
function isWinner() internal view returns (bool) {
if ( games[gameID].squareBoard[0][0] == BoardSquareState.S){
if ((( games[gameID].squareBoard[0][1] ==BoardSquareState.O ) && ( games[gameID].squareBoard[0][2] == BoardSquareState.S )) || (( games[gameID].squareBoard[1][0] == BoardSquareState.O ) && ( games[gameID].squareBoard[2][0] == BoardSquareState.S ))){
return true;
}
}
if ( games[gameID].squareBoard[2][2] == BoardSquareState.S){
if ((( games[gameID].squareBoard[1][2] == BoardSquareState.O ) && ( games[gameID].squareBoard[0][2] == BoardSquareState.S ) ) || (( games[gameID].squareBoard[2][1] == BoardSquareState.O ) && ( games[gameID].squareBoard[2][0] == BoardSquareState.S )) ){
return true;
}
}
if ( games[gameID].squareBoard[1][1] == BoardSquareState.O){
//Checks the diagonals
// if board[1][[1]] == SquareState.S no possible winner diagonally
if ((( games[gameID].squareBoard[0][0] == BoardSquareState.S) && ( games[gameID].squareBoard[2][2] == BoardSquareState.S )) || (( games[gameID].squareBoard[0][2] == BoardSquareState.S) && ( games[gameID].squareBoard[2][0] == BoardSquareState.S ))) {
return true;
}
// checks the 2nd row and the second column of the board
if ((( games[gameID].squareBoard[1][0] == BoardSquareState.S) && ( games[gameID].squareBoard[1][2] == BoardSquareState.S)) || (( games[gameID].squareBoard[0][1] == BoardSquareState.S) && ( games[gameID].squareBoard[2][1] == BoardSquareState.S))) {
return true;
}
}
}
function positionToString(uint8 xpos, uint8 ypos) internal returns (string memory) {
if (games[gameID].squareBoard[xpos][ypos] == BoardSquareState.Empty) {
return ("-");
}
if (games[gameID].squareBoard[xpos][ypos] == BoardSquareState.S) {
return ("S");
}
if (games[gameID].squareBoard[xpos][ypos] ==BoardSquareState.O) {
return ("O");
}
}
function getGameState() public returns (string memory) {
return (string(abi.encodePacked(positionToString(0,0), " | ", positionToString(0,1), " | ",positionToString(0,2), " | ",
positionToString(1,0), "|" , positionToString(1,1),"|", positionToString(1,2),"|", positionToString(2,0),"|",
positionToString(2,1),"|",positionToString(2,2) )));
}
}