编写剪刀(scissor)、石头(stone)、布(cloth)游戏

2015-04-19 0 231
编写剪刀(scissor)、石头(stone)、布(cloth)游戏

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

 

int main(void)

{

    int user = -1;

    int computer = -1;

    int result = -1;

 

    char gesture[3][16] = {"scissor", "stone", "cloth"};

#if

    printf("%s\n", gesture[]);

    printf("%s\n", gesture[1]);

    printf("%c\n", gesture[1][3]);

#endif

 

#if 1

    srand(time(NULL));

    while (1) {

        // 1. 提示用户进行输入

INPUT:

        printf("Please input thegesture (0 – scissor 1 – stone 2 – cloth 3 – quit):\n");

 

        // 2. 接收用户的输入

        scanf("%d", &user);

        if (user < 0 || user > 3) {

            goto INPUT;

        }

 

        if (user == 3) {

            break;       

        }

 

        // 3. 程序随机生成石头、剪刀、布中的一种

        computer = rand() % 3;

 

        // 4. 打印用户输入和电脑生成的信息

        printf("\nYour:%s\tComputer: %s\n", gesture[user], gesture[computer]);

        // 5. 根据游戏规则,判断输赢结果

        // (U – C + 4 ) % 3 – 1

        result = (user – computer + 4) % 3 – 1;

 

        // 6. 输出结果

        if (result > 0) {

            printf("You Win!\n");

        } else if (result == ) {

            printf("Draw!\n");

        } else {

            printf("You lose!\n");

        }

    }

#endif

    printf("Thanks for yourjoin!\n");

    return 0;

}

遇见资源网 c/c++ 编写剪刀(scissor)、石头(stone)、布(cloth)游戏 http://www.ox520.com/10065.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务