// try to make the move private void Chessboard_OnMoveMade(int CurrSquare, int TargetSquare) { Console.WriteLine("A move was made from " + CurrSquare.ToString() + " to " + TargetSquare.ToString()); bool bMoveAllowed = this.ChessRules.IsMoveAllowed(CurrSquare, TargetSquare); Console.WriteLine("The rules says... " + ((bMoveAllowed == true) ? "allowed :-)" : "not allowed :-(")); // send through network if (bMoveAllowed) { // if network game, ask other side to concur with this move if (m_bNetworkGame) { NetworkPackage nwpack = new NetworkPackage(); nwpack.m_Command = NetworkCommand.MAKE_MOVE_REQUEST; nwpack.m_FromSquare = (byte)CurrSquare; nwpack.m_ToSquare = (byte)TargetSquare; nwpack.m_ConnectionID = GameData.g_ConnectionID; m_Client.Send(nwpack); } // otherwise just do the move else { this.ChessRules.DoMove(CurrSquare, TargetSquare); // notify user string from = Etc.SquareToString(CurrSquare); string to = Etc.SquareToString(TargetSquare); notifyIcon1.ShowBalloonTip(1, "OfficeChess", "A move was made from " + from + " to " + to, ToolTipIcon.Info); } engInt.CalculateBestMove(); } }
private void ProcessNetworkData(NetworkPackage nwPackage) { // process network data switch (nwPackage.m_Command) { case NetworkCommand.CONNECT_ACCEPT: { // if these aren't the same there is a problem if (GameData.g_ConnectionID != nwPackage.m_ConnectionID) { Exception e = new Exception("Connection ID's do not match up while connecting... abort."); NetworkError(e); GameData.g_ConnectionID = 0; } Console.WriteLine("Connection synchronized..."); m_bNetworkGame = true; break; } case NetworkCommand.NEW_GAME_REQUEST: { // reset board this.ChessRules.NewGame(); // let other side know we agree nwPackage.m_Command = NetworkCommand.NEW_GAME_ACCEPT; m_Client.Send(nwPackage); break; } case NetworkCommand.NEW_GAME_ACCEPT: { // other side accepted, reset board this.ChessRules.NewGame(); break; } case NetworkCommand.MAKE_MOVE_REQUEST: { bool bMoveAllowed = this.ChessRules.IsMoveAllowed(nwPackage.m_FromSquare, nwPackage.m_ToSquare); if (bMoveAllowed) { // move is allowed, let other side know nwPackage.m_Command = NetworkCommand.MAKE_MOVE_ACCEPT; m_Client.Send(nwPackage); // make the actual move this.ChessRules.DoMove(nwPackage.m_FromSquare, nwPackage.m_ToSquare); // update the board this.Chessboard.Invalidate(); // notify user string from = Etc.SquareToString(nwPackage.m_FromSquare); string to = Etc.SquareToString(nwPackage.m_ToSquare); notifyIcon1.ShowBalloonTip(1, "OfficeChess", "A move was made from " + from + " to " + to, ToolTipIcon.Info); } else { // let other side know we do not agree with this move nwPackage.m_Command = NetworkCommand.MAKE_MOVE_DENY; m_Client.Send(nwPackage); } break; } case NetworkCommand.MAKE_MOVE_ACCEPT: { // move was accepted by the other side, make move this.ChessRules.DoMove(nwPackage.m_FromSquare, nwPackage.m_ToSquare); // update the board this.Chessboard.Invalidate(); break; } case NetworkCommand.MAKE_MOVE_DENY: { Exception e = new Exception("Other side reports move is invalid, boards could be out of sync."); NetworkError(e); break; } default: { Exception e = new Exception("Handling unspecified case!"); NetworkError(e); break; } } }