using System;
namespace Balloons_Pops_Game_Variant_Four
{
public class FillGameBoard
{
const int GameBoardRows = 5;
const int GameBoardColumns = 10;
private readonly string[,] gameBoard =
new string[GameBoardRows, GameBoardColumns];
static readonly Random randomNumber = new Random();
private int GenerateRandomBallonNumber()
{
return randomNumber.Next(4) + 1;
}
public FillGameBoard()
{
for (int i = 0; i < GameBoardRows; i++)
{
for (int j = 0; j < GameBoardColumns; j++)
{
gameBoard[i, j] =
GenerateRandomBallonNumber().ToString();
}
}
}
private void SetColour(int colour)
{
switch (colour)
{
case (int)BallonColours.Red:
Console.ForegroundColor = ConsoleColor.Red;
break;
case (int)BallonColours.Blue:
Console.ForegroundColor = ConsoleColor.Blue;
break;
case (int)BallonColours.Green:
Console.ForegroundColor = ConsoleColor.Green;
break;
case (int)BallonColours.Yellow:
Console.ForegroundColor = ConsoleColor.Yellow;
break;
default:
Console.ForegroundColor = ConsoleColor.Gray;
break;
}
}
public void GeneratePlot()
{
Console.WriteLine();
Console.Write(" ");
for (int j = 0; j < GameBoardColumns; j++)
{
Console.Write("{0} ", j);
}
Console.WriteLine();
Console.WriteLine(" ---------------------");
for (int i = 0; i < GameBoardRows; i++)
{
Console.Write(" {0} | ", i);
for (int j = 0; j < GameBoardColumns; j++)
{
if (gameBoard[i, j] == "")
Console.Write("".PadLeft(2));
else
{
SetColour(Convert.ToInt32(gameBoard[i, j]));
Console.Write("{0} ", gameBoard[i, j]);
SetColour(0);
}
}
Console.Write("|");
Console.WriteLine();
}
Console.WriteLine(" ---------------------");
Console.WriteLine();
SetColour(0);
}
public bool IsEmpty()
{
for (int i = 0; i < GameBoardRows; i++)
{
for (int j = 0; j < GameBoardColumns; j++)
{
if(gameBoard[i, j] != "")
return false;
}
}
return true;
}
public bool IsMoveAllowed(Move move)
{
if (gameBoard[move.Row, move.Col] == "")
return false;
return true;
}
public void Refresh(Move move)
{
string colour = gameBoard[move.Row, move.Col];
gameBoard[move.Row, move.Col] = "";
PopsTheNeighbors(move.Row, move.Col, colour);
FillTheEmptyCellsDown();
}
private void PopsTheNeighbors(int row, int col, string colour)
{
if (row - 1 >= 0 && gameBoard[row - 1, col] == colour)
{
gameBoard[row - 1, col] = "";
PopsTheNeighbors(row - 1, col, colour);
}
if (row + 1 < 5 && gameBoard[row + 1, col] == colour)
{
gameBoard[row + 1, col] = "";
PopsTheNeighbors(row + 1, col, colour);
}
if (col - 1 >= 0 && gameBoard[row, col - 1] == colour)
{
gameBoard[row, col - 1] = "";
PopsTheNeighbors(row, col - 1, colour);
}
if (col + 1 < 10 && gameBoard[row, col + 1] == colour)
{
gameBoard[row, col + 1] = "";
PopsTheNeighbors(row, col + 1, colour);
}
}
private void FillTheEmptyCellsDown()
{
for (int j = 0; j < GameBoardColumns; j++)
{
for (int i = GameBoardRows - 1; i >= 0; i--)
{
if (gameBoard[i, j] == "")
{
int k = i;
while (k > 0)
{
if (gameBoard[k - 1, j] != "")
{
gameBoard[i, j] = gameBoard[k - 1, j];
gameBoard[k - 1, j] = "";
break;
}
k--;
}
}
}
}
}
}
}