码迷,mamicode.com
首页 > 其他好文 > 详细

Chapter3猜拳游戏

时间:2018-01-25 21:53:46      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:std   使用   confirm   手势   blog   const   mit   二维   次数   

//3-1猜拳游戏, 其一
#include<time.h>
#include<stdio.h>
#include<stdlib.h>

int main(){
    int human;
    int comp;
    int judge;
    int retry;
    
    srand(time(NULL));
    printf("猜拳游戏开始!!\n");
    do{
        comp = rand() % 3;
        printf("\n\a石头剪刀布...(0)石头(1)剪刀(2)布:");
        scanf("%d", &human);
        
        printf("我出");
        switch(comp){
            case 0: printf("石头"); break;
            case 1: printf("剪刀"); break;
            case 2: printf(""); break;
        }
        printf("。\n");
        
        judge = (human - comp + 3) % 3;        //判断胜负
        switch(judge){
            case 0: puts("平局。"); break;
            case 1: puts("你输了。"); break;
            case 2: puts("你赢了。"); break;
        }
        
        printf("再来一次吗?(0)否(1)是:");
        scanf("%d", &retry);
    } while(retry == 1);
    return 0;
}
//3-2猜拳游戏,其二 显示双方的手势
#include<time.h>
#include<stdio.h>
#include<stdlib.h>

int main(){
    int human;
    int comp;
    int judge;
    int retry;
    
    srand(time(NULL));
    printf("猜拳游戏开始!!\n");
    do{
        comp = rand() % 3;
        
        do{
            printf("\n\a石头剪刀布...(0)石头(1)剪刀(2)布:");
            scanf("%d", &human);
        }while(human < 0 || human > 2);
        
        printf("我出");
        switch(comp){
            case 0: printf("石头"); break;
            case 1: printf("剪刀"); break;
            case 2: printf(""); break;
        }        
        printf(",你出");
        switch(human){
            case 0: printf("石头"); break;
            case 1: printf("剪刀"); break;
            case 2: printf(""); break;
        }
        printf("。\n");
        
        judge = (human - comp + 3) % 3;        //判断胜负
        switch(judge){
            case 0: puts("平局。"); break;
            case 1: puts("你输了。"); break;
            case 2: puts("你赢了。"); break;
        }
        
        printf("再来一次吗?(0)否(1)是:");
        scanf("%d", &retry);
    } while(retry == 1);
    return 0;
}
//3-3显示字符和字符编码
#include<ctype.h>
#include<stdio.h>
#include<limits.h>

int main(){
    int i;
    for(i = 0; i <= CHAR_MAX; i++){
        switch(i){
            case \a : printf("\\a"); break;
            case \b : printf("\\b"); break;
            case \f : printf("\\f"); break;
            case \n : printf("\\n"); break;
            case \r : printf("\\r"); break;
            case \t : printf("\\t"); break;
            case \v : printf("\\v"); break;
            default      : printf(" %c", isprint(i) ? i :  );    //ctype.h
        }
        printf(" %02x\n", i);
    }
    return 0;
}
//3-4用十六进制和二进制数显示字符串内的字符
#include<ctype.h>
#include<stdio.h>
#include<limits.h>

void strdump(const char *s){
    while(*s){
        int i;
        unsigned char x = (unsigned char)*s;
        
        printf("%c  ", isprint(x) ? x :  );        //字符
        printf("%0*X  ", (CHAR_BIT + 3) / 4, x);    //十六进制数
        for(i = CHAR_BIT - 1; i >= 0; i--)
            putchar(((x >> i) & 1U) ? 1 : 0);    //二进制数
        putchar(\n);
        s++;
    }
}
int main(){
    puts("汉字");         strdump("汉字");         putchar(\n);
    puts("12中国话AB");    strdump("12中国话AB");    putchar(\n);
    return 0;
}
//3-5字符串数组(二维数组)
#include<stdio.h>
int main(){
    int i;
    char a[][6] = {
        "Super", "X", "TRY"
    };
    for(i = 0; i < 3; i++)
        printf("%s\n", a[i]);
    return 0;
}
//3-6字符串数组(指针数组)
#include<stdio.h>
int main(){
    int i;
    char *p[] = {
        "Super", "X", "TRY"
    };
    for(i = 0; i < 3; i++)
        printf("%s\n", p[i]);
    return 0 ;
}
//3-7猜拳游戏三,导入表示手势的字符串
#include<time.h>
#include<stdio.h>
#include<stdlib.h>

int main(){
    int i;
    int human;
    int comp;
    int judge;
    int retry;
    char *hd[] = {"石头", "剪刀", ""};
    
    srand(time(NULL));
    printf("猜拳游戏开始!!\n");
    do{
        comp = rand() % 3;
        do{
            printf("\n\a石头剪刀布...");
            for(i = 0; i < 3; i++)
                printf(" (%d)%s", i, hd[i]);
            printf(" : ");
            scanf("%d", &human);
        }while(human < 0 || human > 2);
        
        printf("我出%s,你出%s。\n", hd[comp], hd[human]);
        judge = (human - comp + 3) % 3;
        switch(judge){
            case 0: puts("平局。"); break;
            case 1: puts("你输了。"); break;
            case 2: puts("你赢了。"); break;
        }
        
        printf("再来一次吗?(0)否(1)是:");
        scanf("%d", &retry);
    }while(retry == 1);
    return 0;
}
//3c-1宽字符使用示例
#include<wcahr.h>
#include<locale.h>
#include<stdio.h>

int main(){
    int i;
    wchar_t c = La;
    wchar_t *h[3] = {L"石头", L"剪刀", L""};
    
    setlocale(LC_ALL, "");    //locale设置地域信息,locale.h
    wprintf(L"%lc\n", c);
    for(i = 0; i < 3; i++)
        wprintf(L"h[%d] = %ls\n", i, h[i]);
    return 0;
}
//3-8将计算机一定赢,计算机后出
#include<time.h>
#include<stdio.h>
#include<stdlib.h>

int main(){
    int i;
    int human;
    int comp;
    int judge;
    int retry;
    char *hd[] = {"石头", "剪刀", ""};
    
    srand(time(NULL));
    printf("猜拳游戏开始!!\n");
    do{
        do{
            printf("\n\a石头剪刀布...");
            for(i = 0; i < 3; i++)
                printf(" (%d)%s", i, hd[i]);
            printf(" : ");
            scanf("%d", &human);
        }while(human < 0 || human > 2);
        
        comp = (human + 2) % 3;    //计算机后出
        
        printf("我出%s,你出%s。\n", hd[comp], hd[human]);
        judge = (human - comp + 3) % 3;
        switch(judge){
            case 0: puts("平局。"); break;
            case 1: puts("你输了。"); break;
            case 2: puts("你赢了。"); break;
        }
        
        printf("再来一次吗?(0)否(1)是:");
        scanf("%d", &retry);
    }while(retry == 1);
    return 0;
}
//3-9猜拳游戏,加入函数分割
#include<time.h>
#include<stdio.h>
#include<stdlib.h>

int human;
int comp;
int win_no;        //胜利次数
int lose_no;    //失败次数
int draw_no;    //平局次数
char *hd[] = {"石头", "剪刀", ""};

void initialize(){
    win_no = 0;
    lose_no = 0;
    draw_no = 0;
    srand(time(NULL));
    printf("猜拳游戏开始!!\n");
}
//运行猜拳游戏 读取/生成手势
void jyanken(){        
    int i;
    comp = rand() % 3;
    do{
        printf("\n\a石头剪刀布...");
        for(i = 0; i < 3; i++)
            printf(" (%d)%s", i, hd[i]);
        printf(" : ");
        scanf("%d", &human);
    }while(human < 0 || human > 2);
}
//更新胜利失败平局的次数
void count_no(int result){
    switch(result){
        case 0: draw_no++;     break;
        case 1: lose_no++;  break;
        case 2: win_no++;     break;
    }
}
//显示判断结果
void disp_result(int result){
    switch(result){
        case 0: puts("平局。");      break;
        case 1: puts("你输了。"); break;
        case 2: puts("你赢了。"); break;
    }
}
//确认是否再次挑战
int confirm_retry(){
    int x; 
    printf("再来一次吗?(0)否(1)是:");
    scanf("%d", &x);
    return x;
}
int main(){
    int judge;
    int retry;
    initialize();
    do{
        jyanken();
        printf("我出%s,你出%s。\n", hd[comp], hd[human]);
        judge = (human - comp + 3) % 3;
        count_no(judge);
        disp_result(judge);
        retry = confirm_retry();
    }while(retry == 1);
    printf("%d胜%d负%d平。", win_no, lose_no, draw_no);
    return 0;
}
//3-10猜拳游戏,先赢满3局者胜
#include<time.h>
#include<stdio.h>
#include<stdlib.h>

int human;
int comp;
int win_no;        //胜利次数
int lose_no;    //失败次数
int draw_no;    //平局次数
char *hd[] = {"石头", "剪刀", ""};

void initialize(){
    win_no = 0;
    lose_no = 0;
    draw_no = 0;
    srand(time(NULL));
    printf("猜拳游戏开始!!\n");
}
//运行猜拳游戏 读取/生成手势
void jyanken(){        
    int i;
    comp = rand() % 3;
    do{
        printf("\n\a石头剪刀布...");
        for(i = 0; i < 3; i++)
            printf(" (%d)%s", i, hd[i]);
        printf(" : ");
        scanf("%d", &human);
    }while(human < 0 || human > 2);
}
//更新胜利失败平局的次数
void count_no(int result){
    switch(result){
        case 0: draw_no++;     break;
        case 1: lose_no++;  break;
        case 2: win_no++;     break;
    }
}
//显示判断结果
void disp_result(int result){
    switch(result){
        case 0: puts("平局。");      break;
        case 1: puts("你输了。"); break;
        case 2: puts("你赢了。"); break;
    }
}
int main(){
    int judge;
    initialize();
    do{
        jyanken();
        printf("我出%s,你出%s。\n", hd[comp], hd[human]);
        judge = (human - comp + 3) % 3;
        count_no(judge);
        disp_result(judge);
    }while(win_no < 3 && lose_no < 3);
    printf(win_no == 3 ? "\n你赢了。\n" : "\n我赢了\n");
    printf("%d胜%d负%d平。", win_no, lose_no, draw_no);
    return 0;
}
//3c-2作用域
#include<stdio.h>
int x = 77;
void print_x(){
    printf("x = %d\n", x);
}
int main(){
    int i;
    int x = 88;
    print_x();
    printf("x = %d\n", x);
    for(i = 0; i < 5; i++){
        int x = i * 11;
        printf("x = %d\n", x);
    }
    printf("x = %d\n", x);
    return 0;
}

 

Chapter3猜拳游戏

标签:std   使用   confirm   手势   blog   const   mit   二维   次数   

原文地址:https://www.cnblogs.com/leosirius/p/8353037.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!