0

So basically my game is a glorified tic tac toe game, the board is 4X4, which would mean 16 empty spaces in total. To determine the win condition the player needs to meet al conditions created and then a bool will confirm as true if conditions are met. This is the bool for the win condition and the error is here somewhere I just don't know what I did wrong:

bool Won(Seed currentPlayer)
{
    bool hasWon = false;

    int[,] allConditions = new int[10, 4] { {0, 1, 2, 3 }, { 4, 5, 6, 7}, { 8, 9, 10, 11}, { 12, 13, 14, 15 },
                                         { 0, 4, 8, 12}, { 1, 5, 9, 13}, { 2, 6, 10, 14 }, {3, 7, 11, 15 },
                                         {0, 5, 10, 15},{3, 6, 9, 12} };

    //Conditions protocol being checked to ensure parameters
    for (int i = 0; i < 16; i++)
    {
        if (player[allConditions[i, 0]] == currentPlayer &
            player[allConditions[i, 1]] == currentPlayer &
            player[allConditions[i, 2]] == currentPlayer & // Double check for error if there is one
            player[allConditions[i, 3]] == currentPlayer)
        {
            hasWon = true; 

            pos1 = allSpawns[allConditions[i, 0]].transform.position;
            pos2 = allSpawns[allConditions[i, 3]].transform.position;
            break;
        }
    }
    return hasWon;
    
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • 3
    you have `allConditions = new int[10, 4]` but try to iterate `[allConditions[i, 0]` for `0 <= i < 16` ... => for any `i > 9` you will get the exception already – derHugo May 28 '21 at 08:01
  • 1
    The debugger is perfect for issues like this. – Retired Ninja May 28 '21 at 08:02
  • 2
    Also in general you don't want to compare two `bool` values using bitwise `&` but rather using logical `&&` – derHugo May 28 '21 at 08:04

0 Answers0