saving player name & score to text file and sorting it question

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 2819 Views - Last Post: 12 December 2011 - 03:54 PM

#1 aolas   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 18-November 11

saving player name & score to text file and sorting it question

Posted 24 November 2011 - 06:39 AM

I want to add a save function to my game, where the program saving the score and the player name to the outer source like text file.

I already have the text file like this, and it already contain some dummy data :
AOLAS, 3500
SITI, 3600
JAMILAH, 2200
SURATNA, 3400
TATANG, 2000


what I want to do is, the program to save the player name & score to the text file. but before write the new data, it check the text file, line-per-line to get the line of data which has the lowest score on it and delete it. and after that the new data will be saved to the text file.

I tried to something like this :
public void saveHighScores()
       {
            string line;
            StreamWriter sw = new StreamWriter("highscore.txt");
            while ((line = sw.WriteLine()) != null)
            {
                string[] parts = line.Split(',');
                hiScoreName = parts[0].Trim();
                QuizScreen.score = Int32.Parse(parts[1].Trim());
            }

            sw.Close();
}



actually I just reversed my read high score data function from before, because I think it'll work like that. but it doesn't. it get the error "cannot implicitly convert type 'void' to 'string'" in the WriteLine().
and I still can't figured the logic(and it's code) how to check the lowest score, delete it, and then save the new data...

anyone can help...?

Is This A Good Question/Topic? 0
  • +

Replies To: saving player name & score to text file and sorting it question

#2 ShadowsEdge19   User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: saving player name & score to text file and sorting it question

Posted 24 November 2011 - 12:27 PM

Unfortunately you can't use the WriteLine() like that, have a look at these examples of using StreamWriter:

MSDN StreamWriter class
C# StreamWriter Tutorial
Was This Post Helpful? 0
  • +
  • -

#3 aolas   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 18-November 11

Re: saving player name & score to text file and sorting it question

Posted 25 November 2011 - 01:49 AM

after I read more about the streamwriter.
I experiment a bit, by doing like this :
public void saveHighScores()
       {

            string line;
            StreamWriter sw = new StreamWriter("highscore.txt");

            for (int i = 0; i < 5; i++)
            {
                sw.WriteLine(hiScoreName + "," + QuizScreen.score);
            }
            sw.Close();
       }



and it successfully write the new data to text file. the problem is, the data written 5 times.
and I still don't know how to check the lowest score, delete it, and then save the new data...
Was This Post Helpful? 0
  • +
  • -

#4 ShadowsEdge19   User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: saving player name & score to text file and sorting it question

Posted 25 November 2011 - 06:38 AM

Shouldn't you be trying to write your high score data array from your QuizScreen and not the base Score?

This post has been edited by ShadowsEdge19: 25 November 2011 - 06:38 AM

Was This Post Helpful? 0
  • +
  • -

#5 aolas   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 18-November 11

Re: saving player name & score to text file and sorting it question

Posted 26 November 2011 - 05:55 AM

ah yeah. I want to write the data from QuizScreen.cs that player got...
Was This Post Helpful? 0
  • +
  • -

#6 ShadowsEdge19   User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: saving player name & score to text file and sorting it question

Posted 26 November 2011 - 07:28 AM

Have you got some code that adds the new score from the player to the existing score list? You do that, sort it again with the 6 entries and rewrite the data with only the top 5.
Was This Post Helpful? 0
  • +
  • -

#7 aolas   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 18-November 11

Re: saving player name & score to text file and sorting it question

Posted 28 November 2011 - 08:00 PM

I tried it to be like this...

public void saveHighScores()
       {
           string line;
           int[] top = new int[5];
           int i = 0;
           StreamReader sr = new StreamReader("highscore.txt");
           while ((line = sr.ReadLine()) != null)
           {
               HighScoreData dataskornya = new HighScoreData();
               string[] parts = line.Split(',');
               dataskornya.PlayerName = parts[0].Trim();
               dataskornya.Score = Int32.Parse(parts[1].Trim());
               highScores.Add(dataskornya);
               top[i] = Int32.Parse(parts[1].Trim());
               i++;
           }
           highScores.Sort(CompareByPlayerScore);
           sr.Close();

           for(i = 0; i < 5; i++)
           {
                if(top[i] > QuizScreen.score)
                {
                    top[i] = QuizScreen.score;
                }
           }

               StreamWriter sw = new StreamWriter("highscore.txt");
               for (i = 0; i < 5; i++)
               {
                   line = hiScoreName + ", " + top[i];
                   sw.WriteLine(line);
               }
               highScores.Sort(CompareByPlayerScore);
               sw.Close();  
           

          
        }



but the new data overwrite the line with the largest score in the text file. and another problem is, all of the player name in the text file also overwrited by the newest player name data, but the score not changed. only the largest one changed with the new score.

