井字棋小游戏强制退出什么原因

问题遇到的现象和发生背景

井字棋的小游戏,我发现有的时候能正常运行判断结果,而有的时候会出现我下完棋后,按两下下enter直接退出显示按任意键继续,这种情况会是什么原因呢?

问题相关代码,请勿粘贴截图
#include<iostream>

using namespace std;
typedef pair<int, int>PII;

char g[100][100];

void init(int n)//初始化棋盘函数
{
    for (int i = 2; i <= n + 1; i++)
        for (int j = 2; j <= n + 1; j++)
            g[i][j] = '*';
}
bool check(int n)//检测棋盘是否占尽
{
    for (int i = 2; i <= n+1; i++)
        for (int j = 2; j <= n+1; j++)
            if (g[i][j] == '*')return false;
    return true;
}
void show(int n)//展示棋盘
{
    for (int i = 2; i <= n+1; i++)
    {
        for (int j = 2; j <= n+1; j++)
            cout << "|" << g[i][j];
        cout << endl;
    }
}
bool judge(char a,char b ,int i,int j)//判断字符
{
    if (g[i + 1][j] == a && g[i + 2][j] == b)return true;//所在列
    else if (g[i + 1][j] == a && g[i - 1][j] == b)return true;
    else if (g[i - 1][j] == a && g[i - 2][j] == b)return true;
    else if (g[i][j + 1] == a && g[i][j + 2] == b) return true;//所在行
    else if (g[i][j + 1] == a && g[i][j - 1] == b)return true;
    else if (g[i][j - 1] == a && g[i][j - 2] == b)return true;
    else if (g[i - 1][j - 1] == a && g[i + 1][j + 1] == b) return true;//正斜线
    else if (g[i - 1][j - 1] == a && g[i - 2][j - 2] == b) return true;
    else if (g[i + 1][j + 1] == a && g[i + 2][j + 2] == b)return true;
    else if (g[i - 1][j + 1] == a && g[i + 1][j - 1] == b)return true;//反斜线
    else if (g[i + 1][j - 1] == a && g[i + 2][j - 2] == b)return true;
    else if (g[i - 1][j + 1] == a && g[i - 2][j + 2] == b)return true;
    return false;
}
PII chess(char c1,char c2,int n)
{
    int w = 0,x,y;
    for (int i = 2; i <= n+1; i++)
    {
        for (int j = 2; j <= n+1; j++)
        {
            int temp = 0;
            if (g[i][j] == '*')
            {
                if (judge(c2, c2, i, j))temp += 50;
                else if (judge(c1, c1, i, j))temp += 25;
                else if (judge(c2, '*', i, j))temp += 10;
                else if (judge(c1, '*', i, j))temp += 8;
                else if (judge('*', '*', i, j))temp += 4;
                if (temp > w)
                {
                    w = temp;
                    x = i; y = j;
                }
            }
        }
    }
    return { x,y };
}
int main()
{
    cout << endl;
    cout << "=======欢迎登陆井字棋小游戏======" << endl;
    cout << "          祝您游戏愉快!" << endl;

label1:cout << "      1.###开始游戏###.1" << endl;
    cout << "      2.###结束游戏###.2" << endl;
    for (int i = 0; i <10; i++)
    {
        for (int j = 0; j <10; j++)g[i][j] = '.';
    }
    int op1; cin >> op1;
    if (op1 == 1)
    {
        cout << "请选择棋盘边长:(3-5)" << endl;
        int x; cin >> x;
        init(x);//初始化棋盘
        cout << "你选择 X 还是 O" << endl;
        cout << "1.X 2.O" << endl;
        int op3; cin >> op3;
        int pos1 = 0, pos2 = 0;//棋盘坐标
        char c1,c2;//c1代表玩家棋子符号,c2代表电脑
        if (op3 == 1)
        {
            c1 = 'X';
            c2 = 'O';
        }
        else
        {
            c1 = 'O';
            c2 = 'X';
        }
            while (1)
            {
                cout << "请选择下棋的位置输入坐标" << endl;
                cin >> pos1 >> pos2;
                g[pos1+1][pos2+1] = c1;//你下了一个位置
                //玩家先出,所以玩家可能先赢,所以玩家出完,立马判断
                for (int i = 2; i <= x + 1; i++)
                {
                    for (int j = 2; j <= x + 1; j++)
                    {
                        if (g[i][j] == c1)
                        {
                            if (judge(c1, c1, i, j))
                            {
                                cout << "恭喜你,你赢了" << endl;
                                goto label1;
                            }
                        }
                    }
                }
                if (check(x))
                {
                    cout << "平局" << endl;
                    goto label1;
                }
                PII temp = chess(c1, c2, x);
                g[temp.first][temp.second] = c2;//电脑下了一个位置
                show(x);
                cout << endl;
                for (int i = 2; i <= x + 1; i++)
                {
                    for (int j = 2; j <= x + 1; j++)
                    {
                        if (g[i][j] == c1)
                        {
                            if (judge(c1, c1, i, j))
                            {
                                cout << "恭喜你,你赢了" << endl;
                                goto label1;
                            }
                            
                        }
                    }
                }
                if (check(x))
                {
                    cout << "平局" << endl;
                    goto label1;
                }
                for (int i = 2; i <= x + 1; i++)
                {
                    for (int j = 2; j <= x + 1; j++)
                    {
                        if (g[i][j] == c2)
                        {
                            if (judge(c2, c2, i, j))
                            {
                                cout << "很遗憾,你输了" << endl;
                                goto label1;
                            }
                            
                        }
                    }
                }
                    
            }
        }

    else exit(1);
    return 0;
}

