views:

309

answers:

2
+1  Q: 

Guess Game.. in C#

hey all.. this is my code..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Guess_Game
{
  class Program
  {
    static void Main(string[] args)
    {
        int quantity;
        int min, max;
        Console.WriteLine("Enter the Quantity of Numbers : ");
        quantity = int.Parse(Console.ReadLine());

        Console.WriteLine("Enter the Maximum and Minimum Number that you wish to be in your file : \n");
        Console.WriteLine("MIN : ");
        min = int.Parse(Console.ReadLine());
        Console.WriteLine("MAX : ");
        max = int.Parse(Console.ReadLine());
        Console.WriteLine("Now, Guess the Number : ");
        int number = int.Parse(Console.ReadLine());

        Console.WriteLine("Now Guess a Number That already contains in a File : ");
         number = int.Parse(Console.ReadLine());
         GuessGame(min, max, quantity, number);

        //if(number!= // number not equal to that number that contains in a file.. how to write it in that 
            //if condition..
    }

    static void GuessGame(int min,int max,int quantity,int number)
    {
        Random r = new Random();
        StreamWriter sw = new StreamWriter(@"D:\nasir\Guess Game\Guess Game\bin\Debug\ Guess Game.txt");

        for (int i = 0; i < quantity; i++)
        {
            int temp = r.Next(min, max);
            sw.WriteLine(temp);
        }
       // while(number != sw.WriteLine())
        {
            Console.WriteLine("Your Guess is Wrong!");
        }
        sw.Close();
    }
  }
}

i am not getting that how do i compare the numbers that are into the file from the number that is been passed from the function..

A: 

I have no idea how your file is ...

image that is:

12
17
9

then you use:

string[] fileNumbers = File.ReadAllLines("C:\MyGuessNumbers.txt");

so in fileNumbers you get all numbers into a string array, just loop and compare :)

TRY PARSE vs PARSE

from your code:

int quantity;
Console.WriteLine("Enter the Quantity of Numbers : ");
quantity = int.Parse(Console.ReadLine());

if the Console.ReadLine() is not an integer you will get an Exception saying that the object can not be parsed, witch is write, an "A" is a string and not an integer

if you use

int quantity = 0;
Console.WriteLine("Enter the Quantity of Numbers : ");
while(! int.TryParse(Console.ReadLine(), out quantity) )
   Console.WriteLine("That does not seam to be an integer, please only use numbers!\nEnter the Quantity of Numbers : ");

this will validate if that is a number, as it will TRY parsing and it will return a boolean TRUE or FALSE if it parsed the integer successfully or not.

in this example, I loop until the user enters a number, in progaming language, while the TryParse returns false.

balexandre
yea. as u can c in my program, i have generated random numbers.. that are stored in my .txt file extention..
Abid
if your program have already the numbers, why are you stuffing them into a file for the next line of code read that file??? it's much quicker to just return all the random numbers and compare no?
balexandre
added TRY PARSE vs PARSE for your understanding...
balexandre
no no no... i have generated random number class.. every time a execute my program, there will be new series of random numbers that will be stored in a path that is given..
Abid
+2  A: 

You're rolling dice every time (unfair, IMO) and doesn't read that file, but just writes it. Let's start with this:

static void Main()
{
    Random r = new Random();
    bool lucky = false;
    int maxTries, minValue, maxValue, guess = 0;

    GetInput("Enter number of tries: ", out maxTries);
    GetInput("Enter minimum number : ", out minValue);
    GetInput("Enter maximum number : ", out maxValue);

    int magical = r.Next(minValue, maxValue); // only once
    for (int i = 1; i <= maxTries; i++)
    {
        GetInput("Enter your guess : ", out guess);
        if (guess == magical)
        {
            lucky = true;
            break;
        }
    }

    Console.WriteLine("you.Lucky = {0};", lucky);
    Console.ReadLine();
}

static void GetInput(string text, out int variable)
{
    do Console.Write(text);          // avoiding stackoverflow.com scroll
    while (!Int32.TryParse(Console.ReadLine(), out variable));
}

If you want to get your numbers from a file, try something like:

List<int> magicals = new List<int>();
using (StreamReader reader = new StreamReader("GuessGame.txt"))
{
    int magical = 0;
    string line = "";
    while (!String.IsNullOrEmpty(line = reader.ReadLine()))
    {
        if (Int32.TryParse(line, out magical))
            magicals.Add(magical);
    }
}

and test is like this:

if(magicals.Contains(guess))
Rubens Farias
I would believe this is some kind'a homework ... it's not good to show/present an entire solution, let the fellow learn something! valeu? :)
balexandre
@balexandre, doesn't looks like homework for me, but I understood your point. curintia :)
Rubens Farias
So you think he's solving a real-world problem, then?
BlueRaja - Danny Pflughoeft