ooh,this problem is quite hard for me. I already spent more than a couple of a day to solve this problem, but it still get wrong.... :sad3:
Was This Post Helpful? 0
  • +
  • -

#8 ShadowsEdge19   User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: saving player name & score to text file and sorting it question

Posted 29 November 2011 - 12:47 PM

Try this:

public void saveHighScores()
       {
           string line;
           int i = 0;
           StreamReader sr = new StreamReader("highscore.txt");
           while ((line = sr.ReadLine()) != null)
           {
               HighScoreData dataskornya = new HighScoreData();
               string[] parts = line.Split(',');
               dataskornya.PlayerName = parts[0].Trim();
               dataskornya.Score = Int32.Parse(parts[1].Trim());
               highScores.Add(dataskornya);
               i++;
           }
           sr.Close();

           highScores.Sort(CompareByPlayerScore);

           StreamWriter sw = new StreamWriter("highscore.txt");
           for (i = 0; i < 5; i++)
           {
               line = highScores[i].PlayerName + ", " + highScores[i].Score;
               sw.WriteLine(line);
           }
           sw.Close();  
        }


This post has been edited by ShadowsEdge19: 29 November 2011 - 12:49 PM

Was This Post Helpful? 0
  • +
  • -

#9 aolas   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 18-November 11

Re: saving player name & score to text file and sorting it question

Posted 29 November 2011 - 09:36 PM

I tried that. but it only sorting the data in the score text file. and not even write the new data in it...
Was This Post Helpful? 0
  • +
  • -

#10 ShadowsEdge19   User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: saving player name & score to text file and sorting it question

Posted 29 November 2011 - 11:58 PM

Sorry I missed a part out in between the read and write stages, just before you sort after the read you need to add the newest Player Name and Score from Quiz Screen to HighScores as a new dataskornya and then sort, the rest already handles working out the top 5 players.

public void saveHighScores()
       {
           string line;
           int i = 0;
           StreamReader sr = new StreamReader("highscore.txt");
           while ((line = sr.ReadLine()) != null)
           {
               HighScoreData dataskornya = new HighScoreData();
               string[] parts = line.Split(',');
               dataskornya.PlayerName = parts[0].Trim();
               dataskornya.Score = Int32.Parse(parts[1].Trim());
               highScores.Add(dataskornya);
               i++;
           }
           sr.Close();

HighScoreData dataskornya2 = new HighScoreData();
dataskornya2.PlayerName = QuizScreen.PlayerName;
dataskornya2.Score = QuizScreen.Score;
highScores.Add(dataskornya2);

highScores.Sort(CompareByPlayerScore);

StreamWriter sw = new StreamWriter("highscore.txt");
           for (i = 0; i < 5; i++)
           {
               line = highScores[i].PlayerName + ", " + highScores[i].Score;
               sw.WriteLine(line);
           }
           sw.Close();  
        }


This post has been edited by ShadowsEdge19: 30 November 2011 - 12:06 AM

Was This Post Helpful? 1
  • +
  • -

#11 aolas   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 18-November 11

Re: saving player name & score to text file and sorting it question

Posted 02 December 2011 - 05:24 PM

I tried that and it gives me an exception "Index was outside the bounds of the array."
in the :
dataskornya.Score = Int32.Parse(parts[1].Trim());


and also, it seems the new data still not writed in the text file....
Was This Post Helpful? 0
  • +
  • -

#12 ShadowsEdge19   User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: saving player name & score to text file and sorting it question

Posted 03 December 2011 - 02:21 AM

Can you tell me what the contents of "parts[]" is after the line.split(",") for both elements?
Was This Post Helpful? 0
  • +
  • -

#13 aolas   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 18-November 11

Re: saving player name & score to text file and sorting it question

Posted 03 December 2011 - 04:00 AM

i'm using line.split(",") to split the data, because i make 1 line contain 2 data (player name & score)
and use array parts to contain it in the program.
the parts[0] is to contain the player name data. which is positioned before the "," split in the line.
and the parts[1] is to contain the score, which is positioned after the "," split in each line.
Was This Post Helpful? 0
  • +
  • -

#14 ShadowsEdge19   User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: saving player name & score to text file and sorting it question

Posted 03 December 2011 - 11:34 AM

I know but when you try and run it what are each part's contents?
Was This Post Helpful? 0
  • +
  • -

#15 aolas   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 18-November 11

Re: saving player name & score to text file and sorting it question

Posted 03 December 2011 - 07:05 PM

oh, right.

after I tried running it. the parts[] array contain exactly like I told you before, the [0] contain the player name and [1] contain its score. it's same data as in the text file, from the first line to the last one.

but I found some weirdness in "dataskornya". because, the playername it's contain is right like in the text file, but the score is always "0" for each playername.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2