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_gamefunction avalueof eth they would like to bet, as well as an address of theiropponentand an amount of time in seconds until the game challenge is expired. - The
challengeris returned an integergame_idof the created game. - The
challengerwill get the second turn in the game.
- The opponent address (the
challenged) can call theaccept_challengefunction with thegame_idof the game created by theirchallenger, passing avalueof eth >= the value sent by thechallenger. - The
challengedwill be refunded any amount above the value sent by thechallenger. - If this function succeeds, the game will be ready to be played.
- If the
challengedfails to call theaccept_challengefunction before the game is expired, thechallengerwill be able to call thecancel_challengefunction to be refunded the eth they deposited.
- Once a game is created by a
challengerand then accepted by thechallenged, each player alternates calling theplay_a_turnfunction, starting with thechallenged. - The
play_a_turnfunction takes agame_idof the game to take a turn on, and alocationinteger 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
challengervs thechallengedcallingplay_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_turnchecks 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.