0

The if statement in my for loop code is not filtering as intended, I want to believe I am missing something here but I can not seem to figure it out.

Here is the code, I hid the unnecessary code:

contract SolahParchiThap {
address owner = msg.sender;
address[4] p;
uint8[4][4] aparchis;
uint endgame;
uint newgame;
uint startTime;
uint8 turn;
uint id;
uint8 value;

modifier onlyOwner {require(msg.sender == owner); _;}

mapping (address => uint8[4]) _players;
mapping(uint => mapping(address => bool)) allPlayers;
mapping(address => uint) users;
mapping(address => uint256) wins;

The issue is with the code below, the claimWin function. A win is only valid when a player in the game has at least one of the numbers in his/her array[] equal to 4. For example, _players contains a mapping of the four players to the following numbers for each person.

[["4","0","0","0"],["0","2","1","1"],["0","1","2","1"],["0","1","1","2"]]

for every player of the game,

clearly, the current winner is player 1, i.e array[0] because he has on number that is 4.

when I call the below function player two is able to claim a win, and sometimes the code goes straight to the revert() code line.

// To claim win
function claimWin() public {
    uint v = checkP();
    require (v != 7, "You are not a valid member of this game");

    uint8[4] memory dt =_players[msg.sender];

    for (uint8 e; e < 4; e++){
        if(dt[e] == 4){
            wins[msg.sender] += 1;
        } else {
            revert();
        }
    } 
}


//To end the game
function endGame() public {
    newgame = 0;
    p[0] = p[1] = p[2] = p[3] = address(0);
}

function checkP() view internal returns (uint8){
    for (uint8 i; i < 4; i++){
        if(msg.sender == p[i]) return i;
    } return 7;
}


 function newGame() internal {
    newgame = 1;
}

}

1 Answer 1

0

You need to add "return" after wins[msg.sender] += 1; and move revert() after for

function claimWin() public {
    uint v = checkP();
    require (v != 7, "You are not a valid member of this game");

    uint8[4] memory dt =_players[msg.sender];

    for (uint8 e; e < 4; e++){
        if(dt[e] == 4){
            wins[msg.sender] += 1;
            return; // add it
        }
    } 
    revert();
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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