-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
153 lines (139 loc) · 3.49 KB
/
main.cpp
File metadata and controls
153 lines (139 loc) · 3.49 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
144
145
146
147
148
149
150
151
152
153
// main.cpp
// chess
#include <iostream>
#include <string>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include "board.h"
#include "movement.h"
#include "promotion.h"
using namespace std;
string get_turn(int);
bool valid_move(Board, string&, int);
int main() {
Board new_game;
new_game.print();
int player = 0;
string turn;
char piece;
while (1) {
turn = get_turn(player);
if (valid_move(new_game, turn, player)) {
if (can_promote(new_game, turn, player)) {
cout << "\nPawn promtion available. What piece would you like to add? ";
cin >> piece;
// ensure correct case for new piece
if (player%2 == 0)
piece = toupper(piece);
else
piece = tolower(piece);
make_promotion(new_game, turn, piece);
}
new_game.make_move(turn);
system("clear");
} else {
continue;
}
new_game.print();
player++;
}
}
string get_turn(int player) {
string turn;
player = player%2;
switch (player) {
case 0:
cout << endl;
cout << "WHITE: ";
break;
case 1:
cout << endl;
cout << "BLACK: ";
break;
}
cin >> turn;
return turn;
}
bool valid_move(Board b, string &turn, int player) {
int src_row = turn[0]-'0', src_col = turn[1]-'0', dest_row = turn[2]-'0', dest_col = turn[3]-'0';
player = player%2;
if ( (src_row < 0) || (src_col > 7) ) {
cout << "Out of range." << endl;
return false;
}
if ( (src_col < 0) || (src_col > 7)) {
cout << "Out of range." << endl;
return false;
}
if ( (dest_row < 0) || (dest_row > 7)) {
cout << "Out of range." << endl;
return false;
}
if ( (dest_col < 0) || (dest_col > 7)) {
cout << "Out of range." << endl;
return false;
}
char c = b.get_piece(src_row, src_col);
if (player == 0) { // White player
if (isupper(c) == 0) {
cout << "Invalid move." << endl;
return false;
}
}
if (player == 1) { // Black player
if (islower(c) == 0) {
cout << "Invalid move." << endl;
return false;
}
}
if ( (c == 'p') || (c == 'P') ) { // Pawn
if (move_pawn(b, turn, player)) {
return true;
} else {
cout << "Invalid move." << endl;
return false;
}
}
if ( (c == 'n') || (c == 'N') ) {
if (move_knight(b, turn, player)) {
return true;
} else {
cout << "Invalid move." << endl;
return false;
}
}
if ( (c == 'r') || (c == 'R') ) {
if (move_rook(b, turn, player)) {
return true;
} else {
cout << "Invalid move." << endl;
return false;
}
}
if ( (c == 'b') || (c == 'B') ) {
if (move_bishop(b, turn, player)) {
return true;
} else {
cout << "Invalid move." << endl;
return false;
}
}
if ( (c == 'q') || (c == 'Q') ) {
if (move_queen(b, turn, player)) {
return true;
} else {
cout << "Invalid move." << endl;
return false;
}
}
if ( (c == 'k') || (c == 'K') ) {
if (move_king(b, turn, player)) {
return true;
} else {
cout << "Invalid move." << endl;
return false;
}
}
return true;
}