-
Notifications
You must be signed in to change notification settings - Fork 0
/
Challenge26.java
77 lines (67 loc) · 1.56 KB
/
Challenge26.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
import java.util.*;
/**
*
*/
/**
* @author kvito
*
*/
public class Challenge26 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
Random rnd = new Random();
int[] code = new int[4];
int[] guess = new int[4];
for(int i = 0; i < code.length; i++) {
code[i] = rnd.nextInt(10);
System.out.print(code[i] + " ");
}
System.out.print("\n\nGuess 4 digit code: ");
int input = in.nextInt();
guess[3] = input%10;
input /= 10;
guess[2] = input%10;
input /= 10;
guess[1] = input%10;
input /= 10;
guess[0] = input%10;
int lives = 12;
while(lives > 0){
int correctPlace = 0;
for(int i = 0; i < code.length; i++){
if(code[i]==guess[i]) correctPlace++;
}
System.out.println("You guessed " + correctPlace + " digit(s) in the correct place");
if(correctPlace == 4) {
System.out.println("You win!");
break;
}
int count = 0;
for(int i = 0; i < code.length; i++){
for(int j = 0; j < guess.length; j++){
if(code[i] == guess[j] && i != j) count++;
}
}
System.out.println("You guessed " + count + " digit(s) but in the wrong place");
System.out.println("Lives remaining: " + lives);
System.out.print("Guess 4 digit code: ");
input = in.nextInt();
guess[3] = input%10;
input /= 10;
guess[2] = input%10;
input /= 10;
guess[1] = input%10;
input /= 10;
guess[0] = input%10;
lives--;
if(lives < 1){
System.out.println("You lose!");
}
}
in.close();
}
}