-
Notifications
You must be signed in to change notification settings - Fork 69
/
tictactoe.html
255 lines (214 loc) · 14.7 KB
/
tictactoe.html
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
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/milligram/1.2.3/milligram.min.css">
</head>
<body class="container" style="padding-top: 3%;">
<h2><a href="index.html">ethjs</a></h2>
<h4>TicTacToe</h4>
<hr />
<div id="startGameContainer">
<h5>Start Game</h5>
<div class="row">
<div class="column">
<label>Player 1</label>
<select id="player1" class="addressSelector"></select>
</div>
<div class="column">
<label>Player 2</label>
<select id="player2" class="addressSelector"></select>
</div>
</div>
<button id="startGame">Start Game</button>
</div>
<div id="gameContainer" style="display: none;">
<h5>Make Move</h5>
<div class="row">
<div class="column">
<label>Player Address<label>
<span id="player">loading...</span>
</div>
<div class="column">
<label>Turn<label>
player <span id="turn"></span>
</div>
<div class="column">
<label>Winner<label>
<span id="won">none</span>
</div>
</div>
<hr />
<div style="width: 300px;">
<div data-row="0" class="row" style="height: 100px; background: #F1F1F1;">
<div id="b00" data-col="0" class="block column column-33" style="border-right: 2px solid #aaa"></div>
<div id="b01" data-col="1" class="block column column-33" style="border-right: 2px solid #aaa"></div>
<div id="b02" data-col="2" class="block column column-33"></div>
</div>
<div data-row="1" class="row" style="border-top: 2px solid #aaa; height: 100px; background: #F1F1F1;">
<div id="b10" data-col="0" class="block column column-33" style="border-right: 2px solid #aaa"></div>
<div id="b11" data-col="1" class="block column column-33" style="border-right: 2px solid #aaa"></div>
<div id="b12" data-col="2" class="block column column-33"></div>
</div>
<div data-row="2" class="row" style="border-top: 2px solid #aaa; height: 100px; background: #F1F1F1;">
<div id="b20" data-col="0" class="block column column-33" style="border-right: 2px solid #aaa"></div>
<div id="b21" data-col="1" class="block column column-33" style="border-right: 2px solid #aaa"></div>
<div id="b22" data-col="2" class="block column column-33"></div>
</div>
</div>
</div>
<div id="response"></div>
<script type="text/javascript" src="ethereumjs-testrpc.js"></script>
<script type="text/javascript" src="ethjs.js"></script>
<script type="text/javascript">
var eth = new Eth(TestRPC.provider());
var el = function(id){ return document.querySelector(id); };
var TicTacToeABI = JSON.parse('[{\"constant\":false,\"inputs\":[{\"name\":\"_row\",\"type\":\"uint256\"},{\"name\":\"_column\",\"type\":\"uint256\"}],\"name\":\"move\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"addressOf\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"payout\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_player\",\"type\":\"uint256\"}],\"name\":\"hasWon\",\"outputs\":[{\"name\":\"gameWon\",\"type\":\"bool\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"uint256\"},{\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"board\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"turn\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"address\"}],\"name\":\"players\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"_player1\",\"type\":\"address\"},{\"name\":\"_player2\",\"type\":\"address\"}],\"type\":\"constructor\"},{\"payable\":true,\"type\":\"fallback\"}]');
var TicTacToeBytecode = '606060408181526001600355806109c4833960a081529051608051600160a060020a03808316600090815260016020818152868320919091559183168152938420600290819055908190527fe90b7bceb6e7df5418fb78d8ee546e97c83a08bbccc01a0644d599ccd2a7c2e080546c01000000000000000000000000948502859004600160a060020a0319918216179091559084527f679795a0195a1b76cdebb7c51d74e058aee92919b8c3389af86ef24535e8a28c80549284029390930491161790556108f29081906100d290396000f3606060405236156100615760e060020a60003504630867408f811461006757806311a800bc1461014057806363bd1d4a1461016657806375eeadc31461077b5780638086a92a1461078b5780638b299903146107b3578063e2eb41ff146107c1575b6107de5b565b34610002576107de600435602435600160a060020a033316600090815260016020526040812054839183911180156100a0575060028211155b80156100ad575060028111155b80156100ce5750600082815260208181526040808320848452909152902054155b80156100f35750600160a060020a033316600090815260016020526040902054600354145b1561082e57600160a060020a033316600081815260016020818152604080842054898552848352818520898652835290842081905593909252908190521415610822576002600355610828565b34610002576107e0600435600260205260009081526040902054600160a060020a031681565b34610002576107de600160a060020a033316600090815260016020526040902054610833905b60008080526000805160206108d28339815191526020527fed428e1c45e1d9561b62834e1a2d3015a0caae3bfdc16b4da059ac885b01a1455482148015610209575060016000526000805160206108d28339815191526020527e9316a6b27b778fc9c4cd520e332fce845aa0089877f87cfe8c1d77fa4cc1105482145b801561024c575060026000526000805160206108d28339815191526020527f09f922a082099d8bb3cef76f214b97d4ea18be3ab11deb731e4676110a885b485482145b806103005750600080526000805160206108928339815191526020527f871b3b0713ea5d7fd1f75a811c349209996193f3a2535cdfd79d35416bf8012354821480156102bd575060016000526000805160206108928339815191526020526000805160206108b28339815191525482145b8015610300575060026000526000805160206108928339815191526020527fb37150ceb7e138645bfe4dfcef9a75073e1d7aa14c524dfdd20d3f751fad10845482145b806103c65750600080526000805160206108728339815191526020527f5876eea3bc0f5fdc8a8f5162bbf9ae0de1b6932ac4f065fb87ba4934bb51e90d5482148015610383575060016000526000805160206108728339815191526020527f074f1a94432de106f4a8e9fe8ba3e1cfc1d83e37bec74dfb1723c4b3a135d7235482145b80156103c6575060026000526000805160206108728339815191526020527f96d8f21026e23863466cad7746a0becd8e1c681e868774bcd95eb153dd75d10c5482145b8061048a5750600080526000805160206108d28339815191526020527fed428e1c45e1d9561b62834e1a2d3015a0caae3bfdc16b4da059ac885b01a14554821480156104485750600080526000805160206108928339815191526020527f871b3b0713ea5d7fd1f75a811c349209996193f3a2535cdfd79d35416bf801235482145b801561048a5750600080526000805160206108728339815191526020527f5876eea3bc0f5fdc8a8f5162bbf9ae0de1b6932ac4f065fb87ba4934bb51e90d5482145b8061053e575060016000526000805160206108d28339815191526020527e9316a6b27b778fc9c4cd520e332fce845aa0089877f87cfe8c1d77fa4cc11054821480156104fb575060016000526000805160206108928339815191526020526000805160206108b28339815191525482145b801561053e575060016000526000805160206108728339815191526020527f074f1a94432de106f4a8e9fe8ba3e1cfc1d83e37bec74dfb1723c4b3a135d7235482145b80610605575060026000526000805160206108d28339815191526020527f09f922a082099d8bb3cef76f214b97d4ea18be3ab11deb731e4676110a885b4854821480156105c2575060026000526000805160206108928339815191526020527fb37150ceb7e138645bfe4dfcef9a75073e1d7aa14c524dfdd20d3f751fad10845482145b8015610605575060026000526000805160206108728339815191526020527f96d8f21026e23863466cad7746a0becd8e1c681e868774bcd95eb153dd75d10c5482145b806106b95750600080526000805160206108d28339815191526020527fed428e1c45e1d9561b62834e1a2d3015a0caae3bfdc16b4da059ac885b01a1455482148015610676575060016000526000805160206108928339815191526020526000805160206108b28339815191525482145b80156106b9575060026000526000805160206108d28339815191526020527f09f922a082099d8bb3cef76f214b97d4ea18be3ab11deb731e4676110a885b485482145b8061076d5750600080526000805160206108728339815191526020527f5876eea3bc0f5fdc8a8f5162bbf9ae0de1b6932ac4f065fb87ba4934bb51e90d548214801561072a575060016000526000805160206108928339815191526020526000805160206108b28339815191525482145b801561076d575060026000526000805160206108728339815191526020527f96d8f21026e23863466cad7746a0becd8e1c681e868774bcd95eb153dd75d10c5482145b15610776575060015b919050565b34610002576107fc60043561018c565b3461000257610810600435602435600060208181529281526040808220909352908152205481565b346100025761081060035481565b346100025761081060043560016020526000908152604090205481565b005b60408051600160a060020a039092168252519081900360200190f35b604080519115158252519081900360200190f35b60408051918252519081900360200190f35b60016003555b50505050565b610002565b151561083e57610002565b604051600160a060020a0333811691309091163180156108fc02916000818181858888f1935050505015156100655761000256abbb5caa7dda850e60932de0934eb1f9d0f59695050f761dc64e443e5030a569ada5013122d395ba3c54772283fb069b10426056ef8ca54750cb9bb552a59e7de3be84349242383ed4b31bbb7ede08a8080ac9d5e828342f449d34bf731539e9ad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5';
var gameInstance;
var renderInterval;
var playerWon;
function renderBlock(row, column) {
gameInstance.board(row, column).then(function(player){
var block = el('#b' + String(row) + '' + String(column));
if (player[0].toString(10) == '1') {
block.style.background = '#F22738';
}
if (player[0].toString(10) == '2') {
block.style.background = '#000';
}
});
}
function render() {
for(var row = 0; row < 3; row ++) {
for (var column = 0; column < 3; column ++) {
renderBlock(row, column);
}
}
gameInstance.turn().then(function(turnNumber){
el('#turn').innerHTML = turnNumber[0].toString(10);
gameInstance.addressOf(turnNumber[0]).then(function(playerAddress){
el('#player').innerHTML = playerAddress[0].substr(0, 15) + '...';
});
gameInstance.hasWon('1').then(function(player1HasWon){
gameInstance.hasWon('2').then(function(player2HasWon){
if (player1HasWon[0]) { playerWon = '1'; }
if (player2HasWon[0]) { playerWon = '2'; }
if (playerWon) {
el('#won').innerHTML = 'player ' + String(playerWon) + '!!';
}
});
});
});
}
function startGame() {
el('#startGameContainer').style.display = 'none';
el('#gameContainer').style.display = 'block';
document.querySelectorAll('.block').forEach(function(blockEl){
blockEl.addEventListener('click', function(block) {
var row = block.target.parentNode.dataset.row;
var col = block.target.dataset.col;
if (playerWon) { return; }
gameInstance.turn().then(function(turnNumber){
gameInstance.addressOf(turnNumber[0]).then(function(playerAddress){
gameInstance.move(row, col, { from: playerAddress[0] })
.then(function(txHash){
el('#response').innerHTML = 'making move with tx hash: ' + String(txHash);
render();
})
.catch(function(moveError){
el('#response').innerHTML = 'error while making move: ' + String(moveError);
});
});
});
});
});
renderInterval = setInterval(render, 3000);
render();
}
eth.accounts(function(accountsError, accounts){
var TicTacToe = eth.contract(TicTacToeABI, TicTacToeBytecode, { from: accounts[0], gas: '3000000' })
document.querySelectorAll('.addressSelector').forEach(function(selector){
accounts.forEach(function(account){
selector.innerHTML += '<option value="' + account + '">' + account + '</option>';
});
});
el('#startGame').addEventListener('click', function(){
if (el('#player1').value == el('#player2').value) {
return el('#response').innerHTML = 'The player addresses cannot be the same!';
}
TicTacToe.new(el('#player1').value, el('#player2').value,
function(deployError, txHash){
if (deployError) {
return el('#response').innerHTML = 'Hmm... there was an error: ' + String(deployError);
}
var receiptInterval = setInterval(function(){
eth.getTransactionReceipt(txHash, function(receiptError, receipt){
if (receipt) {
clearInterval(receiptInterval);
gameInstance = TicTacToe.at(receipt.contractAddress);
startGame();
}
});
}, 300);
});
});
});
</script>
<br /><br />
<pre><code>
pragma solidity ^0.4.4;
contract TicTacToe {
modifier validMove(uint256 _row, uint256 _column) {
if (players[msg.sender] > 0
&& _row <= 2
&& _column <= 2
&& board[_row][_column] == 0
&& turn == players[msg.sender]) {
_;
} else {
throw;
}
}
function () payable {}
function move(uint256 _row, uint256 _column) validMove(_row, _column) {
board[_row][_column] = players[msg.sender];
if (players[msg.sender] == 1) {
turn = 2;
} else {
turn = 1;
}
}
function hasWon(uint256 _player) public constant returns (bool gameWon) {
// horizontal lines
if ((board[0][0] == _player && board[0][1] == _player && board[0][2] == _player)
|| (board[1][0] == _player && board[1][1] == _player && board[1][2] == _player)
|| (board[2][0] == _player && board[2][1] == _player && board[2][2] == _player)
// verticle lines
|| (board[0][0] == _player && board[1][0] == _player && board[2][0] == _player)
|| (board[0][1] == _player && board[1][1] == _player && board[2][1] == _player)
|| (board[0][2] == _player && board[1][2] == _player && board[2][2] == _player)
// diagonal lines
|| (board[0][0] == _player && board[1][1] == _player && board[0][2] == _player)
|| (board[2][0] == _player && board[1][1] == _player && board[2][2] == _player)) {
return true;
}
}
function payout() {
if (!hasWon(players[msg.sender])) { throw; }
if (!msg.sender.send(this.balance)) {
throw;
}
}
function TicTacToe(address _player1, address _player2) {
players[_player1] = 1;
players[_player2] = 2;
addressOf[1] = _player1;
addressOf[2] = _player2;
}
mapping(uint256 => mapping(uint256 => uint256)) public board;
mapping(address => uint256) public players;
mapping(uint256 => address) public addressOf;
uint256 public turn = 1;
}
</code></pre>
</body>
</html>