An Ethereum smart contract which manages Tic Tac Toe games. Users challenge one another to games for an amount of eth, and are awarded for winning.
- A game is created when a wallet (the
challenger
) calls thecreate_game
function avalue
of eth they would like to bet, as well as an address of theiropponent
and an amount of time in seconds until the game challenge is expired. - The
challenger
is returned an integergame_id
of the created game. - The
challenger
will get the second turn in the game.
- The opponent address (the
challenged
) can call theaccept_challenge
function with thegame_id
of the game created by theirchallenger
, passing avalue
of eth >= the value sent by thechallenger
. - The
challenged
will be refunded any amount above the value sent by thechallenger
. - If this function succeeds, the game will be ready to be played.
- If the
challenged
fails to call theaccept_challenge
function before the game is expired, thechallenger
will be able to call thecancel_challenge
function to be refunded the eth they deposited.
- Once a game is created by a
challenger
and then accepted by thechallenged
, each player alternates calling theplay_a_turn
function, starting with thechallenged
. - The
play_a_turn
function takes agame_id
of the game to take a turn on, and alocation
integer 0-8 which corresponds to a board cell (see TicTacToe.sol). - When a game is won, the winner recieves the total amount of eth deposited for that game (double the amount they sent to create/accept the game -- their original amount as well as the amount sent by their opponent).
- If a game is tied, both players are refunded they amount they sent.
- It seems that there is a difference in gas cost for the
challenger
vs thechallenged
callingplay_a_function
(3123 gas for the challenged but only 3095 gas for the challenger). This might be due to player1's turn and player2's turns using different functions behind the hood, or something to do with howplay_a_turn
checks which players turn it is. - Testing could be a lot more exhaustive.
- The implementation of the player boards could probably be changed to use one uint32 instead of two uint16s -- might solve the gas issue.