Advertisement
end1dream

Game of Bits

Nov 4th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.89 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 BitsGame
  8. {
  9.     class BitsGame
  10.     {
  11.         static string Reverse(string binaryNumber)
  12.         {
  13.             char[] charArray = binaryNumber.ToCharArray();
  14.             Array.Reverse(charArray);
  15.             return new string(charArray);
  16.         }
  17.  
  18.         static string ExtractOddBits(uint number)
  19.         {
  20.             if (number == 1)
  21.             {
  22.                 return "1";
  23.             }
  24.             string numberString = Convert.ToString(number, 2);          
  25.             StringBuilder num = new StringBuilder();            
  26.             int bitPosition = 1;
  27.             for (int i = numberString.Length - 1; i >= 0; i--)
  28.             {
  29.                 if (bitPosition % 2 != 0)
  30.                 {
  31.                     num.Append(numberString[i]);
  32.                 }
  33.                 bitPosition++;
  34.             }
  35.  
  36.             string result = Reverse(num.ToString());
  37.             return result;
  38.         }
  39.  
  40.         static string ExtractEvenBits(uint number)
  41.         {            
  42.             if (number == 1)
  43.             {
  44.                 return "1";
  45.             }
  46.             string oddBits = Convert.ToString(number, 2);
  47.             StringBuilder num = new StringBuilder();
  48.             int bitPosition = 1;
  49.             for (int i = oddBits.Length - 1; i >= 0; i--)
  50.             {
  51.                 if (bitPosition % 2 == 0)
  52.                 {
  53.                     num.Append(oddBits[i]);
  54.                 }
  55.                 bitPosition++;
  56.             }
  57.  
  58.             string result = Reverse(num.ToString());
  59.             return result;
  60.         }
  61.  
  62.         static string GameOver(uint number)
  63.         {
  64.             string num = Convert.ToString(number, 2);
  65.             int count = 0;
  66.             foreach (char bit in num)
  67.             {
  68.                 if (bit == '1')
  69.                 {
  70.                     count++;
  71.                 }
  72.             }
  73.             int finalNumber = Convert.ToInt32(num, 2);
  74.  
  75.             string result = finalNumber + " -> " + count;
  76.             return result;
  77.         }
  78.  
  79.         static void Main(string[] args)
  80.         {
  81.             uint number = uint.Parse(Console.ReadLine());
  82.             string command = null;
  83.            
  84.             do
  85.             {                
  86.                 command = Console.ReadLine();
  87.                 switch (command)
  88.                 {
  89.                     case "Odd":
  90.                         number = Convert.ToUInt32(ExtractOddBits(number), 2);
  91.                         break;
  92.                     case "Even":
  93.                         number = Convert.ToUInt32(ExtractEvenBits(number), 2);
  94.                         break;
  95.                 }
  96.  
  97.             } while ((command != "Game Over!"
  98.                 && number != 0));
  99.             Console.WriteLine(GameOver(number));
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement