I'm making this connect4 game for a school project and it seems that I've gotten stuck on the issue of checking if a player has won, aka if a player has 4 tokens/markers in a row. I've written a solution which checks all the possible ways of winning but, and here comes the problem, it throws me the "Index was outside the bounds of the array".
I can understand why it reaches outside the array since I'm adding and subtracting from all possible values in the array. But when you try to play the game a strange thing which I can't explain happends:
I can put a token/marker in every way shown in the picture and even get a fully functional winning situation within. But as soon as I try to place one in the outer layer it stops working. I have tried looking up what other have done and even tried debuging the code but I can't seem to figure out what the problem is. Below is the all the relevant parts of the code.
I will greatly appreciate help, thank you for your time.
using System;
namespace _4_i_rad
{
class Program
{
static void Main(string[] args)
{
//variables
bool p1turn = true;
int[,] playingField = new int[6, 7];
//taking turns
while (CheckWin(playingField) == 0)
{
Console.Clear();
PlayingField(playingField);
if (p1turn == true)
{
Console.Write("Player1's(X) turn.\nIndicate column of choise: ");
PlaceHolder(playingField, p1turn);
p1turn = false;
}
else
{
Console.Write("Player2's(O) turn.\nIndicate column of choise: ");
PlaceHolder(playingField, p1turn);
p1turn = true;
}
}
Console.Clear();
PlayingField(playingField);
if (CheckWin(playingField) == 1)
{ Console.WriteLine("X wins!"); }
else
{ Console.WriteLine("O wins!"); }
}
//checking if someone has won
static int CheckWin(int[,] playingField)
{
int win = 0;
for (int a = 0; a < playingField.GetLength(0); a++)
{
for (int b = 0; b < playingField.GetLength(1); b++)
{
// X win
if (playingField[a, b] == 1 &&
playingField[a - 1, b - 1] == 1 &&
playingField[a - 2, b - 2] == 1 &&
playingField[a - 3, b - 3] == 1)
{ win = 1; }
if (playingField[a, b] == 1 &&
playingField[a, b - 1] == 1 &&
playingField[a, b - 2] == 1 &&
playingField[a, b - 3] == 1)
{ win = 1; }
if (playingField[a, b] == 1 &&
playingField[a - 1, b] == 1 &&
playingField[a - 2, b] == 1 &&
playingField[a - 3, b] == 1)
{ win = 1; }
if (playingField[a, b] == 1 &&
playingField[a - 1, b + 1] == 1 &&
playingField[a - 2, b + 2] == 1 &&
playingField[a - 3, b + 3] == 1)
{ win = 1; }
if (playingField[a, b] == 1 &&
playingField[a, b + 1] == 1 &&
playingField[a, b + 2] == 1 &&
playingField[a, b + 3] == 1)
{ win = 1; }
// O win
if (playingField[a, b] == 2 &&
playingField[a - 1, b - 1] == 2 &&
playingField[a - 2, b - 2] == 2 &&
playingField[a - 3, b - 3] == 2)
{ win = 2; }
if (playingField[a, b] == 2 &&
playingField[a, b - 1] == 2 &&
playingField[a, b - 2] == 2 &&
playingField[a, b - 3] == 2)
{ win = 2; }
if (playingField[a, b] == 2 &&
playingField[a - 1, b] == 2 &&
playingField[a - 2, b] == 2 &&
playingField[a - 3, b] == 2)
{ win = 2; }
if (playingField[a, b] == 2 &&
playingField[a - 1, b + 1] == 2 &&
playingField[a - 2, b + 2] == 2 &&
playingField[a - 3, b + 3] == 2)
{ win = 2; }
if (playingField[a, b] == 2 &&
playingField[a, b + 1] == 2 &&
playingField[a, b + 2] == 2 &&
playingField[a, b + 3] == 2)
{ win = 2; }
}
}
return win;
}
//drawing playingfield
static void PlayingField(int[,] playingField)
{
Console.WriteLine("\n 1 2 3 4 5 6 7");
for (int a = 0; a < playingField.GetLength(0); a++)
{
Console.WriteLine(" -----------------------------");
Console.Write(" ");
Console.Write("| ");
for (int b = 0; b < playingField.GetLength(1); b++)
{
if (playingField[a, b] == 1)
{ Console.Write("X" + " | "); }
else if (playingField[a, b] == 2)
{ Console.Write("O" + " | "); }
else
{ Console.Write(" " + " | "); }
}
Console.WriteLine(" ");
}
Console.WriteLine(" -----------------------------");
}
//choosing column
static int PlaceHolder(int[,] playingField, bool p1turn)
{
int x; int y = 5;
while (!int.TryParse(Console.ReadLine(), out x) || x < 1 || x > 7)
{ Console.Write("\nOut of bounds.\nSubmit new column between 1 and 7: "); }
while (playingField[y, x - 1] == 1 || playingField[y, x - 1] == 2)
{
y--;
if (y < 0)
{
Console.Write("\nColumn already has maximum amount of tokens.\nChose new column: ");
while (!int.TryParse(Console.ReadLine(), out x) || x < 1 || x > 7)
{ Console.Write("\nOut of bounds.\nSubmit new column between 1 and 7: "); }
y = 5;
}
}
if (p1turn == true)
{ return playingField[y, x - 1] = 1; }
else
{ return playingField[y, x - 1] = 2; }
}
}
}