locked
battleship game in c# RRS feed

  • Question

  • hi i have to do battleship game in c# but i'm stock because i dont know how to do it. Can anyone help me please?
    this is what i've done so far but i still need to get player move method, make computer attack, and create the game behaviour.
    Can anyone help me please?

    using System;
    class BattleShips
    {
     static void Main()
     {

      const int ATTACKED = 1;
     const int BATTLESHIP = 2;
     const int CRUISER = 3;
            const int SUBMARINE = 4;
            const int ROWINGBOAT = 5;
            const int SEA_WIDTH = 10;
     const int SEA_HEIGHT = 10;
            const int EMPTY_SEA = 0;
            const int ROW;
            const int COLUMN;


     
     Console.WriteLine("Welcome to Battleships");

     // Put the sea into our game as a 10 by 10 array
     
     static int[,] sea = new int[SEA_WIDTH, SEA_HEIGHT];

     static void clearSea() // Clears the sea to empty

     {
     for ( ROW = EMPTY_SEA; ROW < SEA_WIDTH, SEA_HEIGHT; ROW++)

     Console.WriteLine();
     for ( COLUMN = EMPTY_SEA; COLUMN < SEA_WIDTH, SEA_HEIGHT; COLUMN++)
     Console.Write(sea[COLUMN,ROW]+ " ");


     static void placeShips() // Places ships in the sea
     {
      sea [0, 5] = BATTLESHIP;
      sea [2, 7] = BATTLESHIP;
      sea [0, 0] = CRUISER;
      sea [2, 0] = CRUISER;
      sea [4, 3] = CRUISER;
      sea [5, 6] = SUBMARINE;
      sea [8, 8] = ROWINGBOAT;

     
      
      }

    }

    • Moved by CoolDadTx Thursday, April 14, 2016 1:56 PM Homework
    Friday, December 10, 2010 9:07 PM

Answers

All replies

  • I don't want to be rude but you have basically asked someone to make a whole game for you lol.  Basically for your player attack you just want to check your array for an enemy ship.  For the cpu move, create 2 random numbers.  1 for column and 1 for row.  Check your array[column, row] to see if it hits the player ship.  Don't forget to error check so the cpu cant hit its own ship.
    Friday, December 10, 2010 11:38 PM
  • In addition to Tux76's suggestion, I would say:
     
    1. Please post code that does not produce errors.  Your code declares ROW as a const, without initialization (an error), and also uses ROW as a for loop variable (an error).  There are other errors as well.
    2. Keep a list of previous guesses.  There is no need reguessing a square.
    3. Traditionally, when you have a "hit" you would keep guessing in straight lines until you see you have sunk a ship.  Keep the old hit's x, y, but vary only one of them by +1, -1, unless guessed before.  This part is not trivial, but important to a good cpu player.
    4. Number three brings up the need to identify somehow that a ship was sunk to let the player know this has occured.
     

    --
    Mike
    Friday, December 10, 2010 11:49 PM
  • The posted code is remarkable.  It is almost identical to this thread.

    http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/b8012609-4592-44ad-90ca-ae9467d22b2c

    The OP on that thread appeared to ignore the given advice.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    • Marked as answer by Alan_chen Friday, December 17, 2010 8:20 AM
    Friday, December 10, 2010 11:56 PM

  • I thought it sounded somewhat familiar.  Maybe the professor gave them the same starting point...

    --
    Mike
    Saturday, December 11, 2010 12:06 AM
  • can you help me with mine. 

    CSP Battleship Assignment

    **You may work with a partner on this assignment.**

    C# and Games

    We have seen that you can create a C# program which will process information. Now we are going to see how you can use a program to play a simple game.

    We will find that even simple games can lead to problems where you need to produce quite complex behaviour. We should also see that even very simple behaviour can make a system appear "clever".

    The Game of Battleships

    The game is played on a rectangular grid of locations in an imaginary "sea". Each location can hold a ship, part of a ship, or empty sea. At the start of the game players place their fleet of a particular number of each kind of ship into the sea. They then take it in turns to nominate a particular location in their opponent's sea for attack. The opponent must state whether the chosen location contains empty sea, or part of a ship. The game is over when one of the players has lost all of his or her fleet. The other player is then the winner.

    C# and Battleships

    We are going to write a program which will take the part of one player in the game. The program will automatically place ships on its sea, generate locations for attack and also report the result of attacks on it. We need to consider:

    • How to hold the "sea" and print it out
    • How to represent different types of craft in the sea
    • How generate attack co-ordinates
    • How to respond to attacks

    You might like to consider how easy it would be to make the computer player cheat if it is losing, but for now the list above will do fine!

    In this session we are going to start with the code which will create and manage the "sea" for the computer program when it is playing the game.

    Representing the Sea

    Each location in the sea will have a particular state, it will either hold nothing, a part of a ship, or it will have been attacked. In computer terms we could say that each location will occupy a given state. We could start by giving values to each of these states:

    • Empty sea:    0
    • Attacked:        1
    • Battleship:     2
    • Cruiser:          3
    • Submarine:   4
      • Rowing boat: 5

    We have already seen that C# maps numbers onto other things, for example the UNICODE character set maps values onto particular printable characters. This is a “ship” code for our game. If we want to add more ships we just have to use more values.

    Creating the Sea Array

    The game will represent the board playfield using a two dimensional array:

     

    0

    1

    2

    3

    4

    5

    6

    7

    8

    9

    0

    3

    0

    3

    0

    0

    0

    0

    0

    0

    0

    1

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    2

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    3

    0

    0

    0

    0

    3

    0

    0

    0

    0

    0

    4

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    5

    2

    0

    0

    0

    0

    0

    0

    0

    0

    0

    6

    0

    0

    0

    0

    0

    4

    0

    0

    0

    0

    7

    0

    0

    2

    0

    0

    0

    0

    0

    0

    0

    8

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    9

    0

    0

    0

    0

    0

    0

    0

    0

    0

    5

     

    The table above shows how this will work. You can regard a 2D array as a grid or table. Each element in the table has a distance across (X) and down (Y) value. The table above shows the initial setting for our game. It shows the following arrangement for ships:

    Battleship:         (0,5) and (2,7)

    Cruiser:            (0,0), (2,0) and (4,3)

    Submarine:          (5,6)

    Rowing boat:        (8,8)

     

    This is the initial arrangement for ships in the game. When you make your game the first version will place ships at these particular positions. This is so that you can test the game and make sure that ships are detected when they should be and that the game ends when the last ship has been found.

    In a program it is easier to think in terms of name rather than numbers. I don't want to have to remember that 2 means battleship. At the start of my program I create constants which I can then use throughout the system. These allow me to give a name to a value.

    In C# you can create variables which have a constant value (i.e. they cannot be changed once they have been given a value). You do this by using final:

    const int EMPTY_SEA =0;

    This would declare a variable called EMPTY_SEA which has the value 0. Note how I have used a convention of all capital letters and the underscore character in the identifier for this variable. This is based on a convention used in the C language for defined constants. Many C# programmers do the same thing.


    Declaring the Sea Array

    We can use a constant to set the size of the sea the game is going to use.

    const int SEA_SIZE = 10;

    We can now create a 2D array which holds the sea:

    int[,] sea = new int[SEA_SIZE, SEA_SIZE];

     

    The first thing we must do is set all the locations in the sea to EMPTY_SEA. The best way to do this is to use two for loops, with one “nested” inside the other.

    For your first exercise you must write a program which contains a declaration of the sea array and some code which "empties" the sea array by setting all the locations to empty sea.

    Placing Ships on the Sea

    We need to have some code which places the ships on the board at the start of the game. In real life you would probably use a random mechanism to do this, but for the purpose of our program (and to make testing easier at this point) we have decided to implement a standard for the placement of the vessels in the program. This is as follows:

    Battleship:         (0,5) and (2,7)

    Cruiser:            (0,0), (2,0) and (4,3)

    Submarine:          (5,6)

    Rowing boat:        (8,8)

    Note that we have two battleships, three cruisers, one submarine and one rowing boat. Note also that all the vessels occupy one square at the moment (you can add multi-square ships as an enhancement later). Write some statements that place the ships “in” the sea after it has been set to empty.

    Displaying the Sea

    Next you need to print the array out on the console screen. For the purpose of our program you can work on the basis that we will never have a sea wider than 70 characters or higher than 23 lines. Start by just printing out the numbers in the array. Later you could use characters to represent particular types of ships. To print out the contents of the array you can use two nested for loops again. Remember that you only need to print a new line at the end of each row of printing.

    Testing your Program

    When you run your program it should print out a copy of the sea display which looks like the one above.

    Thursday, April 14, 2016 1:23 PM