Ejemplo n.º 1
0
        private void TakeTurn(object sender, EventArgs e) //when you click on opponents battle board
        {
            if (txtbMoves.Text == "InitializeEnemyShips")
            {
                txtbMoves.Clear();
            }

            Button num          = (Button)sender;
            String EnemyshipLoc = num.Name.Substring(3);                 //remove btn from button name

            EnemyshipLoc = EnemyshipLoc.Remove(EnemyshipLoc.Length - 1); // remove the E at the end of button name

            byte[] byteTime = Encoding.ASCII.GetBytes(EnemyshipLoc);
            ns.Write(byteTime, 0, byteTime.Length);


            SoundEffects('f');

            txtbMoves.Clear();
            gbxEnemyBoard.Enabled = false;
            lblInstructions.Text  = "[->] : " + opponentName + "'s turn";
            lblInstructions.Refresh();

            //check enemy ships for damage
            if (s2E.IsHit(EnemyshipLoc) == true)
            {
                num.Enabled   = true;
                num.BackColor = Color.DarkRed;
                num.Text      = "X";
                num.Enabled   = false;

                if (s2E.IsSunk == true)// was it sunk?
                {
                    txtbBattleLog.AppendText("[log] : You Fired at " + EnemyshipLoc + " and Sunk a Destroyer!" + Environment.NewLine);
                    SoundEffects('s');
                    pboxEnemy2.Image = Properties.Resources.destroyerX;
                    pboxEnemy2.Refresh();
                }
                else// just a hit
                {
                    txtbBattleLog.AppendText("[log] : You Fired at " + EnemyshipLoc + " and Hit!" + Environment.NewLine);
                    SoundEffects('h');
                }
            }
            else if (s3E.IsHit(EnemyshipLoc) == true)
            {
                num.Enabled   = true;
                num.BackColor = Color.DarkRed;
                num.Text      = "X";
                num.Enabled   = false;

                if (s3E.IsSunk == true)// was it sunk?
                {
                    txtbBattleLog.AppendText("[log] : You Fired at " + EnemyshipLoc + " and Sunk a Cruiser!" + Environment.NewLine);
                    SoundEffects('s');
                    SoundEffects('s');
                    pboxEnemy3.Image = Properties.Resources.cruiserX;
                    pboxEnemy3.Refresh();
                }
                else// just a hit
                {
                    txtbBattleLog.AppendText("[log] : You Fired at " + EnemyshipLoc + " and Hit!" + Environment.NewLine);
                    SoundEffects('h');
                }
            }
            else if (s4E.IsHit(EnemyshipLoc) == true)
            {
                num.Enabled   = true;
                num.BackColor = Color.DarkRed;
                num.Text      = "X";
                num.Enabled   = false;

                if (s4E.IsSunk == true)// was it sunk?
                {
                    txtbBattleLog.AppendText("[log] : You Fired at " + EnemyshipLoc + " and Sunk a BattleShip!" + Environment.NewLine);
                    SoundEffects('s');
                    pboxEnemy4.Image = Properties.Resources.battleshipX1;
                    pboxEnemy4.Refresh();
                }
                else// just a hit
                {
                    txtbBattleLog.AppendText("[log] : You Fired at " + EnemyshipLoc + " and Hit!" + Environment.NewLine);
                    SoundEffects('h');
                }
            }
            else if (s5E.IsHit(EnemyshipLoc) == true)
            {
                num.Enabled   = true;
                num.BackColor = Color.DarkRed;
                num.Text      = "X";
                num.Enabled   = false;

                if (s5E.IsSunk == true)// was it sunk?
                {
                    txtbBattleLog.AppendText("[log] : You Fired at " + EnemyshipLoc + " and Sunk a Carrier!" + Environment.NewLine);
                    SoundEffects('s');
                    pboxEnemy5.Image = Properties.Resources.carrierX;
                    pboxEnemy5.Refresh();
                }
                else// just a hit
                {
                    txtbBattleLog.AppendText("[log] : You Fired at " + EnemyshipLoc + " and Hit!" + Environment.NewLine);
                    SoundEffects('h');
                }
            }
            else // miss
            {
                txtbBattleLog.AppendText("[log] : You Fired at " + EnemyshipLoc + " and Missed!" + Environment.NewLine);
                num.Enabled   = true;
                num.BackColor = Color.LightBlue;
                num.Text      = "O";
                num.Enabled   = false;
                SoundEffects('m');
            }

            //check for win condition
            if (s2E.IsSunk == true && s3E.IsSunk == true && s4E.IsSunk == true && s5E.IsSunk == true)
            {
                txtbBattleLog.AppendText("[log] : You Defeated your opponent! Victory is yours!" + Environment.NewLine);
                SoundEffects('s');
                Thread.Sleep(2500);
                SoundEffects('v');
                MessageBox.Show("Victory is Yours!");
                yourScore++;
                lblPlayer1Score.Text = Convert.ToString(yourScore);
                lblInstructions.Text = "[->] : Place your Carrier on your battle board (5 spots, vertical or horizontal)";
                byteTime             = Encoding.ASCII.GetBytes("reset");
                ns.Write(byteTime, 0, byteTime.Length);
                ResetGame();
            }
        }
