-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcomplete.rs
178 lines (155 loc) · 4.09 KB
/
complete.rs
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//! Green and Yellow, Complete Version
use rand::Rng;
fn calc_green_and_yellow(guess: &[u8; 4], secret: &[u8; 4]) -> String {
let mut result = ["⬜"; 4];
let mut secret_handled = [false; 4];
for i in 0..guess.len() {
if guess[i] == secret[i] {
// that's a match
result[i] = "🟩";
// don't match this secret digit again
secret_handled[i] = true;
}
}
'guess: for g_idx in 0..guess.len() {
// only process guess digits we haven't already dealt with
if result[g_idx] == "🟩" {
continue;
}
for s_idx in 0..secret.len() {
// only process secret digits we haven't already dealt with
if secret_handled[s_idx] {
continue;
}
if guess[g_idx] == secret[s_idx] {
// put a yellow block in for this guess
result[g_idx] = "🟨";
// never match this secret digit again
secret_handled[s_idx] = true;
// stop comparing this guessed digit to any other secret digits
continue 'guess;
}
}
}
result.join("")
}
fn main() {
let mut rng = rand::thread_rng();
let stdin = std::io::stdin();
println!("New game!");
let mut secret = [0u8; 4];
for digit in secret.iter_mut() {
*digit = rng.gen_range(1..=9);
}
loop {
let mut line = String::new();
println!("Enter guess:");
stdin.read_line(&mut line).unwrap();
let mut guess = [0u8; 4];
let mut idx = 0;
for piece in line.trim().split(' ') {
let Ok(digit) = piece.parse::<u8>() else {
println!("{:?} wasn't a number", piece);
continue;
};
if digit < 1 || digit > 9 {
println!("{} is out of range", digit);
continue;
}
if idx >= guess.len() {
println!("Too many numbers, I only want {}", guess.len());
continue;
}
guess[idx] = digit;
idx += 1;
}
if idx < guess.len() {
println!("Not enough numbers, I want {}", guess.len());
continue;
}
println!("Your guess is {:?}", guess);
let score = calc_green_and_yellow(&guess, &secret);
println!("That gives: {}", score);
if guess == secret {
println!("Well done!!");
break;
}
}
}
#[test]
fn all_wrong() {
assert_eq!(
&calc_green_and_yellow(&[5, 6, 7, 8], &[1, 2, 3, 4]),
"⬜⬜⬜⬜"
);
}
#[test]
fn all_green() {
assert_eq!(
&calc_green_and_yellow(&[1, 2, 3, 4], &[1, 2, 3, 4]),
"🟩🟩🟩🟩"
);
}
#[test]
fn one_wrong() {
assert_eq!(
&calc_green_and_yellow(&[1, 2, 3, 5], &[1, 2, 3, 4]),
"🟩🟩🟩⬜"
);
}
#[test]
fn all_yellow() {
assert_eq!(
&calc_green_and_yellow(&[4, 3, 2, 1], &[1, 2, 3, 4]),
"🟨🟨🟨🟨"
);
}
#[test]
fn one_wrong_but_duplicate() {
assert_eq!(
&calc_green_and_yellow(&[1, 2, 3, 1], &[1, 2, 3, 4]),
"🟩🟩🟩⬜"
);
}
#[test]
fn one_right_others_duplicate() {
assert_eq!(
&calc_green_and_yellow(&[1, 1, 1, 1], &[1, 2, 3, 4]),
"🟩⬜⬜⬜"
);
}
#[test]
fn two_right_two_swapped() {
assert_eq!(
&calc_green_and_yellow(&[1, 2, 2, 2], &[2, 2, 2, 1]),
"🟨🟩🟩🟨"
);
}
#[test]
fn two_wrong_two_swapped() {
assert_eq!(
&calc_green_and_yellow(&[1, 3, 3, 2], &[2, 2, 2, 1]),
"🟨⬜⬜🟨"
);
}
#[test]
fn a_bit_of_everything() {
assert_eq!(
&calc_green_and_yellow(&[1, 9, 4, 3], &[1, 2, 3, 4]),
"🟩⬜🟨🟨"
);
}
#[test]
fn two_in_guess_one_in_secret() {
assert_eq!(
&calc_green_and_yellow(&[1, 2, 3, 3], &[3, 9, 9, 9]),
"⬜⬜🟨⬜"
);
}
#[test]
fn two_in_secret_one_in_guess() {
assert_eq!(
&calc_green_and_yellow(&[1, 2, 3, 4], &[3, 3, 9, 9]),
"⬜⬜🟨⬜"
);
}