Cześć, jak w temacie mam problem z poprawnym zresetowaniem gry. Gra niby się resetuje, ale nie od początku (nie można wpisać imienia) i dodatkowo miesza instrukcje... :/
Kod wygląda tak:
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Menu menu = new Menu();
menu.mainMenu();
Game game = new Game();
game.gameInit();
game.countScores();
menu.endMenu();
}
}
class Menu {
public static String name;
public static int rounds;
private final String gameEnd = "x";
private final String restartGame = "n";
private static final Scanner scanner = new Scanner(System.in);
public void mainMenu() {
System.out.print("Please enter your name: ");
name = scanner.nextLine();
System.out.print("Welcome in paper, rock, scisors game " + name + ".\nPlease enter quantity of rounds: ");
rounds = scanner.nextInt();
}
public void endMenu() {
System.out.print(name + ", if you want to end the game, please enter x mark.\nIf you want to restart the game, please enter n: ");
String choice = scanner.next();
if (choice.equals(gameEnd)) {
System.out.println("The Game has been ended.");
System.exit(0);
}
if (choice.equals(restartGame)) {
System.out.println("The game has been restarted.");
Main.main(null);
}
}
}
class Game extends Menu {
private final int rock = 1;
private final int paper = 2;
private final int scisors = 3;
private int roundLaps = 0;
private int playerResult = 0;
private int computerResult = 0;
private boolean end = false;
private static final Scanner scanner = new Scanner(System.in);
private static final Random random = new Random();
//private static final String[] atributes = {"rock", "paper", "scisors"};
public void gameInit() {
System.out.println("1 = rock");
System.out.println("2 = paper");
System.out.println("3 = scisors");
while (!end) {
roundLaps++;
System.out.print(name + ", your move: ");
int playerMove = scanner.nextInt();
//System.out.println(name + " chosen: " + (atributes[playerMove]));
if (playerMove > scisors || playerMove < rock) {
System.out.print(name + ", please enter number from 1 to 3: ");
playerMove = scanner.nextInt();
//System.out.println(name + " chosen: " + (atributes[playerMove]));
}
int computerMove = random.nextInt(3) + 1;
//System.out.println("Now is computer move.\nComputer chose: " + atributes[computerMove]);
if (playerMove == computerMove) {
System.out.println("Round: " + roundLaps + " Draw.");
} else if (playerMove == rock && computerMove == paper) {
System.out.println("Round: " + roundLaps + " Computer win.");
computerResult++;
} else if (playerMove == paper && computerMove == rock) {
System.out.println("Round: " + roundLaps + ' ' + name + " win.");
playerResult++;
} else if (playerMove == paper && computerMove == scisors) {
System.out.println("Round: " + roundLaps + " Computer win.");
computerResult++;
} else if (playerMove == scisors && computerMove == paper) {
System.out.println("Round: " + roundLaps + ' ' + name + " win.");
playerResult++;
} else if (playerMove == rock && computerMove == scisors) {
System.out.println("Round: " + roundLaps + ' ' + name + " win.");
playerResult++;
} else {
System.out.println("Round: " + roundLaps + " Computer win.");
computerResult++;
}
if (roundLaps == rounds) {
end = true;
}
}
}
public void countScores() {
System.out.println(name + " wins: " + playerResult);
System.out.println("Computer wins: " + computerResult);
if (computerResult == playerResult) {
System.out.println("No ones win.");
} else if (computerResult > playerResult) {
System.out.println("Computer is the winner!");
} else {
System.out.println(name + " is the winner!");
}
}
}
Ktoś wie jak zaradzić? :)
- Bez tytułu.jpg (83 KB) - ściągnięć: 5