2016-10-27 4 views
1

이 코드의 for 루프는 끝나지 않고 무한 루프에 고정되어 있지만 그 이유는 알 수 없습니다. for 루프 내에서 할당을 수행 할 때 i 대신 임시 변수를 사용하여 시도했지만 그 중 하나가 작동하지 않았습니다.C - 무한 루프 용

포인터가 10 크기의 배열을 가리키고 나머지 코드는 for 루프가 없어도 정상적으로 실행됩니다. 또한 컴퓨터는 0만을 추측합니다. 나는 이것이 내 부분에 멍청한 실수라고 생각하지만 나는 그것을 알아낼 수 없다.

편집 : 단지 루프의 어느 시점에서 포스트

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 

void setBoard(int *board); 
void setComputerBoard(int *board); 
int playGame(int *human, int *computer); 

int main() { 
    srand(time(NULL)); 

    int humanBoard[10]; 
    int *human = humanBoard; 

    int computerBoard[10]; 
    int *computer = computerBoard; 

    setBoard(human); 
    setComputerBoard(computer); 

    int winner = playGame(human, computer); 

    if (winner == 0) 
     printf("Computer Wins!\n"); 
    else 
     printf("Human Wins!\n"); 

    return 0; 
} 

void setBoard(int *board) { 
    int pos1, pos2, i; 
    printf("Enter 1st position: "); 
    scanf("%d", &pos1); 

    while (pos1 > 9 || pos1 < 0) { 
     printf("Enter 1st position: "); 
     scanf("%d", &pos1); 
    } 

    printf("Enter 2nd position: "); 
    scanf("%d", &pos2); 

    while ((pos2 > 9) || (pos2 < 0) || (abs(pos2 - pos1) != 1)) { 
     printf("Enter 2nd position: "); 
     scanf("%d", &pos2); 
    } 

    for (i = 0; i < 10; i++) { 
     printf("%d", i); 
     *(board + i) = 0; 
    } 

    *(board + pos1) = 2; 
    *(board + pos2) = 2; 
} 

void setComputerBoard(int *board) { 
    int pos1 = rand() % 10, pos2; 
    if (pos1 == 9) 
     pos2 = pos1 - 1; 
    else 
     pos2 = pos1 + 1; 

    int i; 
    for (i = 0; i < 10; i++) { 
     if (i != pos1 && i != pos2) 
      *(board + i) = 0; 
     else 
      *(board + i) = 2;; 
    } 
} 

int playGame(int *human, int *computer) { 
    int winner = -1; 
    int computerShot, humanShot; 
    int computerHit = 0, humanHit = 0; 
    int i; 

    while (winner == -1) { 
     computerShot = humanShot = -1; 

     printf("Computer guesses "); 
     do { 
      computerShot = rand() % 10; 
     } while ((*(human + computerShot) != 1) && (*(human + computerShot) != 3)); 

     printf("%d\n", computerShot); 
     if (*(human + computerShot) == 2) { 
      printf("HIT!\n"); 
      *(human + computerShot) = 3; 
      computerHit++; 
     } else { 
      printf("MISS!\n"); 
      *(human + computerShot) = 1; 
     } 
     printf("Human Board: \n"); 
     printf("0 1 2 3 4 5 6 7 8 9\n"); 
     for (i = 0; i < 10; i++) { 
      if (*(human + i) == 0) 
       printf("* "); 
      else 
      if (*(human + i) == 1) 
       printf("M "); 
      else 
      if (*(human + i) == 2) 
       printf("S "); 
      else 
       printf("H "); 
     } 

     printf("\nComputer Board: \n"); 
     printf("0 1 2 3 4 5 6 7 8 9\n"); 
     for (i = 0; i < 10; i++) { 
      if (*(computer + i) == 0) 
       printf("* "); 
      else 
      if (*(computer + i) == 1) 
       printf("M "); 
      else 
      if (*(computer + i) == 2) 
       printf("S "); 
      else 
       printf("H "); 
     } 

     printf("\nEnter guess: "); 
     scanf("%d", &humanShot); 
     printf("You guessed %d\n", humanShot); 
     if (*(computer + humanShot) == 2) { 
      printf("HIT!\n"); 
      *(computer + humanShot) == 3; 
      humanHit++; 
     } else { 
      printf("MISS!\n"); 
      *(computer + humanShot) == 1; 
     } 

     printf("Human Board: \n"); 
     printf("0 1 2 3 4 5 6 7 8 9\n"); 
     for (i = 0; i < 10; i++) { 
      if (*(human + i) == 0) 
       printf("* "); 
      else 
      if (*(human + i) == 1) 
       printf("M "); 
      else 
      if (*(human + i) == 2) 
       printf("S "); 
      else 
       printf("H "); 
     } 
     printf("\n"); 

     printf("Computer Board: \n"); 
     printf("0 1 2 3 4 5 6 7 8 9\n"); 
     for (i = 0; i < 10; i++) { 
      if (*(computer + i) == 0) 
       printf("* "); 
      else 
      if (*(computer + i) == 1) 
       printf("M "); 
      else 
      if (*(computer + i) == 2) 
       printf("S "); 
      else 
       printf("H "); 
     } 
     printf("\n"); 
     if (computerHit == 2) 
      winner = 0; 
     else 
     if (humanHit == 2) 
      winner = 1; 
    } 
} 
+3

하는 루프가 1 또는 3과 동일한 않을 때 촬영을 다시 실행 (이미 치거나 각각 그리워) 할 때문에 동안의 조건은

while ((*(human+computerShot == 1) || (*(human+computerShot) == 3)); 

로 변경해야? 당신의 의견은 무엇입니까? MCVE 보여줘. –

+1

setBoard 호출자가 스택에서 가리키는 데이터를 가지고있는 경우 메모리 덮어 쓰기가 가능합니까? – Torp

+0

제쳐두고 :'do {} while (condition);'-loop을보세요. 또한'scanf'의 리턴 값을 확인하십시오! – Deduplicator

답변

0

에 모든 코드를 당겨, i 값은 포인터가 제로로 변경됩니다. 또한 코드는 pos1과 pos2를 0으로 지정합니다.

1

문제는 for 루프가 아니며 playGame 함수의 do-while 루프입니다. 당신이

+0

내가 ((* (human + computerShot == 1) || (human + computerShot) == 3)) while로 변경하면 그 코드는 무한 루프에 갇혀있다. – Bruailen

+0

이상하다. 괜찮습니다. 여러분의 코드를 축 어적으로 실행하지 않을 경우를 대비하여 자세히 살펴 보겠습니다. – theKunz

+1

@bruailen은 printf ("% d", i) 행은 수동 호출로 플러시 될 때까지 인쇄하지 않습니다 어쨌든, 나는 여러분의 코드를 그대로 복사하고 위에서 언급 한 행을 변경하고 잘 동작했다. – theKunz