Amirates
Amirates

Reputation: 63

Java game does not start within Java just in CMD

I am a beginner and I have build this battleship game by using tutorial code and I have changed the basic stuff such as amount of ships and column and row numbers. Java wont start my game as a .jar file. It only starts in CMD. Please note that my laptop is jarfixed and I can open any .jar file normally. Is it because my game does not really have an UI? Or is the problem within my manifest file? Yesterday was the first time I've ever compiled to a .jar file.

Manifest

Main-Class: BattleShip

Java code

import java.util.Random;
import java.util.Scanner;

public class BattleShip {

public static void main(String[] args) {
    int[][] board = new int[7][7];
    int[][] ships = new int[5][2];
    int[] shoot = new int[2];
    int attempts=0,
        shotHit=0;

    //method initBoard is triggered to create the board with the number '-1' 
   in all positions
    initBoard(board);
    //method initShips is triggered to fill the position of the 5 ships (row 
   and column)
    initShips(ships);

    System.out.println();

    //the game begins using a do...while loop, game goes on until the player 
hits the 5 ships
    do{
        showBoard(board);
        shoot(shoot);
        attempts++;

        if(hit(shoot,ships)){
            hint(shoot,ships,attempts);
            shotHit++;
        }                
        else
            hint(shoot,ships,attempts);

        changeboard(shoot,ships,board);

    //condition of the loop is "shotHit!=5'
    }while(shotHit!=5);

    System.out.println("\n\n\nWell done soldier! You've destroyed 5 enemy 
   ships in "+attempts+" attempts");
    showBoard(board);
}
//sets the value -1 in all blocks of the board
public static void initBoard(int[][] board){
    for(int row=0 ; row < 7 ; row++ )
        for(int column=0 ; column < 7 ; column++ )
            board[row][column]=-1;
}
//gets the int matrix and shows the board game
public static void showBoard(int[][] board){
    System.out.println("\t1 \t2 \t3 \t4 \t5 \t6 \t7");
    System.out.println();

    for(int row=0 ; row < 7 ; row++ ){
        System.out.print((row+1)+"");
        for(int column=0 ; column < 7 ; column++ ){
            if(board[row][column]==-1){
                System.out.print("\t"+"~");
            }else if(board[row][column]==0){
                System.out.print("\t"+"*");
            }else if(board[row][column]==1){
                System.out.print("\t"+"X");
            }

        }
        System.out.println();
    }

}

//this method randomly select 5 pairs of integers numbers, which are the 
location of the 5 ships
public static void initShips(int[][] ships){
    Random random = new Random();

    for(int ship=0 ; ship < 5 ; ship++){
        ships[ship][0]=random.nextInt(7);
        ships[ship][1]=random.nextInt(7);

        //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(7);
                    ships[ship][1]=random.nextInt(7);
                }while( (ships[ship][0] == ships[last][0])&&(ships[ship][1] 
                 == ships[last][1]) );
        }

    }
}

//gets a shot (row and column) of the user, and stores in variable shot []
 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]--;

}

//checks if given shot hit a ship
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("What a SOLDIER! You hit an enemy ship located 
     in (%d,%d) with a hellstorm missle\n",shoot[0]+1,shoot[1]+1);
            return true;
        }
    }
    return false;
}

//give a hint of how many ships are in that row and that column where the 
shot was given
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);
}

//after the shot is given, the board is changed, showing that the shot was 
give (if hit or missed)
public static void changeboard(int[] shoot, int[][] ships, int[][] board){
    if(hit(shoot,ships))
        board[shoot[0]][shoot[1]]=1;
    else
        board[shoot[0]][shoot[1]]=0;
}
}

Upvotes: 1

Views: 97

Answers (1)

Ben
Ben

Reputation: 1695

Your output is the default System.out. This means that there needs to be some entity catching that output and displaying it to the user.

If you run your .jar file in the cmd the cmd is acting as that entity.

If you just double click the .jar there is noone catching the output - therefore it's simply discarded. Your application is still running, the output is just not shown.

How to fix it? Either use a method of output other than System.out, for example a text file, or make it clear that the application can only be run from the command line (which is a perfectly fine thing to do).

You could also create a simple .bat file that opens a cmd and runs your .jar in it if you don't want to have the user manually execute command line commands.

Upvotes: 1

Related Questions