从技术上来看,还算新人吧。看到这个以前十分喜欢的游戏,忍不住来凑凑热闹。大家别笑。
public
class
GuessGame
{
public int [] Answers { get ; private set ; }
public int MaxAttemptTimes { get ; set ; }
public int AttemptTimes { get ; private set ; }
public bool IsSuccess { get ; private set ; }
public bool isEnd
{
get
{
return ! (AttemptTimes < MaxAttemptTimes) || IsSuccess;
}
}
public GuessGame()
{
Answers = new int [ 4 ];
MaxAttemptTimes = 6 ;
NewGames();
}
public void NewGames()
{
buildAnswers();
AttemptTimes = 0 ;
IsSuccess = false ;
}
private void buildAnswers()
{
Random random = new Random();
do
{
Answers[ 0 ] = random.Next( 9 );
Answers[ 1 ] = random.Next( 9 );
Answers[ 2 ] = random.Next( 9 );
Answers[ 3 ] = random.Next( 9 );
} while ( ! isValidate(Answers));
}
public string Test( int [] answer)
{
string result;
int a = 0 ;
int b = 0 ;
if (isEnd)
{
result = " 对不起,比赛已经结束。 " ;
if (IsSuccess) { result += " 您赢得了比赛! " ; }
else { result += " 一次失败不算什么,请再接再厉! " ; }
return result;
}
if ( ! isValidate(answer)) { return " 对不起,输入结果不符合要求 " ; }
for ( int i = 0 ; i < Answers.Length; i ++ )
{
if (answer[i] == Answers[i]) { a ++ ; }
if (Answers.Any(r => r == answer[i])) { b ++ ; }
}
if ( 4 == a) { IsSuccess = true ; }
b -= a;
AttemptTimes ++ ;
return " 这是你的第 " + AttemptTimes + " 次测试: " + a.ToString() + " A " + b.ToString() + " B " ;
}
private bool isValidate( int [] answer)
{
if ( 4 != answer.Length) { return false ; }
foreach ( int i in answer)
{
if (answer.Where(a => a == i).Count() > 1 ) { return false ; }
}
return true ;
}
}
{
public int [] Answers { get ; private set ; }
public int MaxAttemptTimes { get ; set ; }
public int AttemptTimes { get ; private set ; }
public bool IsSuccess { get ; private set ; }
public bool isEnd
{
get
{
return ! (AttemptTimes < MaxAttemptTimes) || IsSuccess;
}
}
public GuessGame()
{
Answers = new int [ 4 ];
MaxAttemptTimes = 6 ;
NewGames();
}
public void NewGames()
{
buildAnswers();
AttemptTimes = 0 ;
IsSuccess = false ;
}
private void buildAnswers()
{
Random random = new Random();
do
{
Answers[ 0 ] = random.Next( 9 );
Answers[ 1 ] = random.Next( 9 );
Answers[ 2 ] = random.Next( 9 );
Answers[ 3 ] = random.Next( 9 );
} while ( ! isValidate(Answers));
}
public string Test( int [] answer)
{
string result;
int a = 0 ;
int b = 0 ;
if (isEnd)
{
result = " 对不起,比赛已经结束。 " ;
if (IsSuccess) { result += " 您赢得了比赛! " ; }
else { result += " 一次失败不算什么,请再接再厉! " ; }
return result;
}
if ( ! isValidate(answer)) { return " 对不起,输入结果不符合要求 " ; }
for ( int i = 0 ; i < Answers.Length; i ++ )
{
if (answer[i] == Answers[i]) { a ++ ; }
if (Answers.Any(r => r == answer[i])) { b ++ ; }
}
if ( 4 == a) { IsSuccess = true ; }
b -= a;
AttemptTimes ++ ;
return " 这是你的第 " + AttemptTimes + " 次测试: " + a.ToString() + " A " + b.ToString() + " B " ;
}
private bool isValidate( int [] answer)
{
if ( 4 != answer.Length) { return false ; }
foreach ( int i in answer)
{
if (answer.Where(a => a == i).Count() > 1 ) { return false ; }
}
return true ;
}
}
class
Program
{
static void Main( string [] args)
{
Console.WriteLine( " 我猜,我猜,我猜猜猜! " );
GuessGame games = new GuessGame();
while ( true )
{
if (games.isEnd)
{
if (games.IsSuccess) { Console.WriteLine( " 恭喜你,成功了。 " ); }
else { Console.WriteLine( " 失败是成功之母! " ); }
Console.WriteLine( " 是否继续新游戏(Y/N)? " );
char c = ( char )Console.Read();
Console.ReadLine();
if ( " y " == c.ToString().ToLower()) { games.NewGames(); }
else { return ; }
}
Console.Write( " 请输入四位数字: " );
string input = Console.ReadLine();
if ( ! Regex.IsMatch(input, @" ^\d{4}$ " )) { Console.WriteLine( " 输入错误! " ); }
else
{
int [] answer = new int [ 4 ];
for ( int i = 0 ; i < 4 ; i ++ )
{
answer[i] = Int32.Parse(input[i].ToString());
}
Console.WriteLine(games.Test(answer));
}
}
}
}
{
static void Main( string [] args)
{
Console.WriteLine( " 我猜,我猜,我猜猜猜! " );
GuessGame games = new GuessGame();
while ( true )
{
if (games.isEnd)
{
if (games.IsSuccess) { Console.WriteLine( " 恭喜你,成功了。 " ); }
else { Console.WriteLine( " 失败是成功之母! " ); }
Console.WriteLine( " 是否继续新游戏(Y/N)? " );
char c = ( char )Console.Read();
Console.ReadLine();
if ( " y " == c.ToString().ToLower()) { games.NewGames(); }
else { return ; }
}
Console.Write( " 请输入四位数字: " );
string input = Console.ReadLine();
if ( ! Regex.IsMatch(input, @" ^\d{4}$ " )) { Console.WriteLine( " 输入错误! " ); }
else
{
int [] answer = new int [ 4 ];
for ( int i = 0 ; i < 4 ; i ++ )
{
answer[i] = Int32.Parse(input[i].ToString());
}
Console.WriteLine(games.Test(answer));
}
}
}
}
请大家多多指教。
ps: 怎么发表到话题下面???? 不太明白。第一次发首页,有不合适的地方请多多包涵。