Пример #1
0
        /// <summary>
        /// This method handles the computer's turn.
        /// </summary>
        /// <param name="player"></param>
        /// <param name="rendering"></param>
        public void TakeShot(Player player, Rendering rendering)
        {
            int[,] possibilityMap;
            int currentHighestX     = 0;
            int currentHighestY     = 0;
            int currentHighestScore = 0;
            int tempScore           = 0;

            hunting = true;

            for (int x = 0; x < 10; x++)
            {
                for (int y = 0; y < 10; y++)
                {
                    if (playerMap[x, y] == 2) //if there is a hit ship that has not been fully destroyed...
                    {
                        hunting = false;      //go into target mode
                    }
                }
            }

            if (hunting)
            {
                possibilityMap = CalculatepossiblePlacements();
            }
            else
            {
                possibilityMap = CalculateTargetedPlacements();
            }

            //clear all previously fired on tiles just to be safe

            for (int x = 0; x < 10; x++)
            {
                for (int y = 0; y < 10; y++)
                {
                    if (playerMap[x, y] != 0)
                    {
                        possibilityMap[x, y] = 0;
                    }
                }
            }

            //choose most likely location of the ship

            for (int x = 0; x < 10; x++)
            {
                for (int y = 0; y < 10; y++)
                {
                    tempScore = possibilityMap[x, y];
                    if (tempScore > currentHighestScore)
                    {
                        currentHighestX     = x;
                        currentHighestY     = y;
                        currentHighestScore = tempScore;
                    }
                }
            }

            player.SquareHit(currentHighestX, currentHighestY, this, rendering);
        }
Пример #2
0
        /// <summary>
        /// This method allows the player to place their ships in their grid.
        /// </summary>
        public void PlaceShips(Rendering rendering)
        {
            int  shipLength;
            int  shipOldX;
            int  shipOldY;
            int  shipX;
            int  shipY;
            int  userInput;
            bool shipPlaced;
            bool vertical;

            Stream      beep         = Battleships.Properties.Resources.SelectionSound;
            Stream      select       = Battleships.Properties.Resources.SelectionConfirm;
            Stream      error        = Battleships.Properties.Resources.Error;
            SoundPlayer beepPlayer   = new SoundPlayer(beep);
            SoundPlayer selectPlayer = new SoundPlayer(select);
            SoundPlayer errorPlayer  = new SoundPlayer(error);

            for (int shipNumber = 0; shipNumber < 5; shipNumber++)
            {
                shipLength = ships[shipNumber].length;
                shipX      = 0;
                shipY      = 0;
                vertical   = true;

                shipPlaced = false;

                Console.BackgroundColor = ConsoleColor.Black;
                //Console.SetCursorPosition(24, 2);
                //Console.Write("Place:");
                //Console.SetCursorPosition(24, 3);
                //Console.Write("                ");
                //Console.SetCursorPosition(24, 3);
                //Console.Write(ships[shipNumber].name);
                Console.SetCursorPosition(23, 5);
                Console.Write("(use arrows + space)");

                //(hopefully not) Broken code.
                rendering.UpdateLog("Place " + ships[shipNumber].name);

                while (!shipPlaced)
                {
                    rendering.DrawGameScreens(this);
                    rendering.DrawShipPlacement(shipX, shipY, shipLength, vertical);

                    userInput = (int)Console.ReadKey(true).Key;
                    beepPlayer.Play();

                    switch (userInput)
                    {
                    case 82:     //'r' - rotate
                        if (vertical)
                        {
                            if (shipX + shipLength > 10)
                            {
                                shipX = 10 - shipLength;
                            }
                        }
                        else
                        {
                            if (shipY + shipLength > 10)
                            {
                                shipY = 10 - shipLength;
                            }
                        }
                        vertical = !vertical;
                        break;

                    case 37:     //left arrow
                        if (shipX - 1 >= 0)
                        {
                            shipX--;
                        }
                        break;

                    case 38:     //up arrow
                        if (shipY - 1 >= 0)
                        {
                            shipY--;
                        }
                        break;

                    case 39:     //right arrow
                        if (vertical)
                        {
                            if (shipX + 1 < 10)
                            {
                                shipX++;
                            }
                        }
                        else
                        {
                            if (shipX + shipLength < 10)
                            {
                                shipX++;
                            }
                        }
                        break;

                    case 40:     //down arrow
                        if (vertical)
                        {
                            if (shipY + shipLength < 10)
                            {
                                shipY++;
                            }
                        }
                        else
                        {
                            if (shipY + 1 < 10)
                            {
                                shipY++;
                            }
                        }
                        break;

                    case 32:     //Space bar

                        if (ShipCollision(shipX, shipY, shipLength, vertical) == false)
                        {
                            selectPlayer.Play();     //play selection sound
                            for (int c = 0; c < shipLength; c++)
                            {
                                if (vertical)
                                {
                                    map[shipX, shipY + c] = shipNumber + 1;     //this will be a unique identifier in order to allow quick lookups of hit ships.
                                }
                                else
                                {
                                    map[shipX + c, shipY] = shipNumber + 1;     //this will be a unique identifier in order to allow quick lookups of hit ships.
                                }
                            }
                            ships[shipNumber].PlaceShip(shipX, shipY, vertical);
                            shipPlaced = true;
                        }
                        else
                        {
                            errorPlayer.Play();
                        }
                        break;
                    }
                }
            }
            rendering.UpdateLog("All ships placed!");
            Console.BackgroundColor = ConsoleColor.Black;
            //Console.SetCursorPosition(24, 2);
            //Console.Write("       ");
            //Console.SetCursorPosition(24, 3);
            //Console.Write("                ");
            Console.SetCursorPosition(23, 5);
            Console.Write("                    ");

            beep.Dispose();
            beepPlayer.Dispose();
            select.Dispose();
            selectPlayer.Dispose();
            error.Dispose();
            errorPlayer.Dispose();
        }
