Advertisement
Guest User

123

a guest
Jan 20th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Project2_Balls
  8. {
  9.     class Program
  10.     {
  11.         static int[,] FieldMake(int[,] arr, int str, int col, int r)
  12.         {
  13.             Random ran = new Random();
  14.             int x = 0;
  15.             for (int i = 0; i < str; i++)
  16.             {
  17.                 for (int j = 0; j < str; j++)
  18.                 {
  19.                     if (x != col)
  20.                     {
  21.                         arr[i, j] = ran.Next(0, r);
  22.                         if (arr[i, j] != 0)
  23.                         {
  24.                             x++;
  25.                         }
  26.                     }
  27.                 }
  28.             }
  29.             return arr;
  30.         }
  31.         static void OutputField (ref int [,] arr, int des)
  32.         {
  33.             ConsoleColor[,] field = new ConsoleColor[arr.GetLength(0), arr.GetLength(1)];
  34.             for (int i=0; i< arr.GetLength(0); i++)
  35.             {
  36.                 for (int j = 0; j< arr.GetLength(1); j++ )
  37.                 {
  38.                     field[i, j] = (ConsoleColor)(arr[i, j]);
  39.                 }
  40.             }
  41.             int l = 0;
  42.             Console.Write(" ");
  43.             int str = arr.GetLength(0);
  44.             while (str-->=0)
  45.             {
  46.                 Console.Write(" " +l);
  47.                 l++;
  48.             }
  49.             Console.WriteLine();
  50.             for (int i=0; i< arr.GetLength(0); i++)
  51.             {
  52.                 Console.ForegroundColor = ConsoleColor.White;
  53.                 Console.Write(i + " ");
  54.                 for (int j=0; j< arr.GetLength(1); j++)
  55.                 {
  56.                     Console.ForegroundColor = field[i, j];
  57.                     Console.Write("O" + (j >= 10 ? "  " : " "));
  58.                 }
  59.                 Console.Write("\n");
  60.             }
  61.             Console.ForegroundColor = ConsoleColor.White;
  62.             Console.WriteLine("Счет: " + des);
  63.         }
  64.         static void check (int [,] arr, ref int des)
  65.         {
  66.             for (int i=0; i< arr.GetLength(0); i++)
  67.             {
  68.                 des = des + Check_Horizontal(ref arr, i);
  69.             }
  70.             for (int j = 0; j< arr.GetLength(1); j++)
  71.             {
  72.                 des = des + Check_Vertical(ref arr, j);
  73.             }
  74.             for (int i=0; i< arr.GetLength(0) - 1; i++)
  75.             {
  76.                 des = des + Check_DownR(ref arr, i, 0);
  77.             }
  78.             for (int j = 0; j< arr.GetLength(1) - 1; j++)
  79.             {
  80.                 des = des + CheckUpR(ref arr, 0, j);
  81.             }
  82.             for (int i = 0; i < arr.GetLength(0) - 1; i++)
  83.             {
  84.                 des = des + CheckUpR(ref arr, i, 0);
  85.             }
  86.             for (int j = 0; j < arr.GetLength(1) - 1; j++)
  87.             {
  88.                 des = des + Check_DownR(ref arr, 0, j);
  89.             }
  90.         }
  91.         static int Check_Horizontal(ref int[,] arr, int i)
  92.         {
  93.             int numb = 1;
  94.             int x = 0;
  95.             for (int j = 0; j < arr.GetLength(1) - 1; j++)
  96.             {
  97.                 if (arr[i, j] == arr[i, j + 1] && arr[i, j] != 0)
  98.                 {
  99.                     numb++;
  100.                 }
  101.                 else
  102.                 {
  103.                     if (numb != 1)
  104.                     {
  105.                         if (numb >= 3)
  106.                         {
  107.                             x = x + numb;
  108.                             while (numb-- > 0)
  109.                             {
  110.                                 arr[i, j - numb] = 0;
  111.                             }
  112.                             numb = 1;
  113.                         }
  114.                         else
  115.                         {
  116.                             numb = 1;
  117.                         }
  118.                     }
  119.                 }
  120.             }
  121.             return x;
  122.         }
  123.         static int Check_Vertical(ref int[,] arr, int j)
  124.         {
  125.             int numb = 1;
  126.             int x = 0;
  127.             for (int i = 0; i < arr.GetLength(0) - 1; i++)
  128.             {
  129.                 if (arr[i, j] == arr[i + 1, j] && arr[i, j] != 0)
  130.                 {
  131.                     numb++;
  132.                 }
  133.                 else
  134.                 {
  135.                     if (numb != 1)
  136.                     {
  137.                         if (numb >= 3)
  138.                         {
  139.                             x = x + numb;
  140.                             while (numb-- > 0)
  141.                             {
  142.                                 arr[i - numb, j] = 0;
  143.                             }
  144.                             numb = 1;
  145.                         }
  146.                         else
  147.                         {
  148.                             numb = 1;
  149.                         }
  150.                     }
  151.                 }
  152.             }
  153.             return x;
  154.         }
  155.         static int CheckUpR(ref int[,] arr, int i, int j)
  156.         {
  157.             int numb = 1;
  158.             int x = 0;
  159.             if (i > 0)
  160.             {
  161.                 while (i > 0 && j < arr.GetLength(1) - 1)
  162.                 {
  163.                     if (arr[i, j] == arr[i - 1, j + 1] && arr[i, j] != 0)
  164.                     {
  165.                         numb++;
  166.                     }
  167.                     else
  168.                     {
  169.                         if (numb != 1)
  170.                         {
  171.                             if (numb >= 3)
  172.                             {
  173.                                 x = x + numb;
  174.                                 while (numb-- > 0)
  175.                                 {
  176.                                     arr[i + numb, j - numb] = 0;
  177.                                 }
  178.                                 numb = 1;
  179.                             }
  180.                             else
  181.                             {
  182.                                 numb = 1;
  183.                             }
  184.                         }
  185.                     }
  186.                     j--;
  187.                     i--;
  188.                     if (numb >= 3)
  189.                     {
  190.                         x = x + numb;
  191.                         while (numb-- > 0)
  192.                         {
  193.                             arr[i + numb, j - numb] = 0;
  194.                         }
  195.                         numb = 1;
  196.                     }
  197.                 }
  198.                 return x;
  199.             }
  200.             else
  201.             {
  202.                 return 0;
  203.             }
  204.  
  205.         }
  206.         static int Check_DownR(ref int[,] arr, int i, int j)
  207.         {
  208.             int numb = 1;
  209.             int x = 0;
  210.             if (i > 0)
  211.             {
  212.                 while (i < arr.GetLength(0) - 1 && j < arr.GetLength(1) - 1)
  213.                 {
  214.                     if (arr[i, j] == arr[i + 1, j + 1] && arr[i, j] != 0)
  215.                     {
  216.                         numb++;
  217.                     }
  218.                     else
  219.                     {
  220.                         if (numb != 1)
  221.                         {
  222.                             if (numb >= 3)
  223.                             {
  224.                                 x = x + numb;
  225.                                 while (numb-- > 0)
  226.                                 {
  227.                                     arr[i - numb, j - numb] = 0;
  228.                                 }
  229.                                 numb = 1;
  230.                             }
  231.                             else
  232.                             {
  233.                                 numb = 1;
  234.                             }
  235.                         }
  236.                     }
  237.                 }
  238.                 j--;
  239.                 i--;
  240.                 if (numb >= 3)
  241.                 {
  242.                     x = x + numb;
  243.                     while (numb-- > 0)
  244.                     {
  245.                         arr[i - numb, j - numb] = 0;
  246.                     }
  247.                     numb = 1;
  248.                 }
  249.                 return x;
  250.             }
  251.             else
  252.             {
  253.                 return 0;
  254.             }
  255.         }
  256.         static int [,] addBall (int [,] arr, int a)
  257.         {
  258.             int i, j;
  259.             Random ran = new Random();
  260.             do
  261.             {
  262.                 i = ran.Next(0, arr.GetLength(0));
  263.                 j = ran.Next(0, arr.GetLength(1));
  264.             }
  265.             while (arr[i, j] != 0);
  266.             arr[i, j] = ran.Next(0, a);
  267.             Console.WriteLine("Шар был добавлен в поле ({0}, {1})", i, j);
  268.             return arr;
  269.         }
  270.         static void InputBall (ref int [,] arr)
  271.         {
  272.             Console.WriteLine("Введите координату клетки по горизонатли, из которой вы хотите поместить шар:");
  273.             int i = int.Parse(Console.ReadLine());
  274.             Console.WriteLine("Введите координату клетки по вертикали, из которой вы хотите поместить шар:");
  275.             int j = int.Parse(Console.ReadLine());
  276.             Console.WriteLine("Введите координату клетки по горизонатли, в которую вы хотите поместить шар:");
  277.             int i2 = int.Parse(Console.ReadLine());
  278.             Console.WriteLine("Введите координату клетки по вертикали, в которую вы хотите поместить шар:");
  279.             int j2 = int.Parse(Console.ReadLine());
  280.             bool[,] Visited = new bool[arr.GetLength(0), arr.GetLength(1)];
  281.             PossibleToMove(ref arr, Visited, i + 1, j);
  282.             PossibleToMove(ref arr, Visited, i, j + 1);
  283.             PossibleToMove(ref arr, Visited, i - 1, j);
  284.             PossibleToMove(ref arr, Visited, i, j - 1);
  285.             if (Visited[i2, j2])
  286.             {
  287.                 arr[i2, j2] = arr[i, j];
  288.                 arr[i, j] = 0;
  289.                 Console.WriteLine("Шар успешно передвинут.");
  290.             }
  291.             else
  292.             {
  293.                 Console.WriteLine("На пути перпятствия, передвижение шара не представляется возможным");
  294.             }
  295.         }
  296.         static void PossibleToMove (ref int [,] arr, bool [,] Visited, int i, int j)
  297.         {
  298.             if (Is_In_Field(arr, i, j) && !Visited[i, j] && arr[i, j] == 0)
  299.             {
  300.                 Visited[i, j] = true;
  301.                 PossibleToMove(ref arr, Visited, i + 1, j);
  302.                 PossibleToMove(ref arr, Visited, i - 1, j);
  303.                 PossibleToMove(ref arr, Visited, i, j + 1);
  304.                 PossibleToMove(ref arr, Visited, i, j - 1);
  305.             }
  306.         }
  307.         static bool Is_In_Field (int [,] arr, int i, int j)
  308.         {
  309.             if (i>=0 && i<arr.GetLength(0) && j>=0 && j < arr.GetLength(1))
  310.             {
  311.                 return true;
  312.             }
  313.             else
  314.             {
  315.                 return false;
  316.             }
  317.         }
  318.         static bool FieldFull(int [,] arr)
  319.         {
  320.             int x = arr.GetLength(0) * arr.GetLength(1);
  321.             int y = 0;
  322.             for (int i=0; i<arr.GetLength(0); i++)
  323.             {
  324.                 for (int j=0; j<arr.GetLength(1); j++)
  325.                 {
  326.                     if (arr[i,j]!=0)
  327.                     {
  328.                         y++;
  329.                     }
  330.                 }
  331.             }
  332.             if (y==x)
  333.             {
  334.                 return true;
  335.             }
  336.             else
  337.             {
  338.                 return false;
  339.             }
  340.         }
  341.         static bool FieldEmpty (int [,] arr)
  342.         {
  343.             int x = arr.GetLength(0) * arr.GetLength(1);
  344.             int y = 0;
  345.             for (int i = 0; i < arr.GetLength(0); i++)
  346.             {
  347.                 for (int j = 0; j < arr.GetLength(1); j++)
  348.                 {
  349.                     if (arr[i, j] == 0)
  350.                     {
  351.                         y++;
  352.                     }
  353.                 }
  354.             }
  355.             if (y == x)
  356.             {
  357.                 return true;
  358.             }
  359.             else
  360.  
  361.             {
  362.                 return false;
  363.             }
  364.         }
  365.         static void gameplay(int str, int col)
  366.         {
  367.             int[,] arr = new int[str, str];
  368.             int r;
  369.             Console.WriteLine("Введите количество цветов");
  370.             r = int.Parse(Console.ReadLine());
  371.             arr = FieldMake(arr, str, col, r);
  372.             int des = 0;
  373.             OutputField(ref arr, des);
  374.             check(arr, ref des);
  375.             string s;
  376.             string s2 = "Stop";
  377.             do
  378.             {
  379.                 InputBall(ref arr);
  380.                 check(arr, ref des);
  381.                 if (FieldEmpty(arr))
  382.                 {
  383.                     Console.ForegroundColor = ConsoleColor.Red;
  384.                     Console.WriteLine("Мне очень жаль Вас, Вы выиграли. \nСчет: " + des);
  385.                     return;
  386.                 }
  387.                 if (FieldFull(arr))
  388.                 {
  389.                     Console.ForegroundColor = ConsoleColor.Green;
  390.                     Console.WriteLine("Я очень рад за вас, Вы проиграли");
  391.                     return;
  392.                 }
  393.                 addBall(arr, r);
  394.                 check(arr, ref des);
  395.                 if (FieldFull(arr))
  396.                 {
  397.                     Console.ForegroundColor = ConsoleColor.Green;
  398.                     Console.WriteLine("Я очень рад за вас, Вы проиграли");
  399.                     return;
  400.                 }
  401.                 OutputField(ref arr, des);
  402.                 Console.WriteLine("Если хотите закончить, введите Stop. В противном случае введите любой другой символ");
  403.                 s = Console.ReadLine();
  404.             }
  405.             while (s != s2);
  406.             return;
  407.         }
  408.         static void Main(string[] args)
  409.         {
  410.             int a;
  411.             int col;
  412.             Console.WriteLine("Введите размер поля (поле квaдратной формы, поэтому высота = ширине):");
  413.             a = int.Parse(Console.ReadLine());
  414.             Console.WriteLine("Введите количество цветов шариков, которыми вы хотели бы играть:");
  415.             col = int.Parse(Console.ReadLine());
  416.             gameplay(a, col);
  417.         }
  418.     }
  419. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement