gpt4 book ai didi

在 C 中连接 4 个游戏

转载 作者:太空宇宙 更新时间:2023-11-04 00:03:42 26 4
gpt4 key购买 nike

对于一个小项目,我们需要制作一个四人连线游戏。我们的项目分为几个部分,每周我们都会分配不同的部分来处理。

本周我们必须通过这个来弥补栏目上的工作我的意思是我们必须使用一个名为 get_column 的函数,并使用它从用户那里读取一个有效的栏目编号,下一个轨道将在该栏目中播放。

因此,我们获得了以下文件 connect4.h(用于存储函数的文件)、week8_object.o(忽略名称,它只是 sem 的当前周)和 week8.c,这是我当前正在编辑的文件。

注意1:代码中的注释是讲师给我们写的,作为注释。

当我编译时,我收到一条错误消息,指出 if(column_full(board, col)==FALSE) 语句(FALSE 部分)的标识符未声明。我以为这是在 .h 文件中声明的?

编辑- 经过一些谷歌搜索后,我发现人们通过在标题中包含此错误来消除该错误。将其与 .h 文件保持一致是否正确?:

#include <stdio.h>
#include "connect4.h"
#define FALSE 0
#define TRUE 1

/* get_move

Prompts the user to enter a column, then checks that
- the column is in the valid range (1-COLS)
- that the column is not full (use function column_full to check)

If an invalid column is entered, the user is reprompted until it is valid

Returns the column number between 1 and COLS
*/

int column_full ( int board[COLS][ROWS], int col ) { return TRUE;}

int get_move ( int board[COLS][ROWS] ){

int col;

printf("Please enter a column number:");
scanf("%d",&col);

if(col>=1 && col<=COLS){
if(column_full(board, col)==FALSE){
printf("You have placed a token in the %d column\n",col);
}
else{
printf("That column is full");
}
}
while(col<=0 || col>COLS){
printf("Your token has not been placed");
printf("Please enter a valid column: ");
scanf("%d",&col);
}
return(col);
}

头文件:

#ifndef CONNECT4_H
#define CONNEXT4_H 1

#define ROWS 6
#define COLS 7

// displays the board to the screen
int display_board ( int[COLS][ROWS] ) ;

// sets up the board to an empty state
int setup_board ( int[COLS][ROWS] ) ;

// Returns TRUE if the specified column in the board is completely full
// FALSE otherwise
// col should be between 1 and COLS
int column_full ( int[COLS][ROWS], int col ) ;

// prompts the user to enter a move, and checks that it is valid
// for the supplied board and board size
// Returns the column that the user has entered, once it is valid (1-COLS)
int get_move ( int[COLS][ROWS] ) ;

// adds a token of the given value (1 or 2) to the board at the
// given column (col between 1 and COLS inclusive)
// Returns 0 if successful, -1 otherwise
int add_move ( int b[COLS][ROWS], int col, int colour ) ;

// determines who (if anybody) has won. Returns the player id of the
// winner, otherwise 0
int winner ( int[COLS][ROWS] ) ;

// determines if the board is completely full or not
int board_full ( int[COLS][ROWS] ) ;


#endif

最佳答案

您的代码中几乎没有问题:

  1. 在 get_move() 函数中,给定的条件“如果输入的列无效,将重新提示用户,直到它有效” 没有得到注意。如果输入的列无效,您可能希望使用 while 循环。

  2. 在您的代码中调用 column_full() 时不带参数。这个函数有两个参数(引用你的 .h 文件)。所以,不要忘记在通话中添加它们。

  3. 您必须使用 return(col) 来处理“返回 1 和 COLS 之间的列号”

我不想为您编写代码。如果您自己编写代码,从长远来看,这会对您有所帮助。

现在,回答你的问题:

  • 嵌套 if-else 语句非常好。

关于在 C 中连接 4 个游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32667112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com