Advertisement
zdravko7

Exam 29 March 2015 Evening Ex_5

Mar 29th, 2015
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Five
  6. {
  7.     static void Main()
  8.     {
  9.         int[,] matrix = new int[8, 8];
  10.  
  11.         //InitialiseMatrix(matrix);
  12.         Console.WriteLine();
  13.  
  14.         string input = "";
  15.         int row = 0;
  16.         int col = 7;
  17.  
  18.         matrix[0, 7] = 1;
  19.  
  20.         //command loop
  21.         while (true)
  22.         {
  23.             input = Console.ReadLine();
  24.  
  25.             if (input == "stop")
  26.                 break;
  27.  
  28.             //commands
  29.             if (input == "left up" && row > 0 && col > 1)
  30.             {
  31.                 row = row - 1;
  32.                 col = col - 2;
  33.  
  34.                 matrix[row, col] = ExchangeValue(matrix, row, col);
  35.             }
  36.             else if (input == "left down" && row < 7 && col > 1)
  37.             {
  38.                
  39.                 row = row + 1;
  40.                 col = col - 2;
  41.  
  42.                 matrix[row, col] = ExchangeValue(matrix, row, col);
  43.             }
  44.             else if (input == "right up" && row > 0 && col < 6)
  45.             {
  46.                 row = row - 1;
  47.                 col = col + 2;
  48.  
  49.                 matrix[row, col] = ExchangeValue(matrix, row, col);
  50.             }
  51.             else if (input == "right down" && row < 7 && col < 6)
  52.             {
  53.                 row = row + 1;
  54.                 col = col + 2;
  55.  
  56.                 matrix[row, col] = ExchangeValue(matrix, row, col);
  57.             }
  58.             else if (input == "up left" && row > 1 && col > 0)
  59.             {
  60.                 row = row - 2;
  61.                 col = col - 1;
  62.  
  63.                 matrix[row, col] = ExchangeValue(matrix, row, col);
  64.             }
  65.             else if (input == "up right" && row > 1 && col > 0)
  66.             {
  67.                 row = row - 2;
  68.                 col = col + 1;
  69.  
  70.                 matrix[row, col] = ExchangeValue(matrix, row, col);
  71.             }
  72.             else if (input == "down left" && row < 6 && col > 0)
  73.             {
  74.                 row = row + 2;
  75.                 col = col - 1;
  76.  
  77.                 matrix[row, col] = ExchangeValue(matrix, row, col);
  78.             }
  79.             else if (input == "down right" && row < 6 && col < 7)
  80.             {
  81.                 row = row + 2;
  82.                 col = col + 1;
  83.  
  84.                 matrix[row, col] = ExchangeValue(matrix, row, col);
  85.             }
  86.             else
  87.             {
  88.                 continue;
  89.             }
  90.            // InitialiseMatrix(matrix);
  91.         }
  92.  
  93.         //InitialiseMatrix(matrix);
  94.  
  95.         for (int i = 0; i < 8; i++)
  96.         {
  97.             Result(matrix, i);
  98.         }
  99.  
  100.         int emptyCounter = 0;
  101.  
  102.         for (int rows = 0; rows < 8; rows++)
  103.         {
  104.             for (int cols = 0; cols < 8; cols++)
  105.             {
  106.                 emptyCounter += matrix[rows, cols];
  107.             }
  108.  
  109.         }
  110.  
  111.         if (emptyCounter == 0)
  112.         {
  113.             Console.WriteLine("[Board is empty]");
  114.         }
  115.     }
  116.  
  117.     public static void InitialiseMatrix(int[,] matrix)
  118.     {
  119.         for (int row = 0; row < 8; row++)
  120.         {
  121.             for (int col = 0; col < 8; col++)
  122.             {
  123.                 Console.Write(matrix[row, col] + " ");
  124.             }
  125.             Console.WriteLine();
  126.         }
  127.     }
  128.  
  129.     public static void Result(int[,] matrix,int row)
  130.     {
  131.         int result = 0;
  132.         int counter = 0;
  133.  
  134.         for (int i = 7; i > -1; i--)
  135.         {
  136.             result = ExchangeBits(result, counter, matrix[row, i]);
  137.             counter++;
  138.         }
  139.  
  140.         //InitialiseMatrix(matrix);
  141.         if (result != 0)
  142.         {
  143.             Console.WriteLine(result);
  144.         }
  145.     }
  146.  
  147.     public static int ExchangeValue(int[,] matrix, int row, int col)
  148.     {
  149.         if (matrix[row,col] == 1)
  150.         {
  151.             return 0;
  152.         }
  153.         else if (matrix[row,col] == 0)
  154.         {
  155.             return 1;
  156.         }
  157.         else return 100;
  158.     }
  159.  
  160.  
  161. public static int GetBits(int number, int position)
  162.     {
  163.         int newPosition = number >> position;
  164.         int bit = newPosition & 1;
  165.  
  166.        // Console.WriteLine(bit);
  167.  
  168.         return bit;
  169.     }
  170.  
  171.     public static int ExchangeBits(int number, int position, int value)
  172.     {
  173.         int result = 0;
  174.  
  175.         if (value == 0)
  176.         {
  177.             int mask = ~(1 << position);
  178.             result = number & mask;
  179.  
  180.         }
  181.         else
  182.         {
  183.             int mask = 1 << position;
  184.             result = number | mask;
  185.         }
  186.  
  187.         return result;
  188.     }
  189.  
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement