Beispiel #1
0
        /// <summary>
        /// Start new game
        /// </summary>
        private void OnNewGame()
        {
            if (gameMode == enGameMode.Socket_Create || gameMode == enGameMode.Socket_Connect)
            {
                byte[] message = new byte[256];
                string incomingMsg;
                frmEntryForm.socket.Send(Encoding.UTF8.GetBytes(" Score?P1"));
                frmEntryForm.socket.Receive(message);
                incomingMsg = Edit_IncomingMessage(message);

                P1.score = Convert.ToInt32(incomingMsg);

                message = new byte[256];
                frmEntryForm.socket.Send(Encoding.UTF8.GetBytes(" Score?P2"));
                frmEntryForm.socket.Receive(message);
                incomingMsg = Edit_IncomingMessage(message);

                P2.score = Convert.ToInt32(incomingMsg);
            } // Socket game mode control

            LabelRegulations();

            Label[] allLabels = { lbl0, lbl1, lbl2, lbl3, lbl4, lbl5, lbl6, lbl7, lbl8 };

            foreach (Label label in allLabels)
            {
                label.Text      = null;
                label.BackColor = Color.LightGreen;
            } // Label reset
            switch (starting)
            {
            case enStarting.None:
            case enStarting.Player2:
                playerTurn = enPlayerTurn.Player1;
                starting   = enStarting.Player1;
                ShowTurn(playerTurn.ToString());
                break;

            case enStarting.Player1:
                playerTurn = enPlayerTurn.Player2;
                starting   = enStarting.Player2;
                ShowTurn(playerTurn.ToString());
                break;

            default:
                break;
            }

            if (gameMode == enGameMode.Timed)
            {
                timer.Start();
                timer.Interval = 500;
                timer_countdown.Start();
                timer_countdown.Interval = 1000; // timer settings
                pnlTime.Show();
            } // Timed gamed control

            winner = enWinner.None;
        }
Beispiel #2
0
        /// <summary>
        /// Computer game mode
        /// </summary>
        private void Computer()
        {
            Label[] allLabels   = { lbl0, lbl1, lbl2, lbl3, lbl4, lbl5, lbl6, lbl7, lbl8 };
            Label[] emptyLabels = new Label[9];
            int     empty       = -1;

            //Clear all game board cells
            foreach (Label label in allLabels)
            {
                if (label.Text == "")
                {
                    emptyLabels[++empty] = label;
                }
            }

            Random rand = new Random();

            if (empty != -1)
            {
                emptyLabels[rand.Next(empty)].Text = P2.choice;
            }

            winner = GetWinner();

            if (winner != enWinner.None)
            {
                playerTurn = enPlayerTurn.None;
                OnNewGame();
                if (starting == enStarting.Player2)
                {
                    Computer();
                }
            }

            playerTurn = enPlayerTurn.Player1;
            ShowTurn(playerTurn.ToString());
        }
Beispiel #3
0
        /// <summary>
        /// Clicking refres button
        /// </summary>
        /// <param name="sender">Clicked button(refresh button)</param>
        /// <param name="e">Event args</param>
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            LabelXO_Placement();

            byte[] message = new byte[256];
            string incomingMsg;

            frmEntryForm.socket.Send(Encoding.UTF8.GetBytes(" Play?"));
            frmEntryForm.socket.Receive(message);
            incomingMsg = Edit_IncomingMessage(message);

            winner = GetWinner();

            if (winner != enWinner.None)
            {
                foreach (var server in Process.GetProcessesByName("GameServer"))
                {
                    server.Kill();
                }

                this.Close();
            }

            else if (playerTurn == enPlayerTurn.Player1)
            {
                if (incomingMsg == "NY") // Y = Yes ; N = No
                {
                    startGame = true;
                    ShowTurn(playerTurn.ToString());
                }

                else if (gameMode == enGameMode.Socket_Create && incomingMsg == "--")
                {
                    startGame = true;
                    ShowTurn(playerTurn.ToString());
                    lblChoiceP1.BackColor = Color.IndianRed;
                    lblChoiceP2.BackColor = Color.Transparent;
                }

                else if (gameMode == enGameMode.Socket_Connect && incomingMsg == "YN")
                {
                    startGame  = true;
                    playerTurn = enPlayerTurn.Player2;
                    ShowTurn(playerTurn.ToString());
                }

                else if (gameMode == enGameMode.Socket_Connect && incomingMsg == "--")
                {
                    MetroMessageBox.Show(this, "Player1 hasn't played yet", "WAITING...", 90);
                }
                else
                {
                    MetroMessageBox.Show(this, "Player2 hasn't played yet", "WAITING...", 90);
                }
            }

            else if (playerTurn == enPlayerTurn.Player2)
            {
                if (incomingMsg == "YN")
                {
                    startGame = true;
                    ShowTurn(playerTurn.ToString());
                }

                else
                {
                    MetroMessageBox.Show(this, "Player1 hasn't played yet", "WAITING...", 90);
                }
            }
            else
            {
            }

            refreshGame = true;
        }
Beispiel #4
0
        /// <summary>
        /// Clicking X-Os
        /// </summary>
        /// <param name="sender">Clicked label</param>
        /// <param name="e">Event args</param>
        private void OnClick(object sender, EventArgs e)
        {
            Label  clickedLabel = (Label)sender;
            string labelName    = clickedLabel.Name;
            string labelText    = clickedLabel.Text;

            if (gameMode == enGameMode.Socket_Connect || gameMode == enGameMode.Socket_Create)
            {
                if (refreshGame && startGame)
                {
                    if (playerTurn == enPlayerTurn.None || labelText != "")
                    {
                        return;
                    }

                    OnClick_Socket(clickedLabel);
                }

                else
                {
                    MetroMessageBox.Show(this, "Please click the \"Refresh\" button", "", 90);
                }

                refreshGame = false;
                startGame   = false;
            } // Socket game mode control

            else
            {
                if (playerTurn == enPlayerTurn.None || labelText != "")
                {
                    return;
                }

                else if (playerTurn == enPlayerTurn.Player1)
                {
                    clickedLabel.Text = P1.choice;
                    ShowTurn(playerTurn.ToString());
                }

                else
                {
                    clickedLabel.Text = P2.choice;
                    ShowTurn(playerTurn.ToString());
                }

                winner = GetWinner();

                labelText = clickedLabel.Text;
                switch (gameMode)
                {
                case enGameMode.Computer:

                    if (winner == enWinner.None)
                    {
                        Computer();
                    }

                    else if (winner != enWinner.None)
                    {
                        playerTurn = enPlayerTurn.None;
                        OnNewGame();
                        if (starting == enStarting.Player2)
                        {
                            Computer();
                        }
                    }

                    break;

                case enGameMode.Friend:

                    if (winner == enWinner.None)
                    {
                        //Change turns
                        playerTurn = (enPlayerTurn.Player1 == playerTurn) ? enPlayerTurn.Player2 : enPlayerTurn.Player1;
                        ShowTurn(playerTurn.ToString());
                    }
                    else
                    {
                        playerTurn = enPlayerTurn.None;
                        OnNewGame();
                    }

                    break;

                case enGameMode.Timed:
                    time           = -1;
                    time_countdown = 6;

                    if (winner == enWinner.None)
                    {
                        //Change turns
                        playerTurn = (enPlayerTurn.Player1 == playerTurn) ? enPlayerTurn.Player2 : enPlayerTurn.Player1;
                        ShowTurn(playerTurn.ToString());
                    }
                    else
                    {
                        playerTurn = enPlayerTurn.None;
                        OnNewGame();
                    }

                    break;


                default:
                    break;
                } // controls
            }     // Other game modes control
        }