-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
85 lines (67 loc) · 1.77 KB
/
main.cpp
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
/* main.cpp
* Copyright © 2015, Brian Derr <[email protected]>
*/
#include <csignal>
#include <cstdlib>
#include <iostream>
#include <unistd.h>
#include "braincurses.h"
static const int kMinCodeLength = 4;
static const int kMaxCodeLength = 6;
static const int kDefaultGuessCount = 10;
static const int kMinGuessCount = 1;
static const int kMaxGuessCount = 15;
void PrintUsage() {
std::cerr << "usage: braincurses [-c code_length] [-g guesses]" << std::endl;
}
void ProcessArgs(int argc, char* argv[], int& code_length, int& guesses) {
int opt;
while ((opt = getopt(argc, argv, "c:g:h")) != -1) {
switch (opt) {
case 'c':
code_length = atoi(optarg);
break;
case 'g':
guesses = atoi(optarg);
break;
case 'h':
default:
PrintUsage();
exit(EXIT_FAILURE);
}
}
if (code_length > kMaxCodeLength) {
code_length = kMaxCodeLength;
} else if (code_length < kMinCodeLength) {
code_length = kMinCodeLength;
}
if (guesses > kMaxGuessCount) {
guesses = kMaxGuessCount;
} else if (guesses <= 0) {
guesses = kDefaultGuessCount;
}
}
void endwin_atexit_handler() {
endwin();
}
void endwin_signal_handler(int signal) {
endwin();
std::exit(EXIT_SUCCESS);
};
int main(int argc, char* argv[]) {
int code_length = kMinCodeLength;
int guesses = kDefaultGuessCount;
ProcessArgs(argc, argv, code_length, guesses);
Braincurses bc(code_length, guesses);
std::signal(SIGINT, endwin_signal_handler);
int result = std::atexit(endwin_atexit_handler);
if (result != 0) {
std::cerr << "atexit registration failed" << std::endl;
return EXIT_FAILURE;
}
bool winner = false;
do {
winner = bc.PlayGame();
} while (bc.GameOverPlayAgain(winner));
return EXIT_SUCCESS;
}