/// <summary> /// Moves the agent. /// </summary> private void DoMoveAgent() { // Do events before moving //Refresh(); Application.DoEvents(); if ((gameType == CheckersGameType.SinglePlayer) && (CheckersUI.Game.Turn == 2)) { CheckersUI.MovePiece(agent); } }
/// <summary> /// Called after the game is closed. /// </summary> private bool DoCloseGame(bool forced) { if ((!CheckersUI.IsPlaying) && (CheckersUI.Winner == 0)) { return(true); } if (CheckersUI.IsPlaying) { if (!forced) { // Confirm the quit if (MessageBox.Show(this, "Quit current game?", "Checkers", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) { return(false); } } // Forceful end sound PlaySound(CheckersSounds.ForceEnd); } else { PlaySound(CheckersSounds.EndGame); } picTurn.Visible = false; if (gameType == CheckersGameType.NetGame) { tmrConnection.Stop(); panNet.Visible = false; lnkLocalIP.Text = ""; lnkRemoteIP.Text = ""; if (remotePlayer != null) { try { BinaryWriter bw = new BinaryWriter(new NetworkStream(remotePlayer.Socket, false)); bw.Write((byte)ClientMessage.Closed); remotePlayer.Close(); bw.Close(); } catch (IOException) { } catch (SocketException) { } catch (InvalidOperationException) { } AppendMessage("", "Connection closed"); remotePlayer = null; } } CheckersUI.Stop(); return(true); }
/// <summary> /// Starts a new game. /// </summary> private void DoNewGame() { if ((CheckersUI.IsPlaying) || (CheckersUI.Winner != 0)) { if (!DoCloseGame()) { return; } // Stop current game (with no winner) CheckersUI.Stop(); } // Get new game type NewGameDialog newGame = new NewGameDialog(settings, agentNames); // Set defaults newGame.GameType = gameType; newGame.Player1Name = lblNameP1.Text; newGame.Player2Name = lblNameP2.Text; // Show dialog if (newGame.ShowDialog(this) == DialogResult.Cancel) { return; } // Set new game parameters gameType = newGame.GameType; agent = null; // Set Game Panel properties lblNameP1.Text = newGame.Player1Name; lblNameP2.Text = newGame.Player2Name; picPawnP1.Image = newGame.ImageSet[0]; picPawnP2.Image = newGame.ImageSet[2]; // Set UI properties switch (gameType) { case CheckersGameType.SinglePlayer: CheckersUI.Player1Active = true; CheckersUI.Player2Active = false; agent = agents[newGame.AgentIndex]; break; case CheckersGameType.Multiplayer: CheckersUI.Player1Active = true; CheckersUI.Player2Active = true; break; case CheckersGameType.NetGame: remotePlayer = newGame.RemotePlayer; if (remotePlayer == null) { MessageBox.Show(this, "Remote user disconnected before the game began", "Checkers", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } CheckersUI.Player1Active = true; CheckersUI.Player2Active = (remotePlayer == null); if (!menuViewNetPanel.Checked) { menuViewNetPanel_Click(menuViewNetPanel, EventArgs.Empty); } tmrConnection.Start(); panNet.Visible = true; lnkLocalIP.Text = Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString(); lnkRemoteIP.Text = ((IPEndPoint)remotePlayer.Socket.RemoteEndPoint).Address.ToString(); AppendMessage("", "Connected to player"); break; default: return; } CheckersUI.CustomPawn1 = newGame.ImageSet[0]; CheckersUI.CustomKing1 = newGame.ImageSet[1]; CheckersUI.CustomPawn2 = newGame.ImageSet[2]; CheckersUI.CustomKing2 = newGame.ImageSet[3]; // Create the new game CheckersGame game = new CheckersGame(); game.FirstMove = newGame.FirstMove; // Start a new checkers game CheckersUI.Play(game); }
/// <summary> /// Handles the Click event of the menuViewLastMoved control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void menuViewLastMoved_Click(object sender, System.EventArgs e) { CheckersUI.ShowLastMove(); }
/// <summary> /// Handles the Click event of the menuViewHint control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void menuViewHint_Click(object sender, System.EventArgs e) { CheckersUI.ShowHint(); }
/// <summary> /// Checks for a message from the opponent. /// </summary> private void CheckForClientMessage() { if (inCheckForClientMessage) { return; } inCheckForClientMessage = true; if (remotePlayer == null) { return; } try { NetworkStream ns = new NetworkStream(remotePlayer.Socket, false); BinaryReader br = new BinaryReader(ns); BinaryWriter bw = new BinaryWriter(ns); while (ns.DataAvailable) { switch ((ClientMessage)br.ReadByte()) { case ClientMessage.Closed: throw new IOException(); case ClientMessage.ChatMessage: AppendMessage(lblNameP2.Text, br.ReadString()); if (settings.FlashWindowOnGameEvents) { DoFlashWindow(); } break; case ClientMessage.AbortGame: AppendMessage("", "Game has been aborted by opponent"); CloseNetGame(); break; case ClientMessage.MakeMove: if (CheckersUI.Game.Turn != 2) { AppendMessage("", "Opponent took turn out of place; game aborted"); CloseNetGame(); bw.Write((byte)ClientMessage.AbortGame); break; } // Get move Point location = RotateOpponentPiece(br); CheckersPiece piece = CheckersUI.Game.PieceAt(location); int count = br.ReadInt32(); Point[] path = new Point[count]; for (int i = 0; i < count; i++) { path[i] = RotateOpponentPiece(br); } // Move the piece an break if successful if (piece != null) { if (CheckersUI.MovePiece(piece, path, true, true)) { if (settings.FlashWindowOnTurn) { DoFlashWindow(); } break; } } AppendMessage("", "Opponent made a bad move; game aborted"); CloseNetGame(); bw.Write((byte)ClientMessage.AbortGame); break; } } br.Close(); bw.Close(); } catch (IOException) { AppendMessage("", "Connection closed"); CloseNetGame(); remotePlayer = null; } catch (SocketException ex) { AppendMessage("", "Disconnected from opponent: " + ex.Message); CloseNetGame(); remotePlayer = null; } catch (InvalidOperationException ex) { AppendMessage("", "Disconnected from opponent: " + ex.Message); CloseNetGame(); remotePlayer = null; } inCheckForClientMessage = false; }