home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / VISUAL_B / CODIGO_1 / ACCRDI / CARDDECK.BAS < prev    next >
Encoding:
BASIC Source File  |  1991-09-26  |  5.2 KB  |  194 lines

  1. DefInt A-Z
  2.  
  3. Declare Function CardVersion Lib "VBCards.Dll" () As Integer
  4.  
  5. Declare Sub GetCard Lib "VBCards.dll" (ByVal Card As Integer)
  6. Declare Function SameSuit Lib "VBCards.Dll" (ByVal c1 As Integer, ByVal c2 As Integer) As Integer
  7. Declare Function SuitOf Lib "VBCards.DLL" (ByVal C As Integer) As Integer
  8. Declare Function CardValue Lib "vbCards.dll" (ByVal C As Integer)
  9. Declare Function SameCardValue Lib "VBCards.Dll" (ByVal c1 As Integer, ByVal c2 As Integer) As Integer
  10.  
  11. Declare Function GetProfileInt Lib "Kernel" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal nDefault As Integer) As Integer
  12. Declare Function GetProfileString Lib "Kernel" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer) As Integer
  13. Declare Function WriteProfileString Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String) As Integer
  14.  
  15. ' This routine deletes the card that has just been moved and
  16. ' moves the cards following it up one.  Then it deletes the
  17. ' last card.  The TABLE array is maintained
  18. Sub Compact (ByVal Source As Integer)
  19.   Piles = Piles - 1
  20.   For i = Source + 1 To Piles
  21.      Form1.Picture1(i - 1).Picture = Form1.Picture1(i).Picture
  22.      Table(i) = Table(i + 1)
  23.   Next i
  24.   Unload Form1.Picture1(Piles)
  25. End Sub
  26.  
  27. ' This returns the actual card position
  28. Function CurrentRow (Pile%) As Integer
  29.   CardHeight = Form1.Picture1(0).Height
  30.   If Compressed Then
  31.     CardHeight = Int(CardHeight / 2)
  32.   End If
  33.   NumRows = CurrentLevel(Pile%) - 1
  34.   
  35.   ' Now calcuate the correct row position
  36.   J = NumRows * CardHeight
  37.   J = J + (NumRows * CardSpace)
  38.   CurrentRow = J + Form1.Picture1(0).Top
  39. End Function
  40.  
  41. ' Calcuate the position of this the next card delt
  42. Function CurrentCol (Pile As Integer) As Integer
  43.   CardWidth = Form1.Picture1(0).Width
  44.   
  45.   ' First calculate the indention
  46.   Level = CurrentLevel(Pile) - 1
  47.   J = Form1.Picture1(0).Left + (Level * CardWidth)
  48.   J = J + (Level * CardSpace)
  49.  
  50.   ' Now calculate the exact card position
  51.   Level = Pile - (4 * Level) - 1
  52.   J = J + (Level * CardSpace) + (Level * CardWidth)
  53.   CurrentCol = J
  54. End Function
  55.  
  56. ' This returns the logical row of the Card% specified
  57. Function CurrentLevel (Pile%) As Integer
  58.   NumRows = Int(Pile% / 4)
  59.   If (NumRows * 4) < Pile% Then
  60.     NumRows = NumRows + 1
  61.   End If
  62.   CurrentLevel = NumRows
  63. End Function
  64.  
  65. Sub ShowError (Msg$)
  66.   If DisplayError Then
  67.     Beep
  68.     MsgBox Msg$, 0, "Sorry!"
  69.   End If
  70. End Sub
  71.  
  72. Sub ShuffleCards ()
  73.    
  74.    For i = 1 To 52
  75.       cards(i) = i
  76.    Next i
  77.  
  78.    For J = 1 To 10
  79.      For i = 1 To 52
  80.         K = Int(52 * Rnd + 1)
  81.         Temp = cards(i)
  82.         cards(i) = cards(K)
  83.         cards(K) = Temp
  84.      Next i
  85.    Next J
  86.  
  87. End Sub
  88.  
  89. Function MorePlays () As Integer
  90.   ' Only check for more plays if we have no cards left to
  91.   ' deal.
  92.   MorePlays = -1
  93.   If NextCard = 53 Then
  94.     If Piles > 1 Then 'we haven't won yet
  95.       MorePlays = 0
  96.       For i = 2 To Piles
  97.         If ValidMove(i - 1, i - 2) Then 'still more moves
  98.           MorePlays = -1
  99.         Else
  100.            If i > 3 Then
  101.              If ValidMove(i - 1, i - 4) Then 'still more moves
  102.                MorePlays = -1
  103.              End If
  104.            End If
  105.         End If
  106.       Next
  107.     End If
  108.   End If
  109. End Function
  110.  
  111. Sub Main ()
  112.    Randomize
  113.      
  114.    ' Get the various WIN.INI options
  115.    DisplayError = GetProfileInt(AppName$, "Errors", 0) - 1
  116.    GamesWon = GetProfileInt(AppName$, "Won", 0)
  117.    GamesLost = GetProfileInt(AppName$, "Lost", 0)
  118.    Compressed = GetProfileInt(AppName$, "Compressed", 1) - 1
  119.    
  120.    Form1.Show
  121.    Form1.WindowState = GetProfileInt(AppName$, "WinState", 0)
  122.    
  123.    Do While -1
  124.      Do While DoEvents() And MorePlays()
  125.      Loop
  126.    
  127.      If NextCard = 53 Then
  128.         If Piles = 1 Then
  129.            WonForm.Show 1
  130.            GamesWon = GamesWon + 1
  131.         Else
  132.            Lost.Show 1
  133.            GamesLost = GamesLost + 1
  134.         End If
  135.       
  136.         ' Start New Game!
  137.         NewGame
  138.  
  139.      Else
  140.         Exit Do
  141.      End If
  142.  
  143.    Loop
  144.  
  145.    UpdateIni
  146.    End
  147. End Sub
  148.  
  149. Sub UpdateIni ()
  150.    ' Write our WIN.INI values
  151.    X = WriteProfileString(AppName$, "Errors", Str$(DisplayError + 1))
  152.    X = WriteProfileString(AppName$, "WinState", Str$(Form1.WindowState))
  153.    X = WriteProfileString(AppName$, "Won", Str$(GamesWon))
  154.    X = WriteProfileString(AppName$, "Lost", Str$(GamesLost))
  155.    X = WriteProfileString(AppName$, "Compressed", Str$(Compressed + 1))
  156. End Sub
  157.  
  158. Sub NewGame ()
  159.    Undone = -1
  160.    Form1.Command1.Enabled = -1
  161.  
  162.    'Clear out old cards first
  163.    For i = 1 To Piles - 1
  164.       Unload Form1.Picture1(i)
  165.    Next
  166.  
  167.    Piles = 1
  168.  
  169.    ShuffleCards
  170.    
  171.    GetCard (cards(1))
  172.    Table(1) = cards(1)
  173.    Form1.Picture1(0).Picture = ClipBoard.GetData(2)
  174.    
  175.    NextCard = 2
  176.  
  177. End Sub
  178.  
  179. Function ValidMove (ByVal c1, ByVal c2 As Integer) As Integer
  180.     c1 = Table(c1 + 1)
  181.     c2 = Table(c2 + 1)
  182.     ValidMove = SameSuit(c1, c2) Or SameCardValue(c1, c2)
  183. End Function
  184.  
  185. Sub UndoSave ()
  186.    Undone = 0
  187.    UndoPiles = Piles
  188.    UndoNextCard = NextCard
  189.    For i = 1 To Piles
  190.       Undoer(i) = Table(i)
  191.    Next
  192. End Sub
  193.  
  194.