2016-12-15 42 views
-2

私はいくつかの助けを得ることを望んでいた。私は戦艦のjavaゲームを持っていて、プレイヤーに終了時に再びプレイするオプションを与えたいと思っていましたが、実際にどのようにしたらいいかわかりません。どんな助けも素晴らしいでしょう!Javaプログラムを再起動

public class Battle { 

public static void main(String[] args) throws IOException{ 

    int[][] board = new int[5][5]; 
    int[][] ships = new int[3][2]; 
    int[] shoot = new int[2]; 
    int size;//for the fleet, under construction! 
    int health= 3; 
    int attempts=0, 
     shotHit=0; 
    boolean sendout = false; 

    //reads the rules 

    File myFile = new File ("Rules.txt"); 
    Scanner inputFile = new Scanner(myFile); 

    while (inputFile.hasNext()) 
     { 
      String str = inputFile.nextLine(); 
      System.out.println(str); 
     } 
    inputFile.close(); 


    Scanner position = new Scanner(System.in); 

    whatShip ship = new whatShip(health); 
    health = ship.WhatShip(health); 



    System.out.println("\n"); 
    System.out.println("Enter the name of your vessel:"); 
    String name = position.nextLine(); 

    //intro read 

    System.out.println("\n"); 
    System.out.println("INTRO"); 
    System.out.println("\n"); 
    System.out.println("Onbard the "+name); 
    File myFile1 = new File ("Intro.txt"); 
    Scanner inputFile1 = new Scanner(myFile1); 

    while (inputFile1.hasNext()) 
    { 
     String str = inputFile1.nextLine(); 
     System.out.println(str); 
    } 
    inputFile1.close(); 
    System.out.println("\n"); 



    int row = entryIntMinMax("Enter "+name+"'s longitude, row", 1, 5); 





    int col = entryIntMinMax("Enter "+name+"'s latitude, column",1,5); 





    System.out.println("ALL HANDS, MAN YOUR STATIONS"); 

    initBoard(board); 
    initShips(ships); 

    System.out.println(); 

    do{ 
     showBoard(board); 
     shoot(shoot); 
     attempts++; 

     if(hit(shoot,ships)){ 
      hint(shoot,ships,attempts); 
      shotHit++; 
     }     
     else 
      hint(shoot,ships,attempts); 
     health =enemyShoot(row, col, health); 
     health =enemyShoot(row, col, health); 
     health =enemyShoot(row, col, health); 
     health =enemyShoot(row, col, health); 
     health =enemyShoot(row, col, health); 
     health =enemyShoot(row, col, health); 
     health =enemyShoot(row, col, health); 

     changeboard(shoot, ships, board); 


    } 


    while(shotHit!=3); 

    System.out.println("\n\n Enemy fleet destroyed sir! You sunk 3 ships in "+attempts+" attempts"); 
    showBoard(board); 
} 

これは私の主な方法です。どんな批判も素晴らしいだろう!

+4

私はすべてのものを大きな単一の 'main'メソッドに書くのではなく、' Object'を作成し、 'methods'と' variables'を使ってstart、pause、endを作成します。ゲームを再開したり、好きなもののステータスを設定することができます。プロセス全体を再起動するために、どこかで 'main'を呼び出しているのは、おそらくあなたが望むものではなく、現在しなければならないことです。 – SomeJavaGuy

+1

@ KevinEscheはい私はそれをアップグレードし、すべてをメソッドに移すことに取り組んでいます。主なものは150行以上のコードでした。しかし、私は技術者にゲームを再開する方法を知らせる方法を知らない。 – ChunkierLizard

+0

ゲームが終了すると、次のラウンドを求める質問が表示されます。答えが "はい"ならば、あなたは 'main(args);'を呼んでもう一度実行することができます。メソッドで作業したい場合は、ループでゲームを実行し、最後にメソッドを呼び出して再起動します。その方法でいくつかの条件を設定し、答えに応じて、ループが続くかどうか – XtremeBaumer

答えて

0

まず、introPhase()メソッドなどで、上記のコードの最初のセクション(do-whileステートメントまで)に(ほとんど)すべてを入れてください。

第2に、do-whileを第2のgamePhase()方法などに入れてください。

最後に、何らかの条件付きループステートメントで両方を囲みます。

あなたはこのようなもので終わる必要があります。

boolean finishFlag = false; 
while(!finishFlag) { 
    introPhase(); 
    gamePhase(); 
    finishFlag = getExitInfoFromUser(); 
} 

幸運を!

0

どのように見えるかについて、さらに抽象的で簡単な例を挙げています。

プログラミングしているものを扱いやすくするために、クラス、メソッド、および変数を定義します。

// Game class, here you define what the game would look like 
public class Game { 
    private Scanner scanner = new Scanner(System.in); 
    // Could probably create a class Board and work on this class. 
    // private Board board; 
    private int[][] board; 
    // Could probably create a class Ship and work on this class. 
    // Could also include the two dimensional array in the Board class 
    // private Ship[][] ships board; 
    private int[][] ships; 
    // Could probably create a class Player and work on this class. 
    // private Player board; 
    private int attemtps; 
    private int health; 

    // Define a status the game is currently in 
    private enum Status { 
     IDLE, STARTED, ENDED; 
    } 

    // Variable for the current game status 
    private Status gamestatus; 

    // Nothing done, default is idle when new game class is initialized 
    // Maybe you have options and stuff and a menu, where you IDLE at. 
    public Game() { 
     gamestatus = Status.IDLE; 
    } 

    // This method "starts" the game, though defines variables to 
    // have Game beeing started 
    public void start() { 
     System.out.println("Welcome to battleship"); 
     // Change status, maybe you want to validate the current gamestatus 
     // at some point 
     gamestatus = Status.STARTED; 
     // this method could initialize some variables and stuff to default values in order to represent a fresh game 
     setInitialValuesOfGame(); 
     // Random loop that gets 5 inputs and emulates a game due to this. 
     int i = 0; 
     while(gamestatus.equals(Status.STARTED)) { 
      System.out.println("Test input: "); 
      String input = scanner.nextLine(); 
      // Just make it look like there would happen something 
      board[i%5][0] = i; 
      ships[i%3][0] = i; 
      // Make game look like it did end after a few iteration 
      ++i; 
      if(i == 5) { 
       end(); 
      } 
     } 
     // Loop is over, ask for restart 
     restart(); 
    } 

    public void restart() { 
     System.out.println("Hey would you like to restart: yes"); // Making it look like input 
     String input = "yes"; // Input here, but make it a literal for showing purposes 
     if (input.equals("yes")) { 
      start(); 
     } else { 
      System.out.println("bye bye"); 
     } 
    } 

    public void end() { 
     gamestatus = Status.ENDED; 
     // Do more stuff that would make the game end 
    } 

    // Setting default values 
    private void setInitialValuesOfGame() { 
     board = new int[5][5]; 
     ships = new int[3][2]; 
     attemtps = 0; 
     health = 3; 
    } 

    // This is just your starting point, i´d programm as less as possible in here. 
    public static void main(String[] args) { 
     Game game = new Game(); 
     game.start(); 
    } 
} 
関連する問題