Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2012
    Posts
    6

    Opinion on old C# homework

    I am currently going through all of the videos in the C# 101 class. I have completed the homework through week 3, and while the "hang in there" game took me a bit, I got it without cheating, and I typed it in the long form coding method, i.e I didn't use any features that weren't taught through week 3.

    So my question I guess is, if any of you have seen those tutorials through week 3, what would you do to improve my code if at all? It took me 176 lines to get this thing to work properly, and I am just not sure if I am effectively using all the different loop structures that were taught to their maximum effectiveness.

    Here is the code:


    Code:
    using System;
    using System.Collections.Generic;
    
    namespace HangInThere
    {
    	class Program
    	{
    		static void Main()
    		{
    			Random random = new Random();
    			int randomNumber = random.Next(100, 999); // gives us the random number to try and guess
    
    			bool exit = false;
    
    			List<int> notCloseList = new List<int>(); // stored all the numbers that arent close
    			List<int> closeList = new List<int>(); // stores all numbers that are close
    			List<int> hangInThereList = new List<int>();  // stores all numbers with at least 1 number in the correct place 
    
    			Console.WriteLine("----Welcome to Hang-In-There!----");
    			Console.WriteLine();
    			Console.WriteLine("The goal of the game is to guess the random 3 digit number the computer has generated. You get a maximum of 15 guesses");
    			Console.WriteLine();
    			Console.WriteLine("Press any key to continue to game.");
    			Console.ReadKey(true);
    
    			while (!exit)
    			{
    
    				int myNumber = 0;  // the number the player inputs 
    
    				/* These next 2 variables are used to store the values of both the original randomNumber variable and the myNumber variable. This is done so when all the subtraction
    				 from the numbers happenes below, it doesnt actually alter the original variables, so they can be reused.*/
    				int randomNumEval = randomNumber;
    				int myNumEval;
    
    				
    
    				while (myNumber < 100 || myNumber > 999) // continues to ask for a number until the player eneters one between 100-999
    				{
    
    					Console.Clear();
    
    					Console.WriteLine("|   Not Close    |     Close      | Hang In There |");
    					Console.WriteLine("|----------------|----------------|---------------|");
    
    					int wrongIndex = 0;
    
    					while (wrongIndex <= notCloseList.Count || wrongIndex <= closeList.Count || wrongIndex <= hangInThereList.Count) // this whole loop is how all of the different wrong answers get displayed into their columns
    					{
    						if (wrongIndex < notCloseList.Count)
    						{
    							Console.Write("| " + notCloseList[wrongIndex] + "            ");
    						}
    						else
    						{
    							Console.Write("|                ");
    						}
    
    						if (wrongIndex < closeList.Count)
    						{
    							Console.Write("| " + closeList[wrongIndex] + "            ");
    						}
    						else
    						{
    							Console.Write("|                ");
    						}
    
    						if (wrongIndex < hangInThereList.Count)
    						{
    							Console.Write("| " + hangInThereList[wrongIndex] + "           |");
    						}
    						else
    						{
    							Console.Write("|               |");
    						}
    						Console.WriteLine();
    
    
    						wrongIndex++;
    					}
    
    					Console.WriteLine();
    					Console.WriteLine();
    					Console.Write("What is your guess (a number between 100-999): ");
    					if (!int.TryParse(Console.ReadLine(), out myNumber) || myNumber < 100 || myNumber > 999) // check to see if the guess the person enters is valid and between 100-999
    					{
    						Console.WriteLine();
    						Console.WriteLine("That was not a valid guess. Try again.(Press a key)");
    						Console.ReadKey(true);
    					}
    					
    				}
    
    				myNumEval = myNumber; // assigns the myNumber variable which the player enetered to the myNumEval variable which will be decontructed into ones, tens and hundreds
    				
    
    				if (myNumber == randomNumber) // if true you win the game
    				{
    					exit = true;
    				}
    				else // if not equal to random number, this extracts the individual numbers (ones, tens, hundreds) from the myNumEval and randomNumEval variables and inputs the humber into the respective (100, 10, 1) index variable
    				{
    					int myNumber100 = 0;
    					int myNumber10 = 0;
    					int myNumber1 = 0;
    					int randomNumber100 = 0;
    					int randomNumber10 = 0;
    					int randomNumber1 = 0;
    					
    					for (int i = myNumEval; i >= 100; i -= 100)
    					{
    						myNumber100++;
    						myNumEval -= 100;
    					}
    
    					for (int i = myNumEval; i >= 10; i -= 10)
    					{
    						myNumber10++;
    						myNumEval -= 10;
    					}
    
    					for (int i = myNumEval; i >= 1; i -= 1)
    					{
    						myNumber1++;
    						myNumEval -= 1;
    					}
    						
    					for (int i = randomNumEval; i >= 100; i -= 100)
    					{
    						randomNumber100++;
    						randomNumEval -= 100;
    					}
    
    					for (int i = randomNumEval; i >= 10; i -= 10)
    					{
    						randomNumber10++;
    						randomNumEval -= 10;
    					}
    
    					for (int i = randomNumEval; i >= 1; i -= 1)
    					{
    						randomNumber1++;
    						randomNumEval -= 1;
    					}
    						
    
    					bool hangInThere = myNumber100 == randomNumber100 || myNumber10 == randomNumber10 || myNumber1 == randomNumber1; // if the number meets any of these conditions then this bool is true and the number will be added to the hang in there column
    					
    					bool notClose = myNumber100 != randomNumber100 && myNumber100 != randomNumber10 && myNumber100 != randomNumber1 && // same of the hang in there bool, but for not close numbers
    					                myNumber10 != randomNumber100 && myNumber10 != randomNumber10 && myNumber10 != randomNumber1 &&
    					                myNumber1 != randomNumber100 && myNumber1 != randomNumber10 && myNumber1 != randomNumber1;
    
    					if (notClose)
    					{
    						notCloseList.Add(myNumber);
    					}
    					
    					if (hangInThere)
    					{
    						hangInThereList.Add(myNumber);
    					}
    
    					if (!notClose && !hangInThere) // this does not have its own bool because I was having conflict issues with numbers showing up in 2 columns, but making where if the number doesnt fit in either one of the other 2 columns, then it must be this column seems to work
    					{
    						closeList.Add(myNumber);
    					}
    				}
    			}
    			Console.WriteLine();
    			Console.WriteLine("You have WON!!!");
    			Console.ReadKey();
    		}
    	}
    }

  2. #2
    Join Date
    Oct 2004
    Posts
    47
    First off I wanted to say every well done for completing the homework without cheating , I know Google is a great temptation.

    As for the program there are some things that can be done to improve performance:
    a. When using Console.WriteLine() it is basically adding a blank line, well by adding a new line tag "\n" to an existing WriteLine it will help; for example:

    Code:
    Console.WriteLine();
    Console.WriteLine("You have WON!!!");
    Can be written as:
    Code:
    Console.WriteLine("\nYou have WON!!!");
    b. (Please forgive me if this wasn't covered in the class yet it’s been quite some time ) A string is an array of chars. Therefore when the user inputs a number like "101" it is becomes a string well we can use that to our advantage. We can access the string like an array to get each section of the number, for example:

    Code:
    string guess = "101;
    int randomNum = 200;
    
    string randNumString = randomNum.ToString();
    
    if (guess[0] == randNumString[0] && guess[1] == randNumString[1] && guess[3] == randNumString[3])
    {
        Console.WriteLine("\nYou have WON!!!");
    }
    else
    {
        Console.WriteLine("\nYou have LOST!!!");
    }
    Just keep in mind you still have to convert the user's input to an int to validate whether the number is between 100 and 999.

    Hopefully this helps you out in some way

  3. #3
    Join Date
    Oct 2012
    Posts
    6
    Thanks for the detailed replay! Yea, actually up through week 3, and what I have watched of week 4 thus far, they still have not covered proper shorthand string formatting yet...they still wanted to see it typed out in long form, hence why I did it like that.

    The whole strings can be accessed like arrays thing is what blew my mind though. Before you even replied I can what Nelsons answer to that homework was, and he also used a character comparison with the individual numbers pulled from the string. I have looked through the pages and pages of notes I had up through that point, and while they did cover how to convert numbers into strings, they never covered how to access individual characters in a string lke an array. Gosh that would have made this so much easier! LOL

    Anyway, at least I didnt cheat, even though it looks like code Shrek invented.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •