猜数字游戏--我也来凑热闹

从技术上来看,还算新人吧。看到这个以前十分喜欢的游戏,忍不住来凑凑热闹。大家别笑。

 

     public   class  GuessGame
    {
        
public   int [] Answers {  get private   set ; }
        
public   int  MaxAttemptTimes {  get set ; }

        
public   int  AttemptTimes {  get private   set ; }
        
public   bool  IsSuccess {  get private   set ; }

        
public   bool  isEnd
        {
            
get
            {
                
return   ! (AttemptTimes  <  MaxAttemptTimes)  ||  IsSuccess;
            }
        }

        
public  GuessGame()
        {
            Answers 
=   new   int [ 4 ];
            MaxAttemptTimes 
=   6 ;

            NewGames();
        }

        
public   void  NewGames()
        {
            buildAnswers();
            AttemptTimes 
=   0 ;
            IsSuccess 
=   false ;
        }

        
private   void  buildAnswers()
        {
            Random random 
=   new  Random();

            
do
            {
                Answers[
0 =  random.Next( 9 );
                Answers[
1 =  random.Next( 9 );
                Answers[
2 =  random.Next( 9 );
                Answers[
3 =  random.Next( 9 );

            } 
while  ( ! isValidate(Answers));
        }

        
public   string  Test( int [] answer)
        {
            
string  result;
            
int  a  =   0 ;
            
int  b  =   0 ;

            
if (isEnd)
            {
                result 
=   " 对不起,比赛已经结束。 " ;

                
if  (IsSuccess) { result  +=   " 您赢得了比赛! " ; }
                
else  { result  +=   " 一次失败不算什么,请再接再厉! " ; }

                
return  result;
            }

            
if  ( ! isValidate(answer)) {  return   " 对不起,输入结果不符合要求 " ; }

            
for  ( int  i  =   0 ; i  <  Answers.Length; i ++ )
            {
                
if  (answer[i]  ==  Answers[i]) { a ++ ; }
                
if  (Answers.Any(r  =>  r  ==  answer[i])) { b ++ ; }
            }

            
if  ( 4   ==  a) { IsSuccess  =   true ; }

            b 
-=  a;

            AttemptTimes
++ ;

            
return   " 这是你的第 "   +  AttemptTimes  +   " 次测试: "   +  a.ToString()  +   " A "   +  b.ToString()  +   " B " ;
        }

        
private   bool  isValidate( int [] answer)
        {
            
if  ( 4   !=  answer.Length) {  return   false ; }
            
            
foreach  ( int  i  in  answer)
            {
                
if  (answer.Where(a  =>  a  ==  i).Count()  >   1 ) {  return   false ; }
            }
            
            
return   true ;
        }

    }

 

 

     class  Program
    {
        
static   void  Main( string [] args)
        {
            Console.WriteLine(
" 我猜,我猜,我猜猜猜! " );

            GuessGame games 
=   new  GuessGame();

            
while  ( true )
            {
                
if  (games.isEnd)
                {
                    
if  (games.IsSuccess) { Console.WriteLine( " 恭喜你,成功了。 " ); }
                    
else  { Console.WriteLine( " 失败是成功之母! " ); }

                    Console.WriteLine(
" 是否继续新游戏(Y/N)? " );
                    
char  c  =  ( char )Console.Read();
                    Console.ReadLine();

                    
if  ( " y "   ==  c.ToString().ToLower()) { games.NewGames(); }
                    
else  {  return ; }
                }

                Console.Write(
" 请输入四位数字: " );
                
string  input  =  Console.ReadLine();

                
if  ( ! Regex.IsMatch(input,  @" ^\d{4}$ " )) { Console.WriteLine( " 输入错误! " ); }
                
else
                {
                    
int [] answer  =   new   int [ 4 ];
                    
for  ( int  i  =   0 ; i  <   4 ; i ++ )
                    {

                        answer[i] 
=  Int32.Parse(input[i].ToString());
                    }

                    Console.WriteLine(games.Test(answer));
                }
            }
        }
    }

 

请大家多多指教。

ps: 怎么发表到话题下面????  不太明白。第一次发首页,有不合适的地方请多多包涵。

 

转载于:https://www.cnblogs.com/MaYong/archive/2009/07/09/1520003.html

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值