Chapter3猜拳游戏

//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 = L‘a‘;
    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;
}

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

时间: 2024-08-01 03:54:33

Chapter3猜拳游戏的相关文章

猜拳游戏

//通过控制台命令方式实现一个猜拳游戏,用户通过输入(1.石头,2.剪刀,3.布)与电脑pk,最后通过积分的多少判断胜负 Scanner sc=new Scanner(System.in); for(;;){ System.out.println("欢迎来到猜拳游戏,游戏规则:1.石头,2.剪刀,3.布.赢一次获得1个积分点,输一次扣一个积分点.您共有5次猜拳机会."); System.out.println("请确定是否开始游戏:1.开始  0.退出"); int

C#面向对象编程-猜拳游戏

1.需求 现在要制作一个游戏,玩家与计算机进行猜拳游戏,玩家出拳,计算机出拳,计算机自动判断输赢. 2.需求分析 根据需求,来分析一下对象,可分析出:玩家对象(Player).计算机对象(Computer).裁判对象(Judge). 玩家出拳由用户控制,使用数字代表:1石头.2剪子.3布 计算机出拳由计算机随机产生 裁判根据玩家与计算机的出拳情况进行判断输赢 3.类对象的实现 玩家类示例代码 class Player { string name; public string Name { get

在学会循环结构语句时就可以写的一个猜拳游戏小项目

package com.etc.for2; import java.util.Scanner; /** * 猜拳游戏规则: * 人或机器可以随机出石头.剪刀.布, * 若一方出石头,另一方出剪刀,则输出打印出石头方获胜, * 若一方出石头,另一方出布,则输出打印出布方获胜, * 若一方出布,另一方出剪刀,则输出打印出剪刀方获胜, * */ public class TestCaiQuan { public static void main(String[] args) { Scanner sc=

猜拳游戏 java基础

//用循环方式实现和计算机玩猜拳的程序 (设定胜出条件--输3次或赢3次即退出) package eduask01; import java.util.*; public class Rask03 { public static void main(String[]args){ Scanner sc=new Scanner(System.in); int m=0,n=0;//累计输或赢的次数 for(;;){//死循环进行游戏 System.out.println("猜拳游戏:1.表示石头 2.

MRC实现猜拳游戏(附:@property理解偏差纠正)

1 /* 猜拳游戏 2 对象1:人, 属性:输入要出的拳并显示,分数,姓名 3 对象2:Bot,属性同上,继承.自动生成出拳,枚举法.显示出拳,与传过来的人的出拳作比较,判定结果并输出分数. 4 其他:每比较一次提示是否继续. 5 6 注意:MRC! 7 */ 8 9 #import <Foundation/Foundation.h> 10 #import "Bot.h" 11 int main(int argc, const char * argv[]) 12 { 13

安卓版猜拳游戏源码

安卓版猜拳游戏源码,该文件中带有安装测试包的,这个游戏源码比较简单的,现在有两个代码,一个自定义VIEW的,一个就是普通的imageView图片,游戏非常适合一些新手的使用和学习. <ignore_js_op> <ignore_js_op> <ignore_js_op>  详细说明:http://android.662p.com/thread-4914-1-1.html 安卓版猜拳游戏源码,布布扣,bubuko.com

简单版猜拳游戏

界面很简单 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

Java 入门课程视频实战-0基础 上线了,猜拳游戏,ATM实战,欢迎围观

Java 入门课程视频实战-0基础 已经上传完了.欢迎小伙伴们过来围观 直接进入: http://edu.csdn.net/course/detail/196 课程文件夹例如以下: 1 初识Java  19:08 2 熟悉Eclipse开发工具  12:42 3 Java语言基础  17:39 4 流程控制  14:53 5 数组  14:44 6 字符串  34:32 7 类和对象  29:30 8 猜拳游戏  33:39 9 模拟银行柜员机程序  36:35 10 退休金结算程序  本课程由

条件结构的实例-水仙花数、猜拳游戏、回文数字

1.判断输入的数是否为水仙花数 int num,ge,shi,bai,he;  //声明变量 printf("请输入三位数");  //由用户输入一个三位数 scanf("%d",&num);   //将用户输入的数字保存给num //用户输入的数=个位的三次方+ 十位数的三次方+ 百位数的三次方 //1.从num获取个位 ge=num%10; //2.从num获取十位 shi=num/10%10; //3.从num获取百位 bai=num/100; he=