椰Ye 2022-12-31 21:24 采纳率: 63.2%
浏览 51
已结题

c++简易五子棋游戏

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;
    }


运行打印了棋盘,然后输入坐标都没有反应了
应该是打印棋盘这个结构出了问题,但是找不到改法
  • 写回答

1条回答 默认 最新

  • ksgpjhqf 2022-12-31 23:22
    关注

    你好像把打印棋盘函数和初始化棋盘函数弄反了
    你的打印棋盘函数(19行)命名为build,初始化棋盘函数(39行)命名为print。每次输入坐标后就调用一次print,于是输入后立即初始化。

    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月6日
  • 创建了问题 12月31日

悬赏问题

  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含