Question

Battle Ship Game Write a Java program from scratch. Simple geometrical reasoning is at the core...

Battle Ship Game

Write a Java program from scratch.

Simple geometrical reasoning is at the core of Battleships. One important notion is adjacency, with two sub-types: edge adjacency and corner adjacency. For example, the square (4,3) is edge-adjacent to the square (3,3), it is corner-adjacent to the square (3,2), and it is not adjacent to the square (6,7).

Write a program that reads a square from the user, and prints out three lists(each with a seperate method):

1) a list of all edge-adjacent squares,

2) a list of all corner-adjacent squares, and

3) a list of all squares which are not adjacent at all.

Our version of battleships is played on a 9 by 9 board. The lower-left square is (0,0). As noted, the program will read from the console a shot specification. You can decide which formats to handle. They may include, for example, specifications like

(5, 5)

3 0

4,1

a7

b 2

An A-level program will handle all the input formats illustrated, output correct answers, be clearly written, and be well designed.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

battleship game

import java.util.Random;

import java.util.Scanner;

public class battleShip

{

    public static void main(String[] args)

    {

        int[][] bord = new int[5][5];

        int[][] ships = new int[3][2];

        int[] shoot = new int[2];

        int attempt = 0,

            shoothit = 0;

        initBoard(bord);

        initShips(ships);

        System.out.println();

        do

        {

            showBoard(bord);

            shoot(shoot);

            attempt++;

            if (hit(shoot, ships))

            {

                hint(shoot, ships, attempt);

                shoothit++;

            }

            else

                hint(shoot, ships, attempt);

            changeboard(shoot, ships, bord);

        } while (shoothit != 3);

        System.out.println("\n\n\nBattleship Java game finished! You hit 3 ships in " + attempt + " attempt");

        showBoard(bord);

    }

    public static void initBoard(int[][] bord)

    {

        for (int row = 0; row < 5; row++)

            for (int column = 0; column < 5; column++)

                bord[row][column] = -1;

    }

    public static void showBoard(int[][] bord)

    {

        System.out.println("\t1 \t2 \t3 \t2 \t5");

        System.out.println();

        for (int row = 0; row < 5; row++)

        {

            System.out.print((row + 1) + "");

            for (int column = 0; column < 5; column++)

            {

                if (bord[row][column] == -1)

                {

                    System.out.print("\t" + "~");

                }

                else if (bord[row][column] == 0)

                {

                    System.out.print("\t" + "*");

                }

                else if (bord[row][column] == 1)

                {

                    System.out.print("\t" + "X");

                }

            }

            System.out.println();

        }

    }

    public static void initShips(int[][] ships)

    {

        Random random = new Random();

        for (int ship = 0; ship < 3; ship++)

        {

            ships[ship][0] = random.nextInt(5);

            ships[ship][1] = random.nextInt(5);

            //let's check if that shot was already tried

            //if it was, just finish the do...while when a new pair was randomly selected

            for (int last = 0; last < ship; last++)

            {

                if ((ships[ship][0] == ships[last][0]) && (ships[ship][1] == ships[last][1]))

                    do

                    {

                        ships[ship][0] = random.nextInt(5);

                        ships[ship][1] = random.nextInt(5);

                    } while ((ships[ship][0] == ships[last][0]) && (ships[ship][1] == ships[last][1]));

            }

        }

    }

    public static void shoot(int[] shoot)

    {

        Scanner input = new Scanner(System.in);

        System.out.print("Row: ");

        shoot[0] = input.nextInt();

        shoot[0]--;

        System.out.print("Column: ");

        shoot[1] = input.nextInt();

        shoot[1]--;

    }

    public static boolean hit(int[] shoot, int[][] ships)

    {

        for (int ship = 0; ship < ships.length; ship++)

        {

            if (shoot[0] == ships[ship][0] && shoot[1] == ships[ship][1])

            {

                System.out.printf("You hit a ship located in (%d,%d)\n", shoot[0] + 1, shoot[1] + 1);

                return true;

            }

        }

        return false;

    }

    public static void hint(int[] shoot, int[][] ships, int attempt)

    {

        int row = 0,

            column = 0;

        for (int line = 0; line < ships.length; line++)

        {

            if (ships[line][0] == shoot[0])

                row++;

            if (ships[line][1] == shoot[1])

                column++;

        }

        System.out.printf("\nHint %d: \nRow %d -> %d ships\n" +

                                 "Column %d -> %d ships\n", attempt, shoot[0] + 1, row, shoot[1] + 1, column);

    }

    public static void changeboard(int[] shoot, int[][] ships, int[][] bord)

    {

        if (hit(shoot, ships))

            bord[shoot[0]][shoot[1]] = 1;

        else

            bord[shoot[0]][shoot[1]] = 0;

    }

}

Add a comment
Know the answer?
Add Answer to:
Battle Ship Game Write a Java program from scratch. Simple geometrical reasoning is at the core...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You will write a Java program that implements Conway’s Game of Life, a simple cellular automaton...

    You will write a Java program that implements Conway’s Game of Life, a simple cellular automaton discussed in class. See for example: http://www.bitstorm.org/gameoflife/ Our version has a 10 x 10 grid, numbered like this: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 The grid is represented by a 10 x 10 2­dimensional integer array. If the grid point (i, j) is "populated", the array element [i][j] contains 1;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT