-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess.cpp~
106 lines (83 loc) · 1.81 KB
/
chess.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Assumes board is no larger than 9x9
*/
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <algorithm>
#include <cstdlib>
using namespace std;
int boardsize = 9;
string board[9][9];
void clearBoard();
void printBoard();
void initBoard();
string int2str(int i);
void move(string pos1, string pos2);
string char_map = "/abcdefgh";
string plyr_map = "PRNBKQBNR";
int main(){
clearBoard();
initBoard();
printBoard();
string input;
getline(cin, input);
while (input.compare("quit") != 0){
cout << "Enter a move: ";
// remove whitespace
input.erase( remove( input.begin(), input.end(), ' '), input.end() );
// move chess piece or start over
if (input.length()==4)
move(input.substr(0, 2), input.substr(2, 2));
else
cout << "That is not a valid move" << endl;
printBoard();
}
return 0;
}
void move(string pos1, string pos2){
int c1 = char_map.find(pos1[0]);
int r1 = atoi(pos1.substr(1,1).c_str());
int c2 = char_map.find(pos2[0]);
int r2 = atoi(pos2.substr(1,1).c_str());
string piece = board[r1][c1];
if (piece.compare("-") != 0){
board[r1][c1] = "-";
board[r2][c2] = piece;
}
}
void clearBoard(){
for (int i=0; i<boardsize; i++){
for (int j=0; j<boardsize; j++){
if (i == 0)
board[0][j] = char_map[j];
else
board[i][j] = "-";
}
if (i != 0)
board[i][0] = int2str(i);
}
}
void initBoard(){
for (int c=1; c<boardsize; c++){
board[1][c] = plyr_map[c];
board[2][c] = "P";
board[8][c] = plyr_map[c];
board[7][c] = "P";
}
}
void printBoard()
{
for (int i=0; i<boardsize; i++){
for (int j=0; j<boardsize; j++){
cout << board[i][j] << " ";
}
cout << endl;
}
}
string int2str(int i){
std::stringstream out;
out << i;
return out.str();
}