猜拳小游戏 C语言

博客搬家了,最近同时更新,没准什么时候就完全搬走了-_-
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);
}
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值