From d11258d3ed3b18ac6f89c200e79579b83ed61452 Mon Sep 17 00:00:00 2001 From: Tim Wundenberg Date: Fri, 25 May 2018 10:34:07 +0200 Subject: [PATCH] expand gamelogic --- CoopSweeper/GameTypes/Game.cs | 60 ++++++++++++++++++++++++++++++++++ CoopSweeper/GameTypes/IGame.cs | 8 +++++ 2 files changed, 68 insertions(+) diff --git a/CoopSweeper/GameTypes/Game.cs b/CoopSweeper/GameTypes/Game.cs index 1116786..1b29aec 100644 --- a/CoopSweeper/GameTypes/Game.cs +++ b/CoopSweeper/GameTypes/Game.cs @@ -37,5 +37,65 @@ namespace CoopSweeper.GameTypes 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; + + } + } } diff --git a/CoopSweeper/GameTypes/IGame.cs b/CoopSweeper/GameTypes/IGame.cs index 7a8455c..2383ace 100644 --- a/CoopSweeper/GameTypes/IGame.cs +++ b/CoopSweeper/GameTypes/IGame.cs @@ -7,5 +7,13 @@ namespace CoopSweeper.GameTypes interface IGame { IField[][] Map { get; } + + void Reveal(int x, int y); + + void SetQuestionMark(int x, int y); + + void SetFlag(int x, int y); + + void ResetField(int x, int y); } }