Example #1
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";
                }
            }
        }
Example #2
0
        private void ShipSetup(object sender, EventArgs e) //set up all ship locations and variables
        {
            Button num     = (Button)sender;
            string shipLoc = num.Name.Substring(3); //remove btn from button name


            if (carrierSpots == 5)
            {
                if (ValidateInitialShipPlacement(5, shipLoc, "carrier") == true)
                {
                    SoundEffects('p');
                    carrierLoc[4] = shipLoc;
                    num.BackColor = Color.Green;
                    num.Enabled   = false;
                    carrierSpots--;
                }
                else
                {
                    SoundEffects('e');
                    string temp = lblInstructions.Text;
                    lblInstructions.Text = "[->] : Invalid Ship Placement, please try another spot.";
                    lblInstructions.Refresh();
                    Thread.Sleep(500);
                    lblInstructions.Text = temp;
                }
            }
            else if (carrierSpots == 4)
            {
                Array.Sort(carrierLoc); // sort array to find the outer values of the ship, nulls go first
                char test = CheckAdditionalShipSpots(carrierOrientation, carrierLoc[4], carrierLoc[4], shipLoc);
                if (test != '\0')
                {
                    SoundEffects('p');
                    carrierLoc[3] = shipLoc;
                    num.BackColor = Color.Green;
                    num.Enabled   = false;
                    carrierSpots--;
                    carrierOrientation = test; // set the orientation of the ship for future checks
                }
                else
                {
                    SoundEffects('e');
                    string temp = lblInstructions.Text;
                    lblInstructions.Text = "[->] : Invalid Placement, Must be connected horizontally or vertically.";
                    lblInstructions.Refresh();
                    Thread.Sleep(500);
                    lblInstructions.Text = temp;
                }
            }
            else if (carrierSpots == 3)
            {
                Array.Sort(carrierLoc); // sort array to find the outer values of the ship, nulls go first
                char test = CheckAdditionalShipSpots(carrierOrientation, carrierLoc[3], carrierLoc[4], shipLoc);
                if (test != '\0')
                {
                    SoundEffects('p');
                    carrierLoc[2] = shipLoc;
                    num.BackColor = Color.Green;
                    num.Enabled   = false;
                    carrierSpots--;
                }
                else
                {
                    SoundEffects('e');
                    string temp = lblInstructions.Text;
                    lblInstructions.Text = "[->] : Invalid Placement, Must be connected to your carrier ship.";
                    lblInstructions.Refresh();
                    Thread.Sleep(500);
                    lblInstructions.Text = temp;
                }
            }
            else if (carrierSpots == 2)
            {
                Array.Sort(carrierLoc); // sort array to find the outer values of the ship, nulls go first
                char test = CheckAdditionalShipSpots(carrierOrientation, carrierLoc[2], carrierLoc[4], shipLoc);
                if (test != '\0')
                {
                    SoundEffects('p');
                    carrierLoc[1] = shipLoc;
                    num.BackColor = Color.Green;
                    num.Enabled   = false;
                    carrierSpots--;
                }
                else
                {
                    SoundEffects('e');
                    string temp = lblInstructions.Text;
                    lblInstructions.Text = "[->] : Invalid Placement, Must be connected to your carrier ship.";
                    lblInstructions.Refresh();
                    Thread.Sleep(500);
                    lblInstructions.Text = temp;
                }
            }
            else if (carrierSpots == 1)
            {
                Array.Sort(carrierLoc); // sort array to find the outer values of the ship, nulls go first
                char test = CheckAdditionalShipSpots(carrierOrientation, carrierLoc[1], carrierLoc[4], shipLoc);
                if (test != '\0')
                {
                    SoundEffects('p');
                    carrierLoc[0] = shipLoc;
                    num.BackColor = Color.Green;
                    num.Enabled   = false;
                    carrierSpots--;
                    lblInstructions.Text = "[->] : Place your BattleShip on your battle board (4 spots, vertical or horizontal)";
                    s5 = new ship(5, carrierLoc);
                }
                else
                {
                    SoundEffects('e');
                    string temp = lblInstructions.Text;
                    lblInstructions.Text = "[->] : Invalid Placement, Must be connected to your carrier ship.";
                    lblInstructions.Refresh();
                    Thread.Sleep(500);
                    lblInstructions.Text = temp;
                }
            }
            else if (battleshipSpots == 4)
            {
                if (ValidateInitialShipPlacement(4, shipLoc, "battleship") == true)
                {
                    SoundEffects('p');
                    battleshipLoc[0] = shipLoc;
                    num.BackColor    = Color.Yellow;
                    num.Enabled      = false;
                    battleshipSpots--;
                }
                else
                {
                    SoundEffects('e');
                    string temp = lblInstructions.Text;
                    lblInstructions.Text = "[->] : Invalid Ship Placement, please try another spot.";
                    lblInstructions.Refresh();
                    Thread.Sleep(500);
                    lblInstructions.Text = temp;
                }
            }
            else if (battleshipSpots == 3)
            {
                Array.Sort(battleshipLoc);
                char test = CheckAdditionalShipSpots(battleshipOrientation, battleshipLoc[3], battleshipLoc[3], shipLoc);
                if (test != '\0')
                {
                    SoundEffects('p');
                    battleshipLoc[2] = shipLoc;
                    num.BackColor    = Color.Yellow;
                    num.Enabled      = false;
                    battleshipSpots--;
                    battleshipOrientation = test;
                }
                else
                {
                    SoundEffects('e');
                    string temp = lblInstructions.Text;
                    lblInstructions.Text = "[->] : Invalid Placement, Must be connected to your battle ship.";
                    lblInstructions.Refresh();
                    Thread.Sleep(500);
                    lblInstructions.Text = temp;
                }
            }
            else if (battleshipSpots == 2)
            {
                Array.Sort(battleshipLoc);
                char test = CheckAdditionalShipSpots(battleshipOrientation, battleshipLoc[2], battleshipLoc[3], shipLoc);
                if (test != '\0')
                {
                    SoundEffects('p');
                    battleshipLoc[1] = shipLoc;
                    num.BackColor    = Color.Yellow;
                    num.Enabled      = false;
                    battleshipSpots--;
                }
                else
                {
                    SoundEffects('e');
                    string temp = lblInstructions.Text;
                    lblInstructions.Text = "[->] : Invalid Placement, Must be connected to your battle ship.";
                    lblInstructions.Refresh();
                    Thread.Sleep(500);
                    lblInstructions.Text = temp;
                }
            }
            else if (battleshipSpots == 1)
            {
                Array.Sort(battleshipLoc);
                char test = CheckAdditionalShipSpots(battleshipOrientation, battleshipLoc[1], battleshipLoc[3], shipLoc);
                if (test != '\0')
                {
                    SoundEffects('p');
                    battleshipLoc[0] = shipLoc;
                    num.BackColor    = Color.Yellow;
                    num.Enabled      = false;
                    battleshipSpots--;
                    lblInstructions.Text = "[->] : Place your Cruiser on your battle board (3 spots, vertical or horizontal)";
                    s4 = new ship(4, battleshipLoc);
                }
                else
                {
                    SoundEffects('e');
                    string temp = lblInstructions.Text;
                    lblInstructions.Text = "[->] : Invalid Placement, Must be connected to your battle ship.";
                    lblInstructions.Refresh();
                    Thread.Sleep(500);
                    lblInstructions.Text = temp;
                }
            }
            else if (cruiserSpots == 3)
            {
                if (ValidateInitialShipPlacement(3, shipLoc, "cruiser") == true)
                {
                    SoundEffects('p');
                    cruiserLoc[0] = shipLoc;
                    num.BackColor = Color.Red;
                    num.Enabled   = false;
                    cruiserSpots--;
                }
                else
                {
                    SoundEffects('e');
                    string temp = lblInstructions.Text;
                    lblInstructions.Text = "[->] : Invalid Ship Placement, please try another spot.";
                    lblInstructions.Refresh();
                    Thread.Sleep(500);
                    lblInstructions.Text = temp;
                }
            }
            else if (cruiserSpots == 2)
            {
                Array.Sort(cruiserLoc);
                char test = CheckAdditionalShipSpots(cruiserOrientation, cruiserLoc[2], cruiserLoc[2], shipLoc);
                if (test != '\0')
                {
                    SoundEffects('p');
                    cruiserLoc[1] = shipLoc;
                    num.BackColor = Color.Red;
                    num.Enabled   = false;
                    cruiserSpots--;
                    cruiserOrientation = test;
                }
                else
                {
                    SoundEffects('e');
                    string temp = lblInstructions.Text;
                    lblInstructions.Text = "[->] : Invalid Ship Placement, please try another spot.";
                    lblInstructions.Refresh();
                    Thread.Sleep(500);
                    lblInstructions.Text = temp;
                }
            }
            else if (cruiserSpots == 1)
            {
                Array.Sort(cruiserLoc);
                char test = CheckAdditionalShipSpots(cruiserOrientation, cruiserLoc[1], cruiserLoc[2], shipLoc);
                if (test != '\0')
                {
                    SoundEffects('p');
                    cruiserLoc[0] = shipLoc;
                    num.BackColor = Color.Red;
                    num.Enabled   = false;
                    cruiserSpots--;
                    lblInstructions.Text = "[->] : Place your Destroyer on your battle board (2 spots, vertical or horizontal)";
                    s3 = new ship(3, cruiserLoc);
                }
                else
                {
                    SoundEffects('e');
                    string temp = lblInstructions.Text;
                    lblInstructions.Text = "[->] : Invalid Ship Placement, please try another spot.";
                    lblInstructions.Refresh();
                    Thread.Sleep(500);
                    lblInstructions.Text = temp;
                }
            }
            else if (destroyerSpots == 2)
            {
                if (ValidateInitialShipPlacement(2, shipLoc, "destroyer") == true)
                {
                    SoundEffects('p');
                    destroyerLoc[0] = shipLoc;
                    num.BackColor   = Color.Orange;
                    num.Enabled     = false;
                    destroyerSpots--;
                }
                else
                {
                    SoundEffects('e');
                    string temp = lblInstructions.Text;
                    lblInstructions.Text = "[->] : Invalid Ship Placement, please try another spot.";
                    lblInstructions.Refresh();
                    Thread.Sleep(500);
                    lblInstructions.Text = temp;
                }
            }
            else if (destroyerSpots == 1)
            {
                Array.Sort(destroyerLoc);
                char test = CheckAdditionalShipSpots(destroyerOrientation, destroyerLoc[1], destroyerLoc[1], shipLoc);
                if (test != '\0')
                {
                    SoundEffects('p');
                    destroyerLoc[0] = shipLoc;
                    num.BackColor   = Color.Orange;
                    num.Enabled     = false;
                    destroyerSpots--;
                    destroyerOrientation = test;
                    lblInstructions.Text = "[->] : All ship are set up! To begin game, click the Battle button!";
                    s2 = new ship(2, destroyerLoc);
                    gbxYourBoard.Enabled = false;
                    if (Ishost == false)
                    {
                        btnStartGame.Visible = true;
                    }
                }
                else
                {
                    SoundEffects('e');
                    string temp = lblInstructions.Text;
                    lblInstructions.Text = "[->] : Invalid Ship Placement, please try another spot.";
                    lblInstructions.Refresh();
                    Thread.Sleep(500);
                    lblInstructions.Text = temp;
                }
            }
        }
