朋友們,剛編寫完這個小遊戲,已經 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 = 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 lost");
Console.WriteLine("-------------------");
return;
}
} while (number != ready);
Console.WriteLine("----------------------------");
Console.WriteLine("OK! You are very clever!");
Console.WriteLine("----------------------------");
}
}
public class MyGame
{
public static void Main()
{
Game g = new Game();
g.Play();
}
}
已經編譯通過。