C語言實(shí)現(xiàn)最簡單的剪刀石頭布小游戲示例
更新時(shí)間:2017年09月05日 09:25:19 作者:e421083458
這篇文章主要介紹了C語言實(shí)現(xiàn)最簡單的剪刀石頭布小游戲,涉及C語言數(shù)組、隨機(jī)數(shù)與數(shù)值運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下
本文實(shí)例講述了C語言實(shí)現(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; /*隨機(jī)數(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è)計(jì)有所幫助。
相關(guān)文章
OpenCV中C++函數(shù)imread讀取圖片的問題及解決方法
利用C++函數(shù)imread讀取圖片的時(shí)候返回的結(jié)果總是空,而利用C函數(shù)cvLoadImage時(shí)卻能讀取到圖像。怎么回事?今天小編通過本教程給大家簡單說明原因2017-03-03C++ 動(dòng)態(tài)數(shù)組模版類Vector實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了C++動(dòng)態(tài)數(shù)組模版類Vector實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02M1 Macbook vscode C++ debug調(diào)試實(shí)現(xiàn)
本文主要介紹了M1 Macbook vscode C++ debug調(diào)試,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08