Guest User

Guest User

  • Tech Writer
  • 2
  • 988

Need help implementing array list in my Random Number Game.

Jul 7 2014 5:26 AM
I'm having troubles with implementing array list to store user input so that the user doesn't insert the same value twice when guessing, but I'm not sure on how to do that. If you could help point me in the right direction I would greatly appreciate it.
 
Here is my code:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RandomNumberGame
{
public class Program
{
public static void Main(string[] args)
{
Random gen = new Random();
int number;
bool over = false;
string userChoice;
int guess = 0;
int counter = 0;
int[] guesses = new int[5];

while (!over)
{
over = true;
Console.WriteLine("1 - Easy: 1 - 10");
Console.WriteLine("2 - Medium: 1 - 50");
Console.WriteLine("3 - Hard: 1 - 100");

Console.Write("Please input the number of the difficulty you wish to play: ");
string str = Console.ReadLine();

switch (str)
{
case "1":
Console.WriteLine("You chose easy difficulty.");
number = gen.Next(1, 11);

Console.WriteLine("I'm thinking of a number between 1 - 10");
do
{
while(!int.TryParse(Console.ReadLine(), out guess))
{
if (guess < 1 || guess > 10)
{
Console.WriteLine("Please enter a number between 1 - 10");
Console.WriteLine("You have " + (5 - counter) + " attempts left");
}
for (int i = 0; i < 4; i++)
{
if (guess == guesses[i])
{
Console.WriteLine("You already tried that, try again.");
Console.WriteLine("You have " + (5 - counter) + " attempts left");
}
}
}
if (guess < number)
{
Console.WriteLine("Higher");
counter++;
guesses[i] = Console.ReadLine();
Console.WriteLine("You have " + (5 - counter) + " attempts left");
}
else if (guess > number)
{
Console.WriteLine("Lower");
counter++;
Console.WriteLine("You have " + (5 - counter) + " attempts left");
}
if(counter >= 5)
{
counter = 0;
break;
}
} while(guess != number);
Console.WriteLine("You guess right! And it only took: " + (counter+1) + " tries");
Console.WriteLine("Would you like to play again? (y/n)");
userChoice = Console.ReadLine();
if(userChoice == "n")
{
Console.WriteLine("Thank you for playing!");
over = true;
counter = 0;
return;
}
if (userChoice == "y")
{
over = false;
counter = 0;
continue;
}
break;
case "2":
Console.WriteLine("You chose medium difficulty.");
number = gen.Next(1, 51);

Console.WriteLine("I'm thinking of a number between 1 - 50");
do
{
while(!int.TryParse(Console.ReadLine(), out guess))
{
if (guess < 1 || guess > 50)
{
Console.WriteLine("Please enter a number between 1 - 50");
Console.WriteLine("You have " + (5 - counter) + " attempts left");
}
for (int i = 0; i < 4; i++)
{
if (guess == guesses[i])
{
Console.WriteLine("You already tried that, try again.");
Console.WriteLine("You have " + (5 - counter) + " attempts left");
}
}
}
if (guess < number)
{
Console.WriteLine("Higher");
counter++;
Console.WriteLine("You have " + (5 - counter) + " attempts left");
}
else if (guess > number)
{
Console.WriteLine("Lower");
counter++;
Console.WriteLine("You have " + (5 - counter) + " attempts left");
}
if(counter >= 5)
{
counter = 0;
break;
}
} while(guess != number);
Console.WriteLine("You guess right! And it only took: " + (counter+1) + " tries");
Console.WriteLine("Would you like to play again? (y/n)");
userChoice = Console.ReadLine();
if(userChoice == "n")
{
over = true;
counter = 0;
return;
}
if (userChoice == "y")
{
over = false;
counter = 0;
continue;
}
break;
case "3":
Console.WriteLine("You chose hard difficulty.");
number = gen.Next(1, 101);

Console.WriteLine("I'm thinking of a number between 1 - 100");
do
{
while(!int.TryParse(Console.ReadLine(), out guess))
{
if (guess < 1 || guess > 100)
{
Console.WriteLine("Please enter a number between 1 - 100");
Console.WriteLine("You have " + (5 - counter) + " attempts left");
}
for (int i = 0; i < 4; i++)
{
if (guess == guesses[i])
{
Console.WriteLine("You already tried that, try again.");
Console.WriteLine("You have " + (5 - counter) + " attempts left");
}
}
}
if (guess < number)
{
Console.WriteLine("Higher");
counter++;
Console.WriteLine("You have " + (5 - counter) + " attempts left");
}
else if (guess > number)
{
Console.WriteLine("Lower");
counter++;
Console.WriteLine("You have " + (5 - counter) + " attempts left");
}
if(counter >= 5)
{
counter = 0;
break;
}
} while(guess != number);
Console.WriteLine("You guess right! And it only took: " + (counter+1) + " tries");
Console.WriteLine("Would you like to play again? (y/n)");
userChoice = Console.ReadLine();
if(userChoice == "n")
{
over = true;
counter = 0;
return;
}
if (userChoice == "y")
{
over = false;
counter = 0;
continue;
}
break;
default:
Console.WriteLine("I don't understand what you want to do, please enter the right input, 1 - 3.\n");
over = false;
break;
}
}
}
}
}