Reputation: 17
I have been trying a basic program that replicates the game called mastermind. I have been able to so far get 4 data points, and 4 user inputs as an integer, but I have a problem comparing 4 data points and the 4 user inputs. The goal is the is there are 4 rngs(ranging from 1-9) and the user guesses 4 numbers at a time. The only hint that he is given is that one number is correct. So far I have been able to make an inefficient code that is set to compare 4 variables, ex( a=rng1, b=rng2, c=rng3, d=rng4) and the user input similarly. How do I efficiently compare the user inputs to all of the rngs that have generated.In this instance I want to be able to compare s1 to poop; poop2; poop3; and poop4. I don't want to write another 100 lines of code to do that. Thanks in advance. Code is below.
import java.util.*;
public class RandomNumberGenerator{;
private static Scanner in;
public static void main(String[] args){
Random r = new Random();
float s;
in = new Scanner(System.in);
int poop = (r.nextInt(10-1)+1);
int poop1 = (r.nextInt(10-1)+1);
int poop2 = (r.nextInt(10-1)+1);
int poop3 = (r.nextInt(10-1)+1);
for (int i = 0; i < 80; i++)
{
System.out.println("Enter a number from 1-9");
s = in.nextFloat();
float s1 = in.nextFloat();
float s2 = in.nextFloat();
float s3 = in.nextFloat();
if(poop == s && poop1 == s1 && poop2 == s2 && poop3 == s3) {
System.out.println("You are correct HeHe XD");
break;
}
if(poop > s && poop1 == s1 && poop2 == s2 && poop3 == s3){
System.out.println("You have 1 wrong");
}
if(poop < s && poop1 == s1 && poop2 == s2 && poop3 == s3){
System.out.println("You have 1 wrong");
}
if(poop > s && poop1 == s1 && poop2 == s2 && poop3 == s3){
System.out.println("You have 1 wrong");
}
if(poop == s && poop1 > s1 && poop2 == s2 && poop3 == s3){
System.out.println("You have 1 wrong");
}
if(poop == s && poop1 < s1 && poop2 == s2 && poop3 == s3){
System.out.println("You have 1 wrong");
}
if(poop == s && poop1 == s1 && poop2 > s2 && poop3 == s3){
System.out.println("You have 1 wrong");
}
if(poop == s && poop1 == s1 && poop2 < s2 && poop3 == s3){
System.out.println("You have 1 wrong");
}
if(poop == s && poop1 == s1 && poop2 == s2 && poop3 > s3){
System.out.println("You have 1 wrong");
}
if(poop == s && poop1 == s1 && poop2 == s2 && poop3 < s3){
System.out.println("You have 1 wrong");
}
if(poop != s && poop1 != s1 && poop2 == s2 && poop3 == s3){
System.out.println("You have 2 wrong");
}
if(poop != s && poop1 == s1 && poop2 != s2 && poop3 == s3){
System.out.println("You have 2 wrong");
}
if(poop != s && poop1 == s1 && poop2 == s2 && poop3 != s3){
System.out.println("You have 2 wrong");
}
if(poop == s && poop1 != s1 && poop2 != s2 && poop3 == s3){
System.out.println("You have 2 wrong");
}
if(poop == s && poop1 != s1 && poop2 == s2 && poop3 != s3){
System.out.println("You have 2 wrong");
}
if(poop == s && poop1 != s1 && poop2 != s2 && poop3 != s3){
System.out.println("You have 3 wrong");
}
if(poop != s && poop1 == s1 && poop2 != s2 && poop3 != s3){
System.out.println("You have 3 wrong");
}
if(poop != s && poop1 != s1 && poop2 == s2 && poop3 != s3){
System.out.println("You have 3 wrong");
}
if(poop != s && poop1 != s1 && poop2 != s2 && poop3 == s3){
System.out.println("You have 3 wrong");
}
if(poop != s && poop1 == s1 && poop2 != s2 && poop3 != s3){
System.out.println("You have 3 wrong");
}
if(poop != s && poop1 != s1 && poop2 != s2 && poop3 != s3){
System.out.println("You have 4 wrong");
}
}
}
}
Upvotes: 1
Views: 555
Reputation: 366
Put your variables in a List
then you can check with contains
if an Element is in the List
.
Actually you get two numbers as feedback in mastermind, how many are a correct Number and how many are also at the correct place.
Since there are duplicates possible in mastermind, you can make a copy
of your original List
and for each correct guess
remove
it from the List
. Otherwise guessing all the same number
would be correct if the master contains
that number.
int size = 4;
List<Integer> poops = new ArrayList<>();
for(int i =0; i<size; i++){
poops.add((r.nextInt(10-1)+1));
}
...
List<Integer> guesses= new ArrayList<>();
for(int i =0; i<size; i++){
guesses.add(in.nextInt());
}
int correctNumberCount = 0;
List<Integer> poopsCopy = new ArrayList<>(poops);
for(Integer guess: guesses){
if(poopsCopy.contains(guess)){
correctNumberCount ++;
//For handling duplicates
poopsCopy.remove(guess);
}
}
int correctCount = 0;
int counter = 0;
for(Integer guess: guesses){
if(guess == poops.get(counter)){
correctCount++;
}
counter++;
}
//Inform the user about correctNumberCount and correctCount
Upvotes: 1
Reputation: 96
how about:
import java.util.*;
public class RandomNumberGenerator{;
private static Scanner in;
public static void main(String[] args){
Random r = new Random();
float s;
in = new Scanner(System.in);
int[] poops = new int[4];
for(int i=0;i<poops.length;i++){
poops[i]=r.nextInt(10-1)+1;
}
float[] s = new float[4];
for (int i = 0; i < 80; i++){
System.out.println("Enter a number from 1-9");
int error =0;
for(int j=0;j<poops.length;j++){
if(poop[j] != in.nextFloat()){
error++;
}
}
if(result == 0){
System.out.println("You are correct HeHe XD");
break;
}else{
System.out.println("You have "+error+" wrong");
}
}
}
Upvotes: -1
Reputation: 140427
You don't do it with variables named p1, p2, p3, ...
You step back and read about the concepts of arrays!
Then you create two arrays (preferable with int; not float!). The first array carries the 4 values that your program thinks up; and the second array is filled with 4 values coming from the user.
Then you iterate both arrays; to figure how many elements in the "user" array are given in the "computer" array. Really: you never ever should write code that contains so many if/else statements. Such code is absolutely impossible to read or maintain. Don't do that. Ever.
Alternatively, you can look Collections, those are more sophisticated data structures. In your case you can work with List/ArrayList of Integer objects. When you are interested in that stuff; use your favorite search engine and read about it.
Upvotes: 1
Reputation: 11075
According to your requirement, you only need to get the wrong number of each input combination but not where it is wrong. So, there is no need for you to enumerate all possibilities.
You can try the code below, the size
is the number indicating how many inputs allowed for user.
I have used List
to store the numbers. It's a good habit to keep a series of data into container so that you can operate them with loop
easily and loop
is one of the strongest power of computer.
import java.util.*;
public class RandomNumberGenerator {
private static Scanner in;
private static List<Integer> expected = new ArrayList<Integer>();
private static List<Integer> input = new ArrayList<Integer>();
static private final int size = 4;
static int correctNumber = 0;
public static void check(List<Integer> expected, List<Integer> input) {
for (int i = 0; i < size; i++) {
if (expected.get(i) == input.get(i)) {
correctNumber++;
}
}
if (correctNumber == size) {
System.out.println("You are correct HeHe XD");
return;
} else {
int wrongNumber = size - correctNumber;
System.out.println("You have " + wrongNumber + " wrong");
correctNumber = 0;
}
}
public static void main(String[] args) {
Random r = new Random();
in = new Scanner(System.in);
int count = size;
while(count > 0){
expected.add((r.nextInt(10 - 1) + 1));
count--;
}
for (int i = 0; i < 80; i++) {
System.out.println("Enter 4 numbers from 1-9");
count = size;
while(count > 0){
input.add(in.nextInt());
count--;
}
check(expected, input);
}
}
}
Upvotes: 1
Reputation: 2781
You can use two ArrayList for storing 4 data points and user inputs. Then comparing using ArrayList.contains()
. You don't have to write a lot of if condition
. Refer to my code below:
public static void main(String[] args) {
final int totalPoints = 4;
Random r = new Random();
in = new Scanner(System.in);
ArrayList<Integer> poops = new ArrayList<Integer>(totalPoints);
ArrayList<Integer> userinputs = new ArrayList<Integer>(totalPoints);
for (int i=0; i<totalPoints; i++) {
int poop = (r.nextInt(10-1)+1);
System.out.println("Random datapoints: " + poop);
poops.add(poop);
}
System.out.println("Enter a number from 1-9");
for (int i=0; i<totalPoints; i++) {
int s = in.nextInt();
userinputs.add(s);
}
int correct = 0;
for (int i=0; i<4; i++) {
if (poops.contains(userinputs.get(i))) {
correct++;
}
}
System.out.println("You have " + correct + "correct numbers");
}
Upvotes: 1