Advertisement
anilak

Bittris55 corrected

Nov 28th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.51 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5.     static int Bitcount(int n)
  6.     {
  7.         int count = 0;
  8.         while (n != 0)
  9.         {
  10.             count++;
  11.             n &= (n - 1);
  12.         }
  13.         return count;
  14.     }
  15.  
  16.     static void Main()
  17.     {
  18.         //get the first input line
  19.         int numberCommands = int.Parse(Console.ReadLine());
  20.         int piecesCount = numberCommands / 4;
  21.  
  22.         //define field variables as bytes so that they hold only bits 0-7
  23.         byte firstRow = 0;
  24.         byte secondRow = 0;
  25.         byte thirdRow = 0;
  26.         byte forthRow = 0;
  27.  
  28.         //define variable to hold the score
  29.         int shapeScore = 0;
  30.         int totalScore = 0;
  31.  
  32.         //define some flags
  33.         bool gameEnd = false;
  34.         bool scored = false;
  35.  
  36.         //get input data for each piece and move it
  37.         for (int i = 0; i < piecesCount; i++)
  38.         {
  39.             int newPiece = int.Parse(Console.ReadLine());
  40.             string dirOne = Console.ReadLine();
  41.             string dirTwo = Console.ReadLine();
  42.             string dirThree = Console.ReadLine();
  43.  
  44.             shapeScore = Bitcount(newPiece); //count 1 bits in integer
  45.             byte pieceShape = (byte)newPiece; //take only bits 0-7
  46.             scored = false;
  47.  
  48.             //place shape on first row
  49.             if ((firstRow & pieceShape) == 0) firstRow = (byte)(firstRow | pieceShape);
  50.             else
  51.             {
  52.                 gameEnd = true;
  53.                 break;
  54.             }
  55.  
  56.             //piece on first row, move to second row
  57.             if (dirOne == "L") //move shape left
  58.             {
  59.                 if ((pieceShape << 1) <= 255)
  60.                 {
  61.                     pieceShape = (byte)(pieceShape << 1);
  62.                     firstRow = (byte)(firstRow ^ (pieceShape / 2)); //divide by 2 to compensate for left shift
  63.                     firstRow = (byte)(firstRow | pieceShape);
  64.                 }
  65.             }
  66.  
  67.             if (dirOne == "R") //move shape right
  68.             {
  69.                 if ((pieceShape & 1) == 0)
  70.                 {
  71.                     pieceShape = (byte)(pieceShape >> 1);
  72.                     firstRow = (byte)(firstRow ^ (pieceShape * 2)); //multiply by 2 to compensate for the right shift
  73.                     firstRow = (byte)(firstRow | pieceShape);
  74.                 }
  75.             }
  76.  
  77.             if ((pieceShape & secondRow) == 0) //move shape down
  78.             {
  79.                 secondRow = (byte)(pieceShape | secondRow); //place shape on second row
  80.                 firstRow = (byte)(firstRow ^ pieceShape); //remove shape from first row
  81.             }
  82.             else if (firstRow == 255)
  83.             {
  84.                 scored = true;
  85.                 totalScore = totalScore + shapeScore * 10;
  86.                 firstRow = 0;
  87.         continue;
  88.             }
  89.             else
  90.             {
  91.                 gameEnd = true;
  92.                 totalScore = totalScore + shapeScore;
  93.                 break;
  94.             }
  95.  
  96.             //check if piece is on second row, move to third
  97.             if (! scored)
  98.             {
  99.                 if (dirTwo == "L")
  100.                 {
  101.                     if ((pieceShape << 1) <= 255)
  102.                     {
  103.                         pieceShape = (byte)(pieceShape << 1);
  104.                         secondRow = (byte)(secondRow ^ (pieceShape / 2)); //divide by 2 to compensate for left shift
  105.                         secondRow = (byte)(secondRow | pieceShape);
  106.                     }
  107.                 }
  108.                 if (dirTwo == "R")
  109.                 {
  110.                     if ((pieceShape & 1) == 0)
  111.                     {
  112.                         pieceShape = (byte)(pieceShape >> 1);
  113.                         secondRow = (byte)(secondRow ^ (pieceShape * 2)); //multiply by 2 to compensate for the right shift
  114.                         secondRow = (byte)(secondRow | pieceShape);
  115.                     }
  116.                 }
  117.  
  118.                 if ((pieceShape & thirdRow) == 0)
  119.                 {
  120.                     thirdRow = (byte)(pieceShape | thirdRow);
  121.                     secondRow = (byte)(secondRow ^ pieceShape);
  122.                 }
  123.                 else if (secondRow == 255)
  124.                 {
  125.                     scored = true;
  126.                     totalScore = totalScore + shapeScore * 10;
  127.                     secondRow = (byte)(secondRow & firstRow);
  128.                     firstRow = 0;
  129.             continue;
  130.                 }
  131.                 else
  132.                 {
  133.                     scored = true;
  134.                     totalScore = totalScore + shapeScore;
  135.                 }
  136.             }
  137.  
  138.             //check if piece is on third row, move to forth
  139.             if (! scored)
  140.             {
  141.                 if (dirThree == "L")
  142.                 {
  143.                     if ((pieceShape << 1) <= 255)
  144.                     {
  145.                         pieceShape = (byte)(pieceShape << 1);
  146.                         thirdRow = (byte)(thirdRow ^ (pieceShape / 2));
  147.                         thirdRow = (byte)(thirdRow | pieceShape);
  148.                     }
  149.                 }
  150.                 if (dirThree == "R")
  151.                 {
  152.                     if ((pieceShape & 1) == 0)
  153.                     {
  154.                         pieceShape = (byte)(pieceShape >> 1);
  155.                         thirdRow = (byte)(thirdRow ^ (pieceShape * 2));
  156.                         thirdRow = (byte)(thirdRow | pieceShape);
  157.                     }
  158.                 }
  159.  
  160.                 if ((pieceShape & forthRow) == 0)
  161.                 {
  162.                     forthRow = (byte)(pieceShape | forthRow);
  163.                     thirdRow = (byte)(thirdRow ^ pieceShape);
  164.                 }
  165.                 else if (thirdRow == 255)
  166.                 {
  167.                     totalScore = totalScore + shapeScore * 10;
  168.                     thirdRow = (byte)(thirdRow & secondRow);
  169.                     secondRow = 0;
  170.                     secondRow = (byte)(secondRow | firstRow);
  171.                     firstRow = 0;
  172.             continue;
  173.                 }
  174.  
  175.                 if (forthRow == 255)
  176.                 {
  177.                     totalScore = totalScore + shapeScore * 10;
  178.                     forthRow = (byte)(forthRow & thirdRow);
  179.                     thirdRow = 0;
  180.                     thirdRow = (byte)(thirdRow | secondRow);
  181.                     secondRow = 0;
  182.                     secondRow = (byte)(secondRow | firstRow);
  183.                     firstRow = 0;
  184.                 }
  185.         else
  186.         {
  187.                     totalScore = totalScore + shapeScore;
  188.         }
  189.             }
  190.  
  191.         }
  192.         Console.WriteLine(totalScore);
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement