Beispiel #1
0
        private void RegisterUser()
        {
            if (Player != null)
            {
                Console.WriteLine("You are already registered");
                return;
            }

            string username;

            do
            {
                Console.Write("Enter a username: "******"Invalid username. Please enter a username");
                    continue;
                }

                break;
            }while (true);

            string jsonRequest = JsonConvert.SerializeObject(new Packet(Command.REGISTER_USER, true, new List <object>()
            {
                username
            }));

            WritePacket(socket, jsonRequest);

            Packet packet = ReadPacket(socket);

            if (packet.Success)
            {
                Player = JsonConvert.DeserializeObject <ClientPlayer>(packet.DataToString()[0]);
                DebugUtils.WriteLine("[CLIENT] Player successfully registered");
                MenuUtils.ShowRegisteredMenu(Player);
            }
            else
            {
                DebugUtils.WriteLine("[CLIENT] Error in registering a user");
            }
        }
Beispiel #2
0
        private void GameLoop()
        {
            DebugUtils.WriteLine($"[CLIENT] {Player.Name} in now in their GameLoop!");
            bool isGameOngoing = true;

            while (isGameOngoing)
            {
                Packet packet = ReadPacket(socket);

                switch (packet.Command)
                {
                case Command.REQUEST_MOVE:
                    // Clear any previous user input.
                    while (Console.KeyAvailable)
                    {
                        Console.ReadKey(false);
                    }

                    Console.WriteLine($"\n{Player.Name}, you have {Player.Chips} chips...\nYour hand is:");
                    Player.Hand.ShowHand();

                    bool isValidChoice = false;
                    int  choice        = -1;

                    while (!isValidChoice)
                    {
                        Console.WriteLine("\n\nSTAY, FOLD, or RAISE? \nTo RAISE type 'RAISE n' where n is the number you want to raise by.");
                        string input = Console.ReadLine().ToLower();

                        if (!String.IsNullOrEmpty(input))
                        {
                            List <string> stringTokens = input.Split(' ').ToList();
                            string        playerChoice = stringTokens[0];
                            switch (playerChoice)
                            {
                            case "fold":
                                choice        = -1;
                                isValidChoice = true;
                                break;

                            case "stay":
                                choice        = 0;
                                isValidChoice = true;
                                break;

                            case "raise":
                                if (stringTokens.Count != 2)
                                {
                                    Console.WriteLine("Please enter a valid number of chips.");
                                    break;
                                }
                                try
                                {
                                    choice = int.Parse(stringTokens[1]);
                                }
                                catch (Exception)
                                {
                                    Console.WriteLine("Please enter a valid number of chips.");
                                    break;
                                }
                                if (choice <= 0)
                                {
                                    Console.WriteLine("You must bet at least 1 chip.");
                                    break;
                                }
                                if (choice > Player.Chips)
                                {
                                    Console.WriteLine("You have insufficient funds to place this bet, please try again.");
                                    break;
                                }
                                isValidChoice = true;
                                break;

                            default:
                                Console.WriteLine("Invalid call made. Try again.");
                                break;
                            }
                        }
                    }

                    string jsonResponse = JsonConvert.SerializeObject(new Packet(Command.SEND_MOVE, true, new List <object>()
                    {
                        choice
                    }));
                    WritePacket(socket, jsonResponse);

                    // If player folds, exit the function.
                    if (choice == -1)
                    {
                        isGameOngoing = false;
                    }

                    break;

                case Command.GIVE_CARD:
                    Card card = JsonConvert.DeserializeObject <Card>(packet.DataToString()[0]);
                    Player.Hand.AddCard(card);
                    Console.WriteLine("You received a card.");
                    Player.Hand.ShowHand();
                    Console.WriteLine();
                    break;

                case Command.ADJUST_CHIPS:
                    int chips = JsonConvert.DeserializeObject <int>(packet.DataToString()[0]);
                    Player.Chips = chips;
                    break;

                case Command.DISPLAY_MESSAGE:
                    string message = JsonConvert.DeserializeObject <string>(packet.DataToString()[0]);
                    Console.WriteLine(message);
                    break;

                case Command.SHOW_HANDS:
                    List <KeyValuePair <string, List <Card> > > players = JsonConvert.DeserializeObject <List <KeyValuePair <string, List <Card> > > >(packet.DataToString()[0]);
                    foreach (KeyValuePair <string, List <Card> > player in players)
                    {
                        Console.Write(player.Key + " has ");
                        Hand playerHand = new Hand(player.Value);
                        playerHand.ShowHand();
                        Console.WriteLine();
                    }
                    break;

                case Command.ANNOUCE_WINNER:
                    string      winnerName     = JsonConvert.DeserializeObject <string>(packet.DataToString()[0]);
                    List <Card> winnerCards    = JsonConvert.DeserializeObject <List <Card> >(packet.DataToString()[1]);
                    string      winnerWinnings = JsonConvert.DeserializeObject <string>(packet.DataToString()[2]);

                    // Only print the ranking if they finished the entire round and have five cards.
                    if (winnerCards.Count == 5)
                    {
                        Console.Write($"\nThe winner is... {winnerName}! Their winning hand is ");
                        Hand winnerHand = new Hand(winnerCards);
                        winnerHand.PrintRanking();
                    }

                    Console.WriteLine(winnerWinnings);
                    isGameOngoing = false;
                    break;

                case Command.REMOVE_PLAYER:
                    string removeMessage = JsonConvert.DeserializeObject <string>(packet.DataToString()[0]);
                    Console.WriteLine(removeMessage);
                    isGameOngoing = false;
                    break;
                }
            }
            // Empty the player's hand for further games
            Player.Hand.ClearHand();
            Console.WriteLine("\nPress any key to return to main menu.");
            Console.ReadLine();
            MenuUtils.ShowRegisteredMenu(Player);
        }