Computer Science Canada

Save/Load stats from text file

Author:  element4l [ Sat Dec 15, 2007 12:52 pm ]
Post subject:  Save/Load stats from text file

OK, I have a Tic Tac Toe game, and I wanted to add a feature that if the user types their name as one of the players, the program should go look in a text file (already created by the program) and check if it can find the same name. Then it should be able to read, for example, how many times that person has won, how many times they have lost, etc. But here's my problem, I want the info to show up inside different textboxes.

So basically I'm looking for a way to read each line individually, checking if it matches with a variable (supplied by the user) then reading the following 4 lines, where each line has a number.
The following is a snippet of codet that I am using in order to write to the text file, and it works great. But I have no idea how I could store info from those files into several variables.

code:

        Directory.CreateDirectory("C:\Tic-Tac-Toe\Profiles\" & player1)
        Dim sWriter As StreamWriter = New StreamWriter("C:\Tic-Tac-Toe\Profiles\" & player1 & ".txt")

        'name of player
        sWriter.WriteLine(player1)

        'the amount of wins
        sWriter.WriteLine(player1Score)

        'the amount of losses
        sWriter.WriteLine(player2Score)

        'how many times they've played
        sWriter.WriteLine(rounds)

        'how many times they've tied the game
        sWriter.WriteLine(draws)
   
        'same for the second player
        sWriter.WriteLine(player2)
        sWriter.WriteLine(player2Score)
        sWriter.WriteLine(player1Score)
        sWriter.WriteLine(rounds)
        sWriter.WriteLine(draws)

        sWriter.Flush()
        sWriter.Close()


Thanks for any help at all. Smile

Author:  Euphoracle [ Sat Dec 15, 2007 6:19 pm ]
Post subject:  RE:Save/Load stats from text file

Use a StreamReader and do <name>.ReadLine()? Who said .Net didn't make sense Very Happy

Author:  element4l [ Sun Dec 16, 2007 1:00 am ]
Post subject:  Re: Save/Load stats from text file

I've decided to settle on saving everything to the player's name text file, which I can easily work on, but when I try reading a text file that has something written in it, it says "The process cannot access the file 'C:\Tic-Tac-Toe\Profiles\Player 1.txt' because it is being used by another process." This doesn't happen when I take off the "statRead" process. So I'm assuming the problem is in there. If you find any french text in there, don't bother asking. the project is to be done in french, since I go to a french school. I just prefer "programming" in english if you know what I mean.

If the following code is useful at all, not all of it is important.
code:

Imports System.IO

Public Class Winner

    Private Sub statWrite()
        Dim sWriter As StreamWriter = New StreamWriter("C:\Tic-Tac-Toe\Profiles\" & player1 & ".txt")
        Dim sWriter2 As StreamWriter = New StreamWriter("C:\Tic-Tac-Toe\Profiles\" & player2 & ".txt")

        sWriter.WriteLine(player1Score)
        sWriter.WriteLine(player2Score)
        sWriter.WriteLine(rounds)
        sWriter.WriteLine(draws)

        sWriter2.WriteLine(player2Score)
        sWriter2.WriteLine(player1Score)
        sWriter2.WriteLine(rounds)
        sWriter2.WriteLine(draws)

        sWriter.Flush()
        sWriter.Close()
        sWriter2.Flush()
        sWriter2.Close()
    End Sub

    Private Sub statRead()
        Dim sReader As StreamReader = New StreamReader("C:\Tic-Tac-Toe\Profiles\" & player1 & ".txt")
        Dim sReader2 As StreamReader = New StreamReader("C:\Tic-Tac-Toe\Profiles\" & player2 & ".txt")

        txtProfiles.AppendText(sReader.ReadToEnd)
    End Sub

    Private Sub Winner_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        gbxp1.Text = player1
        gbxp2.Text = player2
        txtWinner.Text = gagnant
        txtP1Wins.Text = player1Score
        txtP2Wins.Text = player2Score
        txtP1Losses.Text = player2Score
        txtP2Losses.Text = player1Score
        txtRounds.Text = rounds
        txtDraws.Text = draws
        If player1Score > player2Score Then
            txtDominating.Text = player1
        ElseIf player2Score > player1Score Then
            txtDominating.Text = player2
        Else : txtDominating.Text = "Tie for the winner"
        End If

        If gagnant = player1 Then
            pbxwinner.Image = System.Drawing.Image.FromFile("X.jpg")
        ElseIf gagnant = player2 Then
            pbxwinner.Image = System.Drawing.Image.FromFile("O.jpg")
        ElseIf gagnant = "It's a draw!" Then
            pbxwinner.Image = System.Drawing.Image.FromFile("tie.jpg")
        End If
        statWrite()
        statRead()
        tic = True
    End Sub

    Private Sub btnrestart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnrestart.Click
        Me.Close()
    End Sub

    Private Sub btnquit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnquit.Click
        quit = True
        Me.Close()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If gagnant = player1 And tic = True Then
            SoundInst.PlaySoundFile("P1win.wav")
            tic = False
            Exit Sub
        ElseIf gagnant = player2 And tic = True Then
            SoundInst.PlaySoundFile("P2win.wav")
            tic = False
            Exit Sub
        ElseIf gagnant = "It's a draw!" And tic = True Then
            SoundInst.PlaySoundFile("tie.wav")
            tic = False
            Exit Sub
        End If
    End Sub
End Class


: