黑马程序员——C基础之石头剪刀布游戏

来源:互联网 发布:霍华德生涯最好数据 编辑:程序博客网 时间:2024/05/29 11:20

------- iOS培训、android培训、java培训、期待与您交流! ----------

#include <stdio.h>

#include <stdlib.h>


int main(int argc,constchar * argv[]) {

    // 制定游戏规则

    /*

     

     剪刀  干掉 

     石头  干掉  剪刀

       干掉  石头

     

     规定:

     0 剪刀

     1 石头

     2

     

     思路:

     

     计算机随机出一个拳(计算机如何随机出拳)

     

     玩家自己选择一个拳

     

     判断输赢

     

     

     */

    //实现步骤:

    

    //、定义变量,保存计算机出的拳,保存用户输入的拳

    int computer = -1 ,player = -1;

    //2、先让计算机出拳

    //随机产生

    //0 1 2

    //产生随机数的方法 arc4random_uniform随机数产生的函数

    // arc4random_uniform函数使用时,首先要导入一个头文件 stdlib.h

    computer = arc4random_uniform(3); //0 1 2

    //3、各一个提示,让用户出拳

    printf("请出拳: 0.剪刀 1.石头 2.\n");

    //保存用户出的拳

    scanf("%d",&player);

    //效验

    if (player < 0 || player > 2) {

        printf("请按套路出拳!\n");

    }

    //4、开始比较

    //先判断玩家赢的情况

    if ((player == 0 && computer == 2) ||

        (player == 1 && computer == 0) ||

        (player == 2 && computer == 1)) {

        printf("恭喜你,你赢了!\n");

    }else if ((computer == 0 && player == 2) ||

               (computer == 1 && player == 0) ||

               (computer == 2 && player == 1)) {

        printf("很遗憾,你输了!\n");

    }else{

        printf("平局!\n");

    }

    return 0;

}


0 0