Solved

saving game

Posted on 2004-09-29
7
161 Views
Last Modified: 2012-05-05
i have almost completed Chess in VB 6.0.
now i wish to give the "Save Game" and "Load Saved Game" Option to user.
in Save game he can save his game with complete detail of live pieces, their position on the board, dead pieces and the order in which the same were captured by opponent, turn variable that decided whos turn is it now, timer etc.
how do i do that. any suggestions please ?
Thanks.
0
Comment
Question by:astudent
  • 3
7 Comments
 
LVL 50

Accepted Solution

by:
Ryan Chong earned 63 total points
ID: 12177922
try save it as a .ini file, then use APIs to read/write the values to it.

An example from web:

Private Declare Function GetPrivateProfileString Lib "kernel32" _
   Alias "GetPrivateProfileStringA" _
  (ByVal lpSectionName As String, _
   ByVal lpKeyName As Any, _
   ByVal lpDefault As String, _
   ByVal lpReturnedString As String, _
   ByVal nSize As Long, _
   ByVal lpFileName As String) As Long
   
Private Declare Function WritePrivateProfileString Lib "kernel32" _
   Alias "WritePrivateProfileStringA" _
  (ByVal lpSectionName As String, _
   ByVal lpKeyName As Any, _
   ByVal lpString As Any, _
   ByVal lpFileName As String) As Long


Public Sub ProfileSaveItem(inifile As String, lpSectionName As String, lpKeyName As String, lpValue As String)

'This function saves the passed value to the file,
'under the section and key names specified.
'If the ini file does not exist, it is created.
'If the section does not exist, it is created.
'If the key name does not exist, it is created.
'If the key name exists, its value is replaced.
   
   Call WritePrivateProfileString(lpSectionName, _
                                  lpKeyName, _
                                  lpValue, _
                                  inifile)

End Sub


Public Function ProfileGetItem(inifile As String, lpSectionName As String, lpKeyName As String, Optional defaultValue As String = "") As String

'Retrieves a value from an ini file corresponding
'to the section and key name passed.
       
   Dim success As Long
   Dim nSize As Long
   Dim ret As String
 
  'call the API with the parameters passed.
  'The return value is the length of the string
  'in ret, including the terminating null. If a
  'default value was passed, and the section or
  'key name are not in the file, that value is
  'returned. If no default value was passed (""),
  'then success will = 0 if not found.

  'Pad a string large enough to hold the data.
   ret = Space$(2048)
   nSize = Len(ret)
   success = GetPrivateProfileString(lpSectionName, _
                                     lpKeyName, _
                                     defaultValue, _
                                     ret, _
                                     nSize, _
                                     inifile)
   
   If success Then
      ProfileGetItem = Left$(ret, success)
   End If
   
End Function


Public Sub ProfileDeleteItem(lpSectionName As String, _
                             lpKeyName As String, _
                             inifile As String)

'this call will remove the keyname and its
'corresponding value from the section specified
'in lpSectionName. This is accomplished by passing
'vbNullString as the lpValue parameter. For example,
'assuming that an ini file had:
'  [Colours]
'  Colour1=Red
'  Colour2=Blue
'  Colour3=Green
'
'and this sub was called passing "Colour2"
'as lpKeyName, the resulting ini file
'would contain:
'  [Colours]
'  Colour1=Red
'  Colour3=Green
   
   Call WritePrivateProfileString(lpSectionName, _
                                  lpKeyName, _
                                  vbNullString, _
                                  inifile)

End Sub


Public Sub ProfileDeleteSection(lpSectionName As String, _
                                inifile As String)

'this call will remove the entire section
'corresponding to lpSectionName. This is
'accomplished by passing vbNullString
'as both the lpKeyName and lpValue parameters.
'For example, assuming that an ini file had:
'  [Colours]
'  Colour1=Red
'  Colour2=Blue
'  Colour3=Green
'
'and this sub was called passing "Colours"
'as lpSectionName, the resulting Colours
'section in the ini file would be deleted.
   
   Call WritePrivateProfileString(lpSectionName, _
                                  vbNullString, _
                                  vbNullString, _
                                  inifile)

End Sub

else you can do a self-defined format file, and read it in the way you want.

Hope this helps
0
 
LVL 11

Expert Comment

