CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace Game
{
class Program
{
static void Main(string[] args)
{
int gameRows = 9;
int gameCols = 20;
int position = gameCols / 2;
int numberOfMaxMinesPurRow = 4;
string mines = "!$%*#^";
var rand = new Random();
// Set the stage
List<string> stage = new List<string>();
for (var i = 0; i < gameRows; i++)
{
stage.Add(new string(' ', gameCols));
}
while (true)
{
if (Console.KeyAvailable)
{
var key = Console.ReadKey();
if (key.Key == ConsoleKey.Escape)
{
break;
}
else if (key.Key == ConsoleKey.LeftArrow && position > 0)
{
position--;
}
else if (key.Key == ConsoleKey.RightArrow && position < (gameCols - 3))
{
position++;
}
}
// check if game over and remove the last row
var lastRow = stage[0].ToArray();
if (lastRow[position] != ' ' || lastRow[position + 1] != ' ' || lastRow[position + 2] != ' ')
{
Console.WriteLine("Game Over");
Console.ReadLine();
break;
}
stage.RemoveAt(0);
// Generate new game row
int minesCountOnRow = (int)(rand.NextDouble() * 10) % numberOfMaxMinesPurRow;
char[] newGameRow = new string(' ', gameCols).ToCharArray();
for (int i = 0; i < minesCountOnRow; i++)
{
int minePosition = (int)(rand.NextDouble() * 100) % gameCols;
int mineType = (int)(rand.NextDouble() * 10) % mines.Length;
newGameRow[minePosition] = mines[mineType];
}
stage.Add(new string(newGameRow));
Console.Clear();
// Print the scene
Console.WriteLine(new string('=', gameCols + 2));
for (int index = gameRows - 1; index >= 0; index--)
{
Console.WriteLine("=" + stage[index] + "=");
}
// Print the your actor
lastRow[position] = '(';
lastRow[position + 1] = '0';
lastRow[position + 2] = ')';
Console.WriteLine("=" + new string(lastRow) + "=");
Console.WriteLine(new string('=', gameCols + 2));
Thread.Sleep(200);
}
}
}
}