-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhumanPlayerCLI.cpp
68 lines (57 loc) · 1.26 KB
/
humanPlayerCLI.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
//
// Created by James on 30/09/15.
//
#include <utility>
#include <iostream>
#include <limits>
#include "humanPlayerCLI.h"
std::pair<int, int> HumanPlayerCLI::nextMove(Board &board) const
{
std::pair<int,int> nextPos;
while (true)
{
std::cout << "Enter x, y coordinates: x y" << std::endl;
auto pos = cliInput_(board);
if (validInput_(board, pos.first, pos.second))
{
nextPos = pos;
break;
}
else
{
std::cout << "That's not a valid move. Try again." << std::endl;
}
}
return nextPos;
}
bool HumanPlayerCLI::playPieRule(Board &board) const
{
return false;
}
bool HumanPlayerCLI::validInput_(Board &board, int x, int y) const
{
int side = board.side();
if (x > side || x < 1)
{
return false;
}
if (y > side || y < 1)
{
return false;
}
int i = x - 1;
int j = y - 1;
if (board[i*side + j] != TileColour::EMPTY)
{
return false;
}
return true;
}
std::pair<int,int> HumanPlayerCLI::cliInput_(Board &board) const
{
int x, y;
std::cin >> x >> y;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return std::pair<int,int>(x, y);
}