-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathplayer.cpp
More file actions
66 lines (58 loc) · 1.87 KB
/
player.cpp
File metadata and controls
66 lines (58 loc) · 1.87 KB
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
#include <cstdlib>
#include <iostream>
#include "player.h"
using namespace std;
namespace Player{
int getBoardSize() {
int size;
while(true) {
cout << "Enter Game Board Size (11 is standard): ";
cin >> size;
if (cin.fail()) {
cout << "Enter a number please!" << endl << endl;
cin.clear();
cin.ignore(10000, '\n');
continue;
}
if (size < 100 && size > 2) break; // Good input; break here.
if (size >= 100) cout << "Way too ambitious. Try a number less than 100." << endl << endl;
if (size <= 2) cout << "Minimum board size is 3." << endl << endl;
}
return size;
}
pair<int, int> getPlayerMove() {
int arg = 0;
int x;
int y;
cout << "Player (White) connects horizontally." << endl;
cout << "CPU (Black) connects vertically." << endl;
while(true) {
if (arg == 0) {
cout << "Enter move coordinate X: ";
cin >> x;
} else if (arg == 1) {
cout << "Enter move coordinate Y: ";
cin >> y;
} else break;
if (cin.fail()) {
cout << "Enter a number please!" << endl;
cin.clear();
cin.ignore(10000, '\n');
continue;
} else arg++;
}
return pair<int, int>(x, y);
}
bool goesFirst() {
string answer;
while(true) {
cout << "Who should go first? [P]layer, [C]PU, or [R]andom: ";
cin >> answer;
answer = answer.substr(0,1); // Extract first letter
if (answer == "P" || answer == "p") return true;
if (answer == "C" || answer == "c") return false;
if (answer == "R" || answer == "r") return (rand() % 2); // Random
cout << "Type \"p\", \"c\", or \"r\"." << endl;
}
}
}