C编程之旅:猜数字游戏的更好实现

本文介绍了一个简单的猜数字游戏程序设计,使用C++语言实现。该程序通过随机生成一个不重复的四位数字,让用户进行猜测,并给出正确的位数提示。文章详细展示了如何生成随机数、验证用户输入的有效性以及判断用户猜测的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include 
<stdlib.h>

using namespace std;

#define FIGURE_OK        1
#define    FIGURE_ERROR    0
#define RUN_TIM            9

void InputNumber();
void FigureNumber();
void OutputResult();

int nRet = 0;

int    i = 0;
int    j = 0;
int m = 0;
int n = 0;

int main()
{
    InputNumber();
    FigureNumber();
    OutputResult();

    
return 0;
}


//get a 4 bit number 
//ijmn
void InputNumber()
{    
    
int    nRand = rand();

    n 
= nRand % 10;
    nRand 
= nRand / 10;
    
    m 
= nRand % 10;
    nRand 
= nRand / 10;

    
while (m == n)
    
{
        m 
= rand() % 10;
    }


    
while ( (j == n) || (j == m) )
    
{
        j 
= rand() % 10;
    }


    
while ( (i == j) || (i == m) || (i == n))
    
{
        i 
= rand() % 10;
    }


    cout 
<< "guest number: " << i << j << m << n << endl;
}


//for user figure number
void FigureNumber()
{
    
int    nTime = 0;
    
int nNum = 0;
    
int ii = 0;
    
int jj = 0;
    
int mm = 0;
    
int nn = 0;

    
int nA = 0;
    
int nB = 0;

    
for (nTime = 0; nTime < RUN_TIM; nTime++)
    
{
        nA 
= 0;
        nB 
= 0;

        cout 
<< "you still have " << RUN_TIM - nTime << " chances!" << endl;
        cout 
<< "please input a 4 bit number:" << endl;
        cin 
>> nNum;

        
if ( (nNum >= 10000|| (nNum <= 100) )
        
{
            
continue;
        }


        nn 
= nNum % 10;
        nNum 
= nNum / 10;

        mm 
= nNum % 10;
        nNum 
= nNum / 10;

        jj 
= nNum % 10;
        jj 
= nNum / 10;

        ii 
= nNum % 10;

        
//can't be equal

        
if ( (ii == jj) || (ii == mm) || (ii == nn) || (jj == mm) || (jj == nn) || (mm == nn))
        
{
            cout 
<< "the 4 bit number can't be equal to each other!" << endl;

            
continue;
        }


        
if (nn == n) 
        
{
            nA
++;
        }


        
if (mm == m)
        
{
            nA
++;
        }


        
if (jj == j)
        
{
            nA
++;
        }


        
if (ii == i)
        
{
            nA
++;
        }


        
if ( (nn == m) || (nn == j) || (nn == i) )
        
{
            nB
++;
        }


        
if ( (mm == n) || (mm == j) || (mm == i) )
        
{
            nB
++;
        }


        
if ( (jj == m) || (jj == n) || (jj == i) )
        
{
            nB
++;
        }


        
if ( (ii == m) || (ii == n) || (ii == j) )
        
{
            nB
++;
        }


        
if (nA == 4)
        
{
            cout 
<< "you are so smart!" << endl;

            
break;
        }

        
else
        
{
            cout 
<< "figure result: " << nA << "A" << nB << "B" << endl;
        }

    }


    
if (nTime < RUN_TIM)
    
{
        nRet 
= FIGURE_OK;
    }

    
else
    
{
        nRet 
= FIGURE_ERROR;
    }

}


//out the figure result
void OutputResult()
{
    
if (FIGURE_OK == nRet)
    
{
        cout 
<< "figure number successfully!" << endl;
    }

    
else if (FIGURE_ERROR == nRet)
    
{
        cout 
<< "figure number failed!" << endl;
    }

    
else
    
{
        cout 
<< "programe run error!" << endl;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值