Example #3
0
        private void ResetGame()
        {
            //reset variables
            carrierSpots          = 5;
            battleshipSpots       = 4;
            cruiserSpots          = 3;
            destroyerSpots        = 2;
            carrierLoc            = new string[5];
            battleshipLoc         = new string[4];
            cruiserLoc            = new string[3];
            destroyerLoc          = new string[2];
            carrierOrientation    = 'u';
            battleshipOrientation = 'u';
            cruiserOrientation    = 'u';
            destroyerOrientation  = 'u';
            EnemyShips            = new string[14];

            s2 = new ship(2, destroyerLoc);
            s3 = new ship(3, cruiserLoc);
            s4 = new ship(4, battleshipLoc);
            s5 = new ship(5, carrierLoc);

            s2E = new ship(2, destroyerLoc);
            s3E = new ship(3, cruiserLoc);
            s4E = new ship(4, battleshipLoc);
            s5E = new ship(5, carrierLoc);

            //reset buttons
            gbxEnemyBoard.Enabled = true;
            gbxYourBoard.Enabled  = true;
            foreach (var button in gbxYourBoard.Controls.OfType <Button>())
            {
                button.Enabled   = true;
                button.BackColor = Color.Transparent;
                button.Text      = "";
            }
            foreach (var button in gbxEnemyBoard.Controls.OfType <Button>())
            {
                button.Enabled   = true;
                button.BackColor = Color.Transparent;
                button.Text      = "";
            }
            gbxEnemyBoard.Enabled = false;

            //reset controls
            txtbBattleLog.Clear();
            IsInBattle = false;

            pboxEnemy2.Image = Properties.Resources.destroyer;
            pboxEnemy3.Image = Properties.Resources.cruiser;
            pboxEnemy4.Image = Properties.Resources.battleship;
            pboxEnemy5.Image = Properties.Resources.carrier;
            pboxMy2.Image    = Properties.Resources.destroyer;
            pboxMy3.Image    = Properties.Resources.cruiser;
            pboxMy4.Image    = Properties.Resources.battleship;
            pboxMy5.Image    = Properties.Resources.carrier;

            axWMPbattle.Ctlcontrols.stop();

            if (cbxSound.Checked == true)
            {
                axWMPsetup.Ctlcontrols.play();
            }

            txtbMoves.Text = "InitializeEnemyShips";
            this.Refresh();

            //swap whos taking the first turn every game
            if (IsGoingFirst == true)
            {
                IsGoingFirst = false;
            }
            else
            {
                IsGoingFirst = true;
            }
        }