Пример #3
0
        /// <summary>
        /// This method handles hits, determining which ships, if any, were hit and updating the instances accordingly. It will return a 0 if no ships were destroyed and a 1 if some were.
        /// </summary>
        /// <param name="posX"></param>
        /// <param name="posY"></param>
        public int SquareHit(int posX, int posY, Computer computer, Rendering rendering)
        {
            int hitShipID;

            Stream explosion = Battleships.Properties.Resources.Explosion;
            Stream missile   = Battleships.Properties.Resources.Missile;

            SoundPlayer explosionPlayer = new SoundPlayer(explosion);
            SoundPlayer missilePlayer   = new SoundPlayer(missile);

            missilePlayer.Play();
            System.Threading.Thread.Sleep(500);

            if (map[posX, posY] != 0 && map[posX, posY] < 6) //if the map square is a ship.
            {
                hitShipID = map[posX, posY] - 1;
                if (ships[hitShipID].ShipHit() == 0)
                {
                    computer.playerMap[posX, posY] = 2;
                    map[posX, posY] = 7;
                    explosionPlayer.Play();
                    rendering.DrawGameScreens(this);
                    rendering.UpdateLog("The enemy shot hits!");

                    explosion.Dispose();
                    missile.Dispose();
                    explosionPlayer.Dispose();
                    missilePlayer.Dispose();
                    return(0);
                }
                else
                {
                    for (int count = 0; count < ships[hitShipID].length; count++)
                    {
                        computer.playerMap[ships[hitShipID].gridPositions[count, 0], ships[hitShipID].gridPositions[count, 1]] = 3; //Make it known to the computer that a ship has been destroyed.
                    }
                    //computer.playerMap[posX, posY] = 2;
                    map[posX, posY] = 7;
                    rendering.DrawGameScreens(this);
                    rendering.UpdateLog(ships[hitShipID].name + " destroyed!");
                    explosionPlayer.Play();
                    System.Threading.Thread.Sleep(150);
                    explosionPlayer.Play();
                    System.Threading.Thread.Sleep(150);
                    explosionPlayer.Play();
                    System.Threading.Thread.Sleep(150);

                    explosion.Dispose();
                    missile.Dispose();
                    explosionPlayer.Dispose();
                    missilePlayer.Dispose();
                    return(1);
                }
            }
            else
            {
                computer.playerMap[posX, posY] = 1;
                map[posX, posY] = 8;
                rendering.DrawGameScreens(this);
                rendering.UpdateLog("The enemy shot misses!");

                explosion.Dispose();
                missile.Dispose();
                explosionPlayer.Dispose();
                missilePlayer.Dispose();
                return(0);
            }
        }
Пример #4
0
        /// <summary>
        /// This method handles the player making a shot on the enemy ships.
        /// </summary>
        public void TakeShot(Computer computer, Rendering rendering)
        {
            int  xSelection = 0;
            int  ySelection = 0;
            bool shotFired  = false;
            int  userInput;

            Stream select  = Battleships.Properties.Resources.SelectionSound;
            Stream confirm = Battleships.Properties.Resources.SelectionConfirm;
            Stream cancel  = Battleships.Properties.Resources.SelectionCancel;
            Stream missile = Battleships.Properties.Resources.Missile;
            Stream error   = Battleships.Properties.Resources.Error;

            SoundPlayer selectPlayer  = new SoundPlayer(select);
            SoundPlayer confirmPlayer = new SoundPlayer(confirm);
            SoundPlayer cancelPlayer  = new SoundPlayer(cancel);
            SoundPlayer missilePlayer = new SoundPlayer(missile);
            SoundPlayer errorPlayer   = new SoundPlayer(error);

            while (shotFired == false)
            {
                rendering.DrawGameScreens(this);
                rendering.UpdateLog("Select Target");

                bool innerLoop = true;

                while (innerLoop)
                {
                    userInput = (int)Console.ReadKey(true).Key;
                    if (userInput < 75 && userInput > 64) //if the key pressed is a to j
                    {
                        selectPlayer.Play();
                        xSelection = userInput - 65; //converts the keycode to an x co-ordinate;
                        innerLoop  = false;
                    }
                }

                rendering.DrawTarget(this, xSelection);
                innerLoop = true;

                while (innerLoop)
                {
                    userInput = (int)Console.ReadKey(true).Key;
                    if (userInput < 58 && userInput > 47) //if the key pressed is 0 to 9
                    {
                        selectPlayer.Play();
                        ySelection = userInput - 48;
                        innerLoop  = false;
                    }
                }

                rendering.DrawTarget(this, xSelection, ySelection);
                rendering.UpdateLog("Ready to Fire");
                innerLoop = true;

                while (innerLoop)
                {
                    userInput = (int)Console.ReadKey(true).Key;

                    if (userInput == 32 || userInput == 13) //spacebar or enter
                    {
                        if (enemyMap[xSelection, ySelection] != 0)
                        {
                            errorPlayer.Play();
                            rendering.UpdateLog("Error: You've already fired at that square!");
                        }
                        else
                        {
                            missilePlayer.Play();
                            System.Threading.Thread.Sleep(500);
                            computer.SquareHit(xSelection, ySelection, this, rendering);
                            shotFired = true;
                        }
                        innerLoop = false;
                    }
                    else if (userInput == 8) //backspace
                    {
                        cancelPlayer.Play();
                        rendering.UpdateLog("Shot cancelled");
                        System.Threading.Thread.Sleep(1000);
                        innerLoop = false;
                    }
                }
            }

            select.Dispose();
            selectPlayer.Dispose();
            confirm.Dispose();
            confirmPlayer.Dispose();
            cancel.Dispose();
            cancelPlayer.Dispose();
            missile.Dispose();
            missilePlayer.Dispose();
            error.Dispose();
            errorPlayer.Dispose();
        }
Пример #5
0
 public Game()
 {
     rendering = new Rendering();
     player    = new Player();
     computer  = new Computer();
 }