Do While循环退出错误的问题

时间:2014-03-04 21:13:31

标签: c++

我在创建这个游戏时遇到了一个问题,其目的是连续加入四个游戏。目前,程序在第一次达到while条件时就会退出,即使根据我的说法,它也不符合它并且应该再次执行Do While循环。请注意游戏尚未完成。

int main()
{
    cout << "\t \t \t Welcome to Four In A Row" << endl << endl;
    cout << "Player 1: Please enter your name: ";
    cin >> player1name;
    cout << endl;
    cout << "Player 2: Please enter your name: ";
    cin >> player2name;

    do
    {
        cout << player1name << " please enter point" << endl;
        cin >> p1x >> p1y;
        cout << endl;
        player1.setpoint(p1x, p1y); cout << endl;
        point[p1x][p1y] = 'x';
        wincheck(p1x, p1y);

        cout << player2name <<  " please enter point" << endl;
        cin >> p2x >> p2y;
        cout << endl;
        player2.setpoint2(p2x, p2y); cout << endl;
        point[p1x][p1y] = 'o';
        wincheck(p2x, p2y);

        if(wincheck != false) loopexit = 1;

    }while(loopexit == 0);

    return 0;
}

bool wincheck(int, int)
{
    int vertical = 1;
    int horizontal = 1;
    int diagonal1 = 1;
    int diagonal2 = 1;

    char player = point[p1x][p1y];
    int verticalcheck;
    int horizontalcheck;

    for(verticalcheck = p1x + 1; point[verticalcheck][p1y] == player && verticalcheck <= 5; verticalcheck++, vertical++);
    for(verticalcheck = p1y - 1; point[verticalcheck][p1y] == player && verticalcheck >= 0; verticalcheck--, vertical++);
    if(vertical >= 4) return true;

    for(horizontalcheck = p1y -1; point[p1x][horizontalcheck] == player && horizontalcheck >= 0; horizontalcheck--, horizontal++);
    for(horizontalcheck = p1y +1; point[p1x][horizontalcheck] == player && horizontalcheck <= 6; horizontalcheck++, horizontal++);
    if(horizontal>= 4) return true;

    for(verticalcheck = p1x -1, horizontalcheck = p1y -1; point[verticalcheck][horizontalcheck] == player && verticalcheck >= 0 && horizontalcheck >=0; diagonal1++, verticalcheck--, horizontalcheck--);
    for(verticalcheck = p1x +1, horizontalcheck = p1y +1; point[verticalcheck][horizontalcheck] == player && verticalcheck <= 5 && horizontalcheck <=6; diagonal1++, verticalcheck++, horizontalcheck++);
    if(diagonal1 >= 4) return true;

    for(verticalcheck = p1x -1, horizontalcheck = p1y +1; point[verticalcheck][horizontalcheck] == player && verticalcheck >= 0 && horizontalcheck <= 6; diagonal2++, verticalcheck--, horizontalcheck++);
    for(verticalcheck = p1x +1, horizontalcheck = p1y -1; point[verticalcheck][horizontalcheck] == player && verticalcheck <= 5 && horizontalcheck >=0 ; diagonal2++, verticalcheck++, horizontalcheck--);

    if(diagonal2 >= 4) return true;
    else return false;

1 个答案:

答案 0 :(得分:0)

我发现此程序中至少有三个错误:

  1. point[p1x][p1y] = 'o';应该使用p2xp2y代替
  2. 支票应为if (wincheck(p1x, p1y)) loopexit = 1;,并且检查p2x / p2y移动
  3. 相同
  4. 如果玩家1的移动结束了游戏,则您不想要求玩家2的移动。
  5. 现在的程序表现得很奇怪,因为你正在调用wincheck函数但忽略了结果。稍后你的测试

    if (wincheck != false)
    

    没有调用函数,只是推理函数地址和一些奇怪的C ++规则,不幸的是它可以与false进行比较(技术原因是false是一个常量的积分表达式值0因此可以作为空指针;为什么C ++对空指针有这样一个错综复杂的奇怪规则是一个没有人真正知道的秘密。)