binarySearch in Java, Mastermind Game - java

Right now for a Java course I'm trying to build a Mastermind-like game. In this game a 4-digit random number is generated, and the user tries to guess the number. With each guess the computer states how many correct digits are in the right order, and how many correct digits are in the wrong order.
For some reason, everything works up to my binary search for this program, which is really the heart of the program. I've spent hours tweaking it and I still cant get it. Any ideas?
In this example I'm trying to guess 9935, I realize that's not a random number though.
Thanks so much!
EDIT: When I run this program and use the guess "9875", it does not give me the right results.
The guesses and results I'm required to find are:
Please enter a four-digit number: 9874
The number of correct digits but in the wrong place: 0
The number of correct digits in the right place: 1
Please enter a four-digit number: 9899
The number of correct digits but in the wrong place: 1
The number of correct digits in the right place: 1
Please enter a four-digit number: 9593
The number of correct digits but in the wrong place: 3
The number of correct digits in the right place: 1
Please enter a four-digit number: 9935
The number of correct digits but in the wrong place: 0
The number of correct digits in the right place: 4
You are correct!
public class Mastermind {
public static void main(String[] args) {
Random randomGenerator = new Random();
Scanner input = new Scanner(System.in);
int randomNumber = 9935;
int[] randomArray = new int[4];
int temp = randomNumber;
for (int i = 3; i >= 0; i--){
int n = temp%10;
randomArray[i] = n;
temp /= 10;
}
boolean found = false;
while (found == false){
System.out.print(Arrays.toString(randomArray));
int[] guessArray = new int[4];
System.out.print("Please enter a four-digit number: ");
int guessTemp = input.nextInt();
for (int i = 3; i >= 0; i--){
int n = guessTemp%10;
guessArray[i] = n;
guessTemp /= 10;
}
if (Arrays.equals(randomArray, guessArray)){
System.out.println("You are correct!");
found = true;
} else {
int numberRightRight = 0;
int numberRightWrong = 0;
int indexFound = 0;
for(int i = 0; i < guessArray.length; i ++){
System.out.println(randomArray[i]);
indexFound = Arrays.binarySearch(guessArray, randomArray[i]);
System.out.println(indexFound);
if (indexFound >= 0){
if(indexFound == i){
numberRightRight++;
} else {
numberRightWrong++;
}
}
}
System.out.println("The number of correct digits but in the wrong place: " + numberRightWrong);
System.out.println("The number of correct digits in the right place: " + numberRightRight);
}
}
}

If you are not required to use Arrays.binarySearch(int[], int) you could use your own simple lookup method for an unsorted Array:
public static int findInArray(int[] array, int value) {
for (int i = 0; i < array.length; i++) {
if (array[i] == value) {
return i;
}
}
return -1;
}
now instead of calling Arrays.binarySearch(guessArray, randomArray[i]); just call findInArray(guessArray, randomArray[i])

Related

My code runs, but the if statements are not printing their contents when I compare contents of two arrays

I am trying to write a simple mastermind game where a 4 digit number will be randomly selected by the computer and the user inputs a number over and over again until the correct number is found. I am trying to do this by passing the guessed number and the random number to their own separate arrays and then comparing them, position by position to see if they are similar. If two numbers are in the exact same spot
Example:
if guessArray[0] == numsArray[0] then the computer will print a *.
If two numbers are present but not in the exact same spot (eg. you made a guess of 2056 but the actual number is 1203) then one + should be printed. This cycle repeats until the number is guessed.
I've already asked a friend in person what the problem was and he couldn't figure it out. He knows the most code out of my friends so this was my next place to go.
Here is the full project. I did not write the ConvertInt2Array method. I found it on the internet.
import java.util.Scanner;
import java.util.Arrays;
import java.util.Random;
public class Mastermind {
public static Random numGen = new Random();
public static void main(String [] args) {
Scanner Input = new Scanner (System.in);
int x = 0;
int number = 0;
int random = 0;
int guess = 0;
int y = 0;
int numArray[] = new int[4];
int guessArray[] = new int[4];
boolean isGuessed = false;
//Generate Random Number
for(x=0; x<=3; x++) {
int rand = Math.abs(numGen.nextInt());//Get the absolute value
random = (rand % 999 + 1);
numArray[x] = random;
number+=random;
}
while(isGuessed == false){
System.out.println("Guess a four digit random number");
guess = Input.nextInt();
guessArray = convertInt2Array(guess);
for(y=0; y<=3; y++) {
if(numArray[y] == guessArray[y]) {
System.out.print("*");
}
else if(Arrays.equals(numArray, y, y, guessArray, 0, guessArray.length) == true) {
System.out.print("+");
}
else {
}
if(guess==number) {
isGuessed = true;
}
}
}
System.out.println("You guessed it correctly!");
}
public static int[] convertInt2Array(int guess) {
String temp = Integer.toString(guess);
String temp2;
int temp3;
int [] gArray = new int[temp.length()];
for(int i=0;i<temp.length();i++) {
if (i!=temp.length()) {
temp2 = temp.substring(i, i+1);
} else {
temp2 = temp.substring(i);
}
temp3 = Integer.parseInt(temp2);
gArray[i] = temp3;
}
return gArray;
}
}
There may be more than one issue here, but here's a potential problem:
int rand = Math.abs(numGen.nextInt()); // Get the absolute value
random = (rand % 999 + 1);
This will usually result in random being a three-digit number. You mentioned you want this to be a four-digit number. Random.nextInt() can return any of the possible 232 integer numbers (from -2147483648 to 2147483647). To fix this, use a different Random.nextInt and specify your bounds:
int lowerBound = 1000;
int upperBound = 10000;
random = numGen.nextInt(upperBound - lowerBound) + lowerBound;
Let's break this down: numGen.nextInt(upperBound - lowerBound) evaluates to numGen.nextInt(9000), which will return a number between 0 (inclusive) and 9000 (exclusive), i.e. anything in the range 0-8999. You then add the lower bound of 1000 to ensure that random will be at least 1000 and up to 9999.
See the documentation for Random.nextInt(int bound).
Hopefully this gets you pointed in the right track.

Print odd numbers in a descending order

The program needs to take an odd number and output it in a descending order
For example: if the input is 11 the output needs to be 11 , 9 , 7 , 5 , 3, 1.
I tried using a for loop but I can only seem to get it to work with even numbers not odd numbers
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number = input.nextInt();
for (int i = number - 1; i >= 0; i--) {
if (i % 2 == 0) {
int descend = i;
System.out.println(descend + " ");
}
}
}
The output is the number in descending order but as even only. If I add a 1 into the descend variable the numbers would seem to descend in an odd manner but its not ideal.
This line returns true if the number is even:
if (i % 2 == 0) {
If you want to know when the number is odd:
if (i % 2 != 0) {
Also, why are you starting your count at 1 less than the input value:
int i = number - 1;
I think you want to do this:
for (int i = number; i > 0; i--) { // tests for numbers starting at the input and stopping when i == 0
Just replace (i%2==0) to (i%2==1)
Asking if the number % 2 is equal to zero is basically asking if the number is even, so what you really have to do is ask if the number % 2 is not equal to zero, or equal to 1
if (i % 2 != 0) {
int descend = i;
System.out.println(descend + " ");
}
Also, there's no need to subtract 1 from the user input so your for loop can be written like this
for (int i = number; i >= 0; i--) {
if (i % 2 == 0) {
int descend = i;
System.out.println(descend + " ");
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an an odd number: ");
int number = input.nextInt();
while(number%2==0){
System.out.print("Number must be odd number:" +
"(Ex:1, 3,5)\nTry again: ");
number=input.nextInt();
}
for (int i = number; i >= 0; i--) {
if(number%2!=0){
System.out.println(number);}
number-=1;
}
}

how do i get my code to calculate the number of place correct place values?

I am not really good at coding and I've been confused in this step where I don't know to make my code read the correct number of place values.
For example I run my code and it generates 5 random numbers then I input 5 random numbers the output of this would be "They share 0 numbers in the right place value.
For example,in order for me to get what I want, I get 5 random generated numbers which are 54123, I input 00123 it would say they hold 3 numbers in the right place value.
//this is what I think something needs to be edited
//This is my whole code on a google doc: https://docs.google.com/document/d/1ug1rRNwgZwBAf9kvaeAEFoo81r_W9Br6rRX4xc0fFUk/edit?usp=sharing
//I didn't enter my whole code on this post because I think it is to long and the only part I want is where it can calculate how many numbers in the right place value
System.out.println(s);
String f = scan.next();
for(int p=0; p<s.length(); p++) {
for(int z=0;z<f.length();z++){
if (s.substring(p,p+1).equals(f.substring(z,z+1))){
n++;
} }}
int rt= 0;
for(int p=0; p<s.length(); p++) {
for(int z=0;z<f.length();z++){
// this where i count the numbers in the right place
rt=99;
} }
System.out.println(" They share " + rt + " numbers in the right place value");
System.out.println(" They share " + n + " numbers in common");
System.out.println("guess number 1");
if(n ==5){
System.out.println(" Congratulations you win! ");
System.exit(0);
}
String generatedNums = "54123";
String nums = "00123";
int length = generatedNums.length() <= nums.length() ? generatedNums.length() : nums.length();
int count = 0;
for (int i = 0; i < length; i++) {
if (generatedNums.charAt(i) == nums.charAt(i)) {
count++;
}
}
System.out.println(count);

Printing the two highest values from user input

I have an assignment where I have to write a code which lets the user decide an amount of int values to be written in, and then decides what these values should be. There has to be atleast 2 inputs from the user. The program will then compare the values from the input and then print out the two highest values. So far I managed to print out the highest value, but I'm not sure whats wrong with the way I've done it since the output just becomes 0 if I choose to print out 2 numbers and the highest one is entered in first. And I'm also not sure how to keep track of the second highest number either. Would appreciate some help.
import java.util.Scanner;
public class ToStoersteTall{
public static void main(String[] args){
System.out.println("How many numbers? (minimum 2)?:");
Scanner reader = new Scanner(System.in);
if (reader.hasNextInt()) {
int numbers = reader.nextInt();
if (numbers >= 2) {
System.out.println("Enter value #1");
if (reader.hasNextInt()) {
int num1 = reader.nextInt();
System.out.println("Enter value #2");
if (reader.hasNextInt()) {
int num2 = reader.nextInt();
int biggest = 0;
for (int i = 3; i <= tall; i++) {
System.out.println("Enter value #" + i);
int num3 = reader.nextInt();
biggest = num1;
if(biggest < num3){
biggest = num3;
}
}
System.out.println(biggest);
} else {
System.out.println("Please enter an integer");
}
} else {
System.out.println("Please enter an integer");
}
} else {
System.out.println("Please enter an integer equal or higher than 2.");
}
} else {
System.out.print("Vennligst oppgi et heltall større eller lik 2.");
}
}
}
I have an assignment where I have to write a code which lets the user decide an amount of int values to be written in, and then decides what these values should be. There has to be atleast 2 inputs from the user. The program will then compare the values from the input and then print out the two highest values. So far I managed to print out the highest value, but I'm not sure whats wrong with the way I've done it since the output just becomes 0 if I choose to print out 2 numbers and the highest one is entered in first. And I'm also not sure how to keep track of the second highest number either. Would appreciate some help.
A couple things:
good practice to close scanner (and IO-related resources in general)
reduced if-statement blocks bloat for easier readability
you specify 2 guaranteed numbers, so attempt to parse those before looping
can remove system.exit calls or replace system.exit and move bulk of code back into the larger if-else blocks as originally state in OP (but I refer back to the sake of readability)
added check for the first and second numbers input to make sure high1 is highest value, and high2 is second highest value.
keep order while looping and checking values (note: does not use array), if the number is a new high, replace high1 and move high1's value down to high2, or if the number is a second (new) high, replace high2. If values are equal, this logic is excluded and you may want to specify based on your own constraints
import java.io.IOException;
import java.util.Scanner;
public class ToStoersteTall {
public static void main(String[] args) throws IOException {
System.out.println("How many numbers? (minimum 2)?:");
Scanner reader = new Scanner(System.in);
int n = 0;
if (reader.hasNextInt()) {
n = reader.nextInt();
} else {
System.out.println("Vennligst oppgi et heltall større eller lik 2.");
System.exit(-1); // quits execution
}
if (n < 2) {
System.out.println("Please enter an integer equal or higher than 2.");
System.exit(-2);
}
// Since guaranteed 2 numbers, parse and assign now
int high1 = 0, high2 = 0;
System.out.println("Enter value # 1");
if (reader.hasNextInt())
high1 = reader.nextInt();
System.out.println("Enter value # 2");
if (reader.hasNextInt())
high2 = reader.nextInt();
// check to see if a switch to keep correct highest order, swap values if so
if (high1 < high2) {
int t = high2;
high2 = high1;
high1 = t;
}
// loop won't execute if only 2 numbers input, but will if 3 or more specified at start
for (int i = 2; i < n; ++i) {
System.out.println("Enter value #" + (i + 1));
if (reader.hasNextInt()) {
int t = reader.nextInt();
if (t > high1) {
high2 = high1; // throw away high2 value and replace with high1
high1 = t; // replace high1 value with new highest value
} else if (t > high2) {
high2 = t;
}
} else {
System.out.println("Please enter an interger");
}
}
reader.close();
System.out.println("The two highest numbers are: " + high1 + ", " + high2);
}
}
You're already keeping track of the biggest, so why not keep track of the second biggest? Another easy way of solving this problem is to keep all the numbers in a list, sort the list by number size, and grab the two highest entries.
I tried your code and used an array to solve the problem.
import java.util.Scanner;
public class Main {
static int secondHighest(int... nums) {
int high1 = Integer.MIN_VALUE;
int high2 = Integer.MIN_VALUE;
for (int num : nums) {
if (num > high1) {
high2 = high1;
high1 = num;
} else if (num > high2) {
high2 = num;
}
}
return high2;
}
public static void main(String[] args) {
System.out.println("How many numbers? (minimum 2)?:");
Scanner reader = new Scanner(System.in);
if (reader.hasNextInt()) {
int numbers = reader.nextInt();
int[] array = new int[numbers];
if (numbers >= 2) {
System.out.println("Enter value #1");
if (reader.hasNextInt()) {
int num1 = reader.nextInt();
array[0] = num1;
System.out.println("Enter value #2");
if (reader.hasNextInt()) {
int num2 = reader.nextInt();
array[1] = num2;
int biggest = 0;
for (int i = 3; i <= numbers; i++) {
System.out.println("Enter value #" + i);
int num3 = reader.nextInt();
array[i-1] = num3;
}
System.out.println("second largest number is" + secondHighest(array));
int largest = 0;
for(int i =0;i<array.length;i++) {
if(array[i] > largest) {
largest = array[i];
}
}
System.out.println("Largest number in array is : " +largest);
} else {
System.out.println("Please enter an integer");
}
} else {
System.out.println("Please enter an integer");
}
} else {
System.out.println("Please enter an integer equal or higher than 2.");
}
} else {
System.out.print("Vennligst oppgi et heltall større eller lik 2.");
}
}
}
Test
How many numbers? (minimum 2)?:
6
Enter value #1
3
Enter value #2
4
Enter value #3
5
Enter value #4
6
Enter value #5
7
Enter value #6
8
second largest number is7
Largest number in array is : 8
There is a logic error in your program. If numbers is 2, then the for loop never gets executed, and the value of biggest remains zero because it is never updated. Change your declaration of biggest to reflect the current maximum value found so far.
int biggest = num1 > num2 ? num1 : num2;
That way if the for loop never executes then biggest will be the maximum value of the first two numbers.
As for keeping track of the second highest value, you could introduce another variable secondBiggest, initialised in a similar manner to biggest, and then write logic to update this value in your for loop. However, in my opinion, it would be much easier to change your strategy to hold the entered values into an array, then when all inputs have been entered, calculate whichever values you desire from the array. This would lead to a much cleaner solution IMO.
(I have assumed that tall in the for loop is actually meant to be numbers...)
import java.util.Scanner;
public class Foo{
public static void main(String[] args){
System.out.println("How many numbers? (minimum 2)?:");
Scanner reader = new Scanner(System.in);
if(reader.hasNextInt()){
int numbers = reader.nextInt();
if(numbers >= 2){
int[] list = new int[numbers];
for(int i = 0; i < numbers; i++){
System.out.println("Enter value #" + (i + 1));
if(reader.hasNextInt())
list[i] = reader.nextInt();
}//for
int biggest = 0;
int secondBiggest = 0;
// find the values you want
for(int i = 0; i < numbers; i++){
if(list[i] > biggest){
secondBiggest = biggest;
biggest = list[i];
}//if
else if(list[i] > secondBiggest)
secondBiggest = list[i];
}//for
// print your results
System.out.println("The biggest integer is: " + biggest);
System.out.println("The second biggest integer is: " + secondBiggest);
}//if
}//if
}//main
}//class

Code error. Getting wrong answer when it's perfect?

package Basics;
import java.util.Scanner;
public class ForLoop {
public static void main(String args[]){
Scanner Jee = new Scanner(System.in);
int Final = 0;
int HowManyRounds = 1;
for (int counter = 1; counter <= HowManyRounds; counter++){
System.out.println("Type your boundary: ");
int Limit = Jee.nextInt();
System.out.println("Type the number which you want the sum of all multiples in given boundary: ");
int number = Jee.nextInt();
System.out.println("Type your starting number: ");
int StartingNumber = Jee.nextInt();
for(int Answer = StartingNumber; Answer <= Limit;Answer += number){
Final += Answer;
}
}
System.out.println(Final);
Jee.close();
}
}
i'm getting wrong answer. i don't know why. when i type 1000 for boundary 5 for round and 0 for starting number, i'm supposed to get 99500 but i'm getting 100500 and when i type for 1000 3 0, i'm getting right answer where as i get same answer for 99 3 0...
Type your boundary:
1000
Type the number which you want the sum of all multiples in given boundary:
5
Type your starting number:
0
100500
Type your boundary:
1000
Type the number which you want the sum of all multiples in given boundary:
3
Type your starting number:
0
166833
Type your boundary:
999
Type the number which you want the sum of all multiples in given boundary:
3
Type your starting number:
0
166833
If you expect an answer of 99500 in the first case, that probably means you don't want to include the limit itself in your operation (which you are doing right now). Try to change the condition in the for loop to answer < limit (instead of <=):
for(int Answer = StartingNumber; Answer < Limit;Answer += number){
[...]
You are not zeroing Final when you start a new loop.
Instead, move the declaration of Final inside the loop:
for (int counter = 1; counter <= HowManyRounds; counter++){
int Final = 0; // now Final is zeroed automatically for every iteration
// rest of loop the same
}
And please adhere to java naming conventions: variables shoiuld start with a lowercase letter, ie int total, not int Total
You seem to having trouble making this work. Here's the whole method fixed, including fixing style issues. Whether the logic is correct, I can't say, because you haven't told us what it is you're actually doing.
private static final int ROUNDS = 3;
public static void main(String args[]){
Scanner keyboard = new Scanner(System.in);
for (int i = 0; i < ROUNDS; i++) {
System.out.println("Type your boundary: ");
int limit = keyboard.nextInt();
System.out.println("Type the number which you want the sum of all multiples in given boundary: ");
int number = keyboard.nextInt();
System.out.println("Type your starting number: ");
int start = keyboard.nextInt();
int total = 0;
for (int n = start; n <= limit; n += number) {
total += n;
}
System.out.println(total);
}
keyboard.close();
}

Categories