Created
December 2, 2017 22:43
-
-
Save dglittle/4724928ca1fdcf1a61edafbd9958a071 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.0; | |
contract TicTacToe { | |
struct Game { | |
address[2] players; | |
uint256 prize_pool; | |
uint256[9] board; | |
} | |
mapping(uint256 => Game) games; | |
function TicTacToe() { | |
} | |
function join(uint256 game_id, uint256 player_num) public payable { | |
require(msg.value == 1000000000000000000); | |
require(player_num == 0 || player_num == 1); | |
require(games[game_id].players[player_num] == 0); | |
games[game_id].prize_pool += msg.value; | |
games[game_id].players[player_num] = msg.sender; | |
} | |
function move(uint256 game_id, uint256 player_num, uint256 go_here) public { | |
require(games[game_id].prize_pool > 0); | |
require(games[game_id].players[player_num] == msg.sender); | |
uint256 moves = 0; | |
for (uint256 i = 0; i < 9; i++) { | |
if (games[game_id].board[i] != 0) { | |
moves++; | |
} | |
} | |
if (player_num == 0) { | |
require(moves % 2 == 0); | |
} else { | |
require(moves % 2 == 1); | |
} | |
require(games[game_id].board[go_here] == 0); | |
uint256 my_piece = player_num + 1; | |
games[game_id].board[go_here] = my_piece; | |
if ((games[game_id].board[0] == my_piece && | |
games[game_id].board[1] == my_piece && | |
games[game_id].board[2] == my_piece) || | |
(games[game_id].board[3] == my_piece && | |
games[game_id].board[4] == my_piece && | |
games[game_id].board[5] == my_piece) || | |
(games[game_id].board[6] == my_piece && | |
games[game_id].board[7] == my_piece && | |
games[game_id].board[8] == my_piece) || | |
(games[game_id].board[0] == my_piece && | |
games[game_id].board[3] == my_piece && | |
games[game_id].board[6] == my_piece) || | |
(games[game_id].board[1] == my_piece && | |
games[game_id].board[4] == my_piece && | |
games[game_id].board[7] == my_piece) || | |
(games[game_id].board[2] == my_piece && | |
games[game_id].board[5] == my_piece && | |
games[game_id].board[8] == my_piece) || | |
(games[game_id].board[0] == my_piece && | |
games[game_id].board[4] == my_piece && | |
games[game_id].board[8] == my_piece) || | |
(games[game_id].board[2] == my_piece && | |
games[game_id].board[4] == my_piece && | |
games[game_id].board[6] == my_piece)) { | |
msg.sender.transfer(games[game_id].prize_pool); | |
games[game_id].prize_pool = 0; | |
} else if (moves == 8) { | |
games[game_id].players[0].transfer(games[game_id].prize_pool / 2); | |
games[game_id].players[1].transfer(games[game_id].prize_pool / 2); | |
games[game_id].prize_pool = 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment