| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 575 人关注过本帖
标题:前几天发的拈游戏的代码,我最终改好了,大神可以给优化一下吗?
只看楼主 加入收藏
一条沙丁鱼
Rank: 1
等 级:新手上路
威 望:1
帖 子:44
专家分:7
注 册:2015-4-5
结帖率:100%
收藏
 问题点数:0 回复次数:0 
前几天发的拈游戏的代码,我最终改好了,大神可以给优化一下吗?
#include<iostream>
#include<ctime>
#include<string>
#include<cstdlib>
#include<stdio.h>
using namespace std;

void go();

void back()
{
    cout<<"按回车键返回菜单"<<endl;
}

void exit(void)
{
    exit(1);
}

int shezhi1();
int shezhi2();

void renji()
{
    int my;
    int total1,total2;
    int t1=shezhi1();
    int m1=shezhi2();
    int k,flag;
    cout<<"请问谁先谁后?  1.玩家  2.电脑"<<endl;
    cin>>k;
    int i;
    srand(time(0));
    i=rand()%m1+1;
    if(k==1)
    {
        while(t1!=0&&t1>0)
        {
            flag=1;
            cout<<"请输入您要取的个数:"<<endl;
            cin>>my;
            if(my>=1&&my<=m1&&my<=t1)
            {
                total1=t1-my;
                t1=total1;
                cout<<"还剩余"<<t1<<"个"<<endl;
            }
            else
            {
                cout<<"你输入的数值不在范围之内请重新输入:"<<endl;
                continue;
            }
            if(t1!=0)
            {
                cout<<"该电脑取棋子:"<<endl;
                while(t1!=10&&t1>0)
                {
                    flag=2;
                    i=rand()%m1+1;
                    if(i<=t1)
                    {
                        cout<<"电脑取出了"<<i<<"个"<<endl;
                        total2=t1-i;
                        t1=total2;
                        cout<<"还剩余"<<t1<<"个"<<endl;
                    }
                    else
                    {
                        continue;
                    }
                    break;
                }
            }
            if(t1==0)
            {
                if(flag==1)
                {
                    cout<<"恭喜您取得胜利!"<<endl;
                    break;
                }
                if(flag==2)
                {
                    cout<<"恭喜电脑取得胜利,希望您下次加油!"<<endl;
                    break;
                }
            }
        }
    }
    if(k==2)
    {
        while(t1!=0&&t1>0)
        {
            flag=2;
            i=rand()%m1+1;
            cout<<"该电脑取子:"<<endl;
            if(i<=t1)
            {
                cout<<"电脑取出了"<<i<<"个"<<endl;
                total2=t1-i;
                t1=total2;
                cout<<"还剩余"<<t1<<"个"<<endl;
            }
            else
            {
                continue;
            }
            if(t1!=0)
            {
                while(t1!=0&&t1>0)
                {
                    flag=1;
                    cout<<"请输入您要取的个数:"<<endl;
                    cin>>my;
                    if(my>=1&&my<=m1&&my<=t1)
                    {
                        total1=t1-my;
                        t1=total1;
                        cout<<"还剩余"<<t1<<"个"<<endl;
                    }
                    else
                    {
                        cout<<"你输入的数值不在范围之内请重新输入:"<<endl;
                        continue;
                    }
                    break;
                }
            }
            if(t1==0)
            {
                if(flag==1)
                {
                    cout<<"恭喜您获得胜利!"<<endl;
                    break;
                }
                if(flag==2)
                {
                    cout<<"恭喜电脑获得胜利,希望您下次加油!"<<endl;
                    break;
                }
            }
        }
    }
}

void renren()
{
    char play1[20],play2[20];
    cout<<"请先输入游戏玩家的名字:"<<endl;
    cout<<"第一个玩家的名字为:"<<endl;
    cin>>play1;
    cout<<"第二个玩家的名字为:"<<endl;
    cin>>play2;
    int k;
    cout<<"请问谁先谁后?  1.玩家"<<play1<<"  2.玩家"<<play2<<endl;
    cin>>k;
    int p1;
    int p2;
    int total1,total2,flag;
    int t2=shezhi1();
    int m2=shezhi2();
    if(k==1)
    {
        cout<<"玩家"<<play1<<"先开始"<<endl;
        while(t2!=0&&t2>0)
        {
            flag=1;
            cout<<"请输入"<<play1<<"拿取几个:"<<endl;
            cin>>p1;
            if(p1>=1&&p1<=m2&&p1<=t2)
            {
                total1=t2-p1;
                t2=total1;
                cout<<"还剩余"<<t2<<"个"<<endl;
            }
            else
            {
                cout<<play1<<"您输入的数值不在规定范围内请重新输入:"<<endl;
                continue;
            }
            if(t2!=0)
            {
                cout<<"玩家"<<play2<<"取子:"<<endl;
                while(t2!=0&&t2>0)
                {
                    flag=2;
                    cout<<"请输入"<<play2<<"拿取几个:"<<endl;
                    cin>>p2;
                    if(p2<=t2)
                    {
                        cout<<"玩家"<<play2<<"取出了"<<p2<<"个"<<endl;
                        total2=t2-p2;
                        t2=total2;
                        cout<<"还剩余"<<t2<<"个"<<endl;
                    }
                    else
                    {
                        cout<<"您输入的值不在范围之内请重新输入:"<<endl;
                        continue;
                    }
                    break;
                }
            }
            if(t2==0)
            {
                if(flag==1)
                {
                    cout<<"恭喜玩家"<<play1<<"获得胜利,希望玩家"<<play2<<"下次加油!"<<endl;
                    break;
                }
                if(flag==2)
                {
                    cout<<"恭喜玩家"<<play2<<"获得胜利,希望玩家"<<play1<<"下次加油!"<<endl;
                    break;
                }
            }
        }
    }
    if(k==2)
    {
        cout<<"玩家"<<play2<<"先开始"<<endl;
        while(t2!=0&&t2>0)
        {
            flag=2;
            cout<<"玩家"<<play2<<"拿取几个:"<<endl;
            cin>>p2;
            if(p2>=1&&p2<=m2&&p2<=t2)
            {
                total2=t2-p2;
                t2=total2;
                cout<<"还剩余"<<t2<<"个"<<endl;
            }
            else
            {
                cout<<"您输入的数值不在范围之内请重新输入:"<<endl;
                continue;
            }
            if(t2!=0)
            {
                cout<<"玩家"<<play1<<"取子:"<<endl;
                while(t2!=0&&t2>0)
                {
                    flag=1;
                    cout<<"请输入玩家"<<play1<<"拿取几个:"<<endl;
                    cin>>p1;
                    if(p1<=t2)
                    {
                        cout<<"玩家"<<play1<<"取出了"<<p1<<"个"<<endl;
                        total1=t2-p2;
                        t2=total1;
                        cout<<"还剩余"<<t2<<"个"<<endl;
                    }
                    else
                    {
                        cout<<"您输入的值不在范围内请重新输入:"<<endl;
                        continue;
                    }
                    break;
                }
            }
            if(t2==0)
            {
                if(flag==1)
                {
                    cout<<"恭喜玩家"<<play1<<"获得胜利,希望玩家"<<play2<<"下次加油!"<<endl;
                    break;
                }
                if(flag==2)
                {
                    cout<<"恭喜玩家"<<play2<<"获得胜利,希望玩家"<<play1<<"下次加油!"<<endl;
                    break;
                }
            }
        }
    }
}
void aboutgame()
{
    cout<<"拈游戏是比较经典的游戏之一,我们这个游戏所探讨的是单堆拈游戏"<<endl;
    cout<<"即从一堆石子中拿取石子,石子的个数和每次所拿取的石子个数上限"<<endl;
    cout<<"提前人为定好,能够把最后的石子取出来的玩家即为胜者。"<<endl;
}
int shezhi1()
{
    int total;
    while(true)
    {
        cout<<"请设置游戏石子的总数:"<<endl;
        cin>>total;
        if(total>=0)
        {
            break;
        }
        cout<<"设置有误,请重新设置!"<<endl;
    }
    //cout<<"总共有"<<total<<"个"<<endl;
    return total;
    //return m;
}
int shezhi2()
{
    int total=shezhi1();
    int m;
    while(true)
    {
        cout<<"请设置每次最多拿取石子个数:"<<endl;
        cin>>m;
        if(m>0&&total>=m)
        {
            break;
        }
        cout<<"设置有误,请重新设置!"<<endl;
    }
    cout<<"总共有"<<total<<"个,每次最多拿取"<<m<<"个,最少拿取1个..."<<endl;
    return m;
}
void menu()
{
    cout<<"**********欢迎使用拈游戏**********"<<endl;
    cout<<"          1.人机游戏模式          "<<endl;
    cout<<"          2.人人游戏模式          "<<endl;
    cout<<"          3.游戏简要说明          "<<endl;
    cout<<"          4.游戏退出              "<<endl;


    cout<<"请选择想要执行的操作:"<<endl;
    int s;
    cin>>s;
    switch(s)
    {
       case 1:renji();
              back();
              getchar();
              getchar();
              system("CLS");
              go();
              break;
       case 2:renren();
              back();
              getchar();
              getchar();
              system("CLS");
              go();
              break;
       case 3:aboutgame();
              back();
              getchar();
              getchar();
              system("CLS");
              go();
              break;
       case 4:exit(1);
              back();
              getchar();
              getchar();
              system("CLS");
              go();
              break;
       default:cout<<"输入指令错误!系统退出!"<<endl;
               break;
    }
}

void go()
{
    menu();   
}
void main()
{
    go();
}
搜索更多相关主题的帖子: include 天发 游戏 
2015-06-23 16:50
快速回复:前几天发的拈游戏的代码,我最终改好了,大神可以给优化一下吗?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015519 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved