博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一个小游戏

Posted on 2006-07-10 10:51  blackzh  阅读(1037)  评论(3编辑  收藏  举报
    小弟初学C#和面向对象的思想,闲来无事就写个小程序练手,写完后感觉还是没有领悟面向对象的精髓,设计上还有很多毛病,还请各位达人不吝赐教。
    这个游戏是仿造文曲星上的猜数字游戏,程序先生成一个4位无重复的随机数,再要求玩家输入4位数字并将输入的数字入随机数进行比较。若是输入的数字中有随机数中的数字,但是位置与随机数不同则可得到指示B;
若是输入的数字中有随机数中的数字,且位置与随机数相同则可得到指示(如随机数为1234,输入的数为7891 则结果是0A1B;输入为1243,则结果为2A2B),玩家在指定的次数内猜中随机数就算获胜。这是个逻辑性比较强的游戏,希望大家喜欢。

using
 System;

namespace GuessNumeral
{
    
/// <summary>
    
/// GuessNumeral 的摘要说明。
    
/// </summary>
    public class GuessNumeral
    {
        
private string _first;//随机数的第一位
        private string _second;//随机数的第二位
        private string _third;//随机数的第三位
        private string _fouth;//随机数的第四位
        private string _numstr;//随机数字符串
        private int[] array_a;//结果标识
        private int[] array_b;//结果标识
        private int ramnum;//随机数
        private int level;//游戏难度
        protected int count;//执行次数
        private string input;//玩家输入的字符
        public void Initialize()//游戏初始化
        {
            array_a
=new int[]{1,1,1,1,1,1,1,1,1,1};
        }
        
public int CreatRandom()//生成随机数
        {
            Random ram
=new Random();
            ramnum
=ram.Next(0,10);
            
if(array_a[ramnum]==0)
            {
this.CreatRandom();}
            
return ramnum;
        }
        
public void CreatRamstr()//组成字符串
        {
            
string numstr;
            numstr
="";
            
for(int i=0;i<4;i++)
            {
                
int num;
                num
=this.CreatRandom();
                array_a[num]
=0;
                numstr
=numstr+num.ToString();
            }
            
this.Numstr=numstr;
        }
        
public void SetNumber()//分别给每位属性赋值
        {
            _first
=this.Numstr.ToString().Substring(0,1);
            _second
=this.Numstr.ToString().Substring(1,1);
            _third
=this.Numstr.ToString().Substring(2,1);
            _fouth
=this.Numstr.ToString().Substring(3,1);
        }
        
public void GetInput()//获取玩家输入的字符并判断是否合适
        {  
            
bool upshot;
            upshot
=true;
            array_b
=new int[]{1,1,1,1,1,1,1,1,1,1};
            input
="";
            Console.WriteLine(
"请输入四位无重复的数字:");
            input
=Console.ReadLine();
            
if (input.Length!=4)
            {
                
this.GetInput();
            }
            
else
            {
                
for (int i=0;i<4;i++)
                {
                    
try
                    {
                    
if    (array_b[Convert.ToInt32(input.ToString().Substring(i,1))]==0)
                        {    
                            upshot
=false;
                        }
                        
else
                        {
                            array_b[Convert.ToInt32(input.ToString().Substring(i,
1))]=0;
                        }
                    }
                    
catch
                    {
                        upshot
=false;
                    }
                }
            }
             
if (upshot==false)
            {    
                
this.GetInput();
            }
        }
        
public int Execute()//对输入的字符进行一次判断并给出结果
        {
            
int result_a=0;
            
int result_b=0;
            
int end_sign=0;
            
if (count!=0)
            {
                
for(int i=0;i<4;i++)
                {
                    
if (input.ToString().Substring(i,1)==this.First)
                        result_b
=result_b+1;
                    
if (input.ToString().Substring(i,1)==this.Second)
                        result_b
=result_b+1;
                    
if (input.ToString().Substring(i,1)==this.Third)
                        result_b
=result_b+1;
                    
if (input.ToString().Substring(i,1)==this.Fouth)
                        result_b
=result_b+1;
                }
                
if (input.ToString().Substring(0,1)==this.First)
                {    
                    result_a
=result_a+1;
                    result_b
=result_b-1;
                }
                
if (input.ToString().Substring(1,1)==this.Second)
                {    
                    result_a
=result_a+1;
                    result_b
=result_b-1;
                }
                
if (input.ToString().Substring(2,1)==this.Third)
                {    
                    result_a
=result_a+1;
                    result_b
=result_b-1;
                }
                
if (input.ToString().Substring(3,1)==this.Fouth)
                {    
                    result_a
=result_a+1;
                    result_b
=result_b-1;
                }
                
if (result_a==4)
                {
                    Console.WriteLine(
"正确,人才阿!~~~");
                    end_sign
=1;
                    
return end_sign;
                }
                Console.WriteLine(result_a.ToString()
+"A"+result_b.ToString()+"B");
                count
--;
                Console.WriteLine(
"你还有"+(count+1).ToString()+"次机会!");
                end_sign
=0;
                
return end_sign;
            }
            
else    
                Console.WriteLine(
"Sorry,you lose!");
                Console.WriteLine(
"正确结果是: "+this.Numstr.ToString());
            end_sign
=2;
            
return end_sign;
            
        }
        
public void SetLevel()//设定游戏难度
        {
            
string level_str;
            Console.WriteLine(
"请选择游戏难度:!(1-简单;2-普通;3-困难)");
            level_str
=Console.ReadLine();
            
try
            {
                level
=Convert.ToInt32(level_str);
            }
            
catch
            {
                Console.WriteLine(
"请输入你想选择的难度的标号");
                
this.SetLevel();
            }
            
if (level!=1&level!=2&level!=3)
            {
                
this.SetLevel();
            }
            
if (level==1)
            {
                count
=9;
            }    
            
if (level==2)
            {
                count
=7;
            }
            
if (level==3)
            {
                count
=5;
            }
        }
        
public void ReStart()//重新运行游戏
        {
            Console.WriteLine(
"Do you want try again?(Y/N)");
            
string mark=Console.ReadLine();
            
if(mark=="y"||mark=="Y")
                
this.GameExecute();
            
else if (mark=="n"||mark=="N")
            {
                Console.WriteLine(
"Are you sure exit this game?(Y/N) ");
                
string remark=Console.ReadLine();
                
if (remark=="y"||remark=="Y")
                    Console.WriteLine(
"886!");
                
else if (remark=="n"||remark=="N")
                    
this.GameExecute();
                
else
                    
this.ReStart();
            }
            
else if(mark!="y"||mark!="Y"||mark!="n"||mark!="N")
                
this.ReStart();
        }
        
public void GameExecute()//游戏执行主程序
        {
            
this.Initialize();
            
this.SetLevel();
            
this.CreatRamstr();
            Console.WriteLine(
this.Numstr.ToString());
            
this.SetNumber();
            
int end=0;
            
while (end!=1&end!=2)
            {
                
this.GetInput();
                end
=this.Execute();
            }
        }
        
public string First
        {
            
get{return _first;}
            
set{_first=value;}
        }
        
public string Second
        {
            
get{return _second;}
            
set{_second=value;}
        }
        
public string Third
        {
            
get{return _third;}
            
set{_third=value;}
        }
        
public string Fouth
        {
            
get{return _fouth;}
            
set{_fouth=value;}
        }
        
public string Numstr
        {
            
get{return _numstr;}
            
set{_numstr=value;}
        }
    }
    
public class GameStart
    {
        [STAThread]
        
static void Main(string[] args)
        {
            GuessNumeral gn
=new GuessNumeral();
            gn.GameExecute();
            gn.ReStart();
        }
    }
}