运行结果及报错内容

img


img


最后一张图在输入后没有继续执行

我的解答思路和尝试过的方法

核心时让电脑学会下棋,主要为judge函数,用来判断对于可以下棋的点也就是'*'的地方,判断周围的符号,每两种符号对应不同的权值

我想要达到的结果

解决输入后强制退出的情况

问题出在chess函数,在你这种输入顺序下,输入3 2时返回的是-1,0.然后就出错了,修改很简单,如图,改为大于等于就行了,可以保证x和y是可以赋予初始值,另外就是你的判断里没有对已存在棋子的地方不允许下棋的判定,这需要进一步优化

img

你没有对已下的位置做判断,所以输入了一个已下的位置,程序就异常崩溃了

这篇文章讲的很详细,请看:井字棋小游戏

#include<iostream>

using namespace std;
typedef pair<int, int>PII;

char g[100][100];

void init(int n)//初始化棋盘函数
{
    for (int i = 2; i <= n+1; i++)
    for (int j = 2; j <= n+1; j++)
        g[i][j] = '*';
}
bool check(int n)//检测棋盘是否占尽
{
    for (int i = 2; i <= n+1; i++)
    for (int j = 2; j <= n+1; j++)
        if (g[i][j] == '*') return false;
    return true;
}
void show(int n)//展示棋盘
{
    for (int i = 2; i <= n+1; i++) {
        for (int j = 2; j <= n+1; j++) cout << "|" << g[i][j];
        cout << endl;
    }
}
bool judge(char a,char b,int i,int j)//判断字符
{
         if (g[i + 1][j    ] == a && g[i + 2][j    ] == b) return true;//所在列
    else if (g[i + 1][j    ] == a && g[i - 1][j    ] == b) return true;
    else if (g[i - 1][j    ] == a && g[i - 2][j    ] == b) return true;
    else if (g[i    ][j + 1] == a && g[i    ][j + 2] == b) return true;//所在行
    else if (g[i    ][j + 1] == a && g[i    ][j - 1] == b) return true;
    else if (g[i    ][j - 1] == a && g[i    ][j - 2] == b) return true;
    else if (g[i - 1][j - 1] == a && g[i + 1][j + 1] == b) return true;//正斜线
    else if (g[i - 1][j - 1] == a && g[i - 2][j - 2] == b) return true;
    else if (g[i + 1][j + 1] == a && g[i + 2][j + 2] == b) return true;
    else if (g[i - 1][j + 1] == a && g[i + 1][j - 1] == b) return true;//反斜线
    else if (g[i + 1][j - 1] == a && g[i + 2][j - 2] == b) return true;
    else if (g[i - 1][j + 1] == a && g[i - 2][j + 2] == b) return true;
    return false;
}
PII chess(char c1,char c2,int n) {
    int w = 0,x,y;
    for (int i = 2; i <= n+1; i++) {
    for (int j = 2; j <= n+1; j++) {
        int temp = 0;
        if (g[i][j] == '*') {
                 if (judge( c2,  c2, i, j)) temp += 50;
            else if (judge( c1,  c1, i, j)) temp += 25;
            else if (judge( c2, '*', i, j)) temp += 10;
            else if (judge( c1, '*', i, j)) temp +=  8;
            else if (judge('*', '*', i, j)) temp +=  4;
            if (temp > w) {
                w = temp;
                x = i;
                y = j;
            }
        }
    }
    }
    return { x,y };
}
int main() {
    cout << endl;
    cout << "=======欢迎登陆井字棋小游戏======" << endl;
    cout << "          祝您游戏愉快!" << endl;

label1:
    cout << "      1.###开始游戏###.1" << endl;
    cout << "      2.###结束游戏###.2" << endl;
    for (int i = 0; i <10; i++)
    for (int j = 0; j <10; j++)
        g[i][j] = '.';
    int op1;
    cin.clear();
    cin.sync();
    cin >> op1;
    if (op1 == 1) {
        cout << "请选择棋盘边长:(3-5)" << endl;
        int x;
        cin.clear();
        cin.sync();
        cin >> x;
        init(x);//初始化棋盘
        cout << "你选择 X 还是 O" << endl;
        cout << "1.X 2.O" << endl;
        int op3;
        cin.clear();
        cin.sync();
        cin >> op3;
        int pos1 = 0, pos2 = 0;//棋盘坐标
        char c1,c2;//c1代表玩家棋子符号,c2代表电脑
        if (op3 == 1) {
            c1 = 'X';
            c2 = 'O';
        } else {
            c1 = 'O';
            c2 = 'X';
        }
        while (1) {
            cout << "请选择下棋的位置输入坐标" << endl;
            cin.clear();
            cin.sync();
            cin >> pos1 >> pos2;
            g[pos1+1][pos2+1] = c1;//你下了一个位置
            //玩家先出,所以玩家可能先赢,所以玩家出完,立马判断
            for (int i = 2; i <= x + 1; i++)
            for (int j = 2; j <= x + 1; j++) {
                if (g[i][j] == c1) {
                    if (judge(c1, c1, i, j)) {
                        cout << "恭喜你,你赢了" << endl;
                        goto label1;
                    }
                }
            }
            if (check(x)) {
                cout << "平局" << endl;
                goto label1;
            }
            PII temp = chess(c1, c2, x);
            g[temp.first][temp.second] = c2;//电脑下了一个位置
            show(x);
            cout << endl;
            for (int i = 2; i <= x + 1; i++)
            for (int j = 2; j <= x + 1; j++) {
                if (g[i][j] == c1) {
                    if (judge(c1, c1, i, j)) {
                        cout << "恭喜你,你赢了" << endl;
                        goto label1;
                    }

                }
            }
            if (check(x)) {
                cout << "平局" << endl;
                goto label1;
            }
            for (int i = 2; i <= x + 1; i++)
            for (int j = 2; j <= x + 1; j++) {
                if (g[i][j] == c2) {
                    if (judge(c2, c2, i, j)) {
                        cout << "很遗憾,你输了" << endl;
                        goto label1;
                    }
                }
            }
        }
    } else exit(1);
    return 0;
}