Я написал Java-код для реализации игры с числами, в которой также задействованы уровни сложности. Пожалуйста, просмотрите этот код и предложите улучшения, которые можно внести в этот код (все, включая отступы). Как я могу улучшить качество своего кода и чего мне следует избегать при его написании?
import java.util.Scanner;
public class numGamee {
static Scanner sc = new Scanner(System.in);
static int choose, choice;
static String[] difficulty = { "Easy", "Medium", "Hard" };
static int score = 0;
public static void gameStart() {
String[] startMenu = { "Start", "Quit" };
int index;
System.out.println(
"Welcome to WildGuess, the number guessing game! Please choose an option(Type the number to choose the option):");
for (index = 0; index < startMenu.length; index++) {
System.out.println((index + 1) + " => " + startMenu[index]);
}
choose = sc.nextInt();
}
public static void chooseDifficulty() {
int indices;
System.out.println("Please type the number for the difficulty you want to select:");
for (indices = 0; indices < difficulty.length; indices++) {
System.out.println((indices + 1) + " => " + difficulty[indices]);
}
choice = sc.nextInt();
}
public static void playGame() {
if (choice == 1) {
System.out.println(" You have chosen " + difficulty[0] + " level");
int randomNum = (int) (Math.random() * 51);
System.out.println("As you have chosen " + difficulty[0]
+ " difficulty, a random number has been chosen between 0-50. Guess the number to win the game!");
System.out.println("Please type in your guess number:");
int guessNum = sc.nextInt();
while (guessNum != randomNum) {
if (guessNum > randomNum) {
System.out.print(
"Too high! The number is lower than " + guessNum + " .Please guess the number again:");
guessNum = sc.nextInt();
} else if (guessNum < randomNum) {
System.out.print(
"Too low! The number is higher than " + guessNum + " .Please guess the number again:");
guessNum = sc.nextInt();
}
}
if (guessNum == randomNum) {
System.out.println(" Bravo! You have guessed the correct number!");
score++;
System.out.println("Your score is now " + score);
}
} else if (choice == 2) {
System.out.println(" You have chosen " + difficulty[1] + " level");
System.out.println("As you have chosen " + difficulty[1]
+ " level, a random number has been chosen between 1-100. Guess the number to win the game!");
int randomNum = (int) (Math.random() * 101);
System.out.print("Please type in your guess number:");
int guessNum = sc.nextInt();
while (guessNum != randomNum) {
if (guessNum > randomNum) {
System.out.print(
"Too high! The number is lower than " + guessNum + ".Please enter the number again:");
guessNum = sc.nextInt();
} else if (guessNum < randomNum) {
System.out.print(
"Too low! The number is higher than " + guessNum + ". Please enter the number again:");
guessNum = sc.nextInt();
}
}
if (guessNum == randomNum) {
System.out.println("Bravo!you have guessed the correct number!");
score++;
System.out.println("Your score is now " + score);
}
} else if (choice == 3) {
System.out.println(" You have chosen " + difficulty[2] + " level");
System.out.println("As you have chosen " + difficulty[2]
+ " level, a random number has been chosen between 1-500. Guess the number to win the game!");
int randomNum = (int) (Math.random() * 501);
System.out.print("Please type in your guess number:");
int guessNum = sc.nextInt();
while (guessNum != randomNum) {
if (guessNum > randomNum) {
System.out.print(
"Too high! The number is lower than " + guessNum + ".Please enter the number again:");
guessNum = sc.nextInt();
} else if (guessNum < randomNum) {
System.out.print(
"Too low! The number is higher than " + guessNum + ". Please enter the number again:");
guessNum = sc.nextInt();
}
}
if (guessNum == randomNum) {
System.out.println("Bravo!you have guessed the correct number!");
score++;
System.out.println("Your score is now " + score);
} else {
System.out.println("Incorrect input!");
}
}
}
public static void playAgain() {
System.out.println("Do you want to play the game again?(Type 1 for yes and 2 for no)");
int userPrompt = sc.nextInt();
boolean playAgain = true;
while (playAgain) {
if (userPrompt == 1) {
chooseDifficulty();
playGame();
System.out.println("Do you want to play the game again?(Type 1 for yes and 2 for no)");
userPrompt = sc.nextInt();
} else if (userPrompt == 2) {
playAgain = false;
System.out.println("Thank you for playing the game. Hope you enjoyed!");
}
}
}
public static void main(String[] args) {
try {
gameStart();
if (choose == 1) {
System.out.println("Let's begin!");
chooseDifficulty();
playGame();
playAgain();
} else if (choose == 2) {
System.out.println("Thank you for your time");
} else {
System.out.println("Error, wrong input");
}
} catch (Exception e) {
System.out.println("Please enter a numerical input!");
}
}
}
Я написал Java-код для реализации игры с числами, в которой также задействованы уровни сложности. Пожалуйста, просмотрите этот код и предложите улучшения, которые можно внести в этот код (все, включая отступы). Как я могу улучшить качество своего кода и чего мне следует избегать при его написании? [code]import java.util.Scanner;
public class numGamee { static Scanner sc = new Scanner(System.in); static int choose, choice; static String[] difficulty = { "Easy", "Medium", "Hard" }; static int score = 0;
public static void gameStart() { String[] startMenu = { "Start", "Quit" }; int index; System.out.println( "Welcome to WildGuess, the number guessing game! Please choose an option(Type the number to choose the option):"); for (index = 0; index < startMenu.length; index++) { System.out.println((index + 1) + " => " + startMenu[index]); } choose = sc.nextInt(); }
public static void chooseDifficulty() { int indices; System.out.println("Please type the number for the difficulty you want to select:"); for (indices = 0; indices < difficulty.length; indices++) { System.out.println((indices + 1) + " => " + difficulty[indices]); } choice = sc.nextInt(); }
public static void playGame() { if (choice == 1) { System.out.println(" You have chosen " + difficulty[0] + " level"); int randomNum = (int) (Math.random() * 51); System.out.println("As you have chosen " + difficulty[0] + " difficulty, a random number has been chosen between 0-50. Guess the number to win the game!"); System.out.println("Please type in your guess number:"); int guessNum = sc.nextInt(); while (guessNum != randomNum) { if (guessNum > randomNum) { System.out.print( "Too high! The number is lower than " + guessNum + " .Please guess the number again:"); guessNum = sc.nextInt(); } else if (guessNum < randomNum) { System.out.print( "Too low! The number is higher than " + guessNum + " .Please guess the number again:"); guessNum = sc.nextInt(); } } if (guessNum == randomNum) { System.out.println(" Bravo! You have guessed the correct number!"); score++; System.out.println("Your score is now " + score); } } else if (choice == 2) { System.out.println(" You have chosen " + difficulty[1] + " level"); System.out.println("As you have chosen " + difficulty[1] + " level, a random number has been chosen between 1-100. Guess the number to win the game!"); int randomNum = (int) (Math.random() * 101); System.out.print("Please type in your guess number:"); int guessNum = sc.nextInt(); while (guessNum != randomNum) { if (guessNum > randomNum) { System.out.print( "Too high! The number is lower than " + guessNum + ".Please enter the number again:"); guessNum = sc.nextInt(); } else if (guessNum < randomNum) { System.out.print( "Too low! The number is higher than " + guessNum + ". Please enter the number again:"); guessNum = sc.nextInt(); } } if (guessNum == randomNum) { System.out.println("Bravo!you have guessed the correct number!"); score++; System.out.println("Your score is now " + score); } } else if (choice == 3) { System.out.println(" You have chosen " + difficulty[2] + " level"); System.out.println("As you have chosen " + difficulty[2] + " level, a random number has been chosen between 1-500. Guess the number to win the game!"); int randomNum = (int) (Math.random() * 501); System.out.print("Please type in your guess number:"); int guessNum = sc.nextInt(); while (guessNum != randomNum) { if (guessNum > randomNum) { System.out.print( "Too high! The number is lower than " + guessNum + ".Please enter the number again:"); guessNum = sc.nextInt(); } else if (guessNum < randomNum) { System.out.print( "Too low! The number is higher than " + guessNum + ". Please enter the number again:"); guessNum = sc.nextInt(); } } if (guessNum == randomNum) { System.out.println("Bravo!you have guessed the correct number!"); score++; System.out.println("Your score is now " + score); } else { System.out.println("Incorrect input!"); } } }
public static void playAgain() { System.out.println("Do you want to play the game again?(Type 1 for yes and 2 for no)"); int userPrompt = sc.nextInt(); boolean playAgain = true; while (playAgain) { if (userPrompt == 1) { chooseDifficulty(); playGame(); System.out.println("Do you want to play the game again?(Type 1 for yes and 2 for no)"); userPrompt = sc.nextInt(); } else if (userPrompt == 2) { playAgain = false; System.out.println("Thank you for playing the game. Hope you enjoyed!"); } } }
public static void main(String[] args) { try { gameStart(); if (choose == 1) { System.out.println("Let's begin!"); chooseDifficulty(); playGame(); playAgain(); } else if (choose == 2) { System.out.println("Thank you for your time"); } else { System.out.println("Error, wrong input"); } } catch (Exception e) { System.out.println("Please enter a numerical input!"); } } } [/code] Пожалуйста, помогите мне улучшить этот код.
Я делаю игру под названием «Понг» на Python и черепахе (модуль) и хочу сделать ее многопользовательской игрой по локальной сети.
Может кто-нибудь сказать мне, как мне это сделать? .
Я хочу сделать эту игру похожей на локальный хост, чтобы я и мой...
Я написал простую игру на основе консоли в C#, где:
Я загружаю прямоугольную карту (palya.txt), содержащую стены (x), floor (), начало игрока (O), выход (t) и несколько заключенных (*)
Переходит в совет, и они выполняют, что они проходят в виде...
Я создал игру, в которой два ИИ-персонажа играют друг против друга. Эта игра запускается с использованием CLI, ./game . Игра будет запущена при запросе на сервер.
Теперь я хочу транслировать эту игру на конечную точку rtmp. Как мне достичь этой...