Solved

saving game

Posted on 2004-09-29
7
159 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 49

Accepted Solution

by:
Ryan Chong earned 63 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I suggest a split between ryancys and me.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

762 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