From f29beae0faf01a2e323d0c3ad8a8cea9213112aa Mon Sep 17 00:00:00 2001 From: Tim Wundenberg Date: Fri, 25 May 2018 10:58:07 +0200 Subject: [PATCH 1/2] some changes --- CoopSweeper/GameTypes/Field.cs | 2 ++ CoopSweeper/GameTypes/Game.cs | 64 +++++++++++++++++++++++++-------- CoopSweeper/GameTypes/IField.cs | 2 ++ CoopSweeper/GameTypes/IGame.cs | 6 +--- 4 files changed, 54 insertions(+), 20 deletions(-) diff --git a/CoopSweeper/GameTypes/Field.cs b/CoopSweeper/GameTypes/Field.cs index ac69500..6f5afdd 100644 --- a/CoopSweeper/GameTypes/Field.cs +++ b/CoopSweeper/GameTypes/Field.cs @@ -16,6 +16,8 @@ namespace CoopSweeper.GameTypes public FieldState State { get; set; } + public int SurroundingBombs { get; set; } + public char ToChar() { throw new NotImplementedException(); diff --git a/CoopSweeper/GameTypes/Game.cs b/CoopSweeper/GameTypes/Game.cs index 1b29aec..64e4d66 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 @@ -30,6 +31,39 @@ 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++) + { + var field = Map[i, j]; + + + } + } + } + + 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)); + + + return points; } public void GenerateGame(int x, int y) @@ -65,6 +99,7 @@ namespace CoopSweeper.GameTypes public void Reveal(int x, int y) { + CheckMap(); var field = Map[x, y]; if (field.State != FieldState.REVEALED) { @@ -77,24 +112,23 @@ namespace CoopSweeper.GameTypes 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) + 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; + } } } diff --git a/CoopSweeper/GameTypes/IField.cs b/CoopSweeper/GameTypes/IField.cs index ff1e1eb..51ea0c0 100644 --- a/CoopSweeper/GameTypes/IField.cs +++ b/CoopSweeper/GameTypes/IField.cs @@ -17,6 +17,8 @@ namespace CoopSweeper.GameTypes bool ContainsBomb { get; set; } + int SurroundingBombs { get; set; } + FieldState State { get; set; } char ToChar(); 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); } } From e0291a445dc45568381f218bb77406f0138f75bb Mon Sep 17 00:00:00 2001 From: Tim Wundenberg Date: Fri, 25 May 2018 11:21:18 +0200 Subject: [PATCH 2/2] implemented sorrounding bombs --- CoopSweeper/GameTypes/Field.cs | 2 ++ CoopSweeper/GameTypes/Game.cs | 28 +++++++++++++++++++++++----- CoopSweeper/GameTypes/IField.cs | 2 ++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/CoopSweeper/GameTypes/Field.cs b/CoopSweeper/GameTypes/Field.cs index ddb2929..3e9a384 100644 --- a/CoopSweeper/GameTypes/Field.cs +++ b/CoopSweeper/GameTypes/Field.cs @@ -10,6 +10,7 @@ namespace CoopSweeper.GameTypes public Field() { State = FieldState.NONE; + CheckID = 0; } public bool ContainsBomb { get; set; } @@ -17,6 +18,7 @@ namespace CoopSweeper.GameTypes public FieldState State { get; set; } public int SurroundingBombs { get; set; } + public int CheckID { get; set; } public char ToChar() { diff --git a/CoopSweeper/GameTypes/Game.cs b/CoopSweeper/GameTypes/Game.cs index 64e4d66..c6259c2 100644 --- a/CoopSweeper/GameTypes/Game.cs +++ b/CoopSweeper/GameTypes/Game.cs @@ -8,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; } @@ -41,9 +42,14 @@ namespace CoopSweeper.GameTypes { for (int j = 0; j < Map.GetLength(1); j++) { - var field = Map[i, j]; + int bombCounter = 0; + foreach (var point in GetSorroundedFields(i, j)) + { + if (Map[point.X, point.Y].ContainsBomb) + bombCounter++; + } - + Map[i, j].SurroundingBombs = bombCounter; } } } @@ -62,6 +68,11 @@ namespace CoopSweeper.GameTypes 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; } @@ -76,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++) @@ -97,10 +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; @@ -110,6 +126,8 @@ namespace CoopSweeper.GameTypes if (CheckGameFinished()) GameFinished?.Invoke(true); + + //_checkID } public void ToggleMark(int x, int y) @@ -132,4 +150,4 @@ namespace CoopSweeper.GameTypes } } -} +} \ No newline at end of file diff --git a/CoopSweeper/GameTypes/IField.cs b/CoopSweeper/GameTypes/IField.cs index 51ea0c0..3318544 100644 --- a/CoopSweeper/GameTypes/IField.cs +++ b/CoopSweeper/GameTypes/IField.cs @@ -19,6 +19,8 @@ namespace CoopSweeper.GameTypes int SurroundingBombs { get; set; } + int CheckID { get; set; } + FieldState State { get; set; } char ToChar();