-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreversi.cpp
More file actions
143 lines (129 loc) · 2.99 KB
/
Copy pathreversi.cpp
File metadata and controls
143 lines (129 loc) · 2.99 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
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
#include <ncurses.h>
#include <utility>
#include "board.hpp"
#include "enemy.hpp"
#include "log.hpp"
#include "print.hpp"
#include "reversi.hpp"
namespace roadagain
{
Reversi::Reversi(const CellColor& player, Level level) : player_(player), now_(CellColor::BLACK), next_(CellColor::WHITE)
{
board_ = new Board();
enemy_ = new Enemy(level);
logs_ = new std::vector<Cell>();
logs_->reserve(Board::MAX_PUT);
}
Reversi::~Reversi()
{
delete board_;
delete enemy_;
delete logs_;
}
void Reversi::start() const
{
board_->print();
}
void Reversi::play()
{
start();
for (int i = 0; i < Board::MAX_PUT; i++){
if (not board_->can_put(now_)){
change();
if (not board_->can_put(now_)){
break;
}
}
Point p;
if (now_ == player_){
p = move();
}
else {
p = enemy_->select(board_, now_);
if (getch() == 'q'){
p = Point(-1, -1);
}
}
if (p.y == -1 && p.x == -1){
break;
}
board_->put(Cell(p, now_));
change();
logs_->emplace_back(p, now_);
}
end();
}
void Reversi::end() const
{
CellColor winner = board_->winner();
::move(Board::END.y + 2, Board::START.x);
switch (winner){
case CellColor::BLACK:
printw(" Winner is Black ");
break;
case CellColor::WHITE:
printw(" Winner is White ");
break;
default:
printw(" Draw ");
break;
}
::move(Board::END.y + 2, Board::END.x - 6);
printw(" %02d %02d ", board_->black(), board_->white());
log_records(*logs_, winner);
}
Point Reversi::move() const
{
Cell cell(Point(), now_);
int c;
if (board_->empty(cell.point)){
print_stone(cell);
}
else {
print_coordinate(cell.point);
}
c = getch();
while (c != ' ' || not board_->can_put(cell)){
if (board_->empty(cell.point)){
clear_stone(cell.point);
}
else {
clear_coordinate(cell.point);
}
switch (c){
case 'h':
case KEY_LEFT:
cell.point.x = (cell.point.x + Board::COL - 1) % Board::COL;
break;
case 'j':
case KEY_DOWN:
cell.point.y = (cell.point.y + 1) % Board::ROW;
break;
case 'k':
case KEY_UP:
cell.point.y = (cell.point.y + Board::ROW - 1) % Board::ROW;
break;
case 'l':
case KEY_RIGHT:
cell.point.x = (cell.point.x + 1) % Board::COL;
break;
case 'q':
return (Point(-1, -1));
}
if (board_->empty(cell.point)){
print_stone(cell);
}
else {
print_coordinate(cell.point);
}
c = getch();
}
clear_stone(cell.point);
return (cell.point);
}
void Reversi::change()
{
now_ = next_;
next_.reverse();
}
} // namespace roadagain