C語言實現(xiàn)最簡單的剪刀石頭布小游戲示例
更新時間:2017年09月05日 09:25:19 作者:e421083458
這篇文章主要介紹了C語言實現(xiàn)最簡單的剪刀石頭布小游戲,涉及C語言數(shù)組、隨機數(shù)與數(shù)值運算等相關(guān)操作技巧,需要的朋友可以參考下
本文實例講述了C語言實現(xiàn)最簡單的剪刀石頭布小游戲。分享給大家供大家參考,具體如下:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
/*************\
* 剪刀 石頭 布 *
* 最簡單小游戲 *
\*************/
int main(void){
char gesture[3][10] = {"scissor","stone","cloth"};
int man, computer, result, ret;
/*隨機數(shù)初始化函數(shù)*/
srand(time(NULL));
while(1){
computer = rand()%3;
printf("\nInput your gesture 0-scissor 1-stone 2-cloth:\n");
ret = scanf("%d", &man);
if(ret !=1 || man<0 || man>2){
printf("Invalid input!\n");
return 1;
}
printf("Your gesture:%s\tComputer's gesture: %s\n",
gesture[man], gesture[computer]
);
result = (man - computer + 4) %3 -1;
if(result > 0)
printf("YOU WIN!\n");
else if(result == 0)
printf("Draw!\n");
else
printf("You lose!\n");
}
return 0;
}
PS:游戲使用ctrl+c退出程序。
希望本文所述對大家C語言程序設(shè)計有所幫助。
您可能感興趣的文章:

