Overclock.net banner

1 - 3 of 3 Posts

·
Registered
Joined
·
1,530 Posts
Discussion Starter · #1 ·
I did the Waterloo Canadian Computing Competition a month or so ago and I still cannot figure out where this issue I have been having is coming from. I have written the program twice to see if there was a compiler error or something but the exact same issue still happens.

The problem I am having is that the program is supposed to subtract the larger dice roll from the player with the lower dice rolls score but the program is getting strange numbers out of no where. I enter 3 as one players roll and 4 as another players score and the program subtracts 52 from the losers score.

Question Paper:


Code:

Code:
using System;

namespace DoubleDice
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            //Some Variables
            int numRounds;
            String readInput;
            int roundNumber = 0;
            int aScore = 100;
            int dScore = 100;
            Boolean firstRound = true;
            int amountToRemove = 0;
            char space;

            //Start of program
            //Sets however many rounds you want to play.
            Console.WriteLine("Enter the number of rounds you want to play:");
            readInput = Console.ReadLine();
            numRounds = Convert.ToInt16(readInput);
            var aRoll = new int[numRounds];
            var dRoll = new int[numRounds];
            Console.WriteLine("You will be playing: " + numRounds + " round(s).");
            //End of the round setter

            //Start of the rounds
            for (int i = 0; i < numRounds; i++)
            {
                //Displays a help message for the user when its the first round
                //This is so that they will input the scores correctly
                if (i == 0)
                {
                    firstRound = true;
                    Console.WriteLine("===============================================================================");
                    Console.WriteLine("The format for this if Antonia's score was 4 and David's Score was 5 would be: ");
                    Console.WriteLine("4 5");
                    Console.WriteLine("===============================================================================");
                }

                //Gets the score from the command prompt.
                Console.WriteLine("Round: "+roundNumber);
                Console.WriteLine("Please enter the score for Antonia followed by a space, then David's score: ");
                readInput = Console.ReadLine();

                //Splits the string from console into a char array.
                // Slot 0 is Atonia's Score, Slot 1 is a Space, and Slot 2 is David's Score
                char[] splitter = readInput.ToCharArray();

                //Converts the splitters char into an int that I can work with.
                int splitterConvertA = Convert.ToInt32(splitter[0]);
                int splitterConvertD = Convert.ToInt32(splitter[2]);

                //Sets the roll values to the converted value.
                aRoll[i] = splitterConvertA;
                space = splitter[1];
                dRoll[i] = splitterConvertD;

                //Checks if antions roll is larger than davids.
                //If his roll is larger remove his roll from davids score
                if (aRoll[i] > dRoll[i])
                {
                    //Amount to remove is the larger dice
                    amountToRemove = aRoll[i];
                    dScore = dScore - amountToRemove;
                }
                else
                {
                    //Same thing as above but for the opposite people
                    if (dRoll[i] > aRoll[i])
                    {
                        amountToRemove = dRoll[i];
                        aScore = aScore - amountToRemove;
                    }
                    else
                    {
                        //If it is a tie do not remove anything
                        if (dRoll[i] == aRoll[i])
                        {
                            amountToRemove = 0;
                        }
                    }
                }
            }
            Console.WriteLine(amountToRemove);
            //End of the rounds

            //Start of printout of scores
            Console.WriteLine("The game has finished.");
            Console.WriteLine("Antonia finished the game with a score of: " + aScore);
            Console.WriteLine("David finished the game with a score of: " + dScore);

            //Making sure the window does not close after the game is over.
            Console.WriteLine("Press Enter to Close the Program");
            Console.ReadLine();
        }
    }
}

Code:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Double_Dice_V2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Welcome to Double Dice");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write("Enter the number of rounds you want: ");
            int numberRounds = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("You will be playing: " + numberRounds + " rounds of double dice.");
            Boolean firstRun = true;
            int roundNumber = 1;
            String input;
            int aScore = 100;
            int dScore = 100;
            int aRoll;
            int dRoll;
            for (int i = 0; i < numberRounds; i++)
            {
                if (firstRun == true)
                {
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine("Format for Entering Scores: Antonia's score, a space, and finally David's score");
                    Console.ForegroundColor = ConsoleColor.White;
                    firstRun = false;
                }
                Console.WriteLine("Please enter the score for round: " + roundNumber);
                roundNumber++;
                input = Console.ReadLine();
                char[] splitString = input.ToCharArray();
                aRoll = Convert.ToInt32(splitString[0]);
                dRoll = Convert.ToInt32(splitString[2]);
                //Checking values to see where this problem comes about
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine(aRoll);
                Console.WriteLine(dRoll);
                Console.WriteLine("UnFormatted Values");
                Console.WriteLine(splitString[0]);
                Console.WriteLine(splitString[2]);
                Console.ForegroundColor = ConsoleColor.White;
                if (aRoll > dRoll)
                {
                    dScore = dScore - aRoll;
                }
                else
                {
                    if (dRoll > aRoll)
                    {
                        aScore = aScore - dRoll;
                    }
                    else
                    {
                        if (aRoll == dRoll)
                        {

                        }
                    }
                }
            }
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("================================================");
            Console.WriteLine("The final score is: ");
            Console.WriteLine("Antonia: " + aScore);
            Console.WriteLine("David " + dScore);
            Console.WriteLine("================================================");
            //Making sure the program does not shut down early
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Press enter to close program");
            Console.ReadLine();
        }
    }
}
 

·
Registered
Joined
·
1,530 Posts
Discussion Starter · #2 ·
I figured it out. You cannot go directly from a Char to an Int, if you do it gives you the ASCII value of the char and not the actual face value. To get the actual value from a char you need to convert it to a string and then from the string go to an int.
frown.gif
I wish I knew this a month ago.
 

·
Premium Member
Joined
·
10,773 Posts
Quote:
Originally Posted by xNovax View Post

I figured it out. You cannot go directly from a Char to an Int, if you do it gives you the ASCII value of the char and not the actual face value. To get the actual value from a char you need to convert it to a string and then from the string go to an int.
frown.gif
I wish I knew this a month ago.
Oh, but you can
wink.gif

No need for ToString and int conversions, just use your ACSII knowledge.

Code:

Code:
char cval = '9';
int ival = cval - '0';
 
1 - 3 of 3 Posts
Top