by:dbrckovi
ID: 12178081
I mean no disrespect to your sollution ryancys, but I wouldn't use Ini files becouse anyone can edit them in a notepad.

I'd use      Put #     and     Get #     functions to store and load complete variabes and variable arrays into a file.


Try this example:
 - create two command buttons
 - paste this:
'--------------------------------------------------------------
Private Sub Command1_Click()
    Dim TimerStatus As Integer                            'defines some variables (important becouse this way the Put knows how much file space is required for a variable)
    Dim TurnsTaken As Integer
    Dim WhitePieceAlive(1 To 8) As Boolean          'or the complete array
    Dim BlackPieceAlive(1 To 8) As Boolean
   
    TimerStatus = 234                                         'populate variables with values which you want to save
    TurnsTaken = 23

    Randomize Timer
    For X = 1 To 8                                               'for testing purposes just randomize piece conditions
        WhitePieceAlive(X) = Int(Rnd * 2)
        BlackPieceAlive(X) = Int(Rnd * 2)
    Next X

    Open App.Path & "\game1.sav" For Random As #1              'save all variables into a file
        Put #1, 1, TimerStatus                 'save TimerStatus under record no. 1
        Put #1, 2, TurnsTaken                 'save TurnsTakem under record no. 2
        Put #1, 3, WhitePieceAlive            'save the complete array under record no. 3
        Put #1, 4, BlackPieceAlive
    Close #1

    Print "Game saved!"
End Sub

Private Sub Command2_Click()
    Dim TimerStatus As Integer                                         'the variables which will recieve values have to be of the same type as variable from which the data was stored
    Dim TurnsTaken As Integer
    Dim WhitePieceAlive(1 To 8) As Boolean
    Dim BlackPieceAlive(1 To 8) As Boolean

    Open App.Path & "\game1.sav" For Random As #1
        Get #1, 1, TimerStatus                 'get value from record no.1 and put it in TimerStatus variable
        Get #1, 2, TurnsTaken
        Get #1, 3, WhitePieceAlive
        Get #1, 4, BlackPieceAlive              'get the complete array of values from record no. 4 and put it in BlackPieceAlive variable array
    Close #1

    Print "Game loaded!"                      'print result
    Print TimerStatus
    Print TurnsTaken
   
    For X = 1 To 8
        Print "White piece " & X & " alive: " & WhitePieceAlive(X)
    Next X

End Sub

Private Sub Form_Load()
    Command1.Caption = "Save Game"
    Command2.Caption = "LoadGame"
End Sub
'----------------------------------------------------------------------
0
 
LVL 11

Assisted Solution

by:dbrckovi
dbrckovi earned 62 total points
ID: 12178107
Or you can define your own data type, and not worry about record numbers at all.

'--------------------------------------------------------------------------------------
Private Type FileBuffer
    TimerStatus As Integer
    TurnsTaken As Integer
    WhitePieceAlive(1 To 8) As Boolean
    BlackPieceAlive(1 To 8) As Boolean
End Type

Private Sub Command1_Click()
    Dim SaveGameImage As FileBuffer
       
    SaveGameImage.TimerStatus = 111
    SaveGameImage.TurnsTaken = 23

    Randomize Timer
    For X = 1 To 8
        SaveGameImage.WhitePieceAlive(X) = Int(Rnd * 2)
        SaveGameImage.BlackPieceAlive(X) = Int(Rnd * 2)
    Next X

    Open App.Path & "\game1.sav" For Random As #1
        Put #1, 1, SaveGameImage
    Close #1

    Print "Game saved!"
End Sub

Private Sub Command2_Click()
    Dim LoadGameimage As FileBuffer

    Open App.Path & "\game1.sav" For Random As #1
        Get #1, 1, LoadGameimage
    Close #1

    Print "Game loaded!"
    Print LoadGameimage.TimerStatus
    Print LoadGameimage.TurnsTaken
   
    For X = 1 To 8
        Print "White piece " & X & " alive: " & LoadGameimage.WhitePieceAlive(X)
    Next X

End Sub

Private Sub Form_Load()
    Command1.Caption = "Save Game"
    Command2.Caption = "LoadGame"
End Sub
'-----------------------------------------------------------------------------------
0
 
LVL 11

Expert Comment

by:dbrckovi
ID: 12563228
I suggest a split between ryancys and me.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now