102 lines
2.5 KiB
C#
102 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace CoopSweeper.GameTypes
|
|
{
|
|
class Game
|
|
{
|
|
private readonly Random _random = new Random();
|
|
|
|
public IField[,] Map { get; protected set; }
|
|
|
|
private bool IsBomb(int bombratePercent)
|
|
{
|
|
int r = _random.Next(0, 100);
|
|
return r < bombratePercent;
|
|
}
|
|
|
|
public void GenerateGame(int x, int y, int bombratePercent)
|
|
{
|
|
Map = new IField[x, y];
|
|
for (int i = 0; i < x; i++)
|
|
{
|
|
for (int j = 0; j < y; j++)
|
|
{
|
|
var field = new Field
|
|
{
|
|
ContainsBomb = IsBomb(bombratePercent)
|
|
};
|
|
Map[i, j] = field;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void GenerateGame(int x, int y)
|
|
{
|
|
GenerateGame(x, y, 10);
|
|
}
|
|
|
|
private void CheckMap()
|
|
{
|
|
if (Map == null)
|
|
throw new ArgumentNullException("The Map isn't created yet!");
|
|
}
|
|
|
|
public delegate void GameFinishedHandler(bool isGameWon);
|
|
|
|
public event GameFinishedHandler GameFinished;
|
|
|
|
private bool CheckGameFinished()
|
|
{
|
|
for (int i = 0; i < Map.GetLength(0); i++)
|
|
{
|
|
for (int j = 0; j < Map.GetLength(1); j++)
|
|
{
|
|
var field = Map[i, j];
|
|
|
|
if (!field.ContainsBomb && field.State != FieldState.REVEALED)
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public void Reveal(int x, int y)
|
|
{
|
|
var field = Map[x, y];
|
|
if (field.State != FieldState.REVEALED)
|
|
{
|
|
field.State = FieldState.REVEALED;
|
|
if (field.ContainsBomb)
|
|
GameFinished?.Invoke(false);
|
|
}
|
|
|
|
if (CheckGameFinished())
|
|
GameFinished?.Invoke(true);
|
|
}
|
|
|
|
public void SetQuestionMark(int x, int y)
|
|
{
|
|
Map[x, y].State = FieldState.QUESTIONMARK;
|
|
}
|
|
|
|
public void SetFlag(int x, int y)
|
|
{
|
|
Map[x, y].State = FieldState.FLAG;
|
|
}
|
|
|
|
public void ResetField(int x, int y)
|
|
{
|
|
var field = Map[x, y];
|
|
if (field.State == FieldState.REVEALED)
|
|
throw new Exception("A Revealed Field can't be resetet!");
|
|
|
|
field.State = FieldState.FLAG;
|
|
|
|
}
|
|
|
|
}
|
|
}
|