Example #1
0
 private string _UpdateBuffer()
 {
     char[] buffer = new char[71];
     buffer[0] = 'U';
     buffer[1] = 'P';
     buffer[2] = 'D';
     buffer[3] = 'T';
     PieceState[,] pieceStates = new PieceState[8, 8];
     _Reversi.CopyState(pieceStates);
     for (var i = 0; i < 8; i++)
     {
         for (var j = 0; j < 8; j++)
         {
             if (pieceStates[i, j] == PieceState.PieceAvailable)
             {
                 if (_Reversi.GetCurrentPlayer() == _Player)
                 {
                     buffer[4 + i * 8 + j] = (char)((int)pieceStates[i, j]);
                 }
                 else
                 {
                     buffer[4 + i * 8 + j] = (char)0;
                 }
             }
             else
             {
                 buffer[4 + i * 8 + j] = (char)((int)pieceStates[i, j]);
             }
         }
     }
     if (_Reversi.GetCurrentPlayer() == _Player)
     {
         if (_Reversi.GetState() == ReversiState.NoAvailableSpaces)
         {
             buffer[4 + 64] = (char)10;
         }
         else if (((byte)_Reversi.GetState() & (byte)ReversiState.InProgress) != 0)
         {
             buffer[4 + 64] = (char)_Reversi.GetCurrentPlayer();
         }
         else
         {
             buffer[4 + 64] = (char)((int)_Reversi.GetState() + 1);
         }
     }
     else
     {
         if (((byte)_Reversi.GetState() & (byte)ReversiState.InProgress) != 0)
         {
             buffer[4 + 64] = (char)_Reversi.GetCurrentPlayer();
         }
         else
         {
             buffer[4 + 64] = (char)((int)_Reversi.GetState() + 1);
         }
     }
     buffer[4 + 65] = (char)_Reversi.GetDarkCount();
     buffer[4 + 66] = (char)_Reversi.GetLightCount();
     return(new string(buffer));
 }
Example #2
0
        static void Main(string[] args)
        {
            char i, j;

            Console.WriteLine();

            Reversi reversi = new Reversi();

            while (((int)reversi.GetState() & 1) != 0)
            {
                Console.Write(reversi);
                while (Char.IsWhiteSpace(i = (char)Console.Read()))
                {
                    ;
                }
                while (Char.IsWhiteSpace(j = (char)Console.Read()))
                {
                    ;
                }
                Console.Clear();
                if (Char.IsLower(i))
                {
                    i -= ' ';
                }
                if (Char.IsLower(j))
                {
                    j -= ' ';
                }
                if (i == 'Z' && j == 'Z')
                {
                    if (!reversi.Undo())
                    {
                        Console.WriteLine("Cannot undo.");
                    }
                    else
                    {
                        Console.WriteLine("Undid.");
                    }
                }
                else if (i == 'Y' && j == 'Y')
                {
                    if (!reversi.Redo())
                    {
                        Console.WriteLine("Cannot redo.");
                    }
                    else
                    {
                        Console.WriteLine("Redid.");
                    }
                }
                else if (i == 'S' && j == 'S')
                {
                    if (!reversi.SkipTurn())
                    {
                        Console.WriteLine("Cannot skip the turn.");
                    }
                    else
                    {
                        Console.WriteLine("Skipped.");
                    }
                }
                else if (!Char.IsDigit(i) || !Char.IsLetter(j))
                {
                    Console.WriteLine("Invalid command.");
                }
                else
                {
                    if (!reversi.PlacePiece(i - '1', j - 'A'))
                    {
                        Console.WriteLine("Cannot place.");
                    }
                    else
                    {
                        if (reversi.GetState() == ReversiState.NoAvailableSpaces)
                        {
                            Console.WriteLine("No available spaces.");
                        }
                        else
                        {
                            Console.WriteLine();
                        }
                    }
                }
            }
        }