archangelmihail

Bittris

Nov 27th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.05 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class Bittris
  5. {
  6.     static void Main()
  7.     {
  8.         const int Rows = 4;
  9.         const int Cols = 8;
  10.         int numberN = int.Parse(Console.ReadLine());
  11.         // dont need first row, if the game is running
  12.         uint row1 = 0;
  13.         uint row2 = 0;
  14.         uint row3 = 0;
  15.         int score = 0;
  16.         int current = 0;
  17.         while (true)
  18.         {
  19.             current++;
  20.             if (current > numberN)
  21.             {
  22.                 Console.WriteLine(score);
  23.                 break;
  24.             }
  25.             uint number;
  26.             if (!uint.TryParse(Console.ReadLine(), out number))
  27.             {
  28.                 Console.WriteLine(score);
  29.                 return;
  30.             }
  31.             string controls = Console.ReadLine() + Console.ReadLine() + Console.ReadLine();
  32.             int scoreweight = 0;
  33.             for (int i = 0; i < 32; i++)
  34.             {
  35.                 if ( (number & (1<<i)) != 0 )
  36.                 {
  37.                     scoreweight += 1;
  38.                 }
  39.             }
  40.             const uint Bit = (uint)1;
  41.             const uint MaskTopBit = Bit << (Cols - 1);
  42.             const uint MaskLowBit = Bit;
  43.             const uint Mask8Bits = (Bit << Cols) - 1;
  44.  
  45.             uint shape = number & Mask8Bits;
  46.             // move down
  47.             for (int currentRow = 0; currentRow < Rows; currentRow++)
  48.             {
  49.                 if (currentRow < 3)
  50.                 {
  51.                     char ctrl = controls[currentRow];
  52.                     if (ctrl == 'L')
  53.                     {
  54.                         if ((shape & MaskTopBit) == 0 )
  55.                         {
  56.                              shape <<= 1;
  57.                         }
  58.                        
  59.                     }
  60.                     else if (ctrl == 'R')
  61.                     {
  62.                         if ((shape & MaskLowBit) == 0)
  63.                         {
  64.                             shape >>= 1;
  65.                         }
  66.                     }
  67.                 }
  68.  
  69.                 int nextRow = currentRow + 1;
  70.                 uint nextRowBits = 0;
  71.                 switch (nextRow)
  72.                 {
  73.                     case 1: nextRowBits = row1;
  74.                         break;
  75.                     case 2: nextRowBits = row2;
  76.                         break;
  77.                     case 3: nextRowBits = row3;
  78.                         break;
  79.                 }
  80.                 if (nextRow == Rows || (nextRowBits & shape) !=0)
  81.                 {
  82.                     switch (currentRow)
  83.                     {
  84.                         case 1: row1 |= shape; break;
  85.                         case 2: row2 |= shape; break;
  86.                         case 3: row3 |= shape; break;
  87.                     }
  88.                     bool isRowFull = false;
  89.                     switch (currentRow)
  90.                     {
  91.                         case 0: isRowFull = (shape == Mask8Bits); break;
  92.                         case 1: isRowFull = (row1 == Mask8Bits); break;
  93.                         case 2: isRowFull = (row2 == Mask8Bits); break;
  94.                         case 3: isRowFull = (row3 == Mask8Bits); break;
  95.                     }
  96.                     if (isRowFull)
  97.                     {
  98.                         score += scoreweight * 10;
  99.                         for (int i = currentRow; i >= 1; i--)
  100.                         {
  101.                             switch (i)
  102.                             {
  103.                                 case 1: row1 = 0; break;
  104.                                 case 2: row2 = row1; break;
  105.                                 case 3: row3 = row2; break;
  106.                             }
  107.                         }
  108.                     }
  109.                     else
  110.                     {
  111.                         score += scoreweight;
  112.                     }
  113.                     if (currentRow == 0 && !isRowFull)
  114.                     {
  115.                         Console.WriteLine(score);
  116.                         return;
  117.                     }
  118.                     break;
  119.                 }
  120.             }
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment