朋友們,剛編寫完這個小游戲,已經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();
}
}
已經編譯通過.