Exemplo n.º 1
0
 /// <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);
 }
Exemplo n.º 2
0
        /// <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);
        }