您的位置:首页 > 编程语言 > C语言/C++

猜拳小游戏 C语言

2015-03-15 09:03 302 查看
博客搬家了,最近同时更新,没准什么时候就完全搬走了-_-

http://blog.just666.cn 这个是新博客地址,希望大家多多关照

用C语言写的猜拳的小游戏。
源代码下载地址:http://www.oschina.net/code/snippet_2309129_45545


//////////////////////////////////
//名称:猜拳游戏V1.0
//作者:耗子、
//时间:2015.1.19
/////////////////////////////////
#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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: