为了练习阵列技能,我想我会制作一个简单的罐装井字游戏,可以玩六个动作。这是代码:
#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之后结束,当棋盘被填满时,但它并没有#39; t检测到,并用尽其余的动作(在这种情况下,move = 6)
我真的很茫然。任何反馈都将不胜感激。
答案 0 :(得分: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;