Closed Thread
Results 1 to 4 of 4

Thread: BattleShips game. - Ideas.

  1. #1
    maxandron is offline Newbie
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    BattleShips game. - Ideas.

    Hello,
    i just started a new project just to test what i know so far.
    The project is basically a battleships game using console application.

    Thing i have done in the project:
    1. adding a grid and ships to each player.
    2. checking all the possible error

    Now, basically i can now play, but there is one thing missing:
    i have no idea how to determine when the ship was fully destructed (one of them, in the middle of the game).

    I have a few ideas which involve huge and unnecessary code fragment and variables.

    Recursion seemed like the most beautiful and short solution to the problem, but every time i try i make an infinite recursion and get a stackoverflowexeption.

    you can find some info about the game writing "Battleships (game)" in google.

    and here is my complete code so far (its very very long):

    MAIN:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Zolelot
    {
        class Program
        {
            public static void giveInstructions()
            {
                Console.WriteLine("Instructions:");
                Console.WriteLine("How to start: each player places all his navy on a 10X10 grid,");
                Console.WriteLine("the ships must not touch each other in any way");
                Console.WriteLine("Each captain anounces the attack coordinates trying to hit an opponents ship.");
                Console.WriteLine("Direct hit: you get another turn");
                Console.WriteLine("Miss: the turn goes to the next player");
                Console.WriteLine("So who wins? the captain who was first to sink all opponent ships.");
                Console.WriteLine("Directions: 1-right, 2-down, 3-left, 4-up");
                Console.WriteLine("Press 'enter' to start playing!");
                Console.ReadLine();
            }
            static void Main(string[] args)
            {
                Random rnd = new Random();
                int i, j, degel;
                Zolelot p1 = new Zolelot();
                Zolelot p2 = new Zolelot();
                giveInstructions();
                Console.WriteLine("Player 1: please put all your ships:");
                Console.WriteLine("********************************************************************************");
                Console.WriteLine();
                p1.putAllShips();
                Console.WriteLine("Player 2: please put all your ships:");
                Console.WriteLine("********************************************************************************");
                Console.WriteLine();
                p2.putAllShips();
                Console.WriteLine();
                degel = rnd.Next(0, 2);
                if (degel == 0)
                    Console.WriteLine("Player 1 will start this game");
                else
                    Console.WriteLine("Player 2 will start this game");
                Console.WriteLine();
                Console.WriteLine("Let the battle begin!");
                Console.WriteLine();
                while (!(p1.CheckIfWon()) || !(p2.CheckIfWon()))
                {
                    if (degel == 0)
                    {
                        Console.WriteLine("Player 1 enter attack coordinates");
                        Console.Write("i: ");
                        i = int.Parse(Console.ReadLine());
                        Console.Write("j: ");
                        j = int.Parse(Console.ReadLine());
                        if(!(p2.Fire(i-1, j-1)))
                            degel = 1;
                        Console.WriteLine();
                    }
                    else
                    {
                        Console.WriteLine("Player 2 enter attack coordinates");
                        Console.Write("i: ");
                        i = int.Parse(Console.ReadLine());
                        Console.Write("j: ");
                        j = int.Parse(Console.ReadLine());
                        if(!(p1.Fire(i-1, j-1)))
                            degel = 0;
                        Console.WriteLine();
                    }
                }
                if (p1.CheckIfWon())
                    Console.WriteLine("Player 1 wins! congratulations.");
                else
                    Console.WriteLine("Player 2 wins! Congratulations.");
                Console.ReadLine();
            }
        }
    }
    Class Zolelot:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Zolelot
    {
        class Zolelot
        {
            private int[,] _Grid;
            public Zolelot()
            {
                _Grid = new int[10, 10];
            }
            public bool CheckIfWon()
            {
                for (int i = 0; i < 10; i++)
                    for (int j = 0; j < 10; j++)
                        if (_Grid[i, j] == 1)
                            return false;
                return true;
            }
            public bool Fire(int i, int j)
            {
                if (i > 9 || j > 9 || i < 0 || j < 0)
                {
                    Console.WriteLine("Error - Attack is off grid");
                    return true;
                }
                else if (_Grid[i, j] == 1)
                {
                    Console.WriteLine("Derect hit! you get another turn");
                    return true;
                }
                else
                {
                    Console.WriteLine("You Missed!");
                    return false;
                }
    
            }
            public void putAllShips()
            {
                int dir, i, j;
                do
                {
                    Console.WriteLine("Please enter the starting point and direction of the destroyer - size: 2");
                    Console.Write("i: ");
                    i = int.Parse(Console.ReadLine());
                    Console.Write("j: ");
                    j = int.Parse(Console.ReadLine());
                    Console.Write("direction: ");
                    dir = int.Parse(Console.ReadLine());
                }
                while (!(add(2, dir, i - 1, j - 1)));
                Console.WriteLine();
                do
                {
                    Console.WriteLine("Please enter the starting point and direction of the submarine - size: 3");
                    Console.Write("i: ");
                    i = int.Parse(Console.ReadLine());
                    Console.Write("j: ");
                    j = int.Parse(Console.ReadLine());
                    Console.Write("direction: ");
                    dir = int.Parse(Console.ReadLine());
                }
                while (!add(3, dir, i - 1, j - 1));
                Console.WriteLine();
                do
                {
                    Console.WriteLine("Please enter the starting point and direction of the cruiser - size: 3");
                    Console.Write("i: ");
                    i = int.Parse(Console.ReadLine());
                    Console.Write("j: ");
                    j = int.Parse(Console.ReadLine());
                    Console.Write("direction: ");
                    dir = int.Parse(Console.ReadLine());
                }
                while (!(add(3, dir, i - 1, j - 1)));
                Console.WriteLine();
                do
                {
                    Console.WriteLine("Please enter the starting point and direction of the battleship - size: 4");
                    Console.Write("i: ");
                    i = int.Parse(Console.ReadLine());
                    Console.Write("j: ");
                    j = int.Parse(Console.ReadLine());
                    Console.Write("direction: ");
                    dir = int.Parse(Console.ReadLine());
                }
                while (!(add(4, dir, i - 1, j - 1)));
                Console.WriteLine();
                do
                {
                    Console.WriteLine("Please enter the starting point and direction of the aircraft carrier - size: 5");
                    Console.Write("i: ");
                    i = int.Parse(Console.ReadLine());
                    Console.Write("j: ");
                    j = int.Parse(Console.ReadLine());
                    Console.Write("direction: ");
                    dir = int.Parse(Console.ReadLine());
                }
                while (!(add(5, dir, i - 1, j - 1)));
                Console.WriteLine();
            }
            private bool add(int length, int dir, int i, int j)
            {
                if (dir == 1)
                {
                    if (j + length - 1 >= 10)
                    {
                        Console.WriteLine("Error out of grid - ship was not added");
                        return false;
                    }
                    else if (!(TheoreticlyCorrect(length, dir, i, j)))
                    {
                        Console.WriteLine("Error cant put the ship in this place - ship was not added");
                        return false;
                    }
                    else if (Correct(length, dir, i, j))
                    {
                        for (int k = j; k < j + length; k++)
                            _Grid[i, k] = 1;
                        return true;
                    }
                    else
                    {
                        Console.WriteLine("Error cant put the ship in this place - ship was not added");
                        return false;
                    }
                }
                else if (dir == 2)
                {
                    if (i + length - 1 >= 10)
                    {
                        Console.WriteLine("Error out of grid - ship was not added");
                        return false;
                    }
                    else if (!(TheoreticlyCorrect(length, dir, i, j)))
                    {
                        Console.WriteLine("Error cant put the ship in this place - ship was not added");
                        return false;
                    }
                    else if (Correct(length, dir, i, j))
                    {
                        for (int k = i; k < i + length; k++)
                            _Grid[k, j] = 1;
                        return true;
                    }
                    else
                    {
                        Console.WriteLine("Error cant put the ship in this place - ship was not added");
                        return false;
                    }
                }
                else if (dir == 3)
                {
                    if (j - length < -1)
                    {
                        Console.WriteLine("Error out of grid - ship was not added");
                        return false;
                    }
                    else if (!(TheoreticlyCorrect(length, dir, i, j)))
                    {
                        Console.WriteLine("Error cant put the ship in this place - ship was not added");
                        return false;
                    }
                    else if (Correct(length, dir, i, j))
                    {
                        for (int k = j; k > j - length - 1; k--)
                            _Grid[i, k] = 1;
                        return true;
                    }
                    else
                    {
                        Console.WriteLine("Error cant put the ship in this place - ship was not added");
                        return false;
                    }
                }
                else if (dir == 4)
                {
                    if (i - length < -1)
                    {
                        Console.WriteLine("Error out of grid - ship was not added");
                        return false;
                    }
                    else if (!(TheoreticlyCorrect(length, dir, i, j)))
                    {
                        Console.WriteLine("Error cant put the ship in this place - ship was not added");
                        return false;
                    }
                    else if (Correct(length, dir, i, j))
                    {
                        for (int k = i; k > i - length - 1; k--)
                            _Grid[k, j] = 1;
                        return true;
                    }
                    else
                    {
                        Console.WriteLine("Error cant put the ship in this place - ship was not added");
                        return false;
                    }
                }
                else
                {
                    Console.WriteLine("Error wrong dir - ship was not added");
                    return false;
                }
            }
            private bool TheoreticlyCorrect(int length, int dir, int i, int j)
            {
                if (dir == 1)
                {
                    for (int k = j; k < j + length + 1; k++)
                        if (_Grid[i, k] == 1)
                            return false;
                }
                else if (dir == 2)
                {
                    for (int k = i; k < i + length + 1; k++)
                        if (_Grid[k, j] == 1)
                            return false;
                }
                else if (dir == 3)
                {
                    for (int k = j; k > j - length - 1; k--)
                        if (_Grid[i, k] == 1)
                            return false;
                }
                else
                {
                    for (int k = i; k > i - length - 1; k--)
                        if (_Grid[k, j] == 1)
                            return false;
                }
                return true;
            }
            private bool Correct(int length, int dir, int i, int j)
            {
                if (dir == 1)
                    if (j == 0)
                        if (i == 9)
                            return CorrectOver(i - 1, j, i, length + 1);
                        else if (i == 0)
                            return CorrectOver(i, j, 1, length + 1);
                        else
                            return CorrectOver(i - 1, j, i + 1, length + 1);
                    else if (i == 0)
                        return CorrectOver(i, j - 1, i + 1, j + length);
                    else if (i == 9)
                        return CorrectOver(i - 1, j - 1, i, j + length);
                    else
                        return CorrectOver(i - 1, j - 1, i + 1, j + length);
                else if (dir == 2)
                {
                    if (i == 0)
                        if (j == 9)
                            return CorrectOver(i, j - 1, i + length, j);
                        else if (j == 0)
                            return CorrectOver(i, j, i + length, j + 1);
                        else
                            return CorrectOver(i, j - 1, i + length, j + 1);
                    else
                        if (j == 9)
                            return CorrectOver(i - 1, j - 1, i + length, j);
                        else if (j == 0)
                            return CorrectOver(i - 1, j, i + length, j + 1);
                        else
                            return CorrectOver(i - 1, j - 1, i + length, j + 1);
                }
                else if (dir == 3)
                {
                    if (j == 9)
                        if (i == 0)
                            return CorrectOver(i, j - length, i + 1, j);
                        else if (i == 9)
                            return CorrectOver(i - 1, j - length, i, j);
                        else
                            return CorrectOver(i - 1, j - length, i + 1, j);
                    else
                        if (i == 0)
                            return CorrectOver(i, j - length, i + 1, j + 1);
                        else if (i == 9)
                            return CorrectOver(i - 1, j - length, i, j + 1);
                        else
                            return CorrectOver(i - 1, j - length, i + 1, j + 1);
    
                }
                else
                {
                    if (i == 9)
                        if (j == 0)
                            return CorrectOver(i - length, j, i, j + 1);
                        else if (j == 9)
                            return CorrectOver(i - length, j - 1, i, j);
                        else
                            return CorrectOver(i - length, j - 1, i, j + 1);
                    else
                        if (j == 0)
                            return CorrectOver(i - length, j, i + 1, j + 1);
                        else if (j == 9)
                            return CorrectOver(i - length, j - 1, i + 1, j);
                        else
                            return CorrectOver(i - length, j - 1, i + 1, j + 1);
                }
            }
            private bool CorrectOver(int iS, int jS, int iE, int jE)
            {
                for (int i = iS; i < iE; i++)
                    for (int j = jS; j < jE; j++)
                        if (_Grid[i, j] == 1)
                            return false;
                return true;
            }
        }
    }
    P.S.: im sorry i dont have any comments in my code, i will add them as soon as possible

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    alienkinetics's Avatar
    alienkinetics is offline Programmer
    Join Date
    Feb 2010
    Location
    Australia
    Posts
    154
    Rep Power
    0

    Re: BattleShips game. - Ideas.

    Cool project. I see your problem. Instead of a map defined as an array of int, why not a have an array of Ship? ie:

    Code:
    class Ship 
    {
      int hits;
      int type;
    }
    
    private Ship[,] _Grid;
    When you create a ship, each time you insert it into the grid, increment hits. Then each time a ship is hit, decrease hits. When its zero, you know the ship has been destroyed.

    You can also store the ships type, so you can say what kinda ship was destroyed.

    Or, you can keep your int map, and have a second array that records hits. Each ship would need a unique id (int) which would be used to access the hits and type arrays.

    I would go with the first, but it does mean you might have to recode some stuff.

  4. #3
    maxandron is offline Newbie
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Re: BattleShips game. - Ideas.

    Thank man, I'm done with the project now thank to you.

    cant believe i haven't thought about it myself...

  5. #4
    alienkinetics's Avatar
    alienkinetics is offline Programmer
    Join Date
    Feb 2010
    Location
    Australia
    Posts
    154
    Rep Power
    0

    Re: BattleShips game. - Ideas.

    Quote Originally Posted by maxandron View Post
    Thank man, I'm done with the project now thank to you. cant believe i haven't thought about it myself...
    Excellent. Good to hear.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Battleships Project - Please take a look :)
    By emmachallis in forum C# Programming
    Replies: 4
    Last Post: 12-31-2010, 02:48 AM
  2. Need help with my first console app, battleships!
    By Leynolds in forum C# Programming
    Replies: 1
    Last Post: 12-19-2010, 03:19 PM
  3. Java Game Ideas
    By lor in forum Java Help
    Replies: 9
    Last Post: 08-30-2010, 12:49 AM
  4. Ideas for rpg mafia game
    By G33k in forum The Lounge
    Replies: 7
    Last Post: 01-15-2010, 12:10 PM
  5. XNA - ideas for a game to recreate
    By Hignar in forum C# Programming
    Replies: 4
    Last Post: 08-12-2009, 08:31 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts