/* * Updates the GUI game board too reflect * what is stored in ChessModel */ private void UpdateBoard() { List <ChessPiece> board = game.GetBoard(); for (int i = 0; i < 64; i++) { ChessPiece piece = board.ElementAt(i); if (piece.GetColor() == "Black") { if (piece.GetName() == "Bishop") { buttons[i].Image = global::OnlineChess.Properties.Resources.bb; } else if (piece.GetName() == "King") { buttons[i].Image = global::OnlineChess.Properties.Resources.bk; } else if (piece.GetName() == "Knight") { buttons[i].Image = global::OnlineChess.Properties.Resources.bn; } else if (piece.GetName() == "Pawn") { buttons[i].Image = global::OnlineChess.Properties.Resources.bp; } else if (piece.GetName() == "Queen") { buttons[i].Image = global::OnlineChess.Properties.Resources.bq; } else if (piece.GetName() == "Rook") { buttons[i].Image = global::OnlineChess.Properties.Resources.br; } } else if (piece.GetColor() == "White") { if (piece.GetName() == "Bishop") { buttons[i].Image = global::OnlineChess.Properties.Resources.wb; } else if (piece.GetName() == "King") { buttons[i].Image = global::OnlineChess.Properties.Resources.wk; } else if (piece.GetName() == "Knight") { buttons[i].Image = global::OnlineChess.Properties.Resources.wn; } else if (piece.GetName() == "Pawn") { buttons[i].Image = global::OnlineChess.Properties.Resources.wp; } else if (piece.GetName() == "Queen") { buttons[i].Image = global::OnlineChess.Properties.Resources.wq; } else if (piece.GetName() == "Rook") { buttons[i].Image = global::OnlineChess.Properties.Resources.wr; } } else { buttons[i].Image = null; } } }
/* * Turns the game board into a string of symbols */ public String BoardToString() { String boardString = ""; for (int i = 0; i < this.board.Count; i++) { ChessPiece piece = this.board.ElementAt(i); if (piece.GetName() == "Bishop") { if (piece.GetColor() == "Black") { boardString += "B"; } else { boardString += "b"; } } else if (piece.GetName() == "King") { if (piece.GetColor() == "Black") { boardString += "K"; } else { boardString += "k"; } } else if (piece.GetName() == "Knight") { if (piece.GetColor() == "Black") { boardString += "N"; } else { boardString += "n"; } } else if (piece.GetName() == "Pawn") { if (piece.GetColor() == "Black") { boardString += "P"; } else { boardString += "p"; } } else if (piece.GetName() == "Queen") { if (piece.GetColor() == "Black") { boardString += "Q"; } else { boardString += "q"; } } else if (piece.GetName() == "Rook") { if (piece.GetColor() == "Black") { boardString += "R"; } else { boardString += "r"; } } else { boardString += "-"; } } return(boardString); }
/* * 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(); } }