2014-03-04 4 views
1

4 개의 행을 조인하는 것이 목표 인이 게임을 만드는 데 문제가 있습니다. 현재,이 프로그램은 처음에 while 조건에 도달하면 종료됩니다. 그러나 나에게 맞지 않더라도 Do 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

변수 loopexit의 초기 값은 무엇입니까? while 조건은 loopexit이 0인지 확인하지만 게시 한 코드의 어느 곳에서도 0으로 설정하지 않습니다. –

+0

메인 외부에서 0으로 설정되었습니다. 나머지 코드는 불필요하다고 생각했습니다. – user3380905

답변

0

나는 적어도 세 가지 오류가이 프로그램에 있습니다 참조 :

  1. point[p1x][p1y] = 'o'; 아마 p2xp2y 대신
  2. 이 체크가 if (wincheck(p1x, p1y)) loopexit = 1;해야 사용해야하고 p2x/p2y 이동을 확인하기위한 동일
  3. 플레이어 1의 이동이 끝나면 플레이어 2의 이동을 묻지 않아도됩니다.

wincheck 함수를 호출했지만 결과를 무시하기 때문에 프로그램이 이상하게 작동합니다. 나중에 테스트

if (wincheck != false) 

이 함수를 호출하는 것이 아니라, 단지 함수 주소에 대해 몇 가지 이상한 C에 대한 추론 ++가 불행하게도 (기술적 인 이유는 false 가치의 지속적인 통합 표현이다 false 비교 될 수있는 규칙 0 따라서 null 포인터를 의도 할 수 있습니다; 왜 C++ null 포인터에 대한 복잡하고 이상한 규칙을 가지고 아무도 모르는 비밀입니다).

+0

감사합니다! 이것은 효과가 있었다. 제 대신에 어리석은 실수. – user3380905

관련 문제