-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXOGameBoard.sol
153 lines (138 loc) · 5.81 KB
/
XOGameBoard.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
pragma solidity ^0.4.17;
contract XOGameBoard{
address internal player1; // игрок 1, он инициирует игру
address internal player2; // игрок 2, он присоединяется к игре
uint8[9] internal board; // игровая доска
uint8 internal step; // текущий номер хода в игре
uint8 internal chip;
enum StatusGame{PLAYER1, PLAYER2}
StatusGame statusGame;
// модификатор прав владельуа контракта
modifier isPlayer {
require(player1 == msg.sender || player2 == msg.sender);
_;
}
event RunPlayer(string _player);
event Winner(string _player);
function XOGameBoard(address player) public payable{
player1 = player;
step = 0;
statusGame = StatusGame.PLAYER1;
}
function setRate(address player) external payable{
player2 = player;
}
// Game Board //
// 1 2 3
// 4 5 6
// 7 8 9
// Check who winner in game
function checkWinner(uint8 pos) internal isPlayer returns(uint8){
uint8 _pos;
if(msg.sender == player1 && statusGame == StatusGame.PLAYER1){
chip = 1;
statusGame = StatusGame.PLAYER2;
emit RunPlayer("Next step Player 2");
}
if(msg.sender == player2 && statusGame == StatusGame.PLAYER2){
chip = 2;
statusGame = StatusGame.PLAYER1;
emit RunPlayer("Next step Player 1");
}
_pos = pos - 1;
board[_pos] = chip;
if(step == 5 && step < 9){
if(_pos == 0){ return cell1(chip); }
if(_pos == 1){ return cell2(chip); }
if(_pos == 2){ return cell3(chip); }
if(_pos == 3){ return cell4(chip); }
if(_pos == 4){ return cell5(chip); }
if(_pos == 5){ return cell6(chip); }
if(_pos == 6){ return cell7(chip); }
if(_pos == 7){ return cell8(chip); }
if(_pos == 8){ return cell9(chip); }
}
if(step == 9){ return 3; }
}
function cell1(uint8 move) internal view returns(uint8){
if(board[0] == move && board[1] == move && board[2] == move){return move;}
if(board[0] == move && board[3] == move && board[6] == move){return move;}
if(board[0] == move && board[4] == move && board[8] == move){return move;}
}
function cell2(uint8 move) internal view returns(uint8){
if(board[0] == move && board[1] == move && board[2] == move){return move;}
if(board[1] == move && board[4] == move && board[7] == move){return move;}
}
function cell3(uint8 move) internal view returns(uint8){
if(board[0] == move && board[1] == move && board[2] == move){return move;}
if(board[2] == move && board[5] == move && board[8] == move){return move;}
if(board[2] == move && board[4] == move && board[6] == move){return move;}
}
function cell4(uint8 move) internal view returns(uint8){
if(board[0] == move && board[3] == move && board[6] == move){return move;}
if(board[3] == move && board[4] == move && board[5] == move){return move;}
}
function cell5(uint8 move) internal view returns(uint8){
if(board[1] == move && board[4] == move && board[7] == move){return move;}
if(board[3] == move && board[4] == move && board[5] == move){return move;}
if(board[0] == move && board[4] == move && board[8] == move){return move;}
if(board[2] == move && board[4] == move && board[6] == move){return move;}
}
function cell6(uint8 move) internal view returns(uint8){
if(board[2] == move && board[5] == move && board[8] == move){return move;}
if(board[3] == move && board[4] == move && board[5] == move){return move;}
}
function cell7(uint8 move) internal view returns(uint8){
if(board[0] == move && board[3] == move && board[6] == move){return move;}
if(board[2] == move && board[4] == move && board[6] == move){return move;}
if(board[6] == move && board[7] == move && board[8] == move){return move;}
}
function cell8(uint8 move) internal view returns(uint8){
if(board[1] == move && board[4] == move && board[7] == move){return move;}
if(board[6] == move && board[7] == move && board[8] == move){return move;}
}
function cell9(uint8 move) private view returns(uint8){
if(board[2] == move && board[5] == move && board[8] == move){return move;}
if(board[0] == move && board[4] == move && board[8] == move){return move;}
if(board[6] == move && board[7] == move && board[8] == move){return move;}
}
function move(uint8 _pos) public isPlayer{
step++;
if(checkWinner(_pos) == 1){ // player1 winner
emit Winner("Winner Player 1");
selfdestruct(player1);
}
if(checkWinner(_pos) == 2){ // player2 winner
emit Winner("Winner Player 2");
selfdestruct(player2);
}
if(checkWinner(_pos) == 3){ // drawe
emit Winner("DRAWE");
player2.transfer(address(this).balance / 2);
selfdestruct(player1);
}
}
// emulation of the 3x3 game board
function move3x3(uint8 row, uint8 col) public isPlayer{
require(row <=3 || col <= 3);
if(row == 0){
if(col == 0) move(1);
if(col == 1) move(2);
if(col == 2) move(3);
}
if(row == 1){
if(col == 0) move(4);
if(col == 1) move(5);
if(col == 2) move(6);
}
if(row == 3){
if(col == 0) move(7);
if(col == 1) move(8);
if(col == 2) move(9);
}
}
// return gameboard
function getBoard() public view isPlayer returns(uint8[9]){
return board;
}
}