public Tile(GameField game, int x, int y) { this.game = game; this.X = x; this.Y = y; button = new Button { Text = "", BackColor = Color.Transparent, Width = 20, Height = 20, Left = 20*X, Top = 20*Y }; button.MouseDown += Click; game.Panel.Controls.Add(button); }
public static void Main(string[] args) { GameField gameField = new GameField(10,10); char[,] playingField = gameField.Create(); char[,] bombsField = gameField.PlaceBombs(); int maxScore = (gameField.FieldCols * gameField.FieldCols) - (gameField.FieldCols + gameField.FieldCols); Engine engine = new Engine(); while (true) { if (Engine.IsNewGame) { playingField = gameField.Create(); bombsField = gameField.PlaceBombs(); Draw.GameLoad(); Draw.PlayingField(playingField); } Console.Write("Enter row and column: "); string inputCommand = Console.ReadLine(); if (inputCommand == "exit") { break; } inputCommand = engine.ParseInputCommand(inputCommand, playingField); engine.ExecuteCommand(inputCommand, gameField, playingField, bombsField, maxScore); } Console.WriteLine("Press any key to exit the game."); Console.Read(); }
/// <summary> /// Executes a parsed command. /// </summary> /// <param name="inputCommand">The parsed command to execute.</param> /// <param name="gameField">The game field dimensions.</param> /// <param name="fieldWithQuestionmarks">The unrevealed game field.(with question marks)</param> /// <param name="fieldWithBombs">The bombs field.(the places of the bombs)</param> /// <param name="maxScore">The max score formula. /// Calculated by the formula (gameField.FieldCols * gameField.FieldCols) - (gameField.FieldCols + gameField.FieldCols) /// </param> public void ExecuteCommand(string inputCommand, GameField gameField, char[,] fieldWithQuestionmarks, char[,] fieldWithBombs, int maxScore) { switch (inputCommand) { case "top": Draw.ScoreBoard(ScoreBoardTopPlayers); break; case "restart": fieldWithQuestionmarks = gameField.Create(); fieldWithBombs = gameField.PlaceBombs(); Draw.PlayingField(fieldWithQuestionmarks); IsNewGame = true; break; case "exit": Console.WriteLine("Good bye."); break; case "turn": if (fieldWithBombs[this.rowToCheckForBomb, this.colToCheckForBomb] != '*') { IsNewGame = false; this.SetSurroundingBombsCount(fieldWithQuestionmarks, fieldWithBombs, this.rowToCheckForBomb, this.colToCheckForBomb); this.personalScore++; if (maxScore == this.personalScore) { Console.WriteLine("Congrats! You won the game!"); Draw.PlayingField(fieldWithBombs); this.EnterScoreToScoreBoard(); Draw.ScoreBoard(ScoreBoardTopPlayers); fieldWithQuestionmarks = gameField.Create(); fieldWithBombs = gameField.PlaceBombs(); this.personalScore = 0; IsNewGame = true; } else { Draw.PlayingField(fieldWithQuestionmarks); } } else { Draw.PlayingField(fieldWithBombs); Console.WriteLine("You just hit a bomb. Sorry."); EnterScoreToScoreBoard(); Draw.ScoreBoard(ScoreBoardTopPlayers); fieldWithQuestionmarks = gameField.Create(); fieldWithBombs = gameField.PlaceBombs(); this.personalScore = 0; IsNewGame = true; } break; default: Console.WriteLine("Wrong command: {0}", inputCommand); break; } }
// Initiate a new game private void newGameBtn_Click(object sender, EventArgs e) { // Variables to pass to the constructor in GameField int iRow = 0, iCol = 0, bombs = 0; if (miSmallBoardSize.Checked) { iRow = 9; iCol = 9; } if (miMediumBoardSize.Checked) { iRow = 18; iCol = 18; } if (miLargeBoardSize.Checked) { iRow = 30; iCol = 30; } if (miUserDefinedBoardSize.Checked) { iRow = uds.RowSize; iCol = uds.ColumnSize; } if (iRow == 0 || iCol == 0) { MessageBox.Show("Check boardsize!", "Error"); return; } // Calculate amount of bombs based on difficulty and boardsize int size = iRow*iCol; if (miEasyDifficulty.Checked) bombs = size/14; if (miMediumDifficulty.Checked) bombs = size/10; if (miHardDifficulty.Checked) bombs = size/6; gameField = new GameField(iRow, iCol, bombs, pPlayBoard); firstClick = true; gameField.Start(); GameTick(); Height = gameMenuStrip.Height + pPlayBoard.Height; Width = pPlayBoard.Width; }
public Game() { _header = new Header(); _gameField = new GameField(12, 12, 30); }
public static void Main(string[] args) { string inputCommand = string.Empty; GameField gameField = new GameField(10,10); char[,] playingField = gameField.Create(); char[,] bombsField = gameField.PlaceBombs(); int personalScore = 0; bool isBombHit = false; List<Player> scoreBoardTopPlayers = new List<Player>(6); int row = 0; int col = 0; bool isNewGame = true; bool isWon = false; int maxScore = (gameField.FieldCols * gameField.FieldCols) - (gameField.FieldCols + gameField.FieldCols); do { if (isNewGame) { isNewGame = Draw.GameLoad(playingField); } Console.Write("Enter row and column : "); inputCommand = Console.ReadLine().Trim(); if (inputCommand.Length == 3) { if (int.TryParse(inputCommand[0].ToString(), out row) && int.TryParse(inputCommand[2].ToString(), out col) && row <= playingField.GetLength(0) && col <= playingField.GetLength(1)) { inputCommand = "turn"; } } switch (inputCommand) { case "top": ShowScoreBoard(scoreBoardTopPlayers); break; case "restart": playingField = gameField.Create(); bombsField = gameField.PlaceBombs(); Draw.PlayingField(playingField); isBombHit = false; isNewGame = false; break; case "exit": Console.WriteLine("Good bye."); break; case "turn": if (bombsField[row, col] != '*') { if (bombsField[row, col] == '-') { SetSurroundingBombsCount(playingField, bombsField, row, col); personalScore++; } if (maxScore == personalScore) { isWon = true; } else { Draw.PlayingField(playingField); } } else { isBombHit = true; } break; default: Console.WriteLine("Wrong command."); break; } if (isBombHit) { Draw.PlayingField(bombsField); Console.WriteLine("You just hit a bomb. Sorry."); Console.WriteLine("Enter your nickname for the score board: ", personalScore); string nickname = Console.ReadLine(); Player playerPersonalScore = new Player(nickname, personalScore); if (scoreBoardTopPlayers.Count < 5) { scoreBoardTopPlayers.Add(playerPersonalScore); } else { for (int i = 0; i < scoreBoardTopPlayers.Count; i++) { if (scoreBoardTopPlayers[i].PlayerPoints < playerPersonalScore.PlayerPoints) { scoreBoardTopPlayers.Insert(i, playerPersonalScore); scoreBoardTopPlayers.RemoveAt(scoreBoardTopPlayers.Count - 1); break; } } } scoreBoardTopPlayers.Sort((Player firstPlayer, Player secondPlayer) => secondPlayer.PlayerName.CompareTo(firstPlayer.PlayerName)); scoreBoardTopPlayers.Sort((Player firstPlayer, Player secondPlayer) => secondPlayer.PlayerPoints.CompareTo(firstPlayer.PlayerPoints)); ShowScoreBoard(scoreBoardTopPlayers); playingField = gameField.Create(); bombsField = gameField.PlaceBombs(); personalScore = 0; isBombHit = false; isNewGame = true; } if (isWon) { Console.WriteLine("Congrats! You won the game!"); Draw.PlayingField(bombsField); Console.WriteLine("Enter your nickname for the score board: "); string playerNickname = Console.ReadLine(); Player playerCurrentScore = new Player(playerNickname, personalScore); scoreBoardTopPlayers.Add(playerCurrentScore); ShowScoreBoard(scoreBoardTopPlayers); playingField = gameField.Create(); bombsField = gameField.PlaceBombs(); personalScore = 0; isWon = false; isNewGame = true; } } while (inputCommand != "exit"); Console.WriteLine("Press any key to exit the game."); Console.Read(); }
public static void Main(string[] arguments) { string command = string.Empty; char[,] gameField = GameField.CreateGameField(); char[,] mines = CreateMines.Create(); int score = 0; bool gameOver = false; int numOfPlayers = 6; List <Players> topPlayers = new List <Players>(numOfPlayers); int row = 0; int column = 0; bool win = false; bool restart = true; const int MaxOpenSquares = 35; do { if (restart) { Console.WriteLine("Lets play Minesweeper. Find the empty squares while avoiding the mines. " + "\nCommand 'top' showing raiting, 'restart' begin new game, 'exit' ending the game!\nGood Luck! "); GameField.GameFieldRenderer(gameField); restart = false; } Console.Write("Enter which row and column you want to open: "); command = Console.ReadLine().Trim(); if (command.Length >= 3) { if (int.TryParse(command[0].ToString(), out row) && int.TryParse(command[2].ToString(), out column) && row <= gameField.GetLength(0) && column <= gameField.GetLength(1)) { command = "turn"; } } switch (command) { case "top": Raiting.RaitingTopPlayer(topPlayers); break; case "restart": gameField = GameField.CreateGameField(); mines = CreateMines.Create(); GameField.GameFieldRenderer(gameField); gameOver = false; restart = false; break; case "exit": Console.WriteLine("Game Over!"); break; case "turn": if (mines[row, column] != '*') { if (mines[row, column] == '-') { CountMinesAround.NumOfMinesAround(gameField, mines, row, column); score++; } if (MaxOpenSquares == score) { win = true; } else { GameField.GameFieldRenderer(gameField); } } else { gameOver = true; } break; default: Console.WriteLine("\nWrong command!\n"); break; } if (gameOver) { GameField.GameFieldRenderer(mines); Console.Write("\nGameOver! Your score is {0} points. " + "Enter your name: ", score); string playerName = Console.ReadLine(); Players newPlayer = new Players(playerName, score); if (topPlayers.Count < 5) { topPlayers.Add(newPlayer); } else { for (int i = 0; i < topPlayers.Count; i++) { if (topPlayers[i].Score < newPlayer.Score) { topPlayers.Insert(i, newPlayer); topPlayers.RemoveAt(topPlayers.Count - 1); break; } } } topPlayers.Sort((Players player1, Players player2) => player2.Name.CompareTo(player1.Name)); topPlayers.Sort((Players palyer1, Players player2) => player2.Score.CompareTo(palyer1.Score)); Raiting.RaitingTopPlayer(topPlayers); gameField = GameField.CreateGameField(); mines = CreateMines.Create(); score = 0; gameOver = false; restart = true; } if (win) { Console.WriteLine("\nCongratulations! You Win!"); GameField.GameFieldRenderer(mines); Console.WriteLine("Enter your name: "); string nameOfWinner = Console.ReadLine(); Players newPlayer = new Players(nameOfWinner, score); topPlayers.Add(newPlayer); Raiting.RaitingTopPlayer(topPlayers); gameField = GameField.CreateGameField(); mines = CreateMines.Create(); score = 0; win = false; restart = true; } }while (command != "exit"); Console.Read(); }