Merge changes

This commit is contained in:
Marvin Rohrbach
2018-05-25 11:26:27 +02:00
4 changed files with 79 additions and 23 deletions

View File

@@ -10,12 +10,16 @@ namespace CoopSweeper.GameTypes
public Field() public Field()
{ {
State = FieldState.NONE; State = FieldState.NONE;
CheckID = 0;
} }
public bool ContainsBomb { get; set; } public bool ContainsBomb { get; set; }
public FieldState State { get; set; } public FieldState State { get; set; }
public int SurroundingBombs { get; set; }
public int CheckID { get; set; }
public DisplayState GetDisplayState() public DisplayState GetDisplayState()
{ {
switch(State) switch(State)

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing;
using System.Text; using System.Text;
namespace CoopSweeper.GameTypes namespace CoopSweeper.GameTypes
@@ -7,6 +8,7 @@ namespace CoopSweeper.GameTypes
class Game class Game
{ {
private readonly Random _random = new Random(); private readonly Random _random = new Random();
private static int _checkID = 0;
public IField[,] Map { get; protected set; } public IField[,] Map { get; protected set; }
@@ -30,6 +32,49 @@ namespace CoopSweeper.GameTypes
Map[i, j] = field; 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<Point> GetSorroundedFields(int x, int y)
{
var points = new List<Point>();
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) public void GenerateGame(int x, int y)
@@ -63,9 +108,15 @@ namespace CoopSweeper.GameTypes
return true; return true;
} }
public void Reveal(int x, int y) public void Reveal(int x, int y)
{ {
CheckMap();
var field = Map[x, y]; var field = Map[x, y];
//if (field.CheckID < _checkID)
// return;
if (field.State != FieldState.REVEALED) if (field.State != FieldState.REVEALED)
{ {
field.State = FieldState.REVEALED; field.State = FieldState.REVEALED;
@@ -75,26 +126,27 @@ namespace CoopSweeper.GameTypes
if (CheckGameFinished()) if (CheckGameFinished())
GameFinished?.Invoke(true); GameFinished?.Invoke(true);
//_checkID
} }
public void SetQuestionMark(int x, int y) public void ToggleMark(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)
{ {
CheckMap();
var field = Map[x, y]; var field = Map[x, y];
if (field.State == FieldState.REVEALED)
throw new Exception("A Revealed Field can't be resetet!");
switch (field.State)
{
case FieldState.NONE:
field.State = FieldState.FLAG; field.State = FieldState.FLAG;
return;
case FieldState.FLAG:
field.State = FieldState.QUESTIONMARK;
return;
case FieldState.QUESTIONMARK:
field.State = FieldState.NONE;
return;
}
} }
} }

View File

@@ -35,6 +35,10 @@ namespace CoopSweeper.GameTypes
bool ContainsBomb { get; set; } bool ContainsBomb { get; set; }
int SurroundingBombs { get; set; }
int CheckID { get; set; }
FieldState State { get; set; } FieldState State { get; set; }
DisplayState GetDisplayState(); DisplayState GetDisplayState();

View File

@@ -10,10 +10,6 @@ namespace CoopSweeper.GameTypes
void Reveal(int x, int y); void Reveal(int x, int y);
void SetQuestionMark(int x, int y); void ToggleMark(int x, int y);
void SetFlag(int x, int y);
void ResetField(int x, int y);
} }
} }