0

I am trying to implement a TicTacToe smart contract:

pragma solidity ^0.4.19;

contract TicTacToe {
    uint8 public boardSize = 3;
    address[3][3] board;

    address public player1;
    address public player2;

    constructor() public {
        player1 = msg.sender;
    }

    function joinGame() public payable {
        assert(player2 == address(0)); 
        player2 = msg.sender;
    }
}

However, in the joinGame function the assert(player2 == address(0)); does not throw an error when I try to join a game using a different address from the one that initially joined the game. It basically replaces the player2 var with the new address that joined instead of throwing an error (since there should be only 2 players in the game)

Please help thanks!

  • 2
    I put this on remix and it works as expected, It doesn't allow me to join the game when player 2 is different from zero, which is what the code is supposed to do. On another topic, you may want to use require instead. – Jaime May 30 '18 at 14:23
1

I believe what you want to do is replace:

assert(player2 == address(0));

with:

require(player1 != msg.sender && player2 == address(0));

This check makes sure that player1 cannot be player2 and player2 is available.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.