Simple guessing game help

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1323 Views - Last Post: 26 March 2014 - 04:19 AM Rate Topic: -----

#1 tuned  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-March 14

Simple guessing game help

Posted 24 March 2014 - 06:53 AM

Im trying to make a simple guessing game where you guess a number. What i want to add to this code is to add a table to the right size of the console, the table would contain all the players that played during this session and their numbers of guesses. This table would also be written in text file at the end of a game.

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

namespace TEST
{
    
    public class player
    {
        public string name;
        public string surname;


    }


    class Program
    {
        
    
        static void Main(string[] args)
        {
           do{
            Console.Write("name: ");
            string name = Console.ReadLine();
            Console.Write("surname: ");
            string surname = Console.ReadLine();
            player i = new player();
            i.name = name;
            i.surname = surname;





            StreamWriter write = File.CreateText("TEST.txt");
            const int MAX_NUMBER = 10;

            Random random = new Random();
            int numberToGuess = random.Next(MAX_NUMBER) + 1;
            int playersGuess = 0;
            int guesses = 0;

            Console.WriteLine("Welcome to game 'Guess the number'");
            Console.WriteLine("The number is between 1 and {0}.", MAX_NUMBER);

            while (playersGuess != numberToGuess)
            {
                guesses++;

                Console.Write("Write your number: ");
                int.TryParse(Console.ReadLine(), out playersGuess);

                if (playersGuess > numberToGuess)
                {
                    Console.WriteLine("Number{0} is too big!", playersGuess);
                }
                else if (playersGuess < numberToGuess)
                {
                    Console.WriteLine("Number {0} is too small!", playersGuess);
                }
                else
                {
                    Console.WriteLine("Number {0} is right. Congratulations! ", playersGuess);
                    Console.WriteLine("Player " + i.name + " " + i.surname + " guessed the number in {0} trys", guesses);
                    write.WriteLine("Player: " + i.name + " " + i.surname + ", Number of trys: " + guesses);
                    pisi.Close();
                }

            
            }
            Console.WriteLine("\nDo you want to continue the game? Y/N");
            
           }
           
           while (Console.ReadLine().Equals("y", StringComparison.InvariantCultureIgnoreCase));

        }


    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Simple guessing game help

#2 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 646
  • Joined: 07-October 08

Re: Simple guessing game help

Posted 24 March 2014 - 07:24 AM

Cool, where are you having trouble.
Was This Post Helpful? 0
  • +
  • -

#3 tuned  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-March 14

Re: Simple guessing game help

Posted 24 March 2014 - 08:44 AM

Like i said, i want to add a table to the right size of the console, the table would contain all the players that played during this session and their numbers of guesses. This table would also be written in text file at the end of a game.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6110
  • View blog
  • Posts: 21,027
  • Joined: 05-May 12

Re: Simple guessing game help

Posted 24 March 2014 - 08:57 AM

You are telling us what you want to do. You are not telling us what kind of problem you are running into as you try to do what you want.

We are not a code writing service. We will not do your homework for you. If you show us the code where you are trying to do something, we can guide you, but just telling us what you want isn't going to get you much help.
Was This Post Helpful? 0
  • +
  • -

#5 tuned  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-March 14

Re: Simple guessing game help

Posted 24 March 2014 - 10:30 AM

i have added the code at the end to write players scores to a text file, but it only writes them once in a text file and then doesn't want to write again to that text file. It needs to write scores to a text file, for every single player. Any suggestions?

	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;
	using System.Threading.Tasks;
	using System.IO;
	 
	namespace TEST
	{
	     
	    public class player
	    {
	        public string name;
	        public string surname;
	 
	 
	    }
	 
	 
	    class Program
	    {
	         
	     
	        static void Main(string[] args)
	        {
	           do{
	            Console.Write("name: ");
	            string name = Console.ReadLine();
	            Console.Write("surname: ");
	            string surname = Console.ReadLine();
	            player i = new player();
	            i.name = name;
	            i.surname = surname;
	 
	 
	 
	 
	 
	            StreamWriter write = File.CreateText("TEST.txt");
	            const int MAX_NUMBER = 10;
	 
	            Random random = new Random();
	            int numberToGuess = random.Next(MAX_NUMBER) + 1;
	            int playersGuess = 0;
	            int guesses = 0;
	 
	            Console.WriteLine("Welcome to game 'Guess the number'");
	            Console.WriteLine("The number is between 1 and {0}.", MAX_NUMBER);
	 
	            while (playersGuess != numberToGuess)
	            {
	                guesses++;
	 
	                Console.Write("Write your number: ");
	                int.TryParse(Console.ReadLine(), out playersGuess);
	 
	                if (playersGuess > numberToGuess)
	                {
	                    Console.WriteLine("Number{0} is too big!", playersGuess);
	                }
	                else if (playersGuess < numberToGuess)
	                {
	                    Console.WriteLine("Number {0} is too small!", playersGuess);
	                }
	                else
	                {
	                    Console.WriteLine("Number {0} is right. Congratulations! ", playersGuess);
	                    Console.WriteLine("Player " + i.name + " " + i.surname + " guessed the number in {0} trys", guesses);
	                     
string dat = @"c:\aa2.txt"; 
                    if (!File.Exists(dat))
                    {
                        StreamWriter write = File.AppendText(dat);
                        write.WriteLine("Name: " + i.name+"Number of guesess: "+guesses);

                        write.Close();
                       
                        
                    }
	                }
	 
	             
	            }
	            Console.WriteLine("\nDo you want to continue the game? Y/N");
	             
	           }
	            
	           while (Console.ReadLine().Equals("y", StringComparison.InvariantCultureIgnoreCase));
	 
	        }
	 
	 
	    }
	}

Was This Post Helpful? 0
  • +
  • -

#6 kiasta  Icon User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 287
  • Joined: 18-November 07

Re: Simple guessing game help

Posted 24 March 2014 - 11:43 AM

That's because you are attempting to create the text file instead of append.

Try something like this:

if (File.Exists(path)) 
{
    using (StreamWriter sw = File.AppendText(path)) 
    {
        //Do stuff here
    }
{



You can find more examples here:

http://msdn.microsof...(v=vs.110).aspx
http://msdn.microsof...y/ms143356.aspx

This post has been edited by kiasta: 24 March 2014 - 11:48 AM

Was This Post Helpful? 2
  • +
  • -

#7 tuned  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-March 14

Re: Simple guessing game help

Posted 24 March 2014 - 11:51 AM

i removed
if (!File.Exists(dat))
and it now works fine.
Was This Post Helpful? 0
  • +
  • -

#8 Michael26  Icon User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: Simple guessing game help

Posted 24 March 2014 - 12:46 PM

Just a piece of advice regarding this
int.TryParse(Console.ReadLine(), out playersGuess);


TryParse will try to parse the input from ReadLine() to see if it is a int, if it is it will return true and store the result int the playerGuess variable, but what if you enter something that isn't a number, then the method will return false and throw exception error because what you have is not a int and can't be stored in the playersGuess varaible. To that end i suggests you do the following

bool isinteger = int.TryParse(Console.ReadLine(), out playersGuess); 
if(isinteger) 
{
 //code for successful parsing, i.e input was a number
}
else
{
 //code for unsuccessful parsing, exception handling or just a simple message
} 

Was This Post Helpful? 1
  • +
  • -

#9 kiasta  Icon User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 287
  • Joined: 18-November 07

Re: Simple guessing game help

Posted 24 March 2014 - 12:46 PM

View Posttuned, on 24 March 2014 - 02:51 PM, said:

i removed
if (!File.Exists(dat))
and it now works fine.


I never used the "!" operator. But good it worked out for you.
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6110
  • View blog
  • Posts: 21,027
  • Joined: 05-May 12

Re: Simple guessing game help

Posted 24 March 2014 - 01:24 PM

View PostMichael26, on 24 March 2014 - 03:46 PM, said:

if you enter something that isn't a number, then the method will return false and throw exception error

Actually, TryParse() won't throw an exception. It will just return false. If you step through the following code, notice that an exception is not thrown.

            try
            {
                int intValue = 45;
                Console.WriteLine("TryParse returned: {0}", int.TryParse("Slytherin", out intValue));
                Console.WriteLine("Value is {0}", intValue);
            }

            catch(Exception ex)
            {
                Console.WriteLine("This code should never be reached.");
                Console.WriteLine(ex);
            }


Was This Post Helpful? 0
  • +
  • -

#11 tuned  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-March 14

Re: Simple guessing game help

Posted 25 March 2014 - 03:29 AM

I'm reading a text from a file, now how would i change the color of that text, so that in console text is shown in red color? i guess it has to do something with drawing class.


 try
                {
                    using (StreamReader sr = new StreamReader("Intro.txt"))
                    {
                        String line = sr.ReadToEnd();
                        Console.WriteLine(line);
    
                        sr.Close();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("The file could not be read:");
                    Console.WriteLine(e.Message);
                }

Was This Post Helpful? 0
  • +
  • -

#12 Michael26  Icon User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: Simple guessing game help

Posted 25 March 2014 - 03:40 AM

It would be much easier to write text to array instead of string, then you can change any element in the array to the color you like, and here is how to change the color
Was This Post Helpful? 0
  • +
  • -

#13 tuned  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-March 14

Re: Simple guessing game help

Posted 25 March 2014 - 06:07 AM

I added
Console.ForegroundColor = ConsoleColor.Red;
, and then
Console.ResetColor();
at the end, so that only this section will be in color.

Now i just need to read the text from a file and get it into a table that shows in console. Any ideas?
text in file is like this, and there are more lines as game continue: aaa:aa ; bbb:b ; ccc:1
aaa:aa ; bbb:b ; ccc:1
Was This Post Helpful? 0
  • +
  • -

#14 tuned  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-March 14

Re: Simple guessing game help

Posted 25 March 2014 - 09:08 AM

Another problem. I have StreamWriter set up like this:
string dat = @"C:\Users\David\Desktop\TEST\TEST\bin\Debug\Rezultati.txt"; 
                    {

                    using (StreamWriter pisi = File.AppendText(dat))
                        {
                            pisi.WriteLine("Ime: " + i.ime +"; Priimek: "+i.priimek+"; Število ugibanj: "+ ugibanja);
                        }
                            
                    } 


What i want is to have all the columns in text file in order, how would i do that? For example like this:
aaa:aa, bbb:baaa, ccc:1
aaa:aass, bbb:s, ccc:12
aaa:aa, bbb:baaa, ccc:1

Sorry i cant change my previus post. The columns in example should all be lined up.
Was This Post Helpful? 0
  • +
  • -

#15 kiasta  Icon User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 287
  • Joined: 18-November 07

Re: Simple guessing game help

Posted 25 March 2014 - 09:15 AM

Quote

Now i just need to read the text from a file and get it into a table that shows in console. Any ideas?
text in file is like this, and there are more lines as game continue: aaa:aa ; bbb:b ; ccc:1
aaa:aa ; bbb:b ; ccc:1


Aside from reading from the text file, what have you tried in regards to outputting the list on the console? Can't exactly help you without code. I tried something similar with a dice game, displaying the dice horizontally instead of vertically and it was a good challenge for when I was learning programming. Though I used C++ an not C# and I can't seem to find the post as it was so old.

For instance:

Console.WriteLine("First Name: ");
Console.WriteLine("\t"+Part of file here);
string name = Console.ReadLine(); //Program will "halt" and await for user input, so list will not display fully.



You could try adding the score sheet on top of the game and then put the entire game inside of a loop and after each game clear the screen and then display the scores on top once again and the game below. Basically a "refresh" if you will.

If you try to do side by side on the console window it will take some clever code, it's doable but tricky.
I would suggest to remove your code form Main and put it in separate classes. One class for the score sheet and then another class for the first part where you collect the names, then a third class for guessing the number. You will have to read snippets from your text file so make sure you format the text file to make it easy for you to read progmatically. Something like a CSV format, for example:

First Name: David,Surname: Smith,Tries: 6
First Name: Mike, Surname: Peters, Tries: 3
First Name: David,Surname: Smith,Tries: 2



Then you can search through and use the comma as a delimiter and output the data accordingly. Here is a good example:
http://www.dotnetperls.com/split

Also you should definitely make some type of check whether a file exists before trying to create the file, like so:

if (!File.Exists(path)
{
    StreamWriter write = File.CreateText("TEST.txt");
}



Also why are you appending to a file separate from the file you are creating?

Quote

StreamWriter write = File.CreateText("TEST.txt");
string dat = @"c:\aa2.txt";



Anyways, good luck.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2