Esempio n. 1
0
 /*
  * Updates the game based off data sent to the player
  * through the network stream. Updates the game if a move
  * was made or ends the game if the other player disconnected
  * or a cheat was detected.
  */
 public void UpdateGame(String data)
 {
     String[] dataStrings = data.Split(',');
     if (dataStrings[0] == "Moved")
     {
         String     opponentColor;
         int        boardSize = game.GetBoard().Count() - 1;
         ChessPiece p1        = game.GetBoard().ElementAt(boardSize - Convert.ToInt32(dataStrings[1]));
         ChessPiece p2        = game.GetBoard().ElementAt(boardSize - Convert.ToInt32(dataStrings[2]));
         if (game.GetColor() == "Black")
         {
             opponentColor = "White";
         }
         else
         {
             opponentColor = "Black";
         }
         game.MovePieces(p1, p2, opponentColor);
         UpdateBoard();
         InfoLabel.Text = game.GetGameInfo();
         if (game.GetRecentMove() == "Invalid move!")
         {
             InfoLabel.Text = "Opponent cheated. You won!";
             WriteMessage("Cheated");
             game.EndGame();
             stream.Close();
         }
         else
         {
             game.ChangeTurn();
             UpdateBoard();
             if (game.IsGameOver() == true)
             {
                 stream.Close();
             }
         }
     }
     else if (dataStrings[0] == "Disconnected")
     {
         InfoLabel.Text = "Opponent disconnected. You won!";
         game.EndGame();
         stream.Close();
     }
     else if (dataStrings[0] == "Cheated")
     {
         InfoLabel.Text = "You cheated. You lost the game!";
         game.EndGame();
         stream.Close();
     }
 }
Esempio n. 2
0
 /*
  * Updates the game based off data sent to the player
  * through the network stream. Updates the game if a move
  * was made or ends the game if the other player disconnected
  * or a cheat was detected.
  */
 public void UpdateGame(String data)
 {
     String[] dataStrings = data.Split(',');
     if (dataStrings[0] == "Moved")
     {
         String     opponentColor;
         int        boardSize = game.GetBoard().Count() - 1;
         ChessPiece p1        = game.GetBoard().ElementAt(boardSize - Convert.ToInt32(dataStrings[1]));
         ChessPiece p2        = game.GetBoard().ElementAt(boardSize - Convert.ToInt32(dataStrings[2]));
         if (game.GetColor() == "Black")
         {
             opponentColor = "White";
         }
         else
         {
             opponentColor = "Black";
         }
         game.MovePieces(p1, p2, opponentColor);
         UpdateBoard();
         if (game.GetGameInfo().Contains("You can't select an empty space!"))
         {
             listBox1.Items.Add("You can't select an empty space!");
         }
         else if (game.GetGameInfo().Contains("Choose where to move your piece"))
         {
             listBox1.Items.Add("Choose where to move your piece!");
         }
         else if (game.GetGameInfo().Contains("Invalid move!"))
         {
             listBox1.Items.Add("Invalid move!");
         }
         else
         {
             listBox1.Items.Add(opponentColor + " moved " + game.GetGameInfo());
         }
         if (game.GetRecentMove() == "Invalid move!")
         {
             listBox1.Items.Add("Opponent cheated. You won!");
             WriteMessage("!", "Cheated");
             game.EndGame();
             stream.Close();
         }
         else
         {
             game.ChangeTurn();
             UpdateBoard();
             if (game.IsGameOver() == true)
             {
                 stream.Close();
             }
         }
     }
     else if (dataStrings[0] == "Disconnected")
     {
         listBox1.Items.Add("Opponent disconnected. You won!");
         game.EndGame();
         stream.Close();
     }
     else if (dataStrings[0] == "Cheated")
     {
         listBox1.Items.Add("You cheated. You lost the game!");
         game.EndGame();
         stream.Close();
     }
 }