When one gamer is eliminated from the word chain game, it is implemented to remove the number of people, but it is not implemented with one gamer missing.

Asked 2 years ago, Updated 2 years ago, 22 views

import java.util.Scanner;

class Player{ String name; String answer; static String word = "Father"; static int lastIndex = word.length()-1;

void showPlayerName() {
    System.out.println(name);
}

void showPlayerAnswer() {
    System.out.println(answer);
}

 int checkSuccess(String answer, char firstChar) {
    if(word.charAt(lastIndex) == firstChar) {
        word = answer;
        lastIndex = word.length() - 1;
        return 1;
    }
    else {
        word = "father";
        return 0;
    }
}

}

public class WorldGameApp {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println ("Enter the number of participants: ");
    int gamers_num = sc.nextInt();
    Player player[] = new Player[gamers_num];

    for(int i = 0 ; i < gamers_num ; i++) {
        System.out.println("\n" + (i+1) + "Enter the name of the first competitor: ");
        player[i] = new Player();
        player[i].name = sc.next();
    }

    System.out.println("==========================");
    System.out.println ("Start Word Chain, Start Word: Father");

    while(true) {

        if(gamers_num == 1) 
            break;


        else {

            for(int i = 0 ; i  < gamers_num ; i++) {
                System.out.println("==========================\n");
                System.out.println(player[i].name + "Turn");

                System.out.println ("Enter the following words: ");
                player[i].answer = sc.next();
                String Answer = player[i].answer;
                char Firstchar = Answer.charAt(0);

                if((player[i].checkSuccess(Answer, Firstchar)) == 1)
                    continue;

                else {
                    System.out.println(player[i].name + "Out");                      
                    For(int j = i; j < gamers_num; j++) { //Remove from the dropout array if one person is eliminated
                        player[j] = player[j+1];
                    }
                    System.out.println("==========================");
                    System.out.println ("Start Word Chain, Start Word: Father");

It prints up to here, as you can see in the for statement above `` Let's put the next person in the eliminated spot and reduce the number of people The player class array saves the eliminated person without it Why don't we play the next game? Is there a problem?

gamers_num--; // Decrease the number of gamers by one

                }

            }
        }   
    }
}

}

java

2022-09-20 17:02

1 Answers

I didn't turn the source above, so it's hard to answer in detail, but just to read it roughly, the problem is in int gamers_num.

The logic goes around for based on the value of gamers_num and checks if the game can be played. At some point, you will attempt to "drop" the player[i]. Even so, the only thing that really happens is that player[i] does not disappear from player but just gamers_num--;. In other words... The player doesn't really come down from the ring, just the number of billboards seems to be changing.

I don't know Java well, so I can't write specific code, but if you ask me to rearrange it, I'll define one getNextPlayer(currentPlayer) method and process it. If this method returns an instance of Player, you can do something about that instance, and if it is null, you can call break. And the getNextPlayer() method will be a method that will exclusively determine and return the next player if currentPlayer is on the ring as a factor of who is on the ring.

I hope it's useful.


2022-09-20 17:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.