# Simple guessing game help

• (2 Pages)
• 1
• 2

## 17 Replies - 1368 Views - Last Post: 26 March 2014 - 04:19 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=343019&amp;s=07a30b2a0d83393bf5b1faaa4ccfd485&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 tuned

• New D.I.C Head

Reputation: 0
• 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.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: ");

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");

}

}

}
}

```

Is This A Good Question/Topic? 0

## Replies To: Simple guessing game help

### #2 ragingben

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

## Re: Simple guessing game help

Posted 24 March 2014 - 07:24 AM

Cool, where are you having trouble.

### #3 tuned

• New D.I.C Head

Reputation: 0
• 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.

### #4 Skydiver

• Code herder

Reputation: 6285
• Posts: 21,608
• 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.

### #5 tuned

• New D.I.C Head

Reputation: 0
• 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.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: ");

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");

}

}

}
}
```

### #6 kiasta

• D.I.C Regular

Reputation: 30
• 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

### #7 tuned

• New D.I.C Head

Reputation: 0
• 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.

### #8 Michael26

• Futurama: Insert funny joke here

Reputation: 414
• 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
}
```

### #9 kiasta

• D.I.C Regular

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

## Re: Simple guessing game help

Posted 24 March 2014 - 12:46 PM

tuned, 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.

### #10 Skydiver

• Code herder

Reputation: 6285
• Posts: 21,608
• Joined: 05-May 12

## Re: Simple guessing game help

Posted 24 March 2014 - 01:24 PM

Michael26, 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);
}

```

### #11 tuned

• New D.I.C Head

Reputation: 0
• 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
{
{
String line = sr.ReadToEnd();
Console.WriteLine(line);

sr.Close();
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
```

### #12 Michael26

• Futurama: Insert funny joke here

Reputation: 414
• 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

### #13 tuned

• New D.I.C Head

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

## Re: Simple guessing game help

Posted 25 March 2014 - 06:07 AM

```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

### #14 tuned

• New D.I.C Head

Reputation: 0
• 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.

### #15 kiasta

• D.I.C Regular

Reputation: 30
• 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.