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
179
180
181
use std::io::{self, Write, BufWriter, BufRead, BufReader};
use std::fs::File;
use std::str::FromStr;
use game_logic as gl;
use error_handling as eh;
pub fn get_game_arguments() -> Vec<String> {
::std::env::args()
.skip(1)
.collect::<Vec<String>>()
}
pub fn display_usage() {
writeln!(io::stderr(),
"Usage: nogo p1type p2type [height width | filename]")
.unwrap();
eh::exit_with_error(eh::construct_error("insufficient number of arguments",
eh::NogoErrorKind::IncorrectNumberOfArgs));
}
pub fn get_player_move(board: &gl::NogoBoard, player_name: char) -> gl::PlayerInput {
let mut r;
let mut c;
loop {
print!("Player {}> ", player_name);
io::stdout().flush().unwrap();
let mut input = String::new();
if io::stdin().read_line(&mut input).is_err() {
continue;
}
let entries = input.trim()
.split_whitespace()
.collect::<Vec<_>>();
if entries.len() == 1 {
match entries[0].trim().chars().nth(0) {
Some('w') | Some('W') => {
let path =
String::from_utf8(entries[0].trim().bytes().skip(1).collect::<Vec<_>>())
.unwrap();
if path.len() != 0 {
return gl::PlayerInput::Save(path);
}
}
_ => continue,
}
}
if entries.len() > 2 {
continue;
}
r = match i32::from_str(entries[0].trim()) {
Ok(val) => val,
Err(_) => continue,
};
c = match i32::from_str(entries[1].trim()) {
Ok(val) => val,
Err(_) => continue,
};
if r >= board.height() || r < 0 {
continue;
}
if c >= board.width() || c < 0 {
continue;
}
if !eh::validation::validate_user_move(board, (r, c)) {
continue;
}
return gl::PlayerInput::Point(r, c);
}
}
pub fn save_game_state<'a>(path: &str, data: Vec<String>) -> eh::Result<'a, ()> {
let mut writer = BufWriter::new(File::create(path)?);
for line in data.iter() {
writer.write_all(line.as_bytes())?;
writer.write_all(b"\n")?;
}
Ok(())
}
pub fn load_game_state<'a>(path: &str) -> eh::Result<'a, Vec<String>> {
let mut data = Vec::new();
let reader = BufReader::new(File::open(path)?);
for line in reader.lines() {
let line = line?;
data.push(line);
}
Ok(data)
}
pub fn parse_save_file_metadata<'a>
(metadata: &Vec<&str>)
-> eh::Result<'a, (i32, i32, gl::PlayerType, gl::PlayerType, char)> {
Ok((i32::from_str(&metadata[0])?,
i32::from_str(&metadata[1])?,
get_player_type(&metadata[2])?,
get_player_type(&metadata[3])?,
metadata[4].chars().nth(0).unwrap()))
}
fn get_player_type<'a>(p: &str) -> eh::Result<'a, gl::PlayerType> {
match p {
"c" | "C" => Ok(gl::PlayerType::COMPUTER),
"h" | "H" => Ok(gl::PlayerType::HUMAN),
_ => {
return Err(eh::construct_error("incorrect type for player 0",
eh::NogoErrorKind::IncorrectTypes));
}
}
}
pub fn parse_player_strings_from_saved_file<'a>
(data: &Vec<&String>)
-> eh::Result<'a, (Vec<gl::Point>, Vec<gl::Point>)> {
let (mut zero_points, mut x_points) = (Vec::new(), Vec::new());
let (mut i, mut j) = (0, 0);
for line in data.iter() {
for c in line.chars() {
match c {
'0' => zero_points.push(gl::Point::new(i, j, gl::PLAYER_ZERO)),
'x' | 'X' => x_points.push(gl::Point::new(i, j, gl::PLAYER_ONE)),
'.' => {}
_ => {
return Err(eh::construct_error("invalid character found in board data",
eh::NogoErrorKind::IncorrectTypes));
}
}
j += 1;
}
i += 1;
j = 0;
}
Ok((zero_points, x_points))
}