Java - Need help to get random question from an array for my text based game #145247
-
Bodyimport java.util.Scanner;
import java.util.Random;
public class Main{
// The game variables.
public static int player1_score = 0;
public static int player2_score = 0;
public static String player1_name;
public static String player2_name;
public static int times_handled_input_called = 0;
public static boolean gameover = false;
public static Scanner scanner;
public static boolean proper_input = false;
public static int player_lives = 10;
public static int enemy_lives = 10;
public static String player1_guess;
public static String enemy_guess;
public static String[] questions = {"What is 5+5?"};
public static Random random;
/**
* @description This function compares player 1's score and player two's score to see who won
**/
public static void compare_score(){
if(player1_score > player2_score || player_lives > 0 && enemy_lives == 0){
handle_output("Player one won!");
}else if(player2_score > player1_score || enemy_lives > 0 && player_lives == 0){
handle_output("Player two won!");
}else{
handle_output("Tie!");
}
play_again();
}
/**
* @description This function will increment player one's score
**/
public static void increment_player1_score(){
if(player2_score <= 10){
player1_score++;
}else{
compare_score();
}
}
public static void play_again(){
handle_output("Do you want to play again?");
Scanner scanner = new Scanner(System.in);
String user_choice = scanner.nextLine().toLowerCase();
if(user_choice.equals("y")){
gameover = false;
player1_score = 0;
player2_score = 0;
main(null);
}else{
gameover = true;
}
}
/**
* @description This function will increment player two's score
**/
public static void increment_player2_score(){
if(player2_score <= 10){
player2_score++;
decrement_player1_score();
}else{
compare_score();
}
}
/**
* @description This function will decrement player 1's score
**/
public static void decrement_player1_score(){
if(player1_score > 0 && player_lives > 0){
player1_score--;
increment_player2_score();
}
}
/**
* @description This function will decrement player 2's score
**/
public static void decrement_player2_score(){
if(player2_score > 0 && enemy_lives > 0){
player2_score--;
increment_player1_score();
}else{
compare_score();
}
}
public static void generate_random_question(){
}
public static void start_game(){
handle_output(player1_name + " Score: " + player1_score);
handle_output("");
handle_output(player2_name + " Score: " + player2_score);
handle_output("");
handle_output(player1_name + " Lives: " + player_lives);
handle_output("");
handle_output(player2_name + " Lives: " + enemy_lives);
generate_random_question();
}
public static void handle_input(){
times_handled_input_called++;
scanner = new Scanner(System.in);
if(times_handled_input_called == 1){
player1_name = scanner.nextLine();
}else{
player2_name = scanner.nextLine();
}
// Check if the input isn ot empty
if(player1_name != "" && player2_name != ""){
proper_input = true;
}
}
/**
* @description This function handles output
* @param {Object} output
* @outputs {Object} output
**/
public static void handle_output(Object output){
System.out.println(output);
}
/**
* @description This function decrements the player's lives
* @calls play_again if the player lives is not greater than zero
**/
public static void decrement_player_lives(){
if(player_lives > 0){
player_lives--;
increment_enemy_lives();
}else{
compare_score();
}
}
/**
* @description This function increments the player's lives if the player's lives are
* less than 11
**/
public static void increment_player_lives(){
if(player_lives < 11){
player_lives++;
decrement_enemy_lives();
}
}
/**
* @description This function increments the enemy's lives if the enemy's lives are
* less than 11
**/
public static void increment_enemy_lives(){
if(enemy_lives < 11){
enemy_lives++;
decrement_player_lives();
}
}
/**
* @description This function decrements the enemy's lives if the enemy's lives are greater
* than 11
**/
public static void decrement_enemy_lives(){
if(enemy_lives > 0){
enemy_lives--;
increment_player_lives();
}else{
compare_score();
}
}
public static void main (String[] args){
while(gameover == false){
if(proper_input == false){
handle_output("Player 1 enter your name: ");
handle_input();
handle_output("Player 2 enter your name: ");
handle_input();
}
start_game();
}
}
} Guidelines
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Since you're using the java.util.random library, you'll need to first initialize a Random object. Once you've done that, and assuming you are adding your questions to the You can use the nextInt method to pass in the length of your questions array, so that it returns a random integer between 0 and the length of your array. You can use this as a random index to pull a question, i.e. questions[randomIndex] I'm linking the docs here as well for you to read over yourself if you need any further information |
Beta Was this translation helpful? Give feedback.
-
Hi @DumbestPerson224 👋🏾,It looks like @dylan-sloan's response was the answer to your question here. Can you please mark their response as the answer? When you mark a question as an answer, GitHub will highlight the comment and replies to the comment to help visitors quickly find the answer. |
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
import java.util.Random;
public class Main {
// Game variables
public static int player1_score = 0;
public static int player2_score = 0;
public static String player1_name;
public static String player2_name;
public static boolean gameover = false;
public static boolean proper_input = false;
public static int player_lives = 10;
public static int enemy_lives = 10;
public static String[] questions = {
"What is 5 + 5?",
"What is the capital of France?",
"Who wrote 'Hamlet'?",
"What is 12 * 8?",
"What is the square root of 64?"
};
public static String[] answers = {"10", "paris", "shakespeare", "96", "8"};
public static Scanner scanner = new Scanner(System.in);
public static Random random = new Random();
/**
* Compares scores and determines the winner.
*/
public static void compare_score() {
if (player1_score > player2_score || (player_lives > 0 && enemy_lives == 0)) {
handle_output(player1_name + " won!");
} else if (player2_score > player1_score || (enemy_lives > 0 && player_lives == 0)) {
handle_output(player2_name + " won!");
} else {
handle_output("It's a tie!");
}
play_again();
}
/**
* Resets the game or exits based on the user's choice.
*/
public static void play_again() {
handle_output("Do you want to play again? (y/n)");
String user_choice = scanner.nextLine().toLowerCase();
if (user_choice.equals("y")) {
reset_game();
main(null);
} else {
gameover = true;
}
}
/**
* Resets game variables.
*/
public static void reset_game() {
player1_score = 0;
player2_score = 0;
player_lives = 10;
enemy_lives = 10;
gameover = false;
proper_input = false;
}
/**
* Generates a random question and checks the answers.
*/
public static void generate_random_question() {
int questionIndex = random.nextInt(questions.length);
String selectedQuestion = questions[questionIndex];
String correctAnswer = answers[questionIndex];
handle_output(selectedQuestion);
handle_output(player1_name + ", your answer: ");
String player1_answer = scanner.nextLine().trim().toLowerCase();
handle_output(player2_name + ", your answer: ");
String player2_answer = scanner.nextLine().trim().toLowerCase();
// Evaluate player 1's answer
if (player1_answer.equals(correctAnswer)) {
handle_output(player1_name + " got it right!");
increment_player1_score();
} else {
handle_output(player1_name + " got it wrong!");
decrement_player_lives();
}
// Evaluate player 2's answer
if (player2_answer.equals(correctAnswer)) {
handle_output(player2_name + " got it right!");
increment_player2_score();
} else {
handle_output(player2_name + " got it wrong!");
decrement_enemy_lives();
}
}
public static void increment_player1_score() {
player1_score++;
}
public static void increment_player2_score() {
player2_score++;
}
public static void decrement_player_lives() {
player_lives--;
if (player_lives <= 0) {
compare_score();
}
}
public static void decrement_enemy_lives() {
enemy_lives--;
if (enemy_lives <= 0) {
compare_score();
}
}
/**
* Displays current game status and starts the round.
*/
public static void start_game() {
handle_output(player1_name + " Score: " + player1_score + ", Lives: " + player_lives);
handle_output(player2_name + " Score: " + player2_score + ", Lives: " + enemy_lives);
generate_random_question();
}
/**
* Handles player input for names.
*/
public static void handle_input() {
handle_output("Player 1, enter your name: ");
player1_name = scanner.nextLine().trim();
handle_output("Player 2, enter your name: ");
player2_name = scanner.nextLine().trim();
if (!player1_name.isEmpty() && !player2_name.isEmpty()) {
proper_input = true;
} else {
handle_output("Names cannot be empty. Please try again.");
handle_input();
}
}
/**
* Outputs a message to the console.
*/
public static void handle_output(Object output) {
System.out.println(output);
}
public static void main(String[] args) {
if (!proper_input) {
handle_input();
}
while (!gameover) {
start_game();
}
handle_output("Thanks for playing!");
}
} optimized code for your game. it will ask random questions and continue playing.. |
Beta Was this translation helpful? Give feedback.
Since you're using the java.util.random library, you'll need to first initialize a Random object. Once you've done that, and assuming you are adding your questions to the
questions[]
array:You can use the nextInt method to pass in the length of your questions array, so that it returns a random integer between 0 and the length of your array. You can use this as a random index to pull a question, i.e. questions[randomIndex]
I'm linking the docs here as well for you to read over yourself if you need any further information