Question Need help with my risk type game program - I'm lost, please help me

juan.carosi

New member
Joined
Jun 17, 2021
Messages
1
Programming Experience
Beginner
My problems are:
I would have to change the amount of players from 2 to a number introduced by the user (max 6).
I have a problem with the sorting out of the dices results. I don't understand why the program returns always the result of 3 dices even when I tell him to use 1 or 2 dices.
I would need also to confront the results between the dices results between the 2 players and give out a result.
I don't know how to end the cycle of the one attack I'm doing and to restart the attacking cycle if the user responds that the wants to attack again. Also I would need to end the game at some point


C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Text;


namespace ConsoleApp8
{
    class Program
    {
        static List<string> names;
        static List<int> dices; // [2, 3]
        static List<int> player1Dices = new List<int>();
        static List<int> player2Dices = new List<int>();

        static List<string> PromptNames()
        {
            Console.Write("Nome del primo giocatore: ");
            string player1 = Console.ReadLine();
            Console.Write("Nome del secondo giocatore: ");
            string player2 = Console.ReadLine();

            List<string> names = new List<string>();

            names.Add(player1);
            names.Add(player2);
            return names;
        }

        static int GetDieValue(string x, string msg)
        {
            Console.Write("{0}, {1} ", x, msg);
            string rawDie = Console.ReadLine();
            int parsedDie;
            // qui devi usare do, while perché non sai a priori quante volte un giocatore potrebbe inserire un valore sbagliato
            for (!Int32.TryParse(rawDie, out parsedDie))
            {
                do
                {
                    Console.WriteLine("Meti un numero");
                    return GetDieValue(x, msg);
                }
                while (x);
            }
            return parsedDie;
        }

        static List<int> PromptDices()
        {
            int p1Dices = GetDieValue(names[0], "con quanti dadi vuoi attaccare?");
            int p2Dices = GetDieValue(names[1], "con quanti dadi ti vuoi difendere?");

            return new List<int>() { p1Dices, p2Dices };
        }
        static void Play()
        {
            Console.WriteLine("{0} attacca con {1} dadi", names[0], dices[0]);
            Console.WriteLine("{0} difende con {1} dadi", names[1], dices[1]);
            CalculateResult();

        }
        static void CalculateResult()
        {
            System.Random random = new System.Random();
            for (int i = 0; i <= dices.Count; i++)
            {
                player1Dices.Add(random.Next(1, 7));
            }
            Console.WriteLine("Prima: {0}{1}{2}", player1Dices[0], player1Dices[1], player1Dices[2]);
            player1Dices = player1Dices.OrderByDescending(x => x).ToList();
            Console.WriteLine("Dopo: {0}{1}{2}", player1Dices[0], player1Dices[1], player1Dices[2]);


            for (int i = 0; i <= dices.Count; i++)
            {
                player2Dices.Add(random.Next(1, 7));
            }
            Console.WriteLine("Prima: {0}{1}{2}", player2Dices[0], player2Dices[1], player2Dices[2]);
            player2Dices = player2Dices.OrderByDescending(x => x).ToList();
            Console.WriteLine("Dopo: {0}{1}{2}", player2Dices[0], player2Dices[1], player2Dices[2]);

        }

        static void PlayAgain(string x)
        {
            Console.WriteLine("Se vuoi finire il gioco scrivi Exit");
            string Exit = x;
            do
            {
                RunGame();
                player1Dices.Clear();
                player2Dices.Clear();
            }
            while (x != Console.ReadLine());



        }

        static void RunGame()
        {
            names = PromptNames();
            dices = PromptDices();
            CalculateResult();
            Play();
            PlayAgain();

            // generate random numbers - and sort them
            // display results - compare dices
            // repeat or quit?
        }

        static void Main(string[] args)
        {
            RunGame();
            Console.WriteLine("Thanks for playing");
            Console.ReadKey();
        }
    }
}
 
Last edited by a moderator:
As for the issues, you should address one at a time. You need to start by debugging your code. Set a breakpoint at the top and step through it line by line. At each step, determine exactly what you expect to happen based on the algorithm you worked out before writing the code (I know you skipped that part, which is one of the main reasons that you're in a mess now; it always is) and then, when you step, check what actually did happen and compare the two. If reality and expectation don't match then you have found an issue and you can investigate that specifically. If you need our help, you can explain the specific issue, including what you expected, what happened and where.
 
Back
Top Bottom