首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C中的连接4游戏

C中的连接4游戏
EN

Stack Overflow用户
提问于 2015-09-19 10:28:22
回答 2查看 2.6K关注 0票数 2

对于一个小项目,我们需要做一个连接四个游戏。我们的项目被分成几个部分,每周我们被分配一个不同的部分去工作。

本周,我们必须在列上完成工作--我的意思是,我们必须使用一个名为get_column的函数,并使用它从将播放下一曲的用户那里读取一个有效的列号。

因此,我们得到了以下文件: connect4.h (用于存储函数的文件)、week8_object.o (忽略它的名称仅是当前一周的sem),以及week 8.c,这是我目前正在编辑的文件。

注释1:代码中的注释是讲师为我们写的便条。

在编译时,我会收到一个错误,即if(column_full(board, col)==FALSE)语句(FALSE part)的未声明标识符。我以为这是在.h文件中声明的?

编辑-在googling搜索后,我发现人们通过在标题中使用这个错误来保持沉默。与.h文件保持一致是否正确?:

代码语言:javascript
复制
#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);
}

头文件:

代码语言:javascript
复制
#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
EN

回答 2

Stack Overflow用户

发布于 2015-09-19 12:17:33

在您的代码中很少有问题:

  1. 在get_move()函数中,给定的条件“如果输入了无效的列,用户将被重新提示,直到它是有效的”没有得到注意。如果输入的列无效,您可能希望使用while循环。
  2. 代码中的column_full()是在没有参数的情况下调用的。此函数包含两个参数(请参阅.h文件)。所以,别忘了把它们加在电话里。
  3. 您必须使用return(col)来处理“返回1和COLS"之间的列号。

我不想给你写代码。从长远来看,如果你自己编码的话,这将对你很有帮助。

现在,回答你的问题:

  • 拥有嵌套的if-else语句是非常好的。
票数 0
EN

Stack Overflow用户

发布于 2015-09-19 18:36:37

从您的原始代码:

代码语言:javascript
复制
    };   /* <<-- problem 1 */
    else() {
    /* ^^^^ <<-- problem 2 */
        printf("That column is full");
    }

删除这些代码,您的代码就会编译。我不会讨论逻辑问题。

因此,在差异表示法:

代码语言:javascript
复制
--- 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");
         }
     }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32667112

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档