Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- namespace Sapper
- {
- class Program
- {
- const byte fieldSizeX = 10,
- fieldSizeY = 10;
- static byte difficultLevel = 1;
- static char[,] map;
- static byte[,] fogOfWar; // 0 - открыто, 1 - закрыто, 2 - !, 3 -?
- static Dictionary<char, ConsoleColor> ColorDictionary;
- static List<char> symbolList;
- static short playerPositionX,
- playerPositionY;
- static bool game = true,
- loose = false;
- static byte globalMines = 0;
- static ushort openCells = 0;
- static ushort zeroPoints = 0;
- static List<int> zeros = new List<int>();
- static int zeroX,
- zeroY;
- static char zeroOnMap = Convert.ToChar(Convert.ToString(0));
- static void Main(string[] args)
- {
- Start:
- GenerateMap(fieldSizeX, fieldSizeY);
- DetermineUserCursorPosition(); //присвоение значения координатам курсора
- CountCells();
- Console.CursorVisible = false;
- LocalRestart:
- Console.Clear();
- while (game)
- {
- //RenderInvisibleMap();
- //PlayerCursor();
- //PlayerMoveController();
- RenderVisibleMap();
- //RenderInvisibleMap();
- RenderInterface(fieldSizeX);
- PlayerCursor(); //отрисовка курсора
- //FindChar(0, 200);
- PlayerMoveController();
- CheckWin();
- Thread.Sleep(50);
- }
- if (loose)
- {
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("ПОРАЖЕНИЕ");
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine("Чтобы начать игру на новой карте, нажмите 0 + Enter\n" +
- "Чтобы начать игру на этой же карте, нажмите 1 + Enter");
- Console.ForegroundColor = ConsoleColor.Yellow;
- byte end;
- end = Convert.ToByte(Console.ReadLine());
- Console.WriteLine(end);
- if (end == 0)
- {
- game = true;
- loose = false;
- goto Start;
- }
- if (end == 1)
- {
- game = true;
- loose = false;
- goto LocalRestart;
- }
- }
- else
- {
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("ПОБЕДА");
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine("Чтобы начать игру на новой карте, нажмите 0 + Enter\n" +
- "Чтобы начать игру на этой же карте, нажмите 1 + Enter");
- Console.ForegroundColor = ConsoleColor.Yellow;
- byte end;
- end = Convert.ToByte(Console.ReadLine());
- Console.WriteLine(end);
- if (end == 0)
- {
- game = true;
- loose = false;
- goto Start;
- }
- if (end == 1)
- {
- game = true;
- loose = false;
- goto LocalRestart;
- }
- }
- //Exp();
- //Console.ReadKey();
- }
- static void Exp()
- {
- }
- static void CountCells()
- {
- for (byte i = 1; i < map.GetLength(0) - 1; i++)
- {
- for (byte k = 1; k < map.GetLength(1) - 1; k++)
- {
- if (map[i, k] != symbolList[2])
- openCells++;
- }
- }
- }
- static void CheckWin()
- {
- ushort visibleOpenCell = 0;
- for (byte i = 1; i < map.GetLength(0) - 1; i++)
- {
- for (byte k = 1; k < map.GetLength(1) - 1; k++)
- {
- if (map[i, k] != symbolList[2] && fogOfWar[i, k] == 0)
- visibleOpenCell++;
- }
- }
- if (openCells == visibleOpenCell) game = false;
- }
- static void RenderInterface(byte x)
- {
- x += 3;
- Console.SetCursorPosition(x, 0);
- Console.WriteLine("Мин осталось : " + globalMines + " ");
- }
- static void PlayerMoveController()
- {
- ConsoleKeyInfo key = Console.ReadKey();
- switch(key.Key)
- {
- case ConsoleKey.LeftArrow:
- MovePlayer(-1, 0);
- break;
- case ConsoleKey.RightArrow:
- MovePlayer(1, 0);
- break;
- case ConsoleKey.DownArrow:
- MovePlayer(0, 1);
- break;
- case ConsoleKey.UpArrow:
- MovePlayer(0, -1);
- break;
- case ConsoleKey.NumPad0:
- ActionPlayer(0);
- break;
- case ConsoleKey.NumPad1:
- ActionPlayer(1);
- break;
- case ConsoleKey.NumPad2:
- ActionPlayer(2);
- break;
- }
- }
- static void ActionPlayer(byte x)
- {
- if(x == 0)
- {
- fogOfWar[playerPositionX, playerPositionY] = 0;
- if (map[playerPositionX, playerPositionY] == symbolList[2])
- {
- game = false;
- loose = true;
- }
- CheckZeros(playerPositionX, playerPositionY);
- }
- if(x == 1)
- {
- if (fogOfWar[playerPositionX, playerPositionY] == 2)
- {
- fogOfWar[playerPositionX, playerPositionY] = 1;
- globalMines++;
- }
- else if (fogOfWar[playerPositionX, playerPositionY] == 1 || fogOfWar[playerPositionX, playerPositionY] == 3)
- {
- fogOfWar[playerPositionX, playerPositionY] = 2;
- globalMines--;
- }
- }
- if(x == 2)
- {
- if (fogOfWar[playerPositionX, playerPositionY] == 1)
- {
- fogOfWar[playerPositionX, playerPositionY] = 3;
- }
- else if (fogOfWar[playerPositionX, playerPositionY] == 3)
- {
- fogOfWar[playerPositionX, playerPositionY] = 1;
- }
- if (fogOfWar[playerPositionX, playerPositionY] == 2)
- {
- fogOfWar[playerPositionX, playerPositionY] = 3;
- globalMines++;
- }
- }
- }
- static void CheckZeros(int x, int y) // первая итерация
- {
- if (map[x, y] == zeroOnMap) // проверка, является ли клетка нулем
- {
- zeros.Clear();
- zeroX = x;
- zeroY = y;
- //do
- {
- ZeroQuad(zeroX, zeroY); //в выходе из цикла не участвует
- //ZeroSubstitution();
- }
- //while (zeros.Count != 0);
- }
- }
- static void ZeroQuad(int x, int y) // ищет в квадрате нули
- {
- if(zeros.Count != 0)
- {
- zeros.RemoveAt(zeroPoints - 2);
- zeros.RemoveAt(zeroPoints - 1);
- }
- int a, b;
- for (a = x - 1; a < x + 2; a++)
- {
- for (b = y - 1; b < y + 2; b++)
- {
- fogOfWar[a, b] = 0;
- if (map[a, b] == zeroOnMap && (a != x || b != y))
- {
- zeros.Add(a);
- zeros.Add(b);
- zeroPoints += 2;
- }
- }
- }
- //
- //Console.WriteLine("\nZeroQuad zeros.Count " + zeros.Count + " zeroPoints = " + zeroPoints
- // + " ");
- //Console.ReadKey();
- //
- }
- static void ZeroSubstitution()
- {
- if (zeros.Count != 0)
- {
- zeroX = zeros[zeroPoints - 2];
- zeroY = zeros[zeroPoints - 1];
- zeroPoints -= 2;
- }
- //
- //Console.WriteLine("ZeroSub. zeros.Count " + zeros.Count + " zeroPoints = " + zeroPoints+
- // "zeroX = "+ zeroX+ " zeroY = "+ zeroY);
- //Console.ReadKey();
- //
- }
- #region перемещение курсора
- static void MovePlayer(short x, short y)
- {
- if(CheckCollisions(x, y))
- {
- playerPositionX += x;
- playerPositionY += y;
- }
- }
- static bool CheckCollisions(short x, short y)
- {
- int targetX = playerPositionX + x,
- targetY = playerPositionY + y;
- if (map[targetX, targetY] == symbolList[0])
- return false;
- else
- return true;
- }
- static void PlayerCursor()//отрисовка курсора
- {
- Console.SetCursorPosition(playerPositionX, playerPositionY);
- Console.ForegroundColor = ColorDictionary[symbolList[1]];
- Console.Write(symbolList[1]);
- }
- #endregion
- #region карта
- static void DetermineUserCursorPosition()
- {
- playerPositionX = fieldSizeX / 2;
- playerPositionY = fieldSizeY / 2;
- }
- static void GenerateMap(byte x, byte y)
- {
- x += 2;//запас для рамки
- y += 2;//запас для рамки
- map = new char[x, y];
- fogOfWar = new byte[x, y];
- SymbolsAndColors(); //инициализируем словарь и лист
- Frame(x, y); //создаем рамку и поле
- SeedMines(x, y);
- SeedNumbers(x, y);
- }
- static void SymbolsAndColors()
- {
- symbolList = new List<char>();
- symbolList.Add((char)35); // решетка - рамка
- symbolList.Add((char)2); // рожица - курсор
- symbolList.Add((char)15); // мины
- symbolList.Add((char)4); // бубны - неоткрытые клетки
- symbolList.Add((char)33); // воскл. знак - помеченная клетка
- symbolList.Add('?'); //неизвестные клетки
- ColorDictionary = new Dictionary<char, ConsoleColor>();
- ColorDictionary.Add(symbolList[0], ConsoleColor.DarkYellow); // решетка - рамка
- ColorDictionary.Add(symbolList[1], ConsoleColor.Yellow); // рожица - курсор
- ColorDictionary.Add(symbolList[2], ConsoleColor.DarkRed); // мины
- ColorDictionary.Add(symbolList[3], ConsoleColor.DarkGray); // бубны - неоткрытые клетки
- ColorDictionary.Add(symbolList[4], ConsoleColor.Magenta); // вос. зн. - помеченная клетка
- ColorDictionary.Add(symbolList[5], ConsoleColor.Cyan); // вопросительный знак
- }
- static void SeedNumbers(byte x, byte y)
- {
- for (byte i = 1; i < map.GetLength(0) - 1; i++)
- {
- for (byte k = 1; k < map.GetLength(1) - 1; k++)
- {
- if (map[i, k] == symbolList[2]) continue;
- else
- {
- map[i, k] = OneNumber(i, k);
- }
- }
- }
- }
- static char OneNumber (byte x, byte y)
- {
- int a, b;
- byte mines = 0;
- for(a = x - 1; a < x + 2; a++)
- {
- for(b = y - 1; b < y + 2; b++)
- {
- if (map[a, b] == symbolList[2])
- mines++;
- }
- }
- return Convert.ToChar(Convert.ToString(mines));
- }
- static void SeedMines(byte x, byte y)
- {
- Random ran = new Random();
- for (byte i = 1; i < map.GetLength(0) - 1; i++)
- {
- for (byte k = 1; k < map.GetLength(1) - 1; k++)
- {
- if (ran.Next(0, 100) < ValueOfChanceMines(difficultLevel))
- {
- map[i, k] = symbolList[2];
- globalMines++;
- }
- }
- }
- }
- static short ValueOfChanceMines (byte x)
- {
- if (x == 1) return 13;
- if (x == 2) return 16;
- if (x == 3) return 21;
- return 0;
- }
- static void Frame(byte x, byte y)
- {
- for (short i = 0; i < map.GetLength(0); i++)
- {
- for (short k = 0; k < map.GetLength(1); k++)
- {
- if((i == 0 || i == x - 1) || (k == 0 || k == y - 1))
- {
- map[i, k] = symbolList[0];
- fogOfWar[i, k] = 0;
- }
- else
- {
- map[i, k] = ' ';
- fogOfWar[i, k] = 1;
- }
- }
- }
- }
- static void Colors(byte x, byte y, byte z) //z = 0 - туман войны выкл, z = 1 туман войны вкл
- {
- if (z == 1)
- {
- if (fogOfWar[x, y] == 1)
- Console.ForegroundColor = ConsoleColor.DarkGray; //цвет неизвестных обл.
- else if (ColorDictionary.ContainsKey(map[x, y]))
- Console.ForegroundColor = ColorDictionary[map[x, y]];
- else
- Console.ForegroundColor = ConsoleColor.White;
- if (fogOfWar[x, y] == 2)
- Console.ForegroundColor = ColorDictionary[symbolList[4]];
- if (fogOfWar[x, y] == 3)
- Console.ForegroundColor = ColorDictionary[symbolList[5]];
- }
- else
- {
- if (ColorDictionary.ContainsKey(map[x, y]))
- Console.ForegroundColor = ColorDictionary[map[x, y]];
- else
- Console.ForegroundColor = ConsoleColor.Gray;
- }
- }
- static void RenderInvisibleMap()
- {
- for (byte i = 0; i < map.GetLength(0); i++)
- {
- for (byte k = 0; k < map.GetLength(1); k++)
- {
- //Thread.Sleep(70);
- Colors(i, k, 0);
- Console.SetCursorPosition(i, k);
- Console.Write(map[i, k]);
- }
- }
- }
- static void RenderVisibleMap()
- {
- for (byte i = 0; i < map.GetLength(0); i++)
- {
- for (byte k = 0; k < map.GetLength(1); k++)
- {
- Colors(i, k, 1);
- Console.SetCursorPosition(i, k);
- if (fogOfWar[i, k] == 1)
- Console.Write(symbolList[3]);
- else if (fogOfWar[i, k] == 2)
- Console.Write(symbolList[4]);
- else if (fogOfWar[i, k] == 3)
- Console.Write(symbolList[5]);
- else if ((fogOfWar[i, k] == 0) && (map[i, k] == zeroOnMap))
- Console.Write((char)183);
- else
- Console.Write(map[i, k]);
- }
- }
- }
- static void FindChar(short x, short y)
- {
- for(int i = x; i < y; i++)
- {
- Console.WriteLine(i + ". " + (char)i);
- }
- }
- #endregion
- #region свалка
- /*
- //Console.SetCursorPosition(0, 15);
- //Console.WriteLine(map[x, y]);
- //Console.WriteLine(Convert.ToChar(Convert.ToString(f)));
- //char[] map1 = new char[2];
- //map1[0] = (char)48;
- //map1[1] = Convert.ToChar(0);
- //map1.
- //if (map[x, y] == map1[0] || map[x, y] == map1[1])
- //int k = Convert.ToInt32(map[x, y]);
- //if (k == 0)
- //Console.BackgroundColor = ConsoleColor.DarkRed;
- RenderInvisibleMap();
- PlayerCursor();
- Console.SetCursorPosition(fieldSizeX / 2, fieldSizeY / 2);
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("ПОБЕДА"); */
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement