#include
#include
using namespace std;
const char chessboard = ' ';
const char player1 = '●';
const char player2 = '○';
class board {
public:
void build();
void print();
int judge_game();
void change_p1(int x, int y, int& chess);
void change_p2(int x, int y, int& chess);
private:
string chessboard[15][15];
};
//打印棋盘
void board::build(){
const int N = 15;
cout << " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15" << endl;
for (int i = 1; i < 16; i++)
{
cout << "------------------------------------------------" << endl;
if (i < 10) {
cout << i << ' ' << "| ";
}
if (i > 9) {
cout << i << "| ";
}
for (int j = 1; j < 16; j++) {
cout << chessboard[i - 1][j - 1] << "| ";
}
cout << endl;
}
cout << "------------------------------------------------" << endl;
}
//初始化棋盘
void board::print() {
for (int i = 1; i < 16; i++) {
for (int j = 1; j < 16; j++) {
chessboard[i - 1][j - 1] = " ";
}
}
}
//判断坐标合法性
void board::change_p1(int x, int y, int& chess) {
if (chessboard[x][y] != " ") {
cout << "这一点有子了,请p1重新下" << endl;
chess--;
}
else chessboard[x][y] = "●";
}
void board::change_p2(int x, int y, int& chess) {
if (chessboard[x][y] != " ") {
cout << "这一点有子了,请p2重新下" << endl;
chess--;
}
else chessboard[x][y] = "○";
}
//判断是否有玩家胜利
int board::judge_game() {
for (int i = 1; i < 16; i++) {
for (int j = 1; j < 16; j++) {
if (chessboard[i - 1][j - 1] == "●"
&& chessboard[i - 1][j] == "●"
&& chessboard[i - 1][j + 1] == "●"
&& chessboard[i - 1][j + 2] == "●"
&& chessboard[i - 1][j + 3] == "●") {
return 1;
}
if (chessboard[i - 1][j - 1] == "○"
&& chessboard[i - 1][j] == "○"
&& chessboard[i - 1][j + 1] == "○"
&& chessboard[i - 1][j + 2] == "○"
&& chessboard[i - 1][j + 3] == "○") {
return 1;
}
if (chessboard[i - 1][j - 1] =="●"
&&chessboard[i][j - 1] =="●"
&&chessboard[i + 1][j - 1] =="●"
&&chessboard[i + 2][j - 1] =="●"
&&chessboard[i + 3][j - 1] == "●") {
return 1;
}
if (chessboard[i - 1][j - 1] == "○"
&& chessboard[i][j - 1] == "○"
&& chessboard[i + 1][j - 1] == "○"
&& chessboard[i + 2][j - 1] == "○"
&& chessboard[i + 3][j - 1] == "○") {
return 1;
}
if (chessboard[i - 1][j - 1] =="●"
&&chessboard[i][j] =="●"
&&chessboard[i + 1][j + 1] =="●"
&&chessboard[i + 2][j + 2] =="●"
&&chessboard[i + 3][j + 3] == "●") {
return 1;
}
if (chessboard[i - 1][j - 1] == "○"
&& chessboard[i][j] == "○"
&& chessboard[i + 1][j + 1] == "○"
&& chessboard[i + 2][j + 2] == "○"
&& chessboard[i + 3][j + 3] == "○") {
return 1;
}
}
}
return 0;
}
int main() {
board gameboard;
gameboard.build();
gameboard.print();
for (int i = 1; i < 1000; i++) {
if (i % 2 == 0) {
int x = 0, y = 0;
cout << "请输入player2坐标" << endl;
cin >> x >> y;
gameboard.change_p2(x, y, i);
gameboard.print();
if (gameboard.judge_game() == 1) {
cout << "game over player2 win" << endl;
cout << " new game" << endl;
gameboard.build();
}
}
if (i % 2 != 0) {
int x = 0, y = 0;
cout << "请输入player1坐标" << endl;
cin >> x >> y;
gameboard.change_p1(x, y, i);
gameboard.print();
if (gameboard.judge_game() == 1) {
cout << "game over player1 win" << endl;
cout << "new game" << endl;
gameboard.build();
}
}
}
return 0;
}
首先,在类中,定义一个string类型的二维数组chessboard,但是在 build 函数中的输出时,使用的是char类型的chessboard。这会导致编译错误。在 build 函数中,应该将所有的"●"和"○"替换成string类型的"●"和"○"。
其次,在 print 函数中,用于初始化棋盘的循环中,第一个for循环的范围是1到16,而第二个for循环的范围是1到15。这意味着第一个for循环会多跑一次,这可能会导致问题。
再者,在 change_p1 和 change_p2 函数中,当chessboard[x][y]不等于" "时,应该将chessboard[x][y]赋值为对应的玩家的棋子,而不是将chess的值减1。
最后,在judge_game函数中,第一个 if 语句中的判断条件是五个连续的"●",但是在循环中,chessboard[i-1][j-1]被赋值为 "●"。这意味着,如果chessboard[i-1][j-1] =="●",那么五个连续的"●"就变成了四个连续的"●"。同样的,在第二个if语句中,也有同样的问题。