当前位置:编程问答 > C#/ASP.NET >

C#小游戏-------猜数字 (solarsoft原创)

答案:朋友们,刚编写完这个小游戏,已经11点多了.休息一下,欣赏一下.学习任何的语言,都是在不断的编写中提高了.勿以简小而不为.using System;
public class Game
{
    private string number;
    private static int x,y;
    private int count=0;
    
    public void init()
    {
      do{
      Console.WriteLine("Please input number:");
      number=(string)Console.ReadLine();
      }while(number.Length!=4);
    }
    public void play()
    {
     string ready="1234";
//这里ready的值是要随机变化的.否则,没人玩你的游戏了.
//char[] r=new char[]{'0',.......'9'};
// 用循环和随机函数将其顺序打乱.在取其前4个元素,给ready就可以了.
     do{
        x=0;
        y=0;
        init();
        for(int i=0;i<=3;i++)
        {
           for(int j=0;j<=3;j++)
           {
              if(number[i]==ready[j])
              {
                 if (i==j)
                 ++x;
                 else
                 ++y;
              }
           }
         }
     ++count;   
     Console.WriteLine("{0}A{1}B",x.ToString(),y.ToString());
      if(count>=8)
      {
       Console.WriteLine("-------------------");
       Console.WriteLine("You already lossed");
       Console.WriteLine("-------------------");
       return;
       }
     }while(number!=ready);
      Console.WriteLine("----------------------------");
      Console.WriteLine("OK! You are very cleverness!");
      Console.WriteLine("----------------------------");

   }
}
public class MyGame
{
   public static void Main()
{
   Game g=new Game();
   g.play();
}
}
已经编译通过.

上一个:是不是这个?
下一个:简繁体转换例程

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,