Pergunta

I'm having an issue creating this game where the aim is to join four in a row. Currently, the program just exits when it reaches the while condition the first time, even though according to me, it does not meet it and should do the Do While loop again. Please note the game is not finished yet.

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;
Foi útil?

Solução

I see there are at least three errors in this program:

  1. point[p1x][p1y] = 'o'; should probably use p2x and p2y instead
  2. The check should be if (wincheck(p1x, p1y)) loopexit = 1; and the same for checking p2x/p2y move
  3. If move of player 1 ended the game you don't want to ask for the move of player 2.

The program as it is now is behaving strangely because you're calling the wincheck function but are ignoring the result. Later your test

if (wincheck != false)

is not calling the function, but just reasoning about the function address and for some strange C++ rules it can unfortunately be compared to false (the technical reason is that false is a constant integral expression of value 0 and therefore can be intended to be the null pointer; why C++ has such a convoluted and strange rule for the null pointer is a secret no one really knows).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top