private List <ActionResult> FlagTile(GameAction action) { List <ActionResult> actionResults = new List <ActionResult>(); int index = GetIndex(action.x, action.y); MinesweeperTile tile = this.tiles[index]; // if the tile is covered then toggle the flag and return it's new state if (tile.IsCovered()) { tile.ToggleFlagged(); if (tile.IsFlagged()) { actionResults.Add(new ActionResult(action.x, action.y, ResultType.Flagged)); minesLeft--; } else { if (tile.GetExploded()) { actionResults.Add(new ActionResult(action.x, action.y, ResultType.Exploded)); } else { actionResults.Add(new ActionResult(action.x, action.y, ResultType.Hidden)); } minesLeft++; } } return(actionResults); }
// clicks the assigned tile and returns an object containing a list of tiles cleared private List <ActionResult> ClearTile(GameAction action) { List <ActionResult> actionResults = new List <ActionResult>(); int index = GetIndex(action.x, action.y); // if this is the first click then create the tiles and place the mines if (this.gameStatus == GameStatus.NotStarted) { PlaceMines(index); } MinesweeperTile tile = this.tiles[index]; // are we clicking on a flag if (tile.IsFlagged()) { Write("Unable to Clear: Clicked on a Flag"); } else if (tile.GetExploded()) { Write("Unable to Clear: Clicked on an exploded Mine"); } else if (tile.IsMine()) { //Write("Gameover: Clicked on a mine"); deaths++; if (hardcore) { this.gameStatus = GameStatus.Lost; } tile.SetExploded(true); actionResults.Add(new ActionResult(action.x, action.y, ResultType.Exploded)); } else { if (tile.IsCovered()) // make sure the tile is clickable { List <MinesweeperTile> tilesToReveal = new List <MinesweeperTile>(); tilesToReveal.Add(tile); actionResults.AddRange(Reveal(tilesToReveal)); } } return(actionResults); }
public GameResult ProcessActions <T>(IList <T> actions) where T : GameAction // accept any Array of classes extending GameAction //long start = DateTime.Now.Ticks; { List <ActionResult> actionResults = new List <ActionResult>(); foreach (GameAction action in actions) { if (gameStatus != GameStatus.Lost && gameStatus != GameStatus.Won) // only process actions while the game is in play { if (action.action == ActionType.Clear) { actionResults.AddRange(ClearTile(action)); } else if (action.action == ActionType.Flag) { actionResults.AddRange(FlagTile(action)); } else if (action.action == ActionType.Chord) { actionResults.AddRange(ChordTile(action)); } } } GameStatus finalStatus = gameStatus; // take the game status for the game if (gameStatus == GameStatus.Lost) { for (var i = 0; i < this.tiles.Length; i++) { MinesweeperTile tile = this.tiles[i]; if (tile.IsMine() && !tile.IsFlagged() && !tile.GetExploded()) { actionResults.Add(new ActionResult(tile.GetX(), tile.GetY(), ResultType.Mine)); // show remaining mines } if (!tile.IsMine() && tile.IsFlagged()) { actionResults.Add(new ActionResult(tile.GetX(), tile.GetY(), ResultType.FlaggedWrong)); // show flags placed wrong } } } //Write("Game processing actions took " + (DateTime.Now.Ticks - start) + " ticks"); return(new GameResult(finalStatus, actionResults)); }