| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 983 人关注过本帖
标题:求助,小学期,智能猜扑克游戏代码,都有什么错误,可以有偿
只看楼主 加入收藏
望舒
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2021-7-15
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
求助,小学期,智能猜扑克游戏代码,都有什么错误,可以有偿
智能猜扑克牌游戏设计
实践内容:计算机从一副扑克牌(54张)中任意抽出27张, 摆放在不同的三行上(每行9张),用户“暗记”某张纸牌,而后告诉计算机所“暗记”的那张纸牌处于哪一行中;之后计算机再两次将纸牌重新摆放,并让用户再重复回答两次相同的提问(那张纸牌在重新摆放后又处在哪一行上);此时计算机将用户所“暗记”的那张纸牌挑出来。
要求:
(1)、游戏要有主菜单,如进入游戏、输入用户名、显示计算机几次猜出了暗记牌、退出游戏。
(2)、源程序要有适当的注释,使程序容易阅读。
(3)、游戏中,如暗记、重新摆放、是否正确等功能要有相应的文字提示。
(4)、要从一副54张的扑克牌中任意抽出27张,没有抽中的牌在本次中不再出现。
(5)、游戏准许多个人多次玩游戏,并具有统计功能,可实现在后台记录每次玩的用户名和该用户玩游戏时计算机是几次猜出暗记牌的。(至少测试6个用户,并给出不同次数猜出暗记牌的统计信息)

代码:(错误提示在后面)
#include <stdio.h>
#include <time.h>
#include<stdlib.h>

struct poker{
    int val ; // 1(A),2,3,4,5,6,7,8,9,10,11(J),12(Q),13(K)
    int kind ; // 0 , 1 , 2 , 3
    int is_joker ; // 0不是王, 1小王,2大王
    int cv ; // 被选中次数
};

