2016-10-22 48 views
0

爲了練習我的陣列技能,我想我會做一個簡單的罐頭式井字遊戲,這個遊戲有六個動作。這裏的代碼:程序沒有正確地檢測到結束條件

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

#define N_COLS 3 
#define N_ROWS 4 
#define EMPTY '.' 

const char players[] = { 'X', 'O' }; 
const int n_moves = 6; 
const int moves[][2] = { { 0, 0 }, { 1, 1 }, { 0, 1 }, { 1, 2 }, { 0, 2 }, { 2, 
     2 } }; 

void empty(char board[][N_COLS]); 
void show(char* label, char board[][N_COLS]); 
void make_move(char board[][N_COLS], int row, int col, int move_index, 
     int *turn); 
bool game_is_over(char board[][N_COLS]); 

/* 
* Plays a single "game" using the moves encoded in the moves array (above). 
* The first move is for player X, the next for player O, etc. 
*/ 
int main(void) { 
    int turn = 0; 
    char board[N_ROWS][N_COLS]; 
    empty(board); 
    for (int ix = 0; ix < n_moves; ix++) { 
     make_move(board, moves[ix][0], moves[ix][1], ix, &turn); 
     if (game_is_over(board)) { 
      break; 
     } 
    } 
    return EXIT_SUCCESS; 
} 

/* 
* Displays the given label, then the contents of the board. 
*/ 
void show(char* label, char board[][N_COLS]) { 
    printf("%s: \n", label); 
    for (int row = 0; row < N_ROWS; row++) { 
     for (int col = 0; col < N_COLS; col++) { 
      printf(" %c", board[0][col]); 
     } 
     printf("\n"); 
    } 
    printf("\n"); 
} 

/* 
* Sets all squares in the provided board to EMPTY. 
*/ 
void empty(char board[][N_COLS]) { 
    for (int row = 0; row < N_ROWS; row++) { 
     for (int col = 0; col < N_COLS; col++) { 
      board[row][col] = EMPTY; 
     } 
    } 
    show("Ready to play, board empty", board); 
} 

/* 
* Puts the current player's token on the indicated spot on the board, 
* advances to the next player, and displays the current state of the board. 
*/ 
void make_move(char board[][N_COLS], int row, int col, int move_index, 
     int *turn) { 
    char label_buffer[20]; 
    board[row][col] = players[*turn]; 
    *turn = (*turn + 1) % 2; 
    sprintf(label_buffer, "After move %d", move_index + 1); 
    show(label_buffer, board); 
} 

/* 
* Checks whether there is a winner in a column (vertically). Returns 
* EMPTY if there is no winner; otherwise returns the winner's symbol. 
*/ 
char column_winner(char board[][N_COLS]) { 
    bool all_same; 
    for (int col = 0; col < N_COLS; col++) { 
     all_same = true; 
     for (int row = 1; row < N_ROWS; row++) { 
      if (board[row][col] != board[0][col]) { 
       all_same = false; 
       break; 
      } 
     } 
     if (all_same && board[0][col] != EMPTY) { 
      return board[0][col]; 
     } 
    } 
    return EMPTY; 
} 

/* 
* Checks whether there is a winner in a row (horizontally). Returns 
* EMPTY if there is no winner, otherwise returns the winner's symbol. 
*/ 
char row_winner(char board[][N_COLS]) { 
    bool all_same; 
    for (int row = 0; row < N_ROWS; row++) { 
     all_same = true; 
     for (int col = 1; col < N_COLS; row++) { 
      if (board[row][col] != board[row][0]) { 
       all_same = false; 
       break; 
      } 
     } 
     if (all_same && board[row][0] != EMPTY) { 
      return board[row][0]; 
     } 
    } 
    return EMPTY; 
} 

/* 
* Checks whether there is a winner. Returns EMPTY if there is no winner, 
* otherwise returns the winner's symbol. 
*/ 
char winner(char board[][N_COLS]) { 
    char winner = column_winner(board); 
    if (winner != EMPTY) { 
     return winner; 
    } 
    winner = row_winner(board); 
    if (winner != EMPTY) { 
     return winner; 
    } 
    return EMPTY; 
} 

/* 
* Checks whether the board is full (no EMPTY squares left). If so, returns 
* true, otherwise false. 
*/ 
bool is_full(char board[][N_COLS]) { 
    for (int row = 0; row < N_ROWS; row++) { 
     for (int col = 0; col < N_COLS; col++) { 
      if (board[row][col] == EMPTY) { 
       return false; 
      } 
     } 
    } 
    return false; 
} 

/* 
* Checks whether the game is over and returns true if so, false otherwise. 
* If there is a winner, indicates who won; if the board is full, says so. 
*/ 
bool game_is_over(char board[][N_COLS]) { 
    char win = winner(board); 
    if (win != EMPTY) { 
     printf("Winner: %c\n", win); 
     return true; 
    } 
    if (is_full(board)) { 
     printf("Board full, no winner.\n"); 
     return true; 
    } 
    return false; 
} 

不幸的是,有一些奇怪的錯誤,我似乎無法修復。首先,遊戲應該以不同的方式進行,但每一次移動都只有X移動,而O不會第二次,遊戲應該在移動5之後結束,當棋盤被填滿時,但它不會檢測到,並且用完剩下的動作(在這種情況下,move = 6)

我真的很茫然。對於任何反饋,我們都表示感謝。

回答

1

這裏你在這兩種情況下都返回false

bool is_full(char board[][N_COLS]) { 
    for (int row = 0; row < N_ROWS; row++) { 
     for (int col = 0; col < N_COLS; col++) { 
      if (board[row][col] == EMPTY) { 
       return false; 
      } 
     } 
    } 
    return false; // Probably should be: return true 
} 

最後一行也許應該是:return true;