public fMoveBoard
public lXBoard
public lYBoard

public deck[13]
public fCCardsHidden
public lCardBack
public lStage
public selectedCards[6]
public fDoneSendToCrib
public whosCrib
public lPPegCard
public lCPegCard
public PPegCard[4]
public CPegCard[4]
public currCount
public lastCardsPlayed[9]
public PCard[6]
public CCard[6]
public whoPlayedLast
public stStatus
public lGameID
public lPHandPoints
public lCHandPoints
public lPPegPoints
public lCPegPoints
public lPPrevPoints
public lCPrevPoints
public tempHand[5]
public btnDeal2
public btnOK
public btnComputersHand
public btnPlayersHand
public btnCribHand
public btnSendToCrib
public CardBlank

public Deal()
public Shuffle()
public UpdateCards()
public CardClick()
public NumSelected()
public CompSendToCrib()
public Arrange()
public CalcHand()
public AddToCount()
public AddArraySlot()
public CalcPegPoints()
public ArrLength()
public DoPegging()
public PlayerCantPlay()
public ComputerCantPlay()
public ShowPeggingCards()
public PlayPegCard()
public Clone()
public ShowOKButton()
public AddToStatus()
public ComputersHandButtonOpen()
public PlayersHandButtonOpen()
public CribButtonOpen()
public NewGame()
public HideAllCards()
public DoFinalUpdate()
public btnOK_Click()
public btnPlayersHand_Click()
public btnComputersHand_Click()
public btnCribHand_Click()
public btnSendToCrib_Click()


main
    local i
    lStage = 0
	CardBlank = 0
'    DoCmd.RunCommand acCmdDocMaximize
    fMoveBoard = False
'    Randomize
    whosCrib = False
    fCCardsHidden = True
    stStatus = ""

    lCardBack = -11
	lblCompCrib.FontName = "Courier New"
	lblCompCrib.FontSize = 8
	lblCompCrib.FontBold = True
	lblCompCrib.FontUnderline = True
	lblCompCrib.TextAlign = 2
	lblCompCrib.Caption = "Comp Crib"
	lblCompCrib.BackColor = 32768
	lblCompCrib.ForeColor = 65535

	lblPlayerCrib.FontName = "Courier New"
	lblPlayerCrib.FontSize = 8
	lblPlayerCrib.FontBold = True
	lblPlayerCrib.FontUnderline = True
	lblPlayerCrib.TextAlign = 2
	lblPlayerCrib.Caption = "Crib"
	lblPlayerCrib.BackColor = 32768
	lblPlayerCrib.ForeColor = 65535

	cll.FontName = "Courier New"
	cll.FontSize = 18
	cll.FontBold = True
	cll.FontUnderline = True
	cll.TextAlign = 2
	cll.Caption = "Count"
	cll.BackColor = 32768
	cll.ForeColor = 65535

	CountLabel.FontName = "Courier New"
	CountLabel.FontSize = 18
	CountLabel.FontBold = True
	CountLabel.FontUnderline = False
	CountLabel.TextAlign = 2
	CountLabel.BackColor = 32768
	CountLabel.ForeColor = 65535
	CountLabel.Caption = "31"

    lGameID = 0

    PPegCard[1] = PPegCard1
    PPegCard[2] = PPegCard2
    PPegCard[3] = PPegCard3
    PPegCard[4] = PPegCard4
    CPegCard[1] = CPegCard1
    CPegCard[2] = CPegCard2
    CPegCard[3] = CPegCard3
    CPegCard[4] = CPegCard4
    PCard[1] = PCard1
    PCard[2] = PCard2
    PCard[3] = PCard3
    PCard[4] = PCard4
    PCard[5] = PCard5
    PCard[6] = PCard6
    CCard[1] = CCard1
    CCard[2] = CCard2
    CCard[3] = CCard3
    CCard[4] = CCard4
    CCard[5] = CCard5
    CCard[6] = CCard6

    Deal()
End main


Function btnComputersHand_Click()
    local i
    local myCount

'    cb.SetFocus
    For i = 8 To 11
        tempHand[i - 8 + 1] = deck[i]
    End For
    tempHand[5] = deck[13]
    myCount = CalcHand()

    lCPrevPoints = 0
    DoPegging(2, myCount, " for his hand")
    If cb.FrontBluePeg <> 121 And cb.FrontRedPeg <> 121
        If lStage <> 9
            PlayersHandButtonOpen()
        Else
            CribButtonOpen()
        End If
    End If
    lStage = 9


End Function

Function btnCribHand_Click()
    local myCount
    local i

    lStage = 10
    tempHand[1] = deck[1]
    tempHand[2] = deck[6]
    tempHand[3] = deck[7]
    tempHand[4] = deck[12]
    tempHand[5] = deck[13]

    compCrib1.Suite = 0
    compCrib2.Suite = 0
    compCrib3.Suite = 0
    compCrib4.Suite = 0
    playerCrib1.Suite = 0
    playerCrib2.Suite = 0
    playerCrib3.Suite = 0
    playerCrib4.Suite = 0

    myCount = CalcHand()
    If whosCrib
        DoPegging(1, myCount, " for the crib")
        'hands here
        For i = 1 To 4
            PPegCard[i].Suite = int(tempHand[i] / 13) + 1
            PPegCard[i].Number = Mod(tempHand[i], 13) + 1
            CPegCard[i].Suite = 0
        End For
    Else
        DoPegging(2, myCount, " for the crib")
        'hands here
        For i = 1 To 4
            CPegCard[i].Suite = int(tempHand[i] / 13) + 1
            CPegCard[i].Number = Mod(tempHand[i] ,13) + 1
            PPegCard[i].Suite = 0
        End For
    End If
'    cb.SetFocus
    btnCribHand = False
    If cb.FrontBluePeg < 121 And cb.FrontRedPeg < 121
        btnDeal2 = True
	btnDeal.Caption = "Deal"
	btnDeal.Visible = True
    End If




End Function

Function btnDeal_Click()
    if btnDeal2
	    btnDeal2 = False
	    btnDeal.Caption = ""
	    btnDeal.Visible = False
	    Shuffle()
   elseif btnOK
   	btnDeal.Caption = ""
   	btnDeal.Visible = False
	btnOK_Click()
   elseif btnPlayersHand
   	btnDeal.Caption = ""
   	btnDeal.Visible = False
	btnPlayersHand_Click()
   elseif btnComputersHand
   	btnDeal.Caption = ""
   	btnDeal.Visible = False
	btnComputersHand_Click()
   elseif btnCribHand
   	btnDeal.Caption = ""
   	btnDeal.Visible = False
	btnCribHand_Click()
   elseif btnSendToCrib
   	btnDeal.Caption = ""
   	btnDeal.Visible = False
	btnSendToCrib_Click()
   end if
End Function

Function btnOK_Click()
'    cb.SetFocus
    btnOK = False
    PlayPegCard()
End Function

Function btnPlayersHand_Click()
    local i
    local myCount

'    cb.SetFocus
    For i = 2 To 5
        tempHand[i - 1] = deck[i]
    End For
    tempHand[5] = deck[13]
    myCount = CalcHand()

    lPPrevPoints = 0
    DoPegging(1, myCount, " for your hand")
    'add hands here
    If cb.FrontBluePeg <> 121 And cb.FrontRedPeg <> 121
        If lStage <> 9
            ComputersHandButtonOpen()
        Else
            CribButtonOpen()
        End If
    End If
    lStage = 9


End Function

Function btnSendToCrib_Click()
    local i
    local temp

    lStage = 2
    For i = 2 To 5
        If selectedCards[i]
            If Not(selectedCards[1])
                temp = deck[1]
                deck[1] = deck[i]
                deck[i] = temp
                selectedCards[i] = False
                selectedCards[1] = True
            Else
                temp = deck[6]
                deck[6] = deck[i]
                deck[i] = temp
                selectedCards[i] = False
                selectedCards[6] = True
            End If
        End If
    End For
    PCard1.Invert = False
    PCard2.Invert = False
    PCard3.Invert = False
    PCard4.Invert = False
    PCard5.Invert = False
    PCard6.Invert = False
    Arrange(2, 5)

    PCard2.Suite = int(deck[2] / 13) + 1
    PCard3.Suite = int(deck[3] / 13) + 1
    PCard4.Suite = int(deck[4] / 13) + 1
    PCard5.Suite = int(deck[5] / 13) + 1

    UpdateCards()
'    cb.SetFocus
    btnSendToCrib = False
    btnDeal.Caption = ""
    btnDeal.Visible = False

    If fDoneSendToCrib
        ShowPeggingCards()
    End If

















End Function

Function Deal()
    NewGame()
End Function

Function Shuffle()
    local i
    local j
    local iTempValue
    local fDone

    lPHandPoints = 0
    lCHandPoints = 0
    lPPegPoints = 0
    lCPegPoints = 0

    For i = 1 To 13
    	fDone = False
        while not(fDone)
            fDone = True
            iTempValue = Int(uniform(52))
            For j = 1 To i - 1
                If deck[j] = iTempValue
                	fDone = False
                end if
            End For
        end while
        deck[i] = iTempValue
    End For

    For i = 1 To 6
        selectedCards[i] = False
    End For

    Arrange(1, 6)

    AddToStatus("Shuffling...")
    CountLabel.Caption = ""
    cll.Caption = ""
    currCount = 0
    CountLabel.Caption = str(currCount)
    lastCardsPlayed[1] = -1
    lStage = 1
    lPPegCard = 1
    lCPegCard = 1
    fDoneSendToCrib = False
    whosCrib = Not(whosCrib)
    If whosCrib
        lblPlayerCrib.Caption = "Crib"
        lblCompCrib.Caption = ""
    Else
        lblPlayerCrib.Caption = ""
        lblCompCrib.Caption = "Comp Crib"
    End If
    PCard1.Invert = False
    PCard2.Invert = False
    PCard3.Invert = False
    PCard4.Invert = False
    PCard5.Invert = False
    PCard6.Invert = False
    For i = 1 To 4
        PPegCard[i].Suite = CardBlank
        CPegCard[i].Suite = CardBlank
    End For

    PCard1.Suite = int(deck[1] / 13) + 1
    PCard2.Suite = int(deck[2] / 13) + 1
    PCard3.Suite = int(deck[3] / 13) + 1
    PCard4.Suite = int(deck[4] / 13) + 1
    PCard5.Suite = int(deck[5] / 13) + 1
    PCard6.Suite = int(deck[6] / 13) + 1

    If fCCardsHidden
        CCard1.Suite = lCardBack
        CCard2.Suite = lCardBack
        CCard3.Suite = lCardBack
        CCard4.Suite = lCardBack
        CCard5.Suite = lCardBack
        CCard6.Suite = lCardBack
    Else
        CCard1.Suite = int(deck[7] / 13) + 1
        CCard2.Suite = int(deck[8] / 13) + 1
        CCard3.Suite = int(deck[9] / 13) + 1
        CCard4.Suite = int(deck[10] / 13) + 1
        CCard5.Suite = int(deck[11] / 13) + 1
        CCard6.Suite = int(deck[12] / 13) + 1
    End If

    playerCrib1.Suite = 0
    playerCrib2.Suite = 0
    playerCrib3.Suite = 0
    playerCrib4.Suite = 0
    compCrib1.Suite = 0
    compCrib2.Suite = 0
    compCrib3.Suite = 0
    compCrib4.Suite = 0
    lPPrevPoints = cb.FrontBluePeg
    lCPrevPoints = cb.FrontRedPeg

    UpdateCards()
    CompSendToCrib()
End Function

Function UpdateCards()
    If lStage > 0

        PCard1.Number = Mod(deck[1] ,13) + 1
        PCard2.Number = Mod(deck[2] ,13) + 1
        PCard3.Number = Mod(deck[3] ,13) + 1
        PCard4.Number = Mod(deck[4] ,13) + 1
        PCard5.Number = Mod(deck[5] ,13) + 1
        PCard6.Number = Mod(deck[6] ,13) + 1

        CCard1.Number = Mod(deck[7] ,13) + 1
        CCard2.Number = Mod(deck[8] ,13) + 1
        CCard3.Number = Mod(deck[9] ,13) + 1
        CCard4.Number = Mod(deck[10] ,13) + 1
        CCard5.Number = Mod(deck[11] ,13) + 1
        CCard6.Number = Mod(deck[12] ,13) + 1

        If lStage > 1
            PCard1.Suite = 0
            PCard6.Suite = 0
            If whosCrib
                playerCrib1.Suite = lCardBack
                playerCrib2.Suite = lCardBack
            Else
                compCrib1.Suite = lCardBack
                compCrib2.Suite = lCardBack
            End If
        End If
        If fDoneSendToCrib
            CCard1.Suite = 0
            CCard6.Suite = 0
            If whosCrib
                playerCrib3.Suite = lCardBack
                playerCrib4.Suite = lCardBack
            Else
                compCrib3.Suite = lCardBack
                compCrib4.Suite = lCardBack
            End If
        End If
        If fDoneSendToCrib And lStage > 1
            UpCard.Number = Mod(deck[13] ,13) + 1
            UpCard.Suite = int(deck[13] / 13) + 1
        Else
            UpCard.Suite = lCardBack
        End If
    End If
End Function


Function PCard1_Click()
    CardClick(1, PCard1)
End Function
Function PCard2_Click()
    CardClick(2, PCard2)
End Function
Function PCard3_Click()
    CardClick(3, PCard3)
End Function
Function PCard4_Click()
    CardClick(4, PCard4)
End Function
Function PCard5_Click()
    CardClick(5, PCard5)
End Function
Function PCard6_Click()
    CardClick(6, PCard6)
End Function

Function CardClick(lCard, oCard)
    local tempCard

    If lStage = 1
        If NumSelected() < 2 Or selectedCards[lCard]
            selectedCards[lCard] = Not(selectedCards[lCard])
            oCard.Invert = selectedCards[lCard]
            If NumSelected() = 2
                btnSendToCrib = True
                btnDeal.Caption = "Send To Crib"
	   	  btnDeal.Visible = True
            Else
                btnSendToCrib = False
                btnDeal.Caption = ""
   		  btnDeal.Visible = False
            End If
        End If
    End If
    If lStage = 2 And oCard.Suite <> CardBlank And Not(btnOK)
        tempCard = oCard.Number
        If tempCard >= 11
        	tempCard = 10
        end if
        If currCount + tempCard <= 31
            PPegCard[lPPegCard].Suite = oCard.Suite
            PPegCard[lPPegCard].Number = oCard.Number
            oCard.Suite = CardBlank
            AddToCount(PPegCard[lPPegCard].Number)
            lPPegCard = lPPegCard + 1
            PlayPegCard()
        End If
    End If

End Function

Function NumSelected()
    local Count
    local i

    Count = 0
    For i = 1 To 6
        If selectedCards[i]
        	Count = Count + 1
        end if
    End For
    return Count
End Function

Function CompSendToCrib()
    local tempEV
    local tempI
    local tempJ
    local cribOffset
    local maxEV
    local k
    local cardJ
    local cardI
    local i
    local Count
    local c1
    local c2
    local swapCard

    cardJ = 8
    cardI = 7
    maxEV = 0
    While Not(fDoneSendToCrib)
        Count = 1
        For i = 7 To 12
            If i <> cardI And i <> cardJ
                tempHand[Count] = deck[i]
                Count = Count + 1
            End If
        End For


        cribOffset= 0
        tempI = Mod(deck[cardI] ,13) + 1
        tempJ = Mod(deck[cardJ] ,13) + 1
        If tempI = tempJ
        	cribOffset= cribOffset+ 3
        end if
        If tempI = tempJ + 1 Or tempJ = tempI + 1 Or tempI = tempJ + 2 Or tempJ = tempI + 2
        	cribOffset= cribOffset+ 1
        end if
        If tempI >= 11
        	tempI = 10
        end if
        If tempJ >= 11
        	tempJ = 10
        end if
        If tempI = 5 Or tempJ = 5
        	cribOffset= cribOffset+ 3
        end if
        If tempI + tempJ = 15
        	cribOffset= cribOffset+ 4
        end if
        tempEV = 0
        For k = 1 To 52
            If tempHand[1] <> k And tempHand[2] <> k And tempHand[3] <> k And tempHand[4] <> k
                tempHand[5] = k
                If whosCrib
                    tempEV = tempEV + CalcHand() - cribOffset
                Else
                    tempEV = tempEV + CalcHand() + cribOffset
                End If
            End If
        End For
        If tempEV > maxEV
            c1 = cardI
            c2 = cardJ
            maxEV = tempEV
        End If

        cardJ = cardJ + 1
        If cardJ = 13

            cardI = cardI + 1
            cardJ = cardI + 1
            If cardI = 12

                fDoneSendToCrib = True
                If c2 <> 7
                    swapCard = deck[c2]
                    deck[c2] = deck[7]
                    deck[7] = swapCard
                End If
                If c1 <> 12
                    swapCard = deck[c1]
                    deck[c1] = deck[12]
                    deck[12] = swapCard
                End If
'                Arrange 8, 11
                If lStage <> 1
                    ShowPeggingCards()
                End If
                UpdateCards()
            End If
        End If
    end while
End Function

Function Arrange(lStart, lFinish)
    local i
    local j
    local temp

    For i = lStart To lFinish
        For j = lStart To lFinish - 1
            If Mod(deck[j] ,13) > Mod(deck[j + 1] ,13)
                temp = deck[j]
                deck[j] = deck[j + 1]
                deck[j + 1] = temp
            End If
        End For
    End For
End Function

Function CalcHand()
    local total
    local tempTotal
    local multiplier
    local justHere
    local n
    local r
    local q
    local p
    local i
    local j
    local suitsArray[5]
    local valArray[5]
    local temp

    total = 0


        For i = 1 To 5
            suitsArray[i] = int(tempHand[i] / 13)
            valArray[i] = Mod(tempHand[i] ,13)
        End For
        For i = 1 To 5
            For j = 1 To 4
                If valArray[j] > valArray[j + 1]
                    temp = valArray[j]
                    valArray[j] = valArray[j + 1]
                    valArray[j + 1] = temp
                End If
            End For
        End For
       'nobs
        For i = 1 To 4
            If Mod(tempHand[i] ,13 = 10)
                If suitsArray[i] = suitsArray[5]
                	total = total + 1
                end if
            End If
        End For

       'flush
        If lStage = 10

            If (suitsArray[1] = suitsArray[1]) And (suitsArray[1] = suitsArray[3]) And \
                (suitsArray[1] = suitsArray[4]) And (suitsArray[1] = suitsArray[5])
                    total = total + 5
            End If
        Else

            If (suitsArray[1] = suitsArray[2]) And (suitsArray[1] = suitsArray[3]) And \
                (suitsArray[1] = suitsArray[4])

                total = total + 4
                If suitsArray[1] = suitsArray[5]
                    total = total + 1
                End If
            End If
        End If

    'runs
    For n = 1 To 3
            tempTotal = 1
            multiplier = 1
            justHere = False
            For r = n + 1 To 5
                If valArray[r] = valArray[r - 1] + 1
                        tempTotal = tempTotal + 1
                        justHere = False
                ElseIf valArray[r] = valArray[r - 1]
                        If justHere
                            multiplier = multiplier + 1
                        Else
                            multiplier = multiplier * 2
                        End If
                        justHere = True
                Else
                    Exit For
                End If
            End For
            If tempTotal >= 3
                total = total + multiplier * tempTotal
                Exit For
            End If

    End For
    'pairs
    For n = 1 To 4
        For r = n + 1 To 5
            If valArray[n] = valArray[r]
                total = total + 2
            End If
        End For
    End For
    '// 15's
    '//make cards correspond to numerical value
        For n = 1 To 5
            If valArray[n] >= 9
                valArray[n] = 10
            Else
                valArray[n] = valArray[n] + 1
            End If
        End For
    '//two cards
        For n = 1 To 4
            For r = n + 1 To 5
                If valArray[n] + valArray[r] = 15
                    total = total + 2
                End If
            End For
        End For
    '//three cards
        For n = 1 To 3
            For r = n + 1 To 4
                For p = r + 1 To 5
                    If valArray[n] + valArray[r] + valArray[p] = 15
                        total = total + 2
                    End If
                End For
            End For
        End For
    '//four cards
        For n = 1 To 2
            For r = n + 1 To 3
                For p = r + 1 To 4
                    For q = p + 1 To 5
                        If valArray[n] + valArray[r] + valArray[p] + valArray[q] = 15
                            total = total + 2
                        End If
                    End For
                End For
            End For
        End For
    '//five cards
        If valArray[5] + valArray[1] + valArray[2] + valArray[3] + valArray[4] = 15
            total = total + 2
        End If

    return total
End Function

Function AddToCount(newValue)
    If newValue >= 10
        currCount = currCount + 10
    Else
        currCount = currCount + newValue
    End If
    whoPlayedLast = 1
    AddArraySlot(arrayptr(lastCardsPlayed[1]), newValue)
    DoPegging(1, CalcPegPoints(arrayptr(lastCardsPlayed[1])), "")
    CountLabel.Caption =  str(currCount)
    If currCount = 31
        currCount = 0
        lastCardsPlayed[1] = -1
    End If
End Function
Function AddToCountComp(newValue)

        If newValue >= 10
            currCount = currCount + 10
        Else
            currCount = currCount + newValue
        End If
        AddArraySlot(arrayptr(lastCardsPlayed[1]), newValue)
        whoPlayedLast = 2
        CountLabel.Caption = str(currCount)

'        computerBalloonOpen()
'        SetValue(computerBalloon.computerText, 'text, NumberStr(currCount))
        DoPegging(2, CalcPegPoints(arrayptr(lastCardsPlayed[1])), "")

End Function
Function AddArraySlot(rArray, lVal)
    local i

    For i = 1 To 8
        If readptr(rArray, i) = -1
            writeptr(rArray, i + 1, -1)
            writeptr(rArray, i, lVal)
            Exit For
        End If
    End For
End Function

Function CalcPegPoints(pegArray)
    local total
    local lastElement
    local i
    local tempArray[9]
    local j
    local k
    local m
    local n
    local temp
    local tempTotal
    local notARun

    total = 0

    lastElement = ArrLength(pegArray)

    '//pairs, trips, fours
    For i = lastElement - 1 To 1 Step -1
        If readptr(pegArray,i) <> readptr(pegArray, lastElement)
            Exit For
        Else
            total = total + 2 * (lastElement - i)
        End If
    End For

    '//runs
    For i = lastElement To 3 Step -1

        tempArray[1] = -1

        For j = lastElement To (lastElement - i) + 1 Step -1
            AddArraySlot(arrayptr(tempArray[1]), readptr(pegArray, j))
        End For

        For m = 1 To ArrLength(arrayptr(tempArray[1]))
            For n = 1 To ArrLength(arrayptr(tempArray[1])) - 1
                If tempArray[n] > tempArray[n + 1]
                    temp = tempArray[n]
                    tempArray[n] = tempArray[n + 1]
                    tempArray[n + 1] = temp
                End If
            End For
        End For

        notARun = False
        For k = 2 To ArrLength(arrayptr(tempArray[1]))
            If tempArray[k] <> tempArray[k - 1] + 1
                notARun = True
            End If
        End For
        If Not(notARun)

            total = total + i
            Exit For
        End If
    End For


    '//15 or 31
    tempTotal = 0
    For i = 1 To ArrLength(pegArray)
        If readptr(pegArray, i) >= 10
            tempTotal = tempTotal + 10
        Else
            tempTotal = tempTotal + readptr(pegArray, i)
        End If
    End For
    If tempTotal = 15 Or tempTotal = 31
        total = total + 2
    End If





    return total
End Function

Function ArrLength(rArray)
    local i
    local alen

    For i = 1 To 9
        If readptr(rArray, i) = -1
            alen = i - 1
            Exit For
        End If
    End For

    return alen
End Function

Function DoPegging(whichPlayer, howMuch, stForWhat)
    If (lStage = 10 Or lStage = 15 Or lStage = 9) And howMuch = 0
        If whichPlayer = 1
            AddToStatus("You peg 0" | stForWhat | ".")
        Else
            AddToStatus("Computer pegs 0" | stForWhat | ".")
        End If
    End If
    If howMuch <> 0
        If whichPlayer = 1
            cb.DoBluePegging(howMuch)
            AddToStatus("You peg " | str(howMuch) | stForWhat | ".")
            If lStage < 9
                If howMuch > 0
                    ShowOKButton()
                End If
            End If
        Else
            If PlayerCantPlay()
                If lStage < 9
                    If ComputerCantPlay()
                        If howMuch <> 1 And currCount < 31
                            howMuch = howMuch + 1
                        End If
                    End If
                End If
            End If
            cb.DoRedPegging(howMuch)
            AddToStatus("Computer pegs " | str(howMuch) | stForWhat | ".")
            If PlayerCantPlay()
                If lStage < 9
                    If ComputerCantPlay()
                        currCount = 0
                        lastCardsPlayed[1] = -1
                    Else
                        If howMuch > 0
                            ShowOKButton()
                        End If
                    End If
                End If
            End If
        End If
    End If
End Function

Function PlayerCantPlay()
    local i
    local fCanPlay
    local tempCard
    local cantplay

     fCanPlay = False
     For i = 2 To 5
         If PCard[i].Suite <> CardBlank
             tempCard = Mod(deck[i] ,13) + 1
             If tempCard >= 11
                 tempCard = 10
             End If
             If currCount + tempCard <= 31
                 fCanPlay = True
                 Exit For
             End If
         End If
     End For
     If fCanPlay
         cantplay = False
     Else
         cantplay = True
     End If

	return cantplay
End Function

Function ComputerCantPlay()
    local i
    local fCanPlay
    local tempCard
    local cantplay

     fCanPlay = False
     For i = 2 To 5
         If CCard[i].Suite <> CardBlank
             tempCard = Mod(deck[i + 6] ,13) + 1
             If tempCard >= 11
                 tempCard = 10
             End If
             If currCount + tempCard <= 31
                 fCanPlay = True
                 Exit For
             End If
         End If
     End For
     If fCanPlay
         cantplay = False
     Else
         cantplay = True
     End If

	return cantplay
End Function

Function ShowPeggingCards()
    If Mod(deck[13] ,13) = 10
        If whosCrib
            DoPegging(1, 2, " for His Heels")
        Else
            DoPegging(2, 2, " for His Heels")
        End If
    End If
    CountLabel.Caption = str(currCount)
    cll.Caption = "Count"
    If whosCrib
    	PlayPegCard()
    end if
End Function

Function PlayPegCard()
    local lastCardPlayed
    local whichCard
    local i
    local cardVal
    local cardValNoTen
    local calcCardVal
    local tempCardsPlayed1[9]
    local tempCardsPlayed2[9]
    local tempTotal1
    local tempTotal2
    local l
    local cardsPlayed1[9]
    local cardsPlayed2[9]
    local tempCard

    If Not(btnOK)
        lastCardPlayed = -1
        If ArrLength(arrayptr(lastCardsPlayed[1])) > 0
            lastCardPlayed = lastCardsPlayed[ArrLength(arrayptr(lastCardsPlayed[1]))]
        End If
        whichCard = -1


        For i = 2 To 5
            If (CCard[i].Suite <> CardBlank)
                tempCard = Mod(deck[i + 6] ,13) + 1
                If tempCard >= 11
                    tempCard = 10
                End If
                If currCount + tempCard <= 31
                    whichCard = i
                    Exit For
                End If
            End If
        End For

         For i = 2 To 5
            If (CCard[i].Suite <> CardBlank)

                 cardValNoTen = Mod(deck[i + 6] ,13) + 1
                 cardVal = cardValNoTen
                 If cardVal >= 11
                     cardVal = 10
                 End If
                 If (currCount + cardVal <= 31)

                      Clone(arrayptr(lastCardsPlayed[1]), arrayptr(tempCardsPlayed1[1]))
                      Clone(arrayptr(lastCardsPlayed[1]), arrayptr(tempCardsPlayed2[1]))
                     AddArraySlot(arrayptr(tempCardsPlayed1[1]), Mod(deck[whichCard + 6] ,13) + 1)
                     AddArraySlot(arrayptr(tempCardsPlayed2[1]), cardValNoTen)
                     If CalcPegPoints(arrayptr(tempCardsPlayed2[1])) > CalcPegPoints(arrayptr(tempCardsPlayed1[1]))
                         whichCard = i
                     End If
                     '//new
                    If whichCard <> i And CalcPegPoints(arrayptr(tempCardsPlayed1[1])) = 0

                         cardVal = Mod(deck[whichCard + 6] ,13) + 1
                         If cardVal >= 10
                             cardVal = 10
                         End If
                         tempTotal1 = 0
                         tempTotal2 = 0
                         For l = 0 To 12

                             If l >= 10
                                 calcCardVal = 10
                             Else
                                 calcCardVal = l + 1
                             End If
                             If currCount + cardVal + calcCardVal <= 31

                                 Clone(arrayptr(lastCardsPlayed[1]), arrayptr(cardsPlayed1[1]))
                                 Clone(arrayptr(lastCardsPlayed[1]), arrayptr(cardsPlayed2[1]))
                                 AddArraySlot(arrayptr(cardsPlayed1[1]), Mod(deck[whichCard + 6] ,13) + 1)
                                 AddArraySlot(arrayptr(cardsPlayed2[1]), cardValNoTen)
                                 AddArraySlot(arrayptr(cardsPlayed1[1]), l)
                                 AddArraySlot(arrayptr(cardsPlayed2[1]), l)
                                 tempTotal1 = tempTotal1 + CalcPegPoints(arrayptr(cardsPlayed1[1]))
                                 tempTotal2 = tempTotal2 + CalcPegPoints(arrayptr(cardsPlayed2[1]))
                             End If
                         End For
                         If tempTotal2 < tempTotal1
                             whichCard = i
                         End If
                    End If
                 End If
                End If
             End For

            If whichCard = -1

                If PlayerCantPlay()

                    If currCount < 31 And currCount <> 0
                        DoPegging(whoPlayedLast, 1, " for a go")
                    End If
                    currCount = 0
                    lastCardsPlayed[1] = -1
                    If PlayerCantPlay() And ComputerCantPlay()
                        If cb.FrontBluePeg <> 121 And cb.FrontRedPeg <> 121
                            lStage = 15
                            If whosCrib
                                ComputersHandButtonOpen()
                            Else
                                PlayersHandButtonOpen()
                            End If
                        End If
                    ElseIf (whoPlayedLast = 1) Or (PlayerCantPlay())
    '                    If p11 <> 122 And p12 <> 122 And p21 <> 122 And p22 <> 122
                            ShowOKButton()
           '           SetValue(computerText,'text,"")
           '           SetValue(playerText,'text,"")
           '           UpdateCards()
                    End If
                Else
                    AddToStatus("Computer says: 'Go'")
                End If
            Else




                CPegCard[lCPegCard].Suite = int(deck[whichCard + 6] / 13) + 1
                CPegCard[lCPegCard].Number = CCard[whichCard].Number
                CCard[whichCard].Suite = CardBlank
                lCPegCard = lCPegCard + 1
                AddToCountComp(CCard[whichCard].Number)

                If PlayerCantPlay()
                    PlayPegCard()
                End If

            End If
    End If


End Function

Function Clone(rPegArray, tempArray)
    local i

    For i = 1 To 9
        writeptr(tempArray, i, readptr(rPegArray, i))
    End For
End Function

Function ShowOKButton()
    btnOK = True
    btnDeal.Caption = "Ok"
    btnDeal.Visible = True
End Function

Function AddToStatus(stNewString)
'    If stStatus = ""
'        stStatus = stNewString
'    Else
'        stStatus = stStatus + ";" + stNewString
'    End If
'    lstStatus.RowSource = stStatus
'    lstStatus.Value = lstStatus.ListCount - 1
'    lstStatus.SetFocus
'    lstStatus.Selected(lstStatus.ListCount - 1) = True
'    lstStatus.Selected(lstStatus.ListCount - 1) = False
'    Form.SetFocus
'	lstStatus.AddItem(stNewString, 0)
        lstStatus.AddItem(stNewString, lstStatus.ListCount)
        lstStatus.ListIndex = lstStatus.ListCount - 1
        lstStatus.ListIndex = -1
End Function

Function ComputersHandButtonOpen()
    btnOK = False
    btnPlayersHand = False

    btnComputersHand = True
    btnDeal.Caption = "Score Computer's Hand"
    btnDeal.Visible = True
End Function

Function PlayersHandButtonOpen()
    btnOK = False
    btnComputersHand = False

    btnPlayersHand = True
    btnDeal.Caption = "Score Player's Hand"
    btnDeal.Visible = True
End Function

Function CribButtonOpen()
    btnOK = False
    btnComputersHand = False
    btnPlayersHand = False

    btnCribHand = True
    btnDeal.Caption = "Score Crib"
    btnDeal.Visible = True
End Function

Function NewGame()

    If lGameID <> 0
        DoFinalUpdate()
    End If

    btnDeal2 = True
    btnDeal.Caption = "Deal"
    btnDeal.Visible = True
    btnSendToCrib = False
    btnPlayersHand = False
    btnComputersHand = False
    btnCribHand = False

    cb.FrontBluePeg = 0
    cb.BackBluePeg = -1
    cb.FrontRedPeg = 0
    cb.BackRedPeg = -1
    HideAllCards()

    CountLabel.Caption = ""
    cll.Caption = ""
    lblCompCrib.Caption = ""
    lblPlayerCrib.Caption = ""
    AddToStatus("Starting a new game...")




'hands here
End Function

Function HideAllCards()
    local i

    For i = 1 To 6
        PCard[i].Suite = 0
        CCard[i].Suite = 0
    End For
    For i = 1 To 4
        PPegCard[i].Suite = 0
        CPegCard[i].Suite = 0
    End For
    compCrib1.Suite = 0
    compCrib2.Suite = 0
    compCrib3.Suite = 0
    compCrib4.Suite = 0
    playerCrib1.Suite = 0
    playerCrib2.Suite = 0
    playerCrib3.Suite = 0
    playerCrib4.Suite = 0
    UpCard.Suite = 0
End Function

Function DoFinalUpdate()
    If lPPrevPoints <> 0

    End If
    If lCPrevPoints <> 0

    End If


End Function



