c++简易五子棋游戏
游戏的每一个结构都写好了,拼在一起就跑不动了,不知道问题出在哪个地方
#include<iostream>
#include<string>
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;
}
运行打印了棋盘,然后输入坐标都没有反应了
应该是打印棋盘这个结构出了问题,但是找不到改法
我想要达到的结果,如果你需要快速回答,请尝试 “付费悬赏”