- /*********************************************************
- *程序描述:猜拳小游戏
- *运行环境:Windows 7 SP1 X64
- *开发环境:CodeBlocks with Win7
- **********************************************************/
- #include <time.h>
- #include <iostream>
- #include <stdlib.h>
- #include <conio.h>
- #define SEC 1
- using namespace std;
- //函数声明
- int computer();
- char player();
- void putUi();
- void judge(int play, int com);
- void theEnd();
- void out(int x);
- void wait(int n);
- int win=0,lose=0,draw=0;
- int main()
- {
- system("title 猜拳游戏V1.0");
- int play,com;
- while (1)
- {
- putUi(); //显示UI
- play = player(); //玩家选择
- wait(SEC); //设置等待
- com = computer(); //电脑选择
- judge(play,com); //判断
- theEnd(); //冻结屏幕
- }
- return 0;
- }
- int computer() //电脑随机生成一个选项
- {
- srand((unsigned)time(NULL));
- return rand()%3+1;
- }
- char player() //等待玩家选择一个选项
- {
- int play;
- cout<<"\\t\\t\\t\\t 请选择";
- for (;;)
- {
- play = getch();
- if (play>='1'&&play<='3')
- return play-'0';
- }
- }
- void putUi() //UI的输出
- {
- system ("CLS");
- system ("color 0A");
- cout<<"\\t\\t\\t\\t猜拳游戏V1.0\\n";
- cout<<"Win:"<<win<<"\\nLose:"<<lose<<"\\nDraw:"<<draw<<"\\n";
- cout<<"\\t\\t\\t\\t1、我出石头\\n";
- cout<<"\\t\\t\\t\\t2、我出剪刀\\n";
- cout<<"\\t\\t\\t\\t3、我出布\\n";
- }
- void judge(int play, int com) //判断输赢
- {
- if (play == com)
- {
- cout<<"\\n\\t\\t\\t 你们都出了";
- out(play);
- cout<<"所以打成平手!";
- draw++;
- }
- else if (play == 1 && com == 3 ||
- play == 2 && com == 1 ||
- play == 3 && com == 2)
- {
- cout<<"\\n\\t\\t\\t你出了";
- out(play);
- cout<<",电脑出了";
- out(com);
- cout<<",你输了!\\n";
- lose++;
- }
- else
- {
- cout<<"\\n\\t\\t\\t你出了";
- out(play);
- cout<<",电脑出了";
- out(com);
- cout<<",你赢了!";
- win++;
- }
- }
- void theEnd() //屏幕冻结
- {
- getch();
- }
- void out(int x) //根据数值判断出的是什么
- {
- if (x == 1)
- cout<<"石头";
- if (x == 2)
- cout<<"剪刀";
- if (x == 3)
- cout<<"布";
- }
- void wait(int n) //设置等待
- {
- int i;
- cout<<"\\n\\t\\t\\t .....Please wait....\\n";
- time_t t1,t2;
- time(&t1);
- time(&t2);
- while((t2-t1)<n)
- time(&t2);
- }
- //该片段来自于http://www.codesnippet.cn/detail/1004201512172.html
来源: http://www.codesnippet.cn/detail/1004201512172.html