Example #1
0
        private void DrawBoat(GameObjects.Ship ship, bool isAi)
        {
            Image front, mid, mid2, mid3, back;

            if (player1.RemainingShips.Count <= 5 && aiplayer.RemainingShips.Count <= 5)
            {
                //PlayerTargetTable
                if (!isAi)
                {
                    if (ship.rotated == true)
                    {
                        if (ship.shipType.Equals("PatrolBoat"))
                        {
                            front = (Image)FindResource("LeftShipFront");
                            back  = (Image)FindResource("LeftShipBack");

                            Button toDrawStart = (Button)PlayerShipTable.FindName(ship.getCoords()[0, 0] + ship.getCoords()[0, 1]);
                            toDrawStart.Content = front;
                            Button toDrawEnd = (Button)PlayerShipTable.FindName(ship.getCoords()[1, 0] + ship.getCoords()[1, 1]);
                            toDrawEnd.Content = back;
                        }
                        else if (ship.shipType.Equals("Submarine") || ship.shipType.Equals("Destroyer"))
                        {
                            front = (Image)FindResource("LeftShipFront");
                            mid   = (Image)FindResource("LeftShipMid");
                            back  = (Image)FindResource("LeftShipBack");

                            Button toDrawStart = (Button)PlayerShipTable.FindName(ship.getCoords()[0, 0] + "" + ship.getCoords()[0, 1]);
                            toDrawStart.Content = front;
                            Button toDrawMid = (Button)PlayerShipTable.FindName(ship.getCoords()[1, 0] + "" + ship.getCoords()[1, 1]);
                            toDrawMid.Content = mid;
                            Button toDrawEnd = (Button)PlayerShipTable.FindName(ship.getCoords()[2, 0] + "" + ship.getCoords()[2, 1]);
                            toDrawEnd.Content = back;
                        }
                        else if (ship.shipType.Equals("Battleship"))
                        {
                            front = (Image)FindResource("LeftShipFront");
                            mid   = (Image)FindResource("LeftShipMid");
                            mid2  = (Image)FindResource("LeftShipMid");
                            back  = (Image)FindResource("LeftShipBack");

                            Button toDrawStart = (Button)PlayerShipTable.FindName(ship.getCoords()[0, 0] + "" + ship.getCoords()[0, 1]);
                            toDrawStart.Content = front;
                            Button toDrawMid = (Button)PlayerShipTable.FindName(ship.getCoords()[1, 0] + "" + ship.getCoords()[1, 1]);
                            toDrawMid.Content = mid;
                            Button toDrawMid2 = (Button)PlayerShipTable.FindName(ship.getCoords()[2, 0] + "" + ship.getCoords()[2, 1]);
                            toDrawMid2.Content = mid2;
                            Button toDrawEnd = (Button)PlayerShipTable.FindName(ship.getCoords()[3, 0] + "" + ship.getCoords()[3, 1]);
                            toDrawEnd.Content = back;
                        }
                        else if (ship.shipType.Equals("Carrier"))
                        {
                            front = (Image)FindResource("LeftShipFront");
                            mid   = (Image)FindResource("LeftShipMid");
                            mid2  = (Image)FindResource("LeftShipMid");
                            mid3  = (Image)FindResource("LeftShipMid");
                            back  = (Image)FindResource("LeftShipBack");

                            Button toDrawStart = (Button)PlayerShipTable.FindName(ship.getCoords()[0, 0] + "" + ship.getCoords()[0, 1]);
                            toDrawStart.Content = front;
                            Button toDrawMid = (Button)PlayerShipTable.FindName(ship.getCoords()[1, 0] + "" + ship.getCoords()[1, 1]);
                            toDrawMid.Content = mid;
                            Button toDrawMid2 = (Button)PlayerShipTable.FindName(ship.getCoords()[2, 0] + "" + ship.getCoords()[2, 1]);
                            toDrawMid2.Content = mid2;
                            Button toDrawMid3 = (Button)PlayerShipTable.FindName(ship.getCoords()[3, 0] + "" + ship.getCoords()[3, 1]);
                            toDrawMid3.Content = mid3;
                            Button toDrawEnd = (Button)PlayerShipTable.FindName(ship.getCoords()[4, 0] + "" + ship.getCoords()[4, 1]);
                            toDrawEnd.Content = back;
                        }
                    }
                    else
                    {
                        if (ship.shipType.Equals("PatrolBoat"))
                        {
                            front = (Image)FindResource("ShipFront");
                            back  = (Image)FindResource("ShipBack");

                            Button toDrawStart = (Button)PlayerShipTable.FindName(ship.getCoords()[0, 0] + ship.getCoords()[0, 1]);
                            toDrawStart.Content = front;
                            Button toDrawEnd = (Button)PlayerShipTable.FindName(ship.getCoords()[1, 0] + ship.getCoords()[1, 1]);
                            toDrawEnd.Content = back;
                        }
                        else if (ship.shipType.Equals("Submarine") || ship.shipType.Equals("Destroyer"))
                        {
                            front = (Image)FindResource("ShipFront");
                            mid   = (Image)FindResource("ShipMid");
                            back  = (Image)FindResource("ShipBack");

                            Button toDrawStart = (Button)PlayerShipTable.FindName(ship.getCoords()[0, 0] + "" + ship.getCoords()[0, 1]);
                            toDrawStart.Content = front;
                            Button toDrawMid = (Button)PlayerShipTable.FindName(ship.getCoords()[1, 0] + "" + ship.getCoords()[1, 1]);
                            toDrawMid.Content = mid;
                            Button toDrawEnd = (Button)PlayerShipTable.FindName(ship.getCoords()[2, 0] + "" + ship.getCoords()[2, 1]);
                            toDrawEnd.Content = back;
                        }
                        else if (ship.shipType.Equals("Battleship"))
                        {
                            front = (Image)FindResource("ShipFront");
                            mid   = (Image)FindResource("ShipMid");
                            mid2  = (Image)FindResource("ShipMid");
                            back  = (Image)FindResource("ShipBack");
                            Button toDrawStart = (Button)PlayerShipTable.FindName(ship.getCoords()[0, 0] + "" + ship.getCoords()[0, 1]);
                            toDrawStart.Content = front;
                            Button toDrawMid = (Button)PlayerShipTable.FindName(ship.getCoords()[1, 0] + "" + ship.getCoords()[1, 1]);
                            toDrawMid.Content = mid;
                            Button toDrawMid2 = (Button)PlayerShipTable.FindName(ship.getCoords()[2, 0] + "" + ship.getCoords()[2, 1]);
                            toDrawMid2.Content = mid2;
                            Button toDrawEnd = (Button)PlayerShipTable.FindName(ship.getCoords()[3, 0] + "" + ship.getCoords()[3, 1]);
                            toDrawEnd.Content = back;
                        }
                        else if (ship.shipType.Equals("Carrier"))
                        {
                            front = (Image)FindResource("ShipFront");
                            mid   = (Image)FindResource("ShipMid");
                            mid2  = (Image)FindResource("ShipMid");
                            mid3  = (Image)FindResource("ShipMid");
                            back  = (Image)FindResource("ShipBack");

                            Button toDrawStart = (Button)PlayerShipTable.FindName(ship.getCoords()[0, 0] + "" + ship.getCoords()[0, 1]);
                            toDrawStart.Content = front;
                            Button toDrawMid = (Button)PlayerShipTable.FindName(ship.getCoords()[1, 0] + "" + ship.getCoords()[1, 1]);
                            toDrawMid.Content = mid;
                            Button toDrawMid2 = (Button)PlayerShipTable.FindName(ship.getCoords()[2, 0] + "" + ship.getCoords()[2, 1]);
                            toDrawMid2.Content = mid2;
                            Button toDrawMid3 = (Button)PlayerShipTable.FindName(ship.getCoords()[3, 0] + "" + ship.getCoords()[3, 1]);
                            toDrawMid3.Content = mid3;
                            Button toDrawEnd = (Button)PlayerShipTable.FindName(ship.getCoords()[4, 0] + "" + ship.getCoords()[4, 1]);
                            toDrawEnd.Content = back;
                        }
                    }
                }

                //if its ai
                else
                {
                    if (ship.rotated == true)
                    {
                        if (ship.shipType.Equals("PatrolBoat"))
                        {
                            front = (Image)FindResource("LeftShipFront");
                            back  = (Image)FindResource("LeftShipBack");

                            Button toDrawStart = (Button)PlayerTargetTable.FindName(ship.getCoords()[0, 0] + ship.getCoords()[0, 1] + "_t");
                            toDrawStart.Content = front;
                            Button toDrawEnd = (Button)PlayerTargetTable.FindName(ship.getCoords()[1, 0] + ship.getCoords()[1, 1] + "_t");
                            toDrawEnd.Content = back;
                        }
                        else if (ship.shipType.Equals("Submarine") || ship.shipType.Equals("Destroyer"))
                        {
                            front = (Image)FindResource("LeftShipFront");
                            mid   = (Image)FindResource("LeftShipMid");
                            back  = (Image)FindResource("LeftShipBack");

                            Button toDrawStart = (Button)PlayerTargetTable.FindName(ship.getCoords()[0, 0] + "" + ship.getCoords()[0, 1] + "_t");
                            toDrawStart.Content = front;
                            Button toDrawMid = (Button)PlayerTargetTable.FindName(ship.getCoords()[1, 0] + "" + ship.getCoords()[1, 1] + "_t");
                            toDrawMid.Content = mid;
                            Button toDrawEnd = (Button)PlayerTargetTable.FindName(ship.getCoords()[2, 0] + "" + ship.getCoords()[2, 1] + "_t");
                            toDrawEnd.Content = back;
                        }
                        else if (ship.shipType.Equals("Battleship"))
                        {
                            front = (Image)FindResource("LeftShipFront");
                            mid   = (Image)FindResource("LeftShipMid");
                            mid2  = (Image)FindResource("LeftShipMid");
                            back  = (Image)FindResource("LeftShipBack");

                            Button toDrawStart = (Button)PlayerTargetTable.FindName(ship.getCoords()[0, 0] + "" + ship.getCoords()[0, 1] + "_t");
                            toDrawStart.Content = front;
                            Button toDrawMid = (Button)PlayerTargetTable.FindName(ship.getCoords()[1, 0] + "" + ship.getCoords()[1, 1] + "_t");
                            toDrawMid.Content = mid;
                            Button toDrawMid2 = (Button)PlayerTargetTable.FindName(ship.getCoords()[2, 0] + "" + ship.getCoords()[2, 1] + "_t");
                            toDrawMid2.Content = mid2;
                            Button toDrawEnd = (Button)PlayerTargetTable.FindName(ship.getCoords()[3, 0] + "" + ship.getCoords()[3, 1] + "_t");
                            toDrawEnd.Content = back;
                        }
                        else if (ship.shipType.Equals("Carrier"))
                        {
                            front = (Image)FindResource("LeftShipFront");
                            mid   = (Image)FindResource("LeftShipMid");
                            mid2  = (Image)FindResource("LeftShipMid");
                            mid3  = (Image)FindResource("LeftShipMid");
                            back  = (Image)FindResource("LeftShipBack");

                            Button toDrawStart = (Button)PlayerTargetTable.FindName(ship.getCoords()[0, 0] + "" + ship.getCoords()[0, 1] + "_t");
                            toDrawStart.Content = front;
                            Button toDrawMid = (Button)PlayerTargetTable.FindName(ship.getCoords()[1, 0] + "" + ship.getCoords()[1, 1] + "_t");
                            toDrawMid.Content = mid;
                            Button toDrawMid2 = (Button)PlayerTargetTable.FindName(ship.getCoords()[2, 0] + "" + ship.getCoords()[2, 1] + "_t");
                            toDrawMid2.Content = mid2;
                            Button toDrawMid3 = (Button)PlayerTargetTable.FindName(ship.getCoords()[3, 0] + "" + ship.getCoords()[3, 1] + "_t");
                            toDrawMid3.Content = mid3;
                            Button toDrawEnd = (Button)PlayerTargetTable.FindName(ship.getCoords()[4, 0] + "" + ship.getCoords()[4, 1] + "_t");
                            toDrawEnd.Content = back;
                        }
                    }
                    else
                    {
                        if (ship.shipType.Equals("PatrolBoat"))
                        {
                            front = (Image)FindResource("ShipFront");
                            back  = (Image)FindResource("ShipBack");

                            Button toDrawStart = (Button)PlayerTargetTable.FindName(ship.getCoords()[0, 0] + ship.getCoords()[0, 1] + "_t");
                            toDrawStart.Content = front;
                            Button toDrawEnd = (Button)PlayerTargetTable.FindName(ship.getCoords()[1, 0] + ship.getCoords()[1, 1] + "_t");
                            toDrawEnd.Content = back;
                        }
                        else if (ship.shipType.Equals("Submarine") || ship.shipType.Equals("Destroyer"))
                        {
                            front = (Image)FindResource("ShipFront");
                            mid   = (Image)FindResource("ShipMid");
                            back  = (Image)FindResource("ShipBack");

                            Button toDrawStart = (Button)PlayerTargetTable.FindName(ship.getCoords()[0, 0] + "" + ship.getCoords()[0, 1] + "_t");
                            toDrawStart.Content = front;
                            Button toDrawMid = (Button)PlayerTargetTable.FindName(ship.getCoords()[1, 0] + "" + ship.getCoords()[1, 1] + "_t");
                            toDrawMid.Content = mid;
                            Button toDrawEnd = (Button)PlayerTargetTable.FindName(ship.getCoords()[2, 0] + "" + ship.getCoords()[2, 1] + "_t");
                            toDrawEnd.Content = back;
                        }
                        else if (ship.shipType.Equals("Battleship"))
                        {
                            front = (Image)FindResource("ShipFront");
                            mid   = (Image)FindResource("ShipMid");
                            mid2  = (Image)FindResource("ShipMid");
                            back  = (Image)FindResource("ShipBack");
                            Button toDrawStart = (Button)PlayerTargetTable.FindName(ship.getCoords()[0, 0] + "" + ship.getCoords()[0, 1] + "_t");
                            toDrawStart.Content = front;
                            Button toDrawMid = (Button)PlayerTargetTable.FindName(ship.getCoords()[1, 0] + "" + ship.getCoords()[1, 1] + "_t");
                            toDrawMid.Content = mid;
                            Button toDrawMid2 = (Button)PlayerTargetTable.FindName(ship.getCoords()[2, 0] + "" + ship.getCoords()[2, 1] + "_t");
                            toDrawMid2.Content = mid2;
                            Button toDrawEnd = (Button)PlayerTargetTable.FindName(ship.getCoords()[3, 0] + "" + ship.getCoords()[3, 1] + "_t");
                            toDrawEnd.Content = back;
                        }
                        else if (ship.shipType.Equals("Carrier"))
                        {
                            front = (Image)FindResource("ShipFront");
                            mid   = (Image)FindResource("ShipMid");
                            mid2  = (Image)FindResource("ShipMid");
                            mid3  = (Image)FindResource("ShipMid");
                            back  = (Image)FindResource("ShipBack");

                            Button toDrawStart = (Button)PlayerTargetTable.FindName(ship.getCoords()[0, 0] + "" + ship.getCoords()[0, 1] + "_t");
                            toDrawStart.Content = front;
                            Button toDrawMid = (Button)PlayerTargetTable.FindName(ship.getCoords()[1, 0] + "" + ship.getCoords()[1, 1] + "_t");
                            toDrawMid.Content = mid;
                            Button toDrawMid2 = (Button)PlayerTargetTable.FindName(ship.getCoords()[2, 0] + "" + ship.getCoords()[2, 1] + "_t");
                            toDrawMid2.Content = mid2;
                            Button toDrawMid3 = (Button)PlayerTargetTable.FindName(ship.getCoords()[3, 0] + "" + ship.getCoords()[3, 1] + "_t");
                            toDrawMid3.Content = mid3;
                            Button toDrawEnd = (Button)PlayerTargetTable.FindName(ship.getCoords()[4, 0] + "" + ship.getCoords()[4, 1] + "_t");
                            toDrawEnd.Content = back;
                        }
                    }
                }
            }
        }
Example #2
0
        private void CreateShipOnPosition(string ship_name, string m_position, PlayerEntity player, bool aiship)
        {
            string[]         resultAlphabet = System.Text.RegularExpressions.Regex.Split(m_position, @"\d+");
            string[]         resultNumber   = System.Text.RegularExpressions.Regex.Split(m_position, @"\D+");
            string[]         startCoord, endCoord;
            GameObjects.Ship createdShip;

            startCoord = GameObjects.Functions.getStartCoord(resultAlphabet[0], resultNumber[1], ship_name, rotated);
            endCoord   = GameObjects.Functions.getEndCoord(resultAlphabet[0], resultNumber[1], ship_name, rotated);

            if (startCoord[0].Equals("asd") || endCoord[0].Equals("asd"))
            {
                return;
            }

            if (!aiship)
            {
                Canvas.SetLeft(boat_image, 0);
                Canvas.SetTop(boat_image, 0);
                boat_image.RenderTransform = GetBasicScaling(boat_image, false);
            }

            createdShip          = new GameObjects.Ship(startCoord[0], int.Parse(startCoord[1]), endCoord[0], int.Parse(endCoord[1]), ship_name);
            string[,] shipcoords = createdShip.getCoords();
            int len = createdShip.getCoords().Length;

            //if the ship's any coord hits another ship, it wont place the ship to the table
            if (PlayerHitsaShip(m_position, player) ||
                PlayerHitsaShip(startCoord[0] + startCoord[1], player) ||
                PlayerHitsaShip(endCoord[0] + endCoord[1], player)
                )
            {
                old_image = null;
                return;
            }
            if (len == 8)
            {
                if (
                    PlayerHitsaShip(shipcoords[2, 0] + shipcoords[2, 1], player)
                    )
                {
                    old_image = null;
                    return;
                }
            }
            else if (len == 10)
            {
                if (PlayerHitsaShip(shipcoords[1, 0] + shipcoords[1, 1], player) ||
                    PlayerHitsaShip(shipcoords[2, 0] + shipcoords[2, 1], player) ||
                    PlayerHitsaShip(shipcoords[3, 0] + shipcoords[3, 1], player)
                    )
                {
                    old_image = null;
                    return;
                }
            }
            player.fillUpRemainingShips(createdShip);

            if (!aiship)
            {
                DrawBoat(createdShip, false);
                GameObjects.Functions.UpdateRemainingShips(NumberofHits, player_remaining_ships, aiplayer, player1, true);
            }

            if (player1.RemainingShips.Count == 5)
            {
                StartGameAfterPlacement();
            }

            old_image = null;
        }
Example #3
0
        private void WhichPartIsHit(string clickedButtonCoord, GameObjects.Ship ship, int i, int HowLong, bool ai)
        {
            char[] arrayForTrim = { 't', '_' };
            var    name         = clickedButtonCoord.TrimEnd(arrayForTrim);

            if (GameObjects.Functions.CoordsEqual(name, ship.getCoords()[0, 0] + ship.getCoords()[0, 1]))
            {
                if (ship.rotated)
                {
                    if (!ai)
                    {
                        partHit = "ShipHitLeftFront";
                    }
                    else
                    {
                        partHitAi = "ShipHitLeftFront";
                    }
                }
                else if (!ai)
                {
                    partHit = "ShipHitFront";
                }
                else
                {
                    partHitAi = "ShipHitFront";
                }
            }
            if (HowLong == 4)
            {
                if (GameObjects.Functions.CoordsEqual(name, ship.getCoords()[1, 0] + ship.getCoords()[1, 1]))
                {
                    if (ship.rotated)
                    {
                        if (!ai)
                        {
                            partHit = "ShipHitLeftBack";
                        }
                        else
                        {
                            partHitAi = "ShipHitLeftBack";
                        }
                    }
                    else
                    {
                        if (!ai)
                        {
                            partHit = "ShipHitBack";
                        }
                        else
                        {
                            partHitAi = "ShipHitBack";
                        }
                    }
                }
            }
            else if (HowLong == 6)
            {
                if (GameObjects.Functions.CoordsEqual(name, ship.getCoords()[1, 0] + ship.getCoords()[1, 1]))
                {
                    if (ship.rotated)
                    {
                        if (!ai)
                        {
                            partHit = "ShipHitLeftMid";
                        }
                        else
                        {
                            partHitAi = "ShipHitLeftMid";
                        }
                    }
                    else
                    {
                        if (!ai)
                        {
                            partHit = "ShipHitMid";
                        }
                        else
                        {
                            partHitAi = "ShipHitMid";
                        }
                    }
                }
                else if (GameObjects.Functions.CoordsEqual(name, ship.getCoords()[2, 0] + ship.getCoords()[2, 1]))
                {
                    if (ship.rotated)
                    {
                        if (!ai)
                        {
                            partHit = "ShipHitLeftBack";
                        }
                        else
                        {
                            partHitAi = "ShipHitLeftBack";
                        }
                    }
                    else
                    {
                        if (!ai)
                        {
                            partHit = "ShipHitBack";
                        }
                        else
                        {
                            partHitAi = "ShipHitBack";
                        }
                    }
                }
            }
            else if (HowLong == 8)
            {
                if (GameObjects.Functions.CoordsEqual(name, ship.getCoords()[1, 0] + ship.getCoords()[1, 1]) ||
                    GameObjects.Functions.CoordsEqual(name, ship.getCoords()[2, 0] + ship.getCoords()[2, 1])
                    )
                {
                    if (ship.rotated)
                    {
                        if (!ai)
                        {
                            partHit = "ShipHitLeftMid";
                        }
                        else
                        {
                            partHitAi = "ShipHitLeftMid";
                        }
                    }
                    else
                    {
                        if (!ai)
                        {
                            partHit = "ShipHitMid";
                        }
                        else
                        {
                            partHitAi = "ShipHitLeftMid";
                        }
                    }
                }
                else if (GameObjects.Functions.CoordsEqual(name, ship.getCoords()[3, 0] + ship.getCoords()[3, 1]))
                {
                    if (ship.rotated)
                    {
                        if (!ai)
                        {
                            partHit = "ShipHitLeftBack";
                        }
                        else
                        {
                            partHitAi = "ShipHitLeftBack";
                        }
                    }
                    else
                    {
                        if (!ai)
                        {
                            partHit = "ShipHitBack";
                        }
                        else
                        {
                            partHitAi = "ShipHitBack";
                        }
                    }
                }
            }
            else if (HowLong == 10)
            {
                if (GameObjects.Functions.CoordsEqual(name, ship.getCoords()[1, 0] + ship.getCoords()[1, 1]) ||
                    GameObjects.Functions.CoordsEqual(name, ship.getCoords()[2, 0] + ship.getCoords()[2, 1]) ||
                    GameObjects.Functions.CoordsEqual(name, ship.getCoords()[3, 0] + ship.getCoords()[3, 1])
                    )
                {
                    if (ship.rotated)
                    {
                        if (!ai)
                        {
                            partHit = "ShipHitLeftMid";
                        }
                        else
                        {
                            partHitAi = "ShipHitLeftMid";
                        }
                    }
                    else
                    {
                        if (!ai)
                        {
                            partHit = "ShipHitMid";
                        }
                        else
                        {
                            partHitAi = "ShipHitMid";
                        }
                    }
                }
                else if (GameObjects.Functions.CoordsEqual(name, ship.getCoords()[4, 0] + ship.getCoords()[4, 1]))
                {
                    if (ship.rotated)
                    {
                        if (!ai)
                        {
                            partHit = "ShipHitLeftBack";
                        }
                        else
                        {
                            partHitAi = "ShipHitLeftBack";
                        }
                    }
                    else
                    {
                        if (!ai)
                        {
                            partHit = "ShipHitBack";
                        }
                        else
                        {
                            partHitAi = "ShipHitBack";
                        }
                    }
                }
            }
        }