Advertisement
anilak

ExamC1 24.06.2013 Bittris ver.2

Jun 28th, 2013
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. using System;
  2.  
  3. class Bittris
  4. {
  5.     static uint GetScorePiece(byte finalLine)
  6.     {
  7.         byte points = 0;
  8.         for (int i = 0; i < 32; i++)
  9.         {
  10.             if (((fullPiece >> i) & 1) == 1)            //counts the bits equal to 1 in the input int
  11.             {
  12.                 points++;
  13.             }
  14.         }
  15.         if (field[finalLine] == 255)                //checks if the line is full with bits equal to 1
  16.         {
  17.             points = (byte)(points * 10);
  18.             for (byte i = finalLine; i >= 1; i--)   //moves the lines down when the full line is deleted
  19.             {
  20.                 field[i] = field[i - 1];
  21.             }
  22.             field[0] = 0;
  23.         }
  24.         return points;
  25.     }
  26.  
  27.     static void MovePiece(byte piece)
  28.     {
  29.         byte line = 0;
  30.         bool isFinal = false;
  31.         for (byte i = 0; i < 3; i++)
  32.         {
  33.             char com = char.Parse(Console.ReadLine());      //always reads the commands from the console
  34.             if (field[0] != 0 || line >= 3 || isFinal)      //executes the command only if: the piece
  35.             {                                               //is not on the last line, or in its final position
  36.                 continue;                                   //or the first line is full
  37.             }
  38.             if (com == 'L')
  39.             {
  40.                 if (((piece >> 7) & 1) != 1 && (field[line] & (piece << 1)) == 0)
  41.                 {
  42.                     piece = (byte)(piece << 1);
  43.                 }
  44.             }
  45.             else if (com == 'R')
  46.             {
  47.                 if ((piece & 1) != 1 && (field[line] & (piece << 1)) == 0)
  48.                 {
  49.                     piece = (byte)(piece >> 1);
  50.                 }
  51.             }
  52.             if ((field[line + 1] & piece) == 0)
  53.             {
  54.                 line++;
  55.             }
  56.             else
  57.             {
  58.                 isFinal = true;
  59.             }
  60.         }      
  61.         field[line] = (byte)(field[line] | piece);          //when the piece is in final position it is put in the field
  62.         score += GetScorePiece(line);
  63.         return;
  64.     }
  65.  
  66.     static uint score = 0;
  67.     static byte[] field;
  68.     static int fullPiece;
  69.  
  70.     static void Main()
  71.     {  
  72.         field = new byte[4];
  73.         int n = int.Parse(Console.ReadLine());
  74.         for (int i = 0; i < n / 4; i++)
  75.         {
  76.             fullPiece = int.Parse(Console.ReadLine());
  77.             MovePiece((byte)fullPiece);
  78.         }
  79.         Console.WriteLine(score);
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement