2

Trying to make connect4 and I want to check if a column (vertical) is full, so the easy way to do it would be in an if with [i, 1] != O && [i, 2] != O && [i, 3] != O et cetera, but isn't there a more efficient way to go on about this?

A board looks like:

O O O O O O O O
O O O O O O O O
O O O O O O O O
O O O O O O O O
O O O O O O O O
O O O O O O O O

The for-loop:

for (int i = row - 1; i > -1; i--)
{
     if (board[i, column].ToString() == "O" && beurt % 2 == 0)
     {
         board[i, column] = (Veld)player1.color;
         beurt += 1;
         break;
     }
     else if(board[i, column].ToString() == "O" && beurt % 2 == 1)
     {
             board[i, column] = (Veld)player2.color;
             beurt += 1;
             break;
     }
     else if(???)
     {
             //???
     }
}
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
zhrgci
  • 584
  • 1
  • 6
  • 25

2 Answers2

4
bool isRowFull(char[,] array, int row)
{
    for(int i=0;i<array.GetLength(0); i++)
        if(array[row, i] == 'O') return false;
    return true;
}

And you can call it like:

bool isFull = isRowFull(board, 3); // check if row 3 is full 

or

if(isRowFull(board, 3))
{
    //your code
}

To Get First NonFull row:

int FirstNonFull(char[,] array)
{
    for(int i=0;i<array.GetLength(1);i++)
        if(!isRowFull(array, i)) return i;
    return -1; // -1 indicating that all rows are full (not found)
}
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • You don't need any { } on the for-loop or if? Sorry I'm kinda new to this. – zhrgci Mar 25 '19 at 14:17
  • @Zheng-rongCai - Not if it's only one statement. I still put them a lot of the time for consistency/readability, but technically you don't need to here. It also gets confusing incredibly quickly if you nest multiple loops/ifs and don't use brackets... – Broots Waymb Mar 25 '19 at 14:19
  • as there is only one line of code inside for, its not necessary to have {} (but you could). if there were more lines of code {} were necessary – Ashkan Mobayen Khiabani Mar 25 '19 at 14:20
  • note that `return true;` is outside of for loop, meaning that if function didn't return anything inside for loop (no zeros exist) and reached this far, then it is full so return true – Ashkan Mobayen Khiabani Mar 25 '19 at 14:21
  • 3
    You should always put the brackets in, even for single lines. I see it all the time with my students; they have a single line, which works fine initially, but then later they decide to add more code and can't figure out where the bug is... – Idle_Mind Mar 25 '19 at 14:22
  • For a column, you could make the function return an int. The return value would be the topmost row that is available, or -1 to indicate it is full? – Idle_Mind Mar 25 '19 at 14:23
  • @Idle_Mind ident is somehow implying that, and I think its good for new programs to face codes like this and get used to it. – Ashkan Mobayen Khiabani Mar 25 '19 at 14:23
  • @Zheng-rongCai: The correct way to think about it is: **the body of a `for` is always a single statement**. (And moreover, it is never a variable declaration statement.) A *compound statement* is a statement; it consists of zero or more statements surrounded by `{ }`. So `for(i = 0; i < 10; i+=1) Console.WriteLine(i);` is fine, because the body is a single statement. But so is `for(i = 0; i < 10; i += 1) { Console.WriteLine(i); Console.WriteLine("hello");}` because again, the body is a single statement: a *compound statement*. – Eric Lippert Mar 25 '19 at 14:30
  • @AshkanMobayenKhiabani - I don't want to check if the row(horizontal) is full but the column(vertical). And if you use `GetLength()` won't that get the horizontal length instead of the vertcial? So just replace the row with column and it should work I think. – zhrgci Mar 26 '19 at 14:39
2

Similaraly you could use Linq to verify that All items at row equal O:

public static bool IsRowEmpty(char[,]board, int row)
{
    return Enumerable.Range(0, board.GetUpperBound(1)).All(col => board[row, col] == 'O');
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43