diff --git a/CoopSweeper/GameTypes/Field.cs b/CoopSweeper/GameTypes/Field.cs index 7b70130..46dab75 100644 --- a/CoopSweeper/GameTypes/Field.cs +++ b/CoopSweeper/GameTypes/Field.cs @@ -10,12 +10,16 @@ namespace CoopSweeper.GameTypes public Field() { State = FieldState.NONE; + CheckID = 0; } public bool ContainsBomb { get; set; } public FieldState State { get; set; } + public int SurroundingBombs { get; set; } + public int CheckID { get; set; } + public DisplayState GetDisplayState() { switch(State) diff --git a/CoopSweeper/GameTypes/Game.cs b/CoopSweeper/GameTypes/Game.cs index 1b29aec..c6259c2 100644 --- a/CoopSweeper/GameTypes/Game.cs +++ b/CoopSweeper/GameTypes/Game.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Drawing; using System.Text; namespace CoopSweeper.GameTypes @@ -7,6 +8,7 @@ namespace CoopSweeper.GameTypes class Game { private readonly Random _random = new Random(); + private static int _checkID = 0; public IField[,] Map { get; protected set; } @@ -30,6 +32,49 @@ namespace CoopSweeper.GameTypes Map[i, j] = field; } } + + UpdateSorroundingBombs(); + } + + private void UpdateSorroundingBombs() + { + for (int i = 0; i < Map.GetLength(0); i++) + { + for (int j = 0; j < Map.GetLength(1); j++) + { + int bombCounter = 0; + foreach (var point in GetSorroundedFields(i, j)) + { + if (Map[point.X, point.Y].ContainsBomb) + bombCounter++; + } + + Map[i, j].SurroundingBombs = bombCounter; + } + } + } + + private List GetSorroundedFields(int x, int y) + { + var points = new List(); + points.Add(new Point(x - 1, y - 1)); + points.Add(new Point(x, y - 1)); + points.Add(new Point(x + 1, y - 1)); + + points.Add(new Point(x + 1, y)); + points.Add(new Point(x - 1, y)); + + points.Add(new Point(x - 1, y + 1)); + points.Add(new Point(x, y + 1)); + points.Add(new Point(x + 1, y + 1)); + + points.RemoveAll(point => + point.X < 0 + || point.Y < 0 + || point.X >= Map.GetLength(0) + || point.Y >= Map.GetLength(1)); + + return points; } public void GenerateGame(int x, int y) @@ -42,11 +87,11 @@ namespace CoopSweeper.GameTypes 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++) @@ -63,9 +108,15 @@ namespace CoopSweeper.GameTypes return true; } + + public void Reveal(int x, int y) { + CheckMap(); var field = Map[x, y]; + //if (field.CheckID < _checkID) + // return; + if (field.State != FieldState.REVEALED) { field.State = FieldState.REVEALED; @@ -75,27 +126,28 @@ namespace CoopSweeper.GameTypes if (CheckGameFinished()) GameFinished?.Invoke(true); + + //_checkID } - 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) + public void ToggleMark(int x, int y) { + CheckMap(); var field = Map[x, y]; - if (field.State == FieldState.REVEALED) - throw new Exception("A Revealed Field can't be resetet!"); - - field.State = FieldState.FLAG; + switch (field.State) + { + case FieldState.NONE: + field.State = FieldState.FLAG; + return; + case FieldState.FLAG: + field.State = FieldState.QUESTIONMARK; + return; + case FieldState.QUESTIONMARK: + field.State = FieldState.NONE; + return; + } } } -} +} \ No newline at end of file diff --git a/CoopSweeper/GameTypes/IField.cs b/CoopSweeper/GameTypes/IField.cs index afa6b17..ece31e0 100644 --- a/CoopSweeper/GameTypes/IField.cs +++ b/CoopSweeper/GameTypes/IField.cs @@ -35,6 +35,10 @@ namespace CoopSweeper.GameTypes bool ContainsBomb { get; set; } + int SurroundingBombs { get; set; } + + int CheckID { get; set; } + FieldState State { get; set; } DisplayState GetDisplayState(); diff --git a/CoopSweeper/GameTypes/IGame.cs b/CoopSweeper/GameTypes/IGame.cs index 2383ace..da89285 100644 --- a/CoopSweeper/GameTypes/IGame.cs +++ b/CoopSweeper/GameTypes/IGame.cs @@ -10,10 +10,6 @@ namespace CoopSweeper.GameTypes void Reveal(int x, int y); - void SetQuestionMark(int x, int y); - - void SetFlag(int x, int y); - - void ResetField(int x, int y); + void ToggleMark(int x, int y); } }