const char kind_name[5][20] = { " ", "Spade", "Heart", "Diamond", "Club" } ; // 黑,红,片,花
const char val_name[20][3] = { " ", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" } ;
int history_num = 0 ; // 一共玩了多少轮游戏
int pos[4][20] ; // 此次输出每个位置的扑克是哪一张
poker history[100] ; // 记录每次游戏的选择
poker card[27] ; // 被选出的27张扑克
poker card_list[54] ; // 所有54张扑克

void printPoker( struct poker p ) { // 输出扑克p信息
    if ( p.is_joker == 1 ) { // 是小王
        printf( "joker 1 " ) ;
    }
    else if ( p.is_joker == 2 ) { // 是大王
        printf( "JOKER 2 " ) ;
    }
    else { // 其他牌
        printf("%s %s ", kind_name[p.kind], val_name[p.val] ) ;
    }
}
void outInfermation() { // 输出个人信息
    int T=100;
    while ( T-- ) { // 输出上边框
        printf( "*" ) ;
    }
    printf("\n姓名:%s\t学号:%s\t班级:%s\t完成时间:", "小明","222018????","通信工程一班" ) ;
    time_t timep;   time (&timep);  printf("%s\n",ctime(&timep)); // 输出当前时间
    T = 100 ;
    while ( T-- ) { // 输出下边框
        printf( "*" ) ;
    }
    printf( "\n" ) ;
}
void outMenu() { // 输出功能菜单
    printf( "\n\n\n\*****************游戏菜单*********************************\n" ) ;
    printf( "1.开始游戏\n" ) ;
    printf( "2.查看历史记录\n" ) ;
    printf( "3.退出游戏\n" ) ;
    printf( "**********************************************************\n\n" ) ;
}
void outHistory() { // 输出历史记录(每次游戏用户选中的牌)
    int i;
    if ( history_num == 0 ) {
        printf( "历史记录为空\n" ) ;
    }
    for (i=0; i<history_num; i++ ) {
        printf( "第%d次游戏玩家选择的扑克是:", i+1 ) ;
        printPoker( history[i] ) ;
        printf( "\n" ) ;
    }
}
void Init() { // 初始化信息
    history_num = 0 ; // 历史记录清零
    int i, j, n=0 ;
    for (i=1; i<=4; i++) { // 为card_list数组的52张牌初始化
        for (j=1; j<=13; j++) {
            card_list[n].val = j ;
            card_list[n].kind = i ;
            card_list[n].is_joker = 0 ;
            card_list[n].cv = 0 ;
            n++ ;
        }
    }
    card_list[n].cv = 0 ;   card_list[n++].is_joker = 1 ; // 小王
    card_list[n].cv = 0 ;   card_list[n++].is_joker = 2 ; // 大王
    for(i=0;i<54;i++){
        printPoker( card_list[i] ) ;
    }
}
void creatCardList() { // 随机从54张牌中选出27张,被选出的牌存在card数组中
    //每次选择的策略为:随机生成一个数字x,从当前余剩的牌中,数到第x张牌,就是此次被选中的牌。重复27次即可
    int vis[54] = {0}, i, j, rest = 54 ;
    for (i=0; i<27; i++) { // 进行27次选择
        int pos = rand()%rest, now = 0 ;
        for (j=0; j<54; j++) {
            if ( now == pos && vis[j]==0 ) { // 数到了第pos张牌
                break ;
            }
            if ( vis[j] == 0 ) { // 该牌未被选择
                now++ ;
            }
        }
        vis[j] = 1 ; // 标记次张牌已被选中
        card[i] = card_list[j] ; // 把这张牌放入选中的27张牌中
        rest-- ;
    }
}
poker getCard( int turn , int num ) { // 猜用户选中的扑克牌(turn 表示是第几轮猜牌,num表示这一轮每行需要放置多少张cv值为turn-1的牌 )
    int i, j, k=0 ;
    printf( "第%d轮游戏开始!\n", history_num+1 ) ;
    printf( "第%d波筛选:\n" , turn ) ;

    ///按照规则放置扑克(3行,每行只能放num张choose_cnt值为turn-1的牌)
    int vis[27] = {0} ; // vis[i]表示第i张牌是否已经为其分配位置

    for (i=1; i<=3; i++) {
        int rest = num , rest2 = 9-num ; // 当前行还需要放rest张cv值为turn-1的牌,还需要放rest2张cv值小于turn-1的牌
        k = 0 ; // k表示当前第i行已经放了多少张牌
        for (j=0; j<27; j++) { // 考察第j张牌是否可以放入第i行
            if ( vis[j] == 1 ) continue ;
            if ( card[j].cv == turn-1 ) { // vc==true-1的牌
                if ( rest>0 ) {
                    k++ ;
                    pos[i][k] = j ;
                    vis[j] = 1 ;
                    rest--;
                }
            }
            else { // vc<true-1的牌
                if ( rest2>0 ) {
                    k++;
                    pos[i][k] = j ;
                    vis[j] = 1 ;
                    rest2--;
                }
            }
        }
    }

    ///输出摆放情况并等待用户选择
    printf( "请从下列扑克中选出一张,并输入它的所在行数\n" ) ;
    for (i=1; i<=3; i++) {
        printf( "# line %d :   ", i ) ;
        for (j=1; j<=9; j++) {
            printPoker( card[pos[i][j]] ) ;
            printf("  ");
        }
        printf("\n");
    }
    int choose_line = 0 ;
    scanf( "%d", &choose_line ) ;

    /// 标记被选中的行数的扑克
    for (i=1; i<=9; i++) {
        card[ pos[choose_line][i] ].cv++ ;
    }

    ///返回答案
    if ( turn == 3 ) { // 如果是最后一轮就返回标记次数为3的唯一一张牌
        for (i=0; i<27; i++) {
            if ( card[i].cv == 3 ) return card[i] ;
        }
    }
    else { // 否则继续进行下一轮游戏
        return getCard( turn+1 , num/3 ) ;
    }
}
poker playGame() { // 玩游戏
    creatCardList(); // 随机选出27个卡片
    struct poker ret ;
    ret = getCard( 1 , 9 ) ; // 进行第一轮游戏
    return ret ;
}

int main()
{
    outInfermation(); // 输出个人信息
    Init(); // 扑克信息初始化
    int op = 0 ;
    while ( op != 3 ) {
        outMenu(); // 输出功能菜单
        scanf( "%d", &op ) ;
        if ( op == 1 ) { // 开始游戏
            history[history_num] = playGame() ;
            printf("游戏结束:\n你选择的扑克是:");
            printPoker( history[history_num] ) ;
            printf( "\n" ) ;
            history_num++ ;
        }
        else if ( op == 2 ) { // 输出历史游戏信息
            outHistory();
        }
        else if ( op == 3 ) { // 退出游戏
            return 0 ;
        }
        else { // 输入不合法
            printf( "输入功能标号不合法,请重新输入:\n" ) ;
        }
    }
    return 0;
}


--------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.c
D:\测试\test.c(16) : error C2061: syntax error : identifier 'history'
D:\测试\test.c(16) : error C2059: syntax error : ';'
D:\测试\test.c(16) : error C2059: syntax error : '['
D:\测试\test.c(17) : error C2061: syntax error : identifier 'card'
D:\测试\test.c(17) : error C2059: syntax error : ';'
D:\测试\test.c(17) : error C2059: syntax error : '['
D:\测试\test.c(18) : error C2061: syntax error : identifier 'card_list'
D:\测试\test.c(18) : error C2059: syntax error : ';'
D:\测试\test.c(18) : error C2059: syntax error : '['
D:\测试\test.c(37) : error C2275: 'time_t' : illegal use of this type as an expression
        c:\program files (x86)\microsoft visual studio\vc98\include\time.h(79) : see declaration of 'time_t'
D:\测试\test.c(37) : error C2146: syntax error : missing ';' before identifier 'timep'
D:\测试\test.c(37) : error C2065: 'timep' : undeclared identifier
D:\测试\test.c(45) : warning C4129: '*' : unrecognized character escape sequence
D:\测试\test.c(58) : error C2065: 'history' : undeclared identifier
D:\测试\test.c(58) : error C2109: subscript requires array or pointer type
D:\测试\test.c(58) : error C2115: 'function' : incompatible types
D:\测试\test.c(58) : warning C4024: 'printPoker' : different types for formal and actual parameter 1
D:\测试\test.c(64) : error C2143: syntax error : missing ';' before 'type'
D:\测试\test.c(65) : error C2065: 'i' : undeclared identifier
D:\测试\test.c(66) : error C2065: 'j' : undeclared identifier
D:\测试\test.c(67) : error C2065: 'card_list' : undeclared identifier
D:\测试\test.c(67) : error C2065: 'n' : undeclared identifier
D:\测试\test.c(67) : error C2109: subscript requires array or pointer type
D:\测试\test.c(67) : error C2224: left of '.val' must have struct/union type
D:\测试\test.c(68) : error C2109: subscript requires array or pointer type
D:\测试\test.c(68) : error C2224: left of '.kind' must have struct/union type
D:\测试\test.c(69) : error C2109: subscript requires array or pointer type
D:\测试\test.c(69) : error C2224: left of '.is_joker' must have struct/union type
D:\测试\test.c(70) : error C2109: subscript requires array or pointer type
D:\测试\test.c(70) : error C2224: left of '.cv' must have struct/union type
D:\测试\test.c(74) : error C2109: subscript requires array or pointer type
D:\测试\test.c(74) : error C2224: left of '.cv' must have struct/union type
D:\测试\test.c(74) : error C2109: subscript requires array or pointer type
D:\测试\test.c(74) : error C2224: left of '.is_joker' must have struct/union type
D:\测试\test.c(75) : error C2109: subscript requires array or pointer type
D:\测试\test.c(75) : error C2224: left of '.cv' must have struct/union type
D:\测试\test.c(75) : error C2109: subscript requires array or pointer type
D:\测试\test.c(75) : error C2224: left of '.is_joker' must have struct/union type
D:\测试\test.c(77) : error C2109: subscript requires array or pointer type
D:\测试\test.c(77) : error C2115: 'function' : incompatible types
D:\测试\test.c(77) : warning C4024: 'printPoker' : different types for formal and actual parameter 1
D:\测试\test.c(94) : error C2065: 'card' : undeclared identifier
D:\测试\test.c(94) : error C2109: subscript requires array or pointer type
D:\测试\test.c(94) : error C2109: subscript requires array or pointer type
D:\测试\test.c(94) : error C2106: '=' : left operand must be l-value
D:\测试\test.c(98) : error C2061: syntax error : identifier 'getCard'
D:\测试\test.c(98) : error C2059: syntax error : ';'
D:\测试\test.c(98) : error C2059: syntax error : 'type'
D:\测试\test.c(158) : error C2061: syntax error : identifier 'playGame'
D:\测试\test.c(158) : error C2059: syntax error : ';'
D:\测试\test.c(158) : error C2059: syntax error : ')'
D:\测试\test.c(169) : error C2143: syntax error : missing ';' before 'type'
D:\测试\test.c(170) : error C2065: 'op' : undeclared identifier
D:\测试\test.c(174) : error C2109: subscript requires array or pointer type
D:\测试\test.c(174) : warning C4013: 'playGame' undefined; assuming extern returning int
D:\测试\test.c(174) : error C2106: '=' : left operand must be l-value
D:\测试\test.c(176) : error C2109: subscript requires array or pointer type
D:\测试\test.c(176) : error C2115: 'function' : incompatible types
D:\测试\test.c(176) : warning C4024: 'printPoker' : different types for formal and actual parameter 1
执行 cl.exe 时出错.

test.obj - 1 error(s), 0 warning(s)

搜索更多相关主题的帖子: test 游戏 type printf 测试 
2021-07-15 15:34
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
poker history[100] ; 改为 struct poker history[100];
其它也这样改,比如 poker getCard( int turn , int num ) 改为 struct poker getCard( int turn , int num )
不再多说

printf( "\n\n\n\*****************游戏菜单*********************************\n" ) ;
改为
printf( "\n\n\n*****************游戏菜单*********************************\n" ) ;

else { // 否则继续进行下一轮游戏
        return getCard( turn+1 , num/3 ) ;
    }
我猜(懒得看你的代码逻辑了)应该改为
    return getCard( turn+1 , num/3 ) ;

[此贴子已经被作者于2021-7-15 16:05编辑过]

2021-07-15 16:04
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
去除所有虚头巴脑无意义的部分后,游戏主体:

程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void random_27cards( char pakers[27] )
{
    char buf[54];
    for( size_t i=0; i!=sizeof(buf); ++i )
        buf[i] = (char)i;

    for( size_t i=0; i!=27; ++i )
    {
        size_t r = (size_t)(rand()*(54.0-i)/(1+RAND_MAX));
        pakers[i] = buf[r];
        buf[r] = buf[53-i];
    }
}

const char* name_by_card( char paker )
{
    if( paker == 52 )
        return "小王";
    if( paker == 53 )
        return "大王";

    static const char* colour[] = { "", "", "", "" };
    static const char* number[] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
    static char result[10];
    sprintf( result, "%s%s", colour[paker/13], number[paker%13] );
    return result;
}

void show_cards_by_3line( const char pakers[27] )
{
    for( size_t i=0; i!=27; ++i )
        printf( "%-5s%c", name_by_card(pakers[i]), " \n"[(i+1)%9==0] );
}

size_t get_user_selected( void )
{
    printf( "输入你默选的牌所在的行号(1-3): " );

    int lineno;
    while( scanf("%d",&lineno)!=1 || lineno<1 || lineno>3 )
    {
        scanf( "%*[^\n]%*c" );
        printf( "不许瞎鸡巴捣乱,必须输入1、2或3,请重新输入你默选的牌所在的行号: " );
    }
    return lineno-1;
}

void swap_array( char* a, char* b, size_t n )
{
    for( size_t i=0; i!=n; ++i )
    {
        char tmp = a[i];
        a[i] = b[i];
        b[i] = tmp;
    }
}

void game_once( void )
{
    char pakers[27];
    random_27cards( pakers );
    show_cards_by_3line( pakers );

    size_t a = get_user_selected();
    swap_array( pakers+a*9+0, pakers+0, 3 );
    swap_array( pakers+a*9+3, pakers+9, 3 );
    swap_array( pakers+a*9+6, pakers+18, 3 );
    show_cards_by_3line( pakers );

    size_t b = get_user_selected();
    swap_array( pakers+b*9+0, pakers+0, 1 );
    swap_array( pakers+b*9+1, pakers+9, 1 );
    swap_array( pakers+b*9+2, pakers+18, 1 );
    show_cards_by_3line( pakers );

    size_t c = get_user_selected();
    printf( "你默选的牌是: %s\n", name_by_card(pakers[c*9]) );
}

int main( void )
{
    srand( (unsigned)time(NULL) );

    game_once();
}
2021-07-16 08:51
快速回复:求助,小学期,智能猜扑克游戏代码,都有什么错误,可以有偿
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018777 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved