用C#写的猜数字小游戏,控制台的。
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==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();
}
}