本文實例講述了C語言實現最簡單的剪刀石頭布小游戲。分享給大家供大家參考,具體如下:
#include<stdio.h>#include<stdlib.h>#include<time.h>/*************/* 剪刀 石頭 布 ** 最簡單小游戲 */*************/int main(void){ char gesture[3][10] = {"scissor","stone","cloth"}; int man, computer, result, ret; /*隨機數初始化函數*/ 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語言程序設計有所幫助。
新聞熱點
疑難解答
圖片精選