Advertisement
Razhagal

Bittris

Mar 11th, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | None | 0 0
  1. using System;
  2.  
  3. class Bittris
  4. {
  5.     static void Main()
  6.     {
  7.         int rounds = int.Parse(Console.ReadLine());
  8.            
  9.         int[] playField = new int[4];
  10.         int score = 0;
  11.         int points = 0;
  12.         int line = 0;
  13.         int mask = 255;
  14.         bool noCollision = true;
  15.  
  16.         for (int turn = 0; turn < rounds; turn++)
  17.         {
  18.             string input = Console.ReadLine();
  19.             int turnRemainder = turn % 4;
  20.  
  21.             if (turnRemainder == 0) //Time for a new number?
  22.             {
  23.                 noCollision = true;
  24.                 int number = int.Parse(input);
  25.                 line = number;
  26.  
  27.                 while (number != 0)
  28.                 {
  29.                     if ((number & 1) == 1)
  30.                     {
  31.                         points++;
  32.                     }
  33.                     number >>= 1;
  34.                 }
  35.             }
  36.             else if (noCollision) //Can we move?
  37.             {
  38.                 if (input == "L" && (line & 128) != 128) //Left
  39.                 {
  40.                     line <<= 1;
  41.                 }
  42.                 else if (input == "R" && (line & 1) != 1) //Right
  43.                 {
  44.                     line >>= 1;
  45.                 }
  46.  
  47.                 if ((playField[turnRemainder] & line) == 0) //Is the next line clear?
  48.                 {
  49.                     if (turnRemainder == 3)
  50.                     {
  51.                         if ((playField[turnRemainder] + line) == mask)
  52.                         {
  53.                             score += (points * 10);
  54.  
  55.                             for (int i = turnRemainder; i > 0; i--)
  56.                             {
  57.                                 playField[i] = playField[i - 1];
  58.                             }
  59.  
  60.                             playField[0] = 0;
  61.                         }
  62.                         else
  63.                         {
  64.                             score += points;
  65.                             playField[turnRemainder] |= line;
  66.                         }
  67.  
  68.                         points = 0;
  69.                     }
  70.                 }
  71.                 else //Next line is already occupied
  72.                 {
  73.                     if ((playField[turnRemainder - 1] + line) == mask)
  74.                     {
  75.                         for (int i = turnRemainder - 1; i > 0; i--)
  76.                         {
  77.                             playField[i] = playField[i - 1];
  78.                         }
  79.  
  80.                         score += (points * 10);
  81.                         playField[0] = 0;
  82.                     }
  83.                     else
  84.                     {
  85.                         playField[turnRemainder - 1] |= line;
  86.                         score += points;
  87.                     }
  88.  
  89.                     points = 0;
  90.                     noCollision = false;
  91.                 }
  92.             }
  93.         }
  94.  
  95.         Console.WriteLine(score);
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement