-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuessChecker133.java
103 lines (92 loc) · 2.69 KB
/
GuessChecker133.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Activity 1.3.2
*/
import java.util.Scanner;
public class GuessChecker133
{
public static void main(String[] args)
{
/* Add any variables you will need throughout the program here. */
// Generate the random number
String targetStr = makeCode();
//System.out.println(targetStr); // uncomment for debugging
// Break the random number into four variables.
int r1 = Integer.parseInt(targetStr.substring(0, 1));
int r2 = Integer.parseInt(targetStr.substring(1, 2));
int r3 = Integer.parseInt(targetStr.substring(2, 3));
int r4 = Integer.parseInt(targetStr.substring(3, 4));
// Get the user's guess.
String guess = getGuess();
//System.out.println(guess); // uncomment for debugging
// Break the user's guess into four variables.
int hits = 0;
int nearHits= 0;
int g1 = Integer.parseInt(guess.substring(0, 1));
int g2 = Integer.parseInt(guess.substring(1, 2));
int g3 = Integer.parseInt(guess.substring(2, 3));
int g4 = Integer.parseInt(guess.substring(3, 4));
if(r1==g1)
hits++;
if(r2==g2)
hits++;
if(r3==g3)
hits++;
if(r4==g4)
hits++;
if(g1==r1||g1==r2||g1==r3||g1==r4)
{
nearHits++;
}
if(g2==r1||g2==r2||g2==r3||g2==r4)
{
nearHits++;
}
if(g3==r1||g3==r2||g3==r3||g3==r4)
{
nearHits++;
}
if(g4==r1||g4==r2||g4==r3||g4==r4)
{
nearHits++;
}
System.out.println("Hits: "+hits+"\nNear hits: "+nearHits);
}
// Checks to ensure no duplicate digits in a int.
public static boolean hasDupes(int num)
{
boolean[] digs = new boolean[10];
while (num > 0)
{
if (digs[num % 10])
return true;
digs[num % 10] = true;
num /= 10;
}
return false;
}
// Creates a new random 4 digit code 1000-9999 with no duplicates.
public static String makeCode()
{
int target = (int) (Math.random() * 9000 + 1000);
while (hasDupes(target))
target = (int) (Math.random() * 9000 + 1000);
String targetStr = target + "";
return targetStr;
}
// Prompts the user for a guess and repeats until valid guess is made.
public static String getGuess()
{
Scanner sc = new Scanner(System.in);
boolean validGuess = false;
int userGuess = 0;
while (!validGuess)
{
System.out.print("Guess a 4-digit number from 1000 to 9999 with no duplicate digits: ");
userGuess = sc.nextInt();
if (!(hasDupes(userGuess) || (userGuess < 1000)))
validGuess = true;
}
String userStr = userGuess + "";
return userStr;
}
}