Ejemplo n.º 2
0
        private void SetText(string text)
        {
            if (this.txtbBattleLog.InvokeRequired)
            {
                SetTextCallBack del = new SetTextCallBack(SetText);
                this.Invoke(del, new object[] { text });
            }
            else
            {
                if (text == "exitCode")
                {
                    MessageBox.Show("Your Opponent has left the game! closing session . . . ");
                    t1.IsBackground = true;
                    Application.Exit();
                }
                else if (text == "reset")
                {
                    txtbMoves.Text = "InitializeEnemyShips";
                }
                else if (txtbMoves.Text == "")
                {
                    this.txtbMoves.Text   = text;
                    gbxEnemyBoard.Enabled = true;
                    lblInstructions.Text  = "[->] : your turn";
                    //check your ships for damage
                    string num = text.Replace(System.Environment.NewLine, string.Empty); // remove \r\n from reply

                    if (s2.IsHit(num) == true)
                    {
                        gbxYourBoard.Controls["btn" + num].Enabled   = true;
                        gbxYourBoard.Controls["btn" + num].BackColor = Color.DarkRed;
                        gbxYourBoard.Controls["btn" + num].Text      = "X";
                        gbxYourBoard.Controls["btn" + num].Enabled   = false;

                        if (s2.IsSunk == true)// was it sunk?
                        {
                            txtbBattleLog.AppendText("[log] : Your Opponent Fired at " + num + " and Sunk a Destroyer!" + Environment.NewLine);
                            SoundEffects('s');
                            pboxMy2.Image = Properties.Resources.destroyerX;
                            pboxMy2.Refresh();
                        }
                        else// just a hit
                        {
                            txtbBattleLog.AppendText("[log] : Your Opponent Fired at " + num + " and Hit!" + Environment.NewLine);
                            SoundEffects('s');
                            SoundEffects('h');
                        }
                    }
                    else if (s3.IsHit(num) == true)
                    {
                        gbxYourBoard.Controls["btn" + num].Enabled   = true;
                        gbxYourBoard.Controls["btn" + num].BackColor = Color.DarkRed;
                        gbxYourBoard.Controls["btn" + num].Text      = "X";
                        gbxYourBoard.Controls["btn" + num].Enabled   = false;

                        if (s3.IsSunk == true)// was it sunk?
                        {
                            txtbBattleLog.AppendText("[log] : Your Opponent Fired at " + num + " and Sunk a Cruiser!" + Environment.NewLine);
                            SoundEffects('s');
                            pboxMy3.Image = Properties.Resources.cruiserX;
                            pboxMy3.Refresh();
                        }
                        else// just a hit
                        {
                            txtbBattleLog.AppendText("[log] : Your Opponent Fired at " + num + " and Hit!" + Environment.NewLine);
                            SoundEffects('h');
                        }
                    }
                    else if (s4.IsHit(num) == true)
                    {
                        gbxYourBoard.Controls["btn" + num].Enabled   = true;
                        gbxYourBoard.Controls["btn" + num].BackColor = Color.DarkRed;
                        gbxYourBoard.Controls["btn" + num].Text      = "X";
                        gbxYourBoard.Controls["btn" + num].Enabled   = false;

                        if (s4.IsSunk == true)// was it sunk?
                        {
                            txtbBattleLog.AppendText("[log] : Your Opponent Fired at " + num + " and Sunk a Battleship!" + Environment.NewLine);
                            SoundEffects('s');
                            pboxMy4.Image = Properties.Resources.battleshipX1;
                            pboxMy4.Refresh();
                        }
                        else// just a hit
                        {
                            txtbBattleLog.AppendText("[log] : Your Opponent Fired at " + num + " and Hit!" + Environment.NewLine);
                            SoundEffects('h');
                        }
                    }
                    else if (s5.IsHit(num) == true)
                    {
                        gbxYourBoard.Controls["btn" + num].Enabled   = true;
                        gbxYourBoard.Controls["btn" + num].BackColor = Color.DarkRed;
                        gbxYourBoard.Controls["btn" + num].Text      = "X";
                        gbxYourBoard.Controls["btn" + num].Enabled   = false;

                        if (s5.IsSunk == true)// was it sunk?
                        {
                            txtbBattleLog.AppendText("[log] : Your Opponent Fired at " + num + " and Sunk a Carrier!" + Environment.NewLine);
                            SoundEffects('s');
                            pboxMy5.Image = Properties.Resources.carrierX;
                            pboxMy5.Refresh();
                        }
                        else// just a hit
                        {
                            txtbBattleLog.AppendText("[log] : Your Opponent Fired at " + num + " and Hit!" + Environment.NewLine);
                            SoundEffects('h');
                        }
                    }
                    else // miss
                    {
                        txtbBattleLog.AppendText("[log] : Your Opponent Fired at " + num + " and Missed!" + Environment.NewLine);
                        gbxYourBoard.Controls["btn" + num].Enabled   = true;
                        gbxYourBoard.Controls["btn" + num].BackColor = Color.LightBlue;
                        gbxYourBoard.Controls["btn" + num].Text      = "O";
                        gbxYourBoard.Controls["btn" + num].Enabled   = false;
                        SoundEffects('m');
                    }

                    //check for defeat condition
                    if (s2.IsSunk == true && s3.IsSunk == true && s4.IsSunk == true && s5.IsSunk == true)
                    {
                        txtbBattleLog.AppendText("[log] : Your Opponent Fired at " + num + " and Defeated you!" + Environment.NewLine);
                        SoundEffects('s');
                        Thread.Sleep(2500);
                        SoundEffects('d');
                        MessageBox.Show("You have been defeated!");
                        opponentScore++;
                        lblPlayer2Score.Text = Convert.ToString(opponentScore);
                        lblInstructions.Text = "[->] : Place your Carrier on your battle board (5 spots, vertical or horizontal)";
                        ResetGame();
                    }
                }
                else if (txtbMoves.Text == "InitializeEnemyShips")
                {
                    //import all enemy ship placements
                    EnemyShips = text.Split(',');
                    // create enemy ship objects
                    string[] temp = new string[2];
                    temp[0] = EnemyShips[0];
                    temp[1] = EnemyShips[1];
                    s2E     = new ship(2, temp);

                    temp    = new string[3];
                    temp[0] = EnemyShips[2];
                    temp[1] = EnemyShips[3];
                    temp[2] = EnemyShips[4];
                    s3E     = new ship(3, temp);

                    temp    = new string[4];
                    temp[0] = EnemyShips[5];
                    temp[1] = EnemyShips[6];
                    temp[2] = EnemyShips[7];
                    temp[3] = EnemyShips[8];
                    s4E     = new ship(4, temp);

                    temp    = new string[5];
                    temp[0] = EnemyShips[9];
                    temp[1] = EnemyShips[10];
                    temp[2] = EnemyShips[11];
                    temp[3] = EnemyShips[12];
                    temp[4] = EnemyShips[13];
                    s5E     = new ship(5, temp);
                    txtbMoves.Clear();
                    if (Ishost == true)
                    {
                        btnStartGame.Visible = true;
                    }
                }

                else
                {   //get opponents name during game connection handshake
                    string[] temp = text.Split();
                    lblOpponentName.Text = temp[0].Remove(temp[0].Length - 1, 1);
                    opponentName         = temp[0].Remove(temp[0].Length - 1, 1);
                    txtbBattleLog.Text  += text;
                    txtbMoves.Text       = "InitializeEnemyShips";
                }
            }
        }