zoukankan      html  css  js  c++  java
  • 猜数字游戏我也来凑热闹

    从技术上来看,还算新人吧。看到这个以前十分喜欢的游戏,忍不住来凑凑热闹。大家别笑。

        public class GuessGame
        {
            
    public int[] Answers { getprivate set; }
            
    public int MaxAttemptTimes { getset; }

            
    public int AttemptTimes { getprivate set; }
            
    public bool IsSuccess { getprivate 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));
                    }
                }
            }
        }

    请大家多多指教。

    ps: 怎么发表到话题下面????  不太明白。第一次发首页,有不合适的地方请多多包涵。

  • 相关阅读:
    [转] 一文读懂 HTTP/2 特性
    设置VS2019 支持C++17标准
    switch case 字符串表达式支持
    在Fabric实现类似Uniswap的去中心化交易所
    数据上链的原则与方式
    2.4g无线私有协议透传方案特色梳理
    无线数字麦克风解决方案小结
    高保真的音频编解码器模块及方案解析
    基于wifi的音频采集及处理解决方案小结
    基于智能降噪的助听器解决方案解析
  • 原文地址:https://www.cnblogs.com/MaYong/p/1520003.html
Copyright © 2011-2022 走看看