对于一个小项目,我们需要做一个连接四个游戏。我们的项目被分成几个部分,每周我们被分配一个不同的部分去工作。
本周,我们必须在列上完成工作--我的意思是,我们必须使用一个名为get_column的函数,并使用它从将播放下一曲的用户那里读取一个有效的列号。
因此,我们得到了以下文件: connect4.h (用于存储函数的文件)、week8_object.o (忽略它的名称仅是当前一周的sem),以及week 8.c,这是我目前正在编辑的文件。
注释1:代码中的注释是讲师为我们写的便条。
在编译时,我会收到一个错误,即if(column_full(board, col)==FALSE)语句(FALSE part)的未声明标识符。我以为这是在.h文件中声明的?
编辑-在googling搜索后,我发现人们通过在标题中使用这个错误来保持沉默。与.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发布于 2015-09-19 12:17:33
在您的代码中很少有问题:
我不想给你写代码。从长远来看,如果你自己编码的话,这将对你很有帮助。
现在,回答你的问题:
发布于 2015-09-19 18:36:37
从您的原始代码:
}; /* <<-- problem 1 */
else() {
/* ^^^^ <<-- problem 2 */
printf("That column is full");
}删除这些代码,您的代码就会编译。我不会讨论逻辑问题。
因此,在差异表示法:
--- connect4.c.orig 2015-09-19 14:34:36.743337010 -0400
+++ connect4.c 2015-09-19 14:34:23.488222720 -0400
@@ -24,9 +24,9 @@
if(col>=1 && col<=COLS){
if(column_full(board, col)==FALSE){
- printf("You have placed a token in the %d column\n",col);
+ printf("You have placed a token in the %d column\n",col);
}
- else(){
+ else{
printf("That column is full");
}
}https://stackoverflow.com/questions/32667112
复制相似问题