Following some exercises on objected-oriented coding with Java, I was prompted to create a "Guess the Number" game where all the contestants are actually CPU's but are objects of some "Player" class.
The result is printed out after one or more players guessed the target number targetNumber. All guesses are made with (int) (Math.random() * 50) and there is no specific order on how the names are listed: just the first person, followed by the second, then the third and fourth person. Though I do not specifically have the second line of the output to be of the person who won, it always is the person who won (and maybe a tie from the player below).
Example Output:
It took 108 guesses for someone to guess the number 43.
Did Tsubasa guess the right number: true
Did Melissa guess the right number: false
Did Ariana guess the right number: false
Did Gomez guess the right number: false
As you can see, the first player mentioned, Tsubasa, was the winner. This happens every single time (names are randomly chosen).
The code:
package bored_tony;
public class GuessTheNumber {
String[] names = {"Rob", "Tony", "Dennis", "George", "Tom",
"Melissa", "Audrey", "Ariana", "Selena", "Nicole",
"Bill", "Reece", "Rishab", "Tsubasa", "Gomez",
"Maddie", "Mary", "Maya", "Chloe", "Ashley"};
Player p1 = new Player();
Player p2 = new Player();
Player p3 = new Player();
Player p4 = new Player();
String name_1 = names[(int) (Math.random() * 20)];
String name_2 = names[(int) (Math.random() * 20)];
String name_3 = names[(int) (Math.random() * 20)];
String name_4 = names[(int) (Math.random() * 20)];
public static void main(String[] args) {
GuessTheNumber game = new GuessTheNumber();
game.play();
}
public void play() {
while (true) {
if (name_1.equals(name_2)) {
name_2 = names[(int) (Math.random() * 20)];
}
if (name_1.equals(name_3)) {
name_3 = names[(int) (Math.random() * 20)];
}
if (name_1.equals(name_4)) {
name_4 = names[(int) (Math.random() * 20)];
}
if (name_2.equals(name_3)) {
name_3 = names[(int) (Math.random() * 20)];
}
if (name_1.equals(name_4)) {
name_4 = names[(int) (Math.random() * 20)];
}
if (name_3.equals(name_4)) {
name_4 = names[(int) (Math.random() * 20)];
}
break;
}
boolean p1_is_Right = false;
boolean p2_is_Right = false;
boolean p3_is_Right = false;
boolean p4_is_Right = false;
int targetNumber = (int) (Math.random() * 50);
System.out.println("The target number is " + targetNumber);
System.out.println("------------------------------");
int iterations = 0;
while (true) {
iterations += 1;
p1.Guess(name_1);
p2.Guess(name_2);
p3.Guess(name_3);
p4.Guess(name_4);
System.out.println("------------------------------");
if (p1.number == targetNumber) {
p1_is_Right = true;
if (p2.number == targetNumber) {
p2_is_Right = true;
}
if (p3.number == targetNumber) {
p3_is_Right = true;
}
if (p4.number == targetNumber) {
p4_is_Right = true;
}
if (p1_is_Right || p2_is_Right || p3_is_Right || p4_is_Right) {
System.out.println("It took " + iterations + " guesses for someone to guess the number " + targetNumber + ".");
System.out.println("Did " + name_1 + " guess the right number: " + p1_is_Right);
System.out.println("Did " + name_2 + " guess the right number: " + p2_is_Right);
System.out.println("Did " + name_3 + " guess the right number: " + p3_i s_Right);
System.out.println("Did " + name_4 + " guess the right number: " + p4_is_Right);
break;
}
}
}
}
}
class Player {
int number = 0;
public void Guess(String name) {
number = (int) (Math.random() * 50);
System.out.println(name + " guessed that the number was: " + number);
}
}
So why is the first player always one of the players that wins the game? Note: Since I am using NetBeans, there is the package Bored_Tony; at the top.
Aucun commentaire:
Enregistrer un commentaire