Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.63 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5.     static bool[,] mineArray;
  6.     static Random random = new Random();
  7.  
  8.     static int BoolCount(int row, int col, int rowMax, int colMax)
  9.     {
  10.         int boolcount = 0;
  11.  
  12.         for (int i = -1; i <= 1; i++)
  13.             for (int j = -1; j <= 1; j++)
  14.             {
  15.                 if (row + i < 0 ||
  16.                     col + j < 0 ||
  17.                     row + i >= rowMax ||
  18.                     col + j >= colMax ||
  19.                     (row + i == row && col + j == col)) continue;
  20.                 if (mineArray[row + i, col + j] == true)
  21.                     boolcount++;
  22.             }
  23.  
  24.         return boolcount;
  25.     }
  26.  
  27.     static void Main()
  28.     {
  29.         ConsoleKey key;
  30.         do
  31.         {
  32.             int k = random.Next(1, 6),
  33.                 m = random.Next(1, 11); // мне лень писать метод для ввода значений
  34.  
  35.             mineArray = new bool[k, m];
  36.             for (int i = 0; i < k; i++)
  37.             {
  38.                 for (int j = 0; j < m; j++)
  39.                 {
  40.                     mineArray[i, j] = random.Next(0, 2) == 0;
  41.                     Console.Write((mineArray[i, j] == false ? "0" : "1") + "\t"); // для наглядности
  42.                 }
  43.                 Console.WriteLine();
  44.             }
  45.  
  46.             int row = random.Next(0, k),
  47.                 col = random.Next(0, m);
  48.             int boolcount = BoolCount(row, col, k, m);
  49.  
  50.             Console.WriteLine("count @ [{0}, {1}] = {2}", row, col, boolcount);
  51.             key = Console.ReadKey(true).Key;
  52.         }
  53.         while (key != ConsoleKey.Escape);
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement