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;
- namespace Project2_Balls
- {
- class Program
- {
- static int[,] FieldMake(int[,] arr, int str, int col, int r)
- {
- Random ran = new Random();
- int x = 0;
- for (int i = 0; i < str; i++)
- {
- for (int j = 0; j < str; j++)
- {
- if (x != col)
- {
- arr[i, j] = ran.Next(0, r);
- if (arr[i, j] != 0)
- {
- x++;
- }
- }
- }
- }
- return arr;
- }
- static void OutputField (ref int [,] arr, int des)
- {
- ConsoleColor[,] field = new ConsoleColor[arr.GetLength(0), arr.GetLength(1)];
- for (int i=0; i< arr.GetLength(0); i++)
- {
- for (int j = 0; j< arr.GetLength(1); j++ )
- {
- field[i, j] = (ConsoleColor)(arr[i, j]);
- }
- }
- int l = 0;
- Console.Write(" ");
- int str = arr.GetLength(0);
- while (str-->=0)
- {
- Console.Write(" " +l);
- l++;
- }
- Console.WriteLine();
- for (int i=0; i< arr.GetLength(0); i++)
- {
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write(i + " ");
- for (int j=0; j< arr.GetLength(1); j++)
- {
- Console.ForegroundColor = field[i, j];
- Console.Write("O" + (j >= 10 ? " " : " "));
- }
- Console.Write("\n");
- }
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine("Счет: " + des);
- }
- static void check (int [,] arr, ref int des)
- {
- for (int i=0; i< arr.GetLength(0); i++)
- {
- des = des + Check_Horizontal(ref arr, i);
- }
- for (int j = 0; j< arr.GetLength(1); j++)
- {
- des = des + Check_Vertical(ref arr, j);
- }
- for (int i=0; i< arr.GetLength(0) - 1; i++)
- {
- des = des + Check_DownR(ref arr, i, 0);
- }
- for (int j = 0; j< arr.GetLength(1) - 1; j++)
- {
- des = des + CheckUpR(ref arr, 0, j);
- }
- for (int i = 0; i < arr.GetLength(0) - 1; i++)
- {
- des = des + CheckUpR(ref arr, i, 0);
- }
- for (int j = 0; j < arr.GetLength(1) - 1; j++)
- {
- des = des + Check_DownR(ref arr, 0, j);
- }
- }
- static int Check_Horizontal(ref int[,] arr, int i)
- {
- int numb = 1;
- int x = 0;
- for (int j = 0; j < arr.GetLength(1) - 1; j++)
- {
- if (arr[i, j] == arr[i, j + 1] && arr[i, j] != 0)
- {
- numb++;
- }
- else
- {
- if (numb != 1)
- {
- if (numb >= 3)
- {
- x = x + numb;
- while (numb-- > 0)
- {
- arr[i, j - numb] = 0;
- }
- numb = 1;
- }
- else
- {
- numb = 1;
- }
- }
- }
- }
- return x;
- }
- static int Check_Vertical(ref int[,] arr, int j)
- {
- int numb = 1;
- int x = 0;
- for (int i = 0; i < arr.GetLength(0) - 1; i++)
- {
- if (arr[i, j] == arr[i + 1, j] && arr[i, j] != 0)
- {
- numb++;
- }
- else
- {
- if (numb != 1)
- {
- if (numb >= 3)
- {
- x = x + numb;
- while (numb-- > 0)
- {
- arr[i - numb, j] = 0;
- }
- numb = 1;
- }
- else
- {
- numb = 1;
- }
- }
- }
- }
- return x;
- }
- static int CheckUpR(ref int[,] arr, int i, int j)
- {
- int numb = 1;
- int x = 0;
- if (i > 0)
- {
- while (i > 0 && j < arr.GetLength(1) - 1)
- {
- if (arr[i, j] == arr[i - 1, j + 1] && arr[i, j] != 0)
- {
- numb++;
- }
- else
- {
- if (numb != 1)
- {
- if (numb >= 3)
- {
- x = x + numb;
- while (numb-- > 0)
- {
- arr[i + numb, j - numb] = 0;
- }
- numb = 1;
- }
- else
- {
- numb = 1;
- }
- }
- }
- j--;
- i--;
- if (numb >= 3)
- {
- x = x + numb;
- while (numb-- > 0)
- {
- arr[i + numb, j - numb] = 0;
- }
- numb = 1;
- }
- }
- return x;
- }
- else
- {
- return 0;
- }
- }
- static int Check_DownR(ref int[,] arr, int i, int j)
- {
- int numb = 1;
- int x = 0;
- if (i > 0)
- {
- while (i < arr.GetLength(0) - 1 && j < arr.GetLength(1) - 1)
- {
- if (arr[i, j] == arr[i + 1, j + 1] && arr[i, j] != 0)
- {
- numb++;
- }
- else
- {
- if (numb != 1)
- {
- if (numb >= 3)
- {
- x = x + numb;
- while (numb-- > 0)
- {
- arr[i - numb, j - numb] = 0;
- }
- numb = 1;
- }
- else
- {
- numb = 1;
- }
- }
- }
- }
- j--;
- i--;
- if (numb >= 3)
- {
- x = x + numb;
- while (numb-- > 0)
- {
- arr[i - numb, j - numb] = 0;
- }
- numb = 1;
- }
- return x;
- }
- else
- {
- return 0;
- }
- }
- static int [,] addBall (int [,] arr, int a)
- {
- int i, j;
- Random ran = new Random();
- do
- {
- i = ran.Next(0, arr.GetLength(0));
- j = ran.Next(0, arr.GetLength(1));
- }
- while (arr[i, j] != 0);
- arr[i, j] = ran.Next(0, a);
- Console.WriteLine("Шар был добавлен в поле ({0}, {1})", i, j);
- return arr;
- }
- static void InputBall (ref int [,] arr)
- {
- Console.WriteLine("Введите координату клетки по горизонатли, из которой вы хотите поместить шар:");
- int i = int.Parse(Console.ReadLine());
- Console.WriteLine("Введите координату клетки по вертикали, из которой вы хотите поместить шар:");
- int j = int.Parse(Console.ReadLine());
- Console.WriteLine("Введите координату клетки по горизонатли, в которую вы хотите поместить шар:");
- int i2 = int.Parse(Console.ReadLine());
- Console.WriteLine("Введите координату клетки по вертикали, в которую вы хотите поместить шар:");
- int j2 = int.Parse(Console.ReadLine());
- bool[,] Visited = new bool[arr.GetLength(0), arr.GetLength(1)];
- PossibleToMove(ref arr, Visited, i + 1, j);
- PossibleToMove(ref arr, Visited, i, j + 1);
- PossibleToMove(ref arr, Visited, i - 1, j);
- PossibleToMove(ref arr, Visited, i, j - 1);
- if (Visited[i2, j2])
- {
- arr[i2, j2] = arr[i, j];
- arr[i, j] = 0;
- Console.WriteLine("Шар успешно передвинут.");
- }
- else
- {
- Console.WriteLine("На пути перпятствия, передвижение шара не представляется возможным");
- }
- }
- static void PossibleToMove (ref int [,] arr, bool [,] Visited, int i, int j)
- {
- if (Is_In_Field(arr, i, j) && !Visited[i, j] && arr[i, j] == 0)
- {
- Visited[i, j] = true;
- PossibleToMove(ref arr, Visited, i + 1, j);
- PossibleToMove(ref arr, Visited, i - 1, j);
- PossibleToMove(ref arr, Visited, i, j + 1);
- PossibleToMove(ref arr, Visited, i, j - 1);
- }
- }
- static bool Is_In_Field (int [,] arr, int i, int j)
- {
- if (i>=0 && i<arr.GetLength(0) && j>=0 && j < arr.GetLength(1))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- static bool FieldFull(int [,] arr)
- {
- int x = arr.GetLength(0) * arr.GetLength(1);
- int y = 0;
- for (int i=0; i<arr.GetLength(0); i++)
- {
- for (int j=0; j<arr.GetLength(1); j++)
- {
- if (arr[i,j]!=0)
- {
- y++;
- }
- }
- }
- if (y==x)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- static bool FieldEmpty (int [,] arr)
- {
- int x = arr.GetLength(0) * arr.GetLength(1);
- int y = 0;
- for (int i = 0; i < arr.GetLength(0); i++)
- {
- for (int j = 0; j < arr.GetLength(1); j++)
- {
- if (arr[i, j] == 0)
- {
- y++;
- }
- }
- }
- if (y == x)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- static void gameplay(int str, int col)
- {
- int[,] arr = new int[str, str];
- int r;
- Console.WriteLine("Введите количество цветов");
- r = int.Parse(Console.ReadLine());
- arr = FieldMake(arr, str, col, r);
- int des = 0;
- OutputField(ref arr, des);
- check(arr, ref des);
- string s;
- string s2 = "Stop";
- do
- {
- InputBall(ref arr);
- check(arr, ref des);
- if (FieldEmpty(arr))
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("Мне очень жаль Вас, Вы выиграли. \nСчет: " + des);
- return;
- }
- if (FieldFull(arr))
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("Я очень рад за вас, Вы проиграли");
- return;
- }
- addBall(arr, r);
- check(arr, ref des);
- if (FieldFull(arr))
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("Я очень рад за вас, Вы проиграли");
- return;
- }
- OutputField(ref arr, des);
- Console.WriteLine("Если хотите закончить, введите Stop. В противном случае введите любой другой символ");
- s = Console.ReadLine();
- }
- while (s != s2);
- return;
- }
- static void Main(string[] args)
- {
- int a;
- int col;
- Console.WriteLine("Введите размер поля (поле квaдратной формы, поэтому высота = ширине):");
- a = int.Parse(Console.ReadLine());
- Console.WriteLine("Введите количество цветов шариков, которыми вы хотели бы играть:");
- col = int.Parse(Console.ReadLine());
- gameplay(a, col);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement