Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///Snake
- using System;
- using System.Text;
- using System.Timers;
- using System.Threading;
- using System.Threading.Tasks;
- namespace newsnake
- {
- class Program
- {
- int size = 10, snakeSize;
- int[,] grid;
- static Coord direction;
- Coord position, previousDirection;
- System.Timers.Timer mainLoop;
- Random random = new Random();
- Thread keyListen;
- static void Main(string[] args)
- {
- Program p = new Program();
- p.Start();
- }
- public void Start()
- {
- grid = new int[size, size];
- for (int x = 0; x < size; x++)
- for (int y = 0; y < size; y++)
- grid[x, y] = 0;
- snakeSize = 3;
- position = new Coord(size / 2, size / 2);
- grid[position.x, position.y] = snakeSize;
- direction = new Coord(1, 0);
- previousDirection = direction;
- SpawnCherry();
- if (keyListen == null)
- {
- keyListen = new Thread(ListenForKeys);
- keyListen.Start();
- }
- if (mainLoop == null)
- {
- mainLoop = new System.Timers.Timer(500);
- mainLoop.Elapsed += UpdateLoop;
- mainLoop.AutoReset = true;
- mainLoop.Enabled = true;
- }
- }
- void SpawnCherry()
- {
- bool posFound = false;
- while (!posFound)
- {
- Coord newCoord = new Coord(random.Next(0, size - 1), random.Next(0, size - 1));
- if (grid[newCoord.x, newCoord.y] == 0)
- {
- grid[newCoord.x, newCoord.y] = -1;
- posFound = true;
- }
- }
- }
- void UpdateLoop(object sender, ElapsedEventArgs e)
- {
- Move();
- DrawGrid();
- }
- void Move()
- {
- for (int y = 0; y < size; y++)
- for (int x = 0; x < size; x++)
- if (grid[x, y] > 0)
- grid[x, y]--;
- Coord newPos = new Coord(position.x + direction.x, position.y + direction.y);
- if (previousDirection.x + direction.x == 0 || previousDirection.y + direction.y == 0)
- newPos = new Coord(position.x + previousDirection.x, position.y + previousDirection.y);
- if (newPos.x < 0)
- newPos.x = size - 1;
- if (newPos.x >= size)
- newPos.x = 0;
- if (newPos.y < 0)
- newPos.y = size - 1;
- if (newPos.y >= size)
- newPos.y = 0;
- int gridCell = grid[newPos.x, newPos.y];
- if (gridCell > 0)
- {
- Start();
- return;
- }
- if (gridCell == -1)
- {
- snakeSize++;
- SpawnCherry();
- }
- grid[newPos.x, newPos.y] = snakeSize;
- position = newPos;
- if (previousDirection.x != direction.x && direction.y != previousDirection.y)
- previousDirection = direction;
- }
- void ListenForKeys()
- {
- while (true)
- {
- switch (Console.ReadKey(true).Key)
- {
- case ConsoleKey.W:
- direction = new Coord(0, -1);
- break;
- case ConsoleKey.S:
- direction = new Coord(0, 1);
- break;
- case ConsoleKey.A:
- direction = new Coord(-1, 0);
- break;
- case ConsoleKey.D:
- direction = new Coord(1, 0);
- break;
- }
- }
- }
- void DrawGrid()
- {
- Console.Clear();
- Console.WriteLine("Score: " + (snakeSize - 3) + "\n");
- string c;
- for (int y = 0; y < size; y++)
- {
- for (int x = 0; x < size; x++)
- {
- c = " .";
- if (grid[x, y] >= 1)
- {
- Console.ForegroundColor = ConsoleColor.DarkGreen;
- c = " ■";
- if (grid[x, y] == snakeSize)
- Console.ForegroundColor = ConsoleColor.Green;
- }
- if (grid[x, y] == -1)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- c = " %";
- }
- Console.Write(c);
- Console.ResetColor();
- }
- Console.WriteLine();
- }
- }
- struct Coord
- {
- public int x, y;
- public Coord(int X, int Y)
- {
- x = X;
- y = Y;
- }
- }
- }
- }
- ///Minesweeper
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace minesweeper
- {
- class Program
- {
- static void Main(string[] args)
- {
- Program p = new Program();
- p.Start();
- }
- int[,] grid;
- bool[,] revealed;
- bool[,] bombFlags;
- bool bombsSpawned = false;
- int gridHeight = 10, gridWidth = 10, bombCount = 15;
- Coord selected;
- Random random = new Random();
- void Start()
- {
- bombsSpawned = false;
- grid = new int[gridWidth, gridHeight];
- revealed = new bool[gridWidth, gridHeight];
- bombFlags = new bool[gridWidth, gridHeight];
- for (int y = 0; y < gridHeight - 1; y++)
- for (int x = 0; x < gridWidth - 1; x++)
- {
- grid[x, y] = 0;
- revealed[x, y] = false;
- bombFlags[x, y] = false;
- }
- selected = new Coord(gridWidth / 2, gridHeight / 2);
- Draw();
- ChooseAction();
- }
- void ChooseAction()
- {
- switch (Console.ReadKey(true).Key)
- {
- case ConsoleKey.W:
- selected.y--;
- break;
- case ConsoleKey.S:
- selected.y++;
- break;
- case ConsoleKey.D:
- selected.x++;
- break;
- case ConsoleKey.A:
- selected.x--;
- break;
- case ConsoleKey.E:
- Reveal();
- break;
- case ConsoleKey.Q:
- Flag();
- break;
- }
- if (selected.y < 0)
- selected.y = gridHeight - 1;
- if (selected.y == gridHeight)
- selected.y = 0;
- if (selected.x < 0)
- selected.x = gridWidth - 1;
- if (selected.x == gridWidth)
- selected.x = 0;
- Draw();
- }
- void Flag()
- {
- if (!revealed[selected.x, selected.y] && bombsSpawned)
- {
- if (bombFlags[selected.x, selected.y])
- bombFlags[selected.x, selected.y] = false;
- else
- bombFlags[selected.x, selected.y] = true;
- }
- }
- void RevealAdjacent(Coord cellToReveal)
- {
- Queue<Coord> toReveal = new Queue<Coord>();
- Queue<Coord> alreadyChecked = new Queue<Coord>();
- toReveal.Enqueue(cellToReveal);
- while (true)
- {
- if (toReveal.Count > 0)
- {
- Coord working = toReveal.Dequeue();
- alreadyChecked.Enqueue(working);
- for (int x = working.x - 1; x <= working.x + 1; x++)
- for (int y = working.y - 1; y <= working.y + 1; y++)
- {
- if (x >= 0 && x < gridWidth && y >= 0 && y < gridHeight)
- {
- if (!bombFlags[x, y] && grid[x, y] != -1)
- revealed[x, y] = true;
- Coord newCoord = new Coord(x, y);
- if (grid[x, y] == 0 && !alreadyChecked.Contains(newCoord))
- toReveal.Enqueue(newCoord);
- }
- }
- }
- else
- break;
- }
- }
- void Reveal()
- {
- if (!bombsSpawned)
- SpawnBombs();
- if (!bombFlags[selected.x, selected.y])
- {
- if (grid[selected.x, selected.y] != -1)
- {
- revealed[selected.x, selected.y] = true;
- if (grid[selected.x, selected.y] == 0)
- RevealAdjacent(selected);
- }
- else
- {
- DrawEndScreen(false);
- Start();
- }
- }
- int revealedCount = 0, emptyCount = 0;
- foreach (int i in grid)
- if (i != -1)
- emptyCount++;
- foreach (bool b in revealed)
- if (b)
- revealedCount++;
- if (revealedCount == emptyCount)
- {
- DrawEndScreen(true);
- Start();
- }
- }
- void DrawEndScreen(bool win)
- {
- Console.Clear();
- if (!win)
- Console.Write("\n You lost!");
- else
- {
- Console.ReadKey();
- Console.Write("\n You won!");
- }
- Console.ReadKey();
- }
- void Draw()
- {
- Console.Clear();
- string cell;
- for (int y = 0; y < gridHeight; y++)
- {
- for (int x = 0; x < gridWidth; x++)
- {
- cell = " ■";
- if (revealed[x, y])
- if (grid[x, y] > 0)
- cell = " " + grid[x, y];
- else if (grid[x, y] == 0)
- cell = " .";
- if (bombFlags[x, y])
- {
- Console.BackgroundColor = ConsoleColor.Red;
- Console.ForegroundColor = ConsoleColor.Black;
- }
- if (x == selected.x && y == selected.y)
- Console.BackgroundColor = ConsoleColor.DarkGreen;
- Console.Write(cell);
- Console.ResetColor();
- }
- Console.WriteLine();
- }
- ChooseAction();
- }
- void SpawnBombs()
- {
- int count = bombCount;
- while (count > 0)
- {
- Coord rand = new Coord(random.Next(0, gridWidth - 1), random.Next(0, gridHeight - 1));
- if (grid[rand.x, rand.y] != -1 && rand.x != selected.x && rand.y != selected.y)
- {
- grid[rand.x, rand.y] = -1;
- count--;
- }
- }
- for (int Y = 0; Y < gridHeight; Y++)
- for (int X = 0; X < gridWidth; X++)
- {
- for (int x = X - 1; x <= X + 1; x++)
- for (int y = Y - 1; y <= Y + 1; y++)
- {
- if (x >= 0 && x < gridWidth && y >= 0 && y < gridHeight)
- {
- if (grid[X, Y] != -1)
- if (grid[x, y] == -1)
- grid[X, Y]++;
- }
- }
- }
- RevealAdjacent(selected);
- bombsSpawned = true;
- }
- struct Coord
- {
- public int x, y;
- public Coord(int X, int Y)
- {
- x = X;
- y = Y;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement