C#小游戲-------猜數(shù)字 (solarsoft原創(chuàng))
發(fā)表時(shí)間:2024-06-14 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]朋友們,剛編寫完這個(gè)小游戲,已經(jīng)11點(diǎn)多了.休息一下,欣賞一下.學(xué)習(xí)任何的語言,都是在不斷的編寫中提高了.勿以簡小而不為.using System;public class Gameprivate string number;private static int x,y;private int c...
朋友們,剛編寫完這個(gè)小游戲,已經(jīng)11點(diǎn)多了.休息一下,欣賞一下.學(xué)習(xí)任何的語言,都是在不斷的編寫中提高了.勿以簡小而不為.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的值是要隨機(jī)變化的.否則,沒人玩你的游戲了.
//char[] r=new char[]{'0',.......'9'};
// 用循環(huán)和隨機(jī)函數(shù)將其順序打亂.在取其前4個(gè)元素,給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();
}
}
已經(jīng)編譯通過.