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.Diagnostics;
- namespace pratofiorito
- {
- public class BombClass : IEquatable<BombClass>
- {
- public BombClass(int h, int l)
- {
- this.x = h;
- this.y = l;
- }
- public int x { get; set; }
- public int y { get; set; }
- public bool Equals(BombClass other)
- {
- if (this.x == other.x && this.y == other.y)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- class BoxClass
- {
- public BoxClass(int h, int l)
- {
- this.x = h;
- this.y = l;
- }
- public int x { get; set; }
- public int y { get; set; }
- }
- class Program
- {
- static public int lengthX = 6;
- static public int lengthY = 9;
- static public int bombNum = 0;
- static public bool win = false;
- static public char[,] map = new char[lengthX, lengthY];
- static public bool[,] mapBool = new bool[lengthX, lengthY];
- static public List<BombClass> bomb = new List<BombClass>();
- static public BoxClass selected_box = new BoxClass(0, 0);
- static public void create_map()
- {
- for (int i = 0; i < lengthX; i++)
- {
- for (int k = 0; k < lengthY; k++)
- {
- map[i, k] = '~';
- }
- }
- }
- static public void draw_map()
- {
- Console.Clear();
- for(int i = 0; i < lengthX; i++)
- {
- for(int k = 0; k < lengthY; k++)
- {
- if(i == selected_box.x && k == selected_box.y)
- {
- Console.Write("_ ");
- }
- else if (mapBool[i, k])
- {
- Console.Write(map[i, k] + " ");
- }else
- {
- Console.Write("# ");
- }
- }
- Console.WriteLine();
- }
- }
- static public void spawn_bomb()
- {
- for(int i = 0; i < 8; i++)
- {
- Random rnd = new Random();
- int x = rnd.Next(1, lengthX);
- int y = rnd.Next(1, lengthY);
- while (bomb.Contains(new BombClass(x,y)))
- {
- x = rnd.Next(1, lengthX);
- y = rnd.Next(1, lengthY);
- }
- bomb.Add(new BombClass(x, y));
- map[x, y] = '@';
- }
- }
- static public void spawn_number()
- {
- for (int k = 0; k < lengthY; k++)
- {
- for (int i = 0; i < lengthX; i++)
- {
- int bomb_for_pos = 0;
- if (map[i, k] != '@')
- {
- if (i - 1 >= 0 && k - 1 >= 0) // bassso sinistra
- {
- if (map[i - 1, k - 1] == '@') bomb_for_pos++;
- }
- if (k - 1 >= 0) // sinistra
- {
- if (map[i, k - 1] == '@') bomb_for_pos++;
- }
- if (i + 1 < lengthX && k - 1 >= 0) // sinistra alto
- {
- if (map[i + 1, k - 1] == '@') bomb_for_pos++;
- }
- if (i - 1 >= 0) // basso
- {
- if (map[i - 1, k] == '@') bomb_for_pos++;
- }
- if (i + 1 < lengthX) // alto
- {
- if (map[i + 1, k] == '@') bomb_for_pos++;
- }
- if (i - 1 >= 0 && k + 1 < lengthY) // destra basso
- {
- if (map[i - 1, k + 1] == '@') bomb_for_pos++;
- }
- if (k + 1 < lengthY) // destra
- {
- if (map[i, k + 1] == '@') bomb_for_pos++;
- }
- if (i + 1 < lengthX && k + 1 < lengthY) // destra alto
- {
- if (map[i + 1, k + 1] == '@') bomb_for_pos++;
- }
- }
- if (bomb_for_pos > 0)
- {
- map[i, k] = Convert.ToChar(bomb_for_pos.ToString());
- }
- }
- }
- }
- static public void move(string _move)
- {
- switch (_move)
- {
- case "DownArrow":
- if (selected_box.x + 1 < lengthX)
- {
- selected_box.x += 1;
- draw_map();
- }
- break;
- case "UpArrow":
- if (selected_box.x - 1 > -1)
- {
- selected_box.x -= 1;
- draw_map();
- }
- break;
- case "LeftArrow":
- if (selected_box.y - 1 > -1)
- {
- selected_box.y -= 1;
- draw_map();
- }
- break;
- case "RightArrow":
- if (selected_box.x + 1 < lengthY)
- {
- selected_box.y += 1;
- draw_map();
- }
- break;
- case "Enter":
- enter();
- break;
- }
- check_win();
- }
- static public void check_win()
- {
- int tot = 0;
- for(int i = 0; i < lengthX; i++)
- {
- for(int j = 0; j < lengthY; j++)
- {
- if (mapBool[i,j]) tot++;
- }
- }
- if(tot == bombNum)
- {
- win = true;
- draw_map();
- Console.WriteLine("HAI VINTO!");
- }
- }
- static public void enter()
- {
- if(map[selected_box.x, selected_box.y] == '@')
- {
- for(int i = 0; i < lengthX; i++)
- {
- for(int j = 0; j < lengthY; j++)
- {
- mapBool[i, j] = true;
- }
- }
- draw_map();
- win = true;
- Console.WriteLine("HAI PERSO!");
- }else if(map[selected_box.x, selected_box.y] == '~')
- {
- Debug.WriteLine("Find null");
- List<BoxClass> queue = new List<BoxClass>();
- queue.Add(new BoxClass(selected_box.x, selected_box.y));
- while (queue.Count > 0)
- {
- Debug.WriteLine(map[queue[0].x, queue[0].y] + " at pos: " + queue[0].x + " , " + queue[0].y);
- int i = queue[0].x;
- int k = queue[0].y;
- queue.RemoveAt(0);
- if (map[i, k] != '@')
- {
- if (i - 1 >= 0 && k - 1 >= 0) // bassso sinistra
- {
- if (map[i - 1, k - 1] != '~' && map[i - 1, k - 1] != '@') { mapBool[i - 1, k - 1] = true; }
- else if (map[i - 1, k - 1] != '@' && !mapBool[i - 1, k - 1]) { queue.Add(new BoxClass(i - 1, k - 1)); mapBool[i - 1, k - 1] = true; }
- }
- if (k - 1 >= 0) // sinistra
- {
- if (map[i, k - 1] != '~' && map[i, k - 1] != '@') { mapBool[i, k - 1] = true; }
- else if (map[i, k - 1] != '@' && !mapBool[i, k - 1]) { queue.Add(new BoxClass(i, k - 1)); mapBool[i, k - 1] = true; }
- }
- if (i + 1 < lengthX && k - 1 >= 0) // sinistra alto
- {
- if (map[i + 1, k - 1] != '~' && map[i + 1, k - 1] != '@') { mapBool[i + 1, k - 1] = true; }
- else if (map[i + 1, k - 1] != '@' && !mapBool[i + 1, k - 1]) { queue.Add(new BoxClass(i + 1, k - 1)); mapBool[i + 1, k - 1] = true; }
- }
- if (i - 1 >= 0) // basso
- {
- if (map[i - 1, k] != '~' && map[i - 1, k] != '@') { mapBool[i - 1, k] = true; }
- else if (map[i - 1, k] != '@' && !mapBool[i - 1, k]) { queue.Add(new BoxClass(i - 1, k)); mapBool[i - 1, k] = true; }
- }
- if (i + 1 < lengthX) // alto
- {
- if (map[i + 1, k] != '~' && map[i + 1, k] != '@') { mapBool[i + 1, k] = true; }
- else if (map[i + 1, k] != '@' && !mapBool[i + 1, k]) { queue.Add(new BoxClass(i + 1, k)); mapBool[i + 1, k] = true; }
- }
- if (i - 1 >= 0 && k + 1 < lengthY) // destra basso
- {
- if (map[i - 1, k + 1] != '~' && map[i - 1, k + 1] != '@') { mapBool[i - 1, k + 1] = true; }
- else if (map[i - 1, k + 1] != '@' && !mapBool[i - 1, k + 1]) { queue.Add(new BoxClass(i - 1, k + 1)); mapBool[i - 1, k + 1] = true; }
- }
- if (k + 1 < lengthY) // destra
- {
- if (map[i, k + 1] != '~' && map[i, k + 1] != '@') { mapBool[i, k + 1] = true; }
- else if (map[i, k + 1] != '@' && !mapBool[i, k + 1]) { queue.Add(new BoxClass(i, k + 1)); mapBool[i, k + 1] = true; }
- }
- if (i + 1 < lengthX && k + 1 < lengthY) // destra alto
- {
- if (map[i + 1, k + 1] != '~' && map[i + 1, k + 1] != '@') { mapBool[i + 1, k + 1] = true; }
- else if (map[i + 1, k + 1] != '@' && !mapBool[i + 1, k + 1]) { queue.Add(new BoxClass(i + 1, k + 1)); mapBool[i + 1, k + 1] = true; }
- }
- }
- }
- draw_map();
- }else
- {
- mapBool[selected_box.x, selected_box.y] = true;
- draw_map();
- }
- }
- static void Main(string[] args)
- {
- create_map();
- draw_map();
- bombNum = 56 * (8) / (lengthX*lengthY);
- spawn_bomb();
- spawn_number();
- draw_map();
- while (!win)
- {
- var key = Console.ReadKey();
- move(key.Key.ToString());
- }
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement