コード例 #1
0
        static public void Menu(string username)
        {
menuPrompt:
            Console.WriteLine("Use the command 'play' to start the game, 'leaderboard' to show the leaderboard.");
            Console.Write(">");
            string userInput = Console.ReadLine();

            if (userInput.ToLower() == "play")
            {
                Game game = new Game(username);
                game.Play();
            }
            else if (userInput.ToLower() == "leaderboard")
            {
                LeaderboardJSON leaderboardJson = JsonConvert.DeserializeObject <LeaderboardJSON>(File.ReadAllText(Leaderboard));
                List <UserJSON> leaderboard     = leaderboardJson.users.OrderBy(user => user.points).Reverse().ToList(); //sort by highest to lowest point amount

                for (int i = 0; i < 4; i++)
                {
                    try
                    {
                        Console.WriteLine($"{i + 1}. Name: {leaderboard[i].username} Points: {leaderboard[i].points}");
                    } catch { break; }
                }
                goto menuPrompt;
            }
            else
            {
                Console.WriteLine("Invalid input!");
                goto menuPrompt;
            }
        }
コード例 #2
0
        public void Play()
        {
            SongnamesJSON       songNames = JsonConvert.DeserializeObject <SongnamesJSON>(File.ReadAllText(Program.Songnames));
            List <SonginfoJSON> songs     = songNames.songinfo;

            Shuffle(songs);

            foreach (SonginfoJSON song in songs)
            {
                Console.Write($"{song.artist} - ");

                string[] words = song.songname.Split(' ');
                foreach (string word in words)
                {
                    Console.Write(word[0] + " ");
                }
                Console.Write("\n");

                int lives = 2;
guess:
                if (lives > 0)
                {
                    Console.WriteLine($"You have {lives} more tries to guess the song name!");
                    Console.Write(">");
                    string userGuess = Console.ReadLine();

                    if (userGuess.ToLower() == song.songname.ToLower())
                    {
                        Console.WriteLine("Correct!");

                        if (lives == 2)
                        {
                            points += 3;
                            Console.WriteLine($"You got 3 points! You have {points} points in total!");
                        }
                        else
                        {
                            points += 1;
                            Console.WriteLine($"You got 1 point! You have {points} points in total!");
                        }
                        System.Threading.Thread.Sleep(2000);
                        Console.Clear();
                    }
                    else
                    {
                        Console.WriteLine("Inorrect! :(");
                        lives--;
                        goto guess;
                    }
                }
                else
                {
                    break;
                }
            }
            Console.WriteLine($"Game over! You achieved {points} points!");

            LeaderboardJSON leaderboard = JsonConvert.DeserializeObject <LeaderboardJSON>(File.ReadAllText(Program.Leaderboard));

            bool foundUser = false;

            foreach (UserJSON user in leaderboard.users)
            {
                if (user.username == _username)
                {
                    if (user.points < points)
                    {
                        user.points = points;
                    }
                    foundUser = true;
                    break;
                }
            }

            if (!foundUser)
            {
                UserJSON newUser = new UserJSON
                {
                    username = _username,
                    points   = points
                };
                leaderboard.users.Add(newUser);
            }
            string _json = JsonConvert.SerializeObject(leaderboard);

            File.WriteAllText(Program.Leaderboard, _json);

            System.Threading.Thread.Sleep(2000);
            Console.Clear();
            Program.Menu(_username);
        }