GoFish! v.10.1.1
Additional code for interactive play.
Semi-AI computer play.
Computer's card selected automatically and randomly.
Human player must click card to complete action.
by jaba 11/09/11
[play]
auto=1'<--- add this line
hBox=hWnd(#1.g)'graphicbox window handle
.
.
.
[loop]
.
if player=1AND auto=1thengoto[checkIndex]'<--- add this line'set up mouse click event handlers#1.g "setfocus; when leftButtonUp [checkIndex]"
.
.
.
[checkP1Index]'computer selects card to click onif auto=1then'<--- add these 7 lines
i=int(rnd(1)*nP1cards)+1
cardSelected=P1(i)
cardValue=GetCardValue(P1(i))
cPos=i
goto[dealAuto]endif
.
[dealAuto]'<--- add this branch label'redeal the hand with the selected card repositioned
.
//'end of code mods//
GoFish! card game for Card Contest
v.10.1
No artificial intelligence this version.
Player plays both hands.
by jaba 11/07/11
'GoFish.bas'Author: Jack Burman'Date: 11/07/11' DESCRIPTION OF VERSION 10.1' For submission without AI.dim card(52)dim P1(52), P2(38), P3(52)'array of cards in rows 1,2,3dim xP1(52), yP1(52)'x,y each card, computer handdim xP3(52), yP3(52)'x,y each card, player's handdim cardVal(52,13)'rank (value) each card indexdim rankCnt(13,4)'count of total cards in hands by rank (value)dim B1(4), B3(4)'the 4 cards in a book to be discardednomainwinWindowWidth=800:WindowHeight=550UpperLeftX=1:UpperLeftY=1menu#1,"&File","E&xit",[quit],"P&lay again",[playAgain]menu#1,"&About","About",[about]graphicbox#1.g,0,0,800,510open"Card Game"for window_nf as#1#1"trapclose [quit]"[play]
hBox=hWnd(#1.g)'graphicbox window handleopen"qcard32.dll "fordllas#qc
call InitializeDeck hBox
call SetCurrentBack 2#1.g "down; fill 0 113 0"'background color of graphicboxcall DrawSymbol hBox,3,30,50'placeholder symbolscall DrawSymbol hBox,2,12,170call DrawSymbol hBox,3,30,290'The RankCnt is the number of any of the 13 card values that' are in play in the computer hand or the player's hand...'set the count to zero before dealing any cards[setRankCntToZero]for i =1to13
rankCnt(i,1)=i
rankCnt(i,2)=0next i
[fillCardArray]'put all 52 cards into an arrayfor i =1to52
card(i)=i
next[shuffleCards]'shuffle the 52 card array so card order is mixed upfor i =1to52
newIndex=int(rnd(0)*52)+1
tempCard=card(i)
card(i)=card(newIndex)
card(newIndex)=tempCard
next[fillHandArrays]'fill row arrays with the right number of cards before dealing'[P1] Computer hand gets first 7 cards in shuffled array
nP1cards=7for i =1to nP1cards
P1(i)=card(i)
r = GetCardValue(P1(i))'r=rank of card(i)gosub[incrementRankCnt]
r=0next i
'[P2] Draw pile gets 38 cards
nP2cards=38for i =1to nP2cards
P2(i)=card(i+7)next i
'[P3] Player hand gets 7 cards
nP3cards=7for i =1to nP3cards
P3(i)=card(i+45)
r = GetCardValue(P3(i))'r=rank of card(i)gosub[incrementRankCnt]
r=0next i
'sort player's cardscall sortP3 1,7, nP3cards
[newDeal]'deal the first (or new) hand
yP1=50'y coords for rows
yP2=170
yP3=290
margin=0'left hand margin (not used)
reveal=30'card overlap (replaces xOffP... offset)'[P1] Deal Computer hand
xOffP1=30'same as reveal (not used everywhere)for i =1to nP1cards
call SetCardStatus P1(i),0'<---0 shows back; 1 shows facecall DealCard hBox, P1(i), margin+i*reveal, yP1
next'[P2] Deal draw pile
xOffP2=12'space between cards (reveal)for i =1to nP2cards
call SetCardStatus P2(i),0'<---0 shows back; 1 shows facecall DealCard hBox, P2(i), margin+i*xOffP2, yP2
next'[P3] Deal player hand
xOffP3=30for i =1to nP3cards
call SetCardStatus P3(i),1'<---0 shows back; 1 shows facecall DealCard hBox, P3(i), margin+i*reveal, yP3
next'initialize score display
scoreP1=0
scoreP3=0#1.g "font arial 11 bold; color green; backcolor 0 113 0"#1.g "place 30 40"#1.g "\Score: ";scoreP1
#1.g "place 30 410"#1.g "\Score: ";scoreP3
'******************************************************************************'******************************************************************************[loop]
player=3'first to play is human player[newTurn]'display whos turn#1.g "place 30 480"'was 470#1.g "\ "#1.g "place 30 480"#1.g "font arial 12 bold; color green; backcolor 0 113 0"'if a book is discarded, current player keeps playif bookRemoved=1then bookRemoved=0:player=player
if player=3then#1.g "\Your play..."else#1.g "\Computer plays..."'set up mouse click event handlers#1.g "setfocus; when leftButtonUp [checkIndex]"[take]if player=3then#1.g "setfocus; when leftButtonDouble [takeP1Cards]"else#1.g "setfocus; when leftButtonDouble [takeP3Cards]"endifwait'******************************************************************************'******************************************************************************[checkIndex]'stop trapping mouse clicks#1.g "setfocus; when leftButtonUp"if player=3thengosub[checkP3Index]gosub[checkComputerHand]'see if computer hand holds any selected card'--->When computer hand has no matches:if cnt=0thengosub[goFishMsg]'no match, so draw a cardgosub[clickPoolHand]'wait for player to click a pool cardgosub[drawPoolCard]gosub[addToPlayerHand]'If a book is discarded by drawing a pool card,' start current player's turn over again...if bookRemoved=1then bookRemoved=0:goto[newTurn]'...or else, play changes to other playerif player=3then player=1:goto[newTurn]endif'--->When computer hand has matches:if cnt>0thengosub[showComputerHandMatchingCards]goto[take]endif'IF PLAYER = 1elsegosub[checkP1Index]'if player=1 go heregosub[checkPlayerHand]'see if player hand holds any selected card'--->When player hand has no matches:if cnt=0thengosub[goFishMsg]'no match, so draw a cardgosub[clickPoolHand]'wait for player to click a pool cardgosub[drawPoolCard]gosub[addToComputerHand]if bookRemoved=1then bookRemoved=0:goto[newTurn]'play changes to other playerif player=1then player=3:goto[newTurn]endif'--->When player hand has matches:if cnt>0thengosub[showPlayerHandMatchingCards]goto[take]endifendifwait[checkP1Index]'computer selects card to click on
x=0:y=0
mx=MouseX:my=MouseY'check if mouse is within row of computer's cardsifNOT(my >50AND my <150)thennotice"Try again.":goto[newTurn]
cardSelected=0'holds index of clicked card
cardValue=0'holds rank of selected cardfor i =1to nP1cards
'if mouse is within visible part of card, get card indexif mx >i*reveal AND mx <(i+1)*reveal then
cardSelected=P1(i)
cardValue=GetCardValue(P1(i))
cPos=i 'save card's position in handexitforendifnext i
'redeal the hand with the selected card repositionedif cardSelected >0thenfor i = nP1cards to1 step -1call RemoveCard hBox, P1(i)'remove all cardsnext i
'deal cards up to selected cardfor i =1to cPos-1call DealCard hBox, P1(i), margin+i*reveal, yP1
next i
'deal the selected cardcall SetCardStatus, P1(cPos),1call DealCard hBox, P1(cPos), margin+cPos*reveal, yP1+20'deal remainder of cardsfor i =(cPos+1)to nP1cards
call DealCard hBox, P1(i), margin+i*reveal, yP1
next i
endifreturn[checkP3Index]'player selects card to click on
x=0:y=0
mx=MouseX:my=MouseY'check if mouse is within row of player's cardsifNOT(my >290AND my <390)thennotice"Try again.":goto[newTurn]
cardSelected=0'holds index of card player clicks on
cardValue=0'holds rank (value) of selected cardfor i =1to nP3cards
'if mouse is within visible part of card, get card indexif mx >i*reveal AND mx <(i+1)*reveal then
cardSelected=P3(i)
cardValue=GetCardValue(P3(i))
cPos=i 'save card's position in handexitfor'exit loop when a card is selectedendifnext i
' redeal the hand with the selected card repositionedif cardSelected >0thenfor i = nP3cards to1 step -1call RemoveCard hBox, P3(i)next i
' deal cards up to selected cardfor i =1to cPos-1call DealCard hBox, P3(i), margin+i*reveal, yP3
next i
' deal the selected cardcall DealCard hBox, P3(cPos), margin+cPos*reveal, yP3-20' deal remainder of cardsfor i =(cPos+1)to nP3cards
call DealCard hBox, P3(i), margin+i*reveal, yP3
next i
endifreturn[checkComputerHand]'See if computer hand holds any matches to selected card
cnt=0redim match(28)for i =1to nP1cards
cardChecked=GetCardValue(P1(i))if cardChecked=cardValue then
cnt=cnt+1
match(i)=P1(i)endifnext i
return[checkPlayerHand]'See if player hand holds any matches to selected card
cnt=0redim match(28)for i =1to nP3cards
cardChecked=GetCardValue(P3(i))if cardChecked=cardValue then
cnt=cnt+1
match(i)=P3(i)endifnext i
return[showComputerHandMatchingCards]if cnt=0thenreturn'remove all cardsfor i = nP1cards to1 step -1call RemoveCard hBox, P1(i)next i
'save number of P1 cards now
oldnP1cards=nP1cards
'display hand with matching cards turned over and repositionedfor i =1to nP1cards
if match(i)>0thencall SetCardStatus P1(i),1call DealCard hBox, P1(i), margin+i*xOffP1, yP1+20elsecall DealCard hBox, P1(i), margin+i*xOffP1, yP1
endifnext i
return[showPlayerHandMatchingCards]if cnt=0thenreturn'remove all cardsfor i = nP3cards to1 step -1call RemoveCard hBox, P3(i)next i
'save number of P3 cards now
oldnP3cards=nP3cards
'display hand with matching cards repositionedfor i =1to nP3cards
if match(i)>0thencall SetCardStatus P3(i),1call DealCard hBox, P3(i), margin+i*reveal, yP3-20elsecall DealCard hBox, P3(i), margin+i*reveal, yP3
endifnext i
return[takeP1Cards]#1.g "when leftButtonDouble"'cancel double click handler
mc=0'save player's present card count
oldnP3cards=nP3cards
'move matching cards to player's hand and increase total card countredim newP1(nP1cards)
j=1for i =1to nP1cards
if match(i)>0then
P3(nP3cards+1)=P1(i)
nP3cards=nP3cards+1
mc=mc+1else
newP1(j)=P1(i)'track unmatched cards to keep
j=j+1endifnext i
for i = nP1cards to1 step -1call RemoveCard hBox, P1(i)next i
'subtract matched cards from computer hand total card count
nP1cards=nP1cards-mc
'set flag so cards are not removed a second time in [updateP1Hand]
P1removed=1redim P1(28)for i =1to nP1cards
P1(i)=newP1(i)'update P1 array without matchesnext i
gosub[updateP1Hand]gosub[updateP3Hand]goto[newTurn]wait[takeP3Cards]#1.g "when leftButtonDouble"'cancel double click handler
mc=0'save computer hand present card count
oldnP1cards=nP1cards
'save player hand present card count (just in case?)
oldnP3cards=nP3cards
'move matching cards to computer hand and increase total card countredim newP3(nP3cards)
j=1for i =1to nP3cards
if match(i)>0then
P1(nP1cards+1)=P3(i)
nP1cards=nP1cards+1
mc=mc+1else
newP3(j)=P3(i)
j=j+1endifnext i
for i = oldnP3cards to1 step -1call RemoveCard hBox, P3(i)next i
'subtract matched cards from player's hand total card count
nP3cards=nP3cards-mc
'set flag so cards are not removed a second time in [updateP3Hand]
P3removed=1redim P3(28)for i =1to nP3cards
P3(i)=newP3(i)'update P3 array without matchesnext i
gosub[updateP3Hand]gosub[updateP1Hand]goto[newTurn]wait[updateP1Hand]'fishing flag indicates a card has been drawn from the pool and added to handif fishing=1then
allCards=nP1cards
else
allCards=oldnP1cards
endif
fishing=0'cancel flag'if a book has not been discarded, skip removing all cards'otherwise, remove all cards left after discarding the book'this should clear all background images and leave blank table for new dealif P1removed=0thenfor i = allCards to1 step -1call RemoveCard hBox, P1(i)next i
endif
P1removed=0'cancel flagfor i =1to nP1cards
call SetCardStatus P1(i),0'<--- 0= face downcall DealCard hBox, P1(i), margin+i*reveal, yP1
next'If player 3 has taken cards from computer hand, no need to check' computer's hand for all 4 cards, so skip next routineif player=3then[skipCnt]'check card count - see if there are 4 of the cardSelected are in playgosub[chkCardCnt]'if all 4 cards of a book are in play, check if all are in one handif maxCnt=1thengosub[rankInHandP1]'if all 4 in computer hand, go remove themif rankInHand=4thengosub[removeRankSetP1]
rankInHand=0
cardSelected=0endif[skipCnt]'game is over if no cards left in a handif nP1cards=0thenif scoreP1>scoreP3 thennotice"Game over";chr$(13);"Computer wins!"waitelsenotice"Game over";chr$(13);"You win!"endifwaitendifreturn[updateP3Hand]if fishing=1then
allCards=nP3cards
else
allCards=oldnP3cards
endif
fishing=0if P3removed=0thenfor i = allCards to1 step -1call RemoveCard hBox, P3(i)next i
endif
P3removed=0'sort hand (player's hand is only one sorted)call sortP3 1, nP3cards, nP3cards
for i =1to nP3cards
call DealCard hBox, P3(i), margin+i*reveal, yP3
next i
if player=1then[skipP3Cnt]'check card countgosub[chkCardCnt]if maxCnt=1thengosub[rankInHandP3]if rankInHand=4thengosub[removeRankSetP3]
rankInHand=0
cardSelected=0endif[skipP3Cnt]if nP3cards=0thenif scoreP1>scoreP3 thennotice"Game over";chr$(13);"Computer wins!"waitelsenotice"Game over";chr$(13);"You win!"endifwaitendifreturn[rankInHandP1]'checks if all 4 cards are in computer's hand
rankInHand=0for i =1to nP1cards
rank=GetCardValue(P1(i))if rank=cardValue then rankInHand=rankInHand+1next i
return[removeRankSetP1]'remove the 4 cards of a book from the computer's handfor i = nP1cards to1 step -1call RemoveCard hBox, P1(i)'remove all cardsnext i
redim B1(4)'clear any previous countsredim newP1(28)'clear temporary array
j=1
k=0for i =1to nP1cards
getRank=GetCardValue(P1(i))ifNOT(getRank=cardValue)then
newP1(j)=P1(i)
j=j+1else
k=k+1
B1(k)=P1(i)call SetCardStatus, B1(k),1'cards face up'place the cards at bottom of window to show' which book is being discardedcall DealCard hBox, B1(k),200+k*10,400endifnext i
nP1cards=nP1cards-rankInHand
for i =1to nP1cards
P1(i)=newP1(i)next i
for i =1to nP1cards
call SetCardStatus P1(i),0call DealCard hBox, P1(i), margin+i*xOffP1, yP1
next i
scoreP1=scoreP1+1#1.g "font arial 11 bold; color green; backcolor 0 113 0"#1.g "place 30 40"#1.g "\Score: ";scoreP1
'display the book for a bit...then remove itcall Pause 1000'adjust to suitfor i=4to1 step -1call RemoveCard, hBox, B1(i)next i
bookRemoved=1return[rankInHandP3]'checks if all 4 cards are in player's hand
rankInHand=0for i =1to nP3cards
rank=GetCardValue(P3(i))if rank=cardValue then rankInHand=rankInHand+1next i
return[removeRankSetP3]'remove the 4 cards of a book from the player's handfor i = nP3cards to1 step -1call RemoveCard hBox, P3(i)next i
redim B3(4)redim newP3(28)
j=1
k=0for i =1to nP3cards
getRank=GetCardValue(P3(i))ifNOT(getRank=cardValue)then
newP3(j)=P3(i)
j=j+1else
k=k+1
B3(k)=P3(i)call SetCardStatus, B3(k),1call DealCard hBox, B3(k),200+k*10,400endifnext i
nP3cards=nP3cards-rankInHand
for i =1to nP3cards
P3(i)=newP3(i)next i
for i =1to nP3cards
call SetCardStatus P3(i),1call DealCard hBox, P3(i), margin+i*reveal, yP3
next i
scoreP3=scoreP3+1#1.g "font arial 11 bold; color green; backcolor 0 113 0"#1.g "place 30 410"#1.g "\Score: ";scoreP3
call Pause 1000for i=4to1 step -1call RemoveCard, hBox, B3(i)next i
bookRemoved=1return[goFishMsg]if player=3then
d=40else
d=430endif#1.g "place 500 ";d
#1.g "font Georgia 20 bold;color white; backcolor 0 113 0"#1.g "\Go Fish!"'set flag to indicate will draw a pool card
fishing=1return[clickPoolHand]'set doubleclick event#1.g "setfocus; when leftButtonDouble [clickPool]"wait'no coords needed because player can click anywhere on table' and will automatically select the topmost card from the pool hand.'a more complex scheme would allow selecting any card in the pool,' but I don't see any need since it's all random anyway.'...but don't tell the user; he thinks he has to double click' the selected card to draw from the pool...
x=0:y=0
mx=MouseX:my=MouseY[clickPool]if player=3then
d=30else
d=430endif'cancel gofish msg#1.g "color 0 113 0; backcolor 0 113 0; place ";490; " ";d-30#1.g "boxfilled ";650;" ";d+20#1.g "when leftButtonDouble"return[drawPoolCard]'here the topmost card is actually removed from the pool hand' and put in limbo for the current player to take
limboCard=P2(nP2cards)'get top card off draw pile
r = GetCardValue(limboCard)'r=rank of card(i)gosub[incrementRankCnt]'add card to array of number of cards in playcall SetCardStatus limboCard,1'<--- 1 shows facefor i = nP2cards to1 step -1call RemoveCard hBox, P2(i)'remove all cardsnext i
nP2cards=nP2cards-1'remove top card from hand'if all pool cards gone, game is overif nP2cards=0thenif scoreP1>scoreP3 thennotice"Game over";chr$(13);"Computer wins!"waitelsenotice"Game over";chr$(13);"You win!"endifwaitendiffor i =1to nP2cards
call DealCard hBox, P2(i), margin+i*xOffP2, yP2
nextreturn[addToComputerHand]'adds pool card to computer hand
oldnP1cards=nP1cards
nP1cards=nP1cards+1
P1(nP1cards)=limboCard
cardSelected=limboCard
call SetCardStatus P1(nP3cards),1'redraw hand' fishing=1 'may be redundant - will checkgosub[updateP1Hand]return[addToPlayerHand]'adds pool card to player's hand
nP3cards=nP3cards+1
P3(nP3cards)=limboCard
cardSelected=limboCard
call SetCardStatus P3(nP3cards),1'redraw hand' fishing=1gosub[updateP3Hand]return[incrementRankCnt]'counts cards in play of any value
rankCnt(r,2)=rankCnt(r,2)+1return[chkCardCnt]'checks if all 4 cards of selected value are in playif r>0then cardValue=r
if rankCnt(cardValue,2)=4then
maxCnt=1else
maxCnt=0endif
r=0return[playAgain]'menu selectionclose#qc
goto[play][about]notice"Help & About"; chr$(13);_
"GoFish! Card Game";chr$(13);_
"LB Card Contest submission";chr$(13);_
"by jaba 11/30/2011";chr$(13);_
chr$(13);_
"How to play:";chr$(13);_
"-Human plays both hands. (No AI yet.)";chr$(13);_
"-Click card to select it.";chr$(13);_
"-Double click selected card to take card from";chr$(13);_
" other hand or fish from pool.";chr$(13);_
"-You keep your turn if there is a match.";chr$(13);_
"-All 4 cards of one kind in a hand scores 1 point.";chr$(13);_
"-Play ends when any row runs out of cards.";chr$(13);_
"-Highest score wins.";chr$(13);_
"-Select 'Play Again' from menu to play again..."wait[quit]close#qc
close#1end'=================================================================='SUBS AND FUNCTIONS'==================================================================function GetCardValue(nC)calldll#qc,"GetCardValue", _
nC aslong, _
GetCardValue aslongendfunctionsub Pause ms
'pause ms number of millisecondscalldll#kernel32,"Sleep",_
ms aslong,_
re asvoidendsubsub InitializeDeck hndle
calldll#qc,"InitializeDeck",_
hndle asulong,_
r aslongendsubsub DealCard hndle,nC,x,y
'nC is number of card - 1-52calldll#qc,"DealCard",_
hndle asulong,_
nC aslong,_
x aslong,_
y aslong,_
r asvoidendsubsub SetCardStatus nC, face
calldll#qc,"SetCardStatus",_
nC aslong,_
face aslong,_
r asvoidendsubsub SetCurrentBack nV
calldll#qc,"SetCurrentBack",_
nV aslong,_
r asvoidendsubSub RemoveCard hndle,nC
'removes a card from screen that was'drawn with DealCard, replacing screen backgroundcalldll#qc,"RemoveCard",hndle asulong,_
nC aslong,r asvoidEndSubsub sortP3 b, e, nP3
for i = b to e
cardVal(i,1)= P3(i)
cardVal(i,2)= GetCardValue(P3(i))next i
sort cardVal(, b, e,2for i =1to nP3
P3(i)=cardVal(b+(i-1),1)next i
endsubSub DrawSymbol hndle,nV,nx,ny
calldll#qc,"DrawSymbol",_
hndle asulong,_ 'handle of graphicbox
nV aslong,_ '1=X 2=O 3=place holder
nx aslong,_ 'x location
ny aslong,_ 'y location
re asvoid'no returnendsub
Go Fish! card game for Card Contest
A work in progress - not complete
'Questions of forum.'1) In the gofishMsg sub, why don't the cards in the pool hand remove properly;' and why don't they redisplay properly?'2) Why is the pool hand so slow to redeal?'3) The y1, y2, y3 variables are global but do not work right in the' gofishMsg sub for placing the player's hand. Why not?'4) Why doesn't the flush command work after redealing the pool hand' and the player's hand?'52CARDS_sortTest11Bglobal y1, y2, y3, ccfv, noOfCards1, noOfCards2, noOfCards3, x2Offset, x3Offset
dim ind(52,13)'cardIndex 1-52dim P1(25,2)'computer's hand by cardIndex up to 25 cardsdim P2(25,2)'player's hand by cardIndex up to 25 cardsdim P(38,2)'pool hand by cardIndex begins with 38 cards[varSetup]' nomainwinWindowWidth=700:WindowHeight=500UpperLeftX=1:UpperLeftY=1menu#1,"&File","E&xit",[quit]graphicbox#1.g,0,0,700,440open"Card Game"for window_nf as#1#1"trapclose [quit]"
hBox=hWnd(#1.g)open"qcard32.dll "fordllas#qc
call InitializeDeck hBox
call SetCurrentBack 2#1.g "down; fill 10 225 127"#1.g "setfocus; when leftButtonDown [checkIndex]"[fillCardArray]'using the cardIndex from the dll' clubs A - K: 1-13' diamonds A - K: 14-26' hearts A - K: 27-39' spades A - K: 40-52call indexDeck
[shuffleCards]call shuffleDeck
[newDeal]
x1Offset=48
y1=10'[P1]
noOfCards1=7'Computer hand does not need sorted and is dealt face downfor i =1to noOfCards1
scan'place first 7 cards into computer's hand array
P1(i,1)=ind(i,1)
P1(i,2)=ind(i,2)call SetCardStatus P1(i,1),0'<---change to 0 to hide; 1 to viewcall DealCard hBox, P1(i,1),i*x1Offset,y1
call Pause 20next'[P]
x2Offset=12
y2=150
noOfCards2=38'Pool hand is dealt face down, unsortedfor i =1to noOfCards2
scan'place 38 cards into hand array for card pool
P(i,1)=ind(i+7,1)
P(i,2)=ind(i+7,2)'set face up=1; face down=0call SetCardStatus P(i,1),0'<--- set to 0 to hide; 1 to viewcall DealCard hBox, P(i,1),i*x2Offset,y2
call Pause 20next'[P2]
x3Offset=48
y3=300
noOfCards3=7'player's hand needs sorted by ranksort ind(,46,52,2'create player's hand arrayfor i =1to noOfCards3
P2(i,1)=ind(i+45,1)
P2(i,2)=ind(i+45,2)next i
'deal player's handfor i =1to noOfCards3
scan'set face up=1call SetCardStatus P2(i,1),1'<--- set to 0 to hide; 1 to viewcall DealCard hBox, P2(i,1),i*x3Offset,y3
call Pause 20next#1.g "flush"wait[checkIndex]'activated by leftButtonDown event
x=0:y=0'reset values
cardClicked=0
mx=MouseX: my=MouseY'mouse x and y location
nCard=InitDrag(hBox, mx, my)'discover index of card under mouseif nCard=0thenwait'find the leading edge of the card nCardfor i =1to noOfCards3
cardEdge=i*x3Offset
if nCard=P2(i,1)thenexitfornext i
'check if the mouse is within the overlapping area' of the two cards. If so, the clicked card is nCard.' If not, the clicked card is the next one in the array.if mx>cardEdge AND mx<(cardEdge+x3Offset)then
cardClicked=P2(i,1)
ccfv=P2(i,2)',2)else
cardClicked=P2(i+1,1)
ccfv=P2(i+1,2)',2)endif'temporary printout for checking index and rank of selection'will not be in final program#1.g "place 10 420;\Card Index is ";cardClicked
#1.g "place 200 420;\Face value is ";ccfv
x=GetCardX(cardClicked)
y=GetCardY(cardClicked)call AbortDrag 'release DLL mouse capture'remove cards in player's handfor i = noOfCards3 to1 step -1call RemoveCard hBox, P2(i,1)next'redeal player's hand with selected card up +50 pixelsfor i =1to noOfCards3
if P2(i,1)=cardClicked then y=y3-50else y=y3
call DealCard hBox, P2(i,1),i*x3Offset,y
next'---> print "Card clicked = ";cardClicked:print'check to see if computer has any of the selected cardsgosub[checkComputerHand]#1.g "setfocus; when leftButtonDown"'stop mouse event checking#1.g "flush"wait[checkComputerHand]
cnt=0for i=1to noOfCards1
if P1(i,2)=ccfv thencall SetCardStatus P1(i,1),1'0 '<--- change to 0 for game
cnt=cnt+1endifnext i
'if no card in the computer's hand matches the rank' of the selected card, the count is 0 and we Go Fish!if cnt=0thencall gofishMsg y1
goto[nomatch]endiffor i = noOfCards1 to1 step -1call RemoveCard hBox, P1(i,1)nextfor i =1to noOfCards1
if GetCardStatus( P1(i,1))=1then y=y1+20else y=y1
call DealCard hBox, P1(i,1),i*x1Offset,y'1next i
[nomatch]return'======================================================================'THE FOLLOWING SUBS ARE PLACED HERE FOR CONVENIENCE TO FOLLOW CODE'======================================================================='If we Go Fish!, we have to get a card from the pool (make it the' last card in line for simplicity) and add that card to the' player's hand (haven't worked out the routine for adding the card' to the computer's hand yet).'Then we'll remove the player's hand and redeal it with the' added card and all cards back in line. Somewhere, we have to' resort because we added a card.sub gofishMsg y
d=y+80#1.g "place 500 ";d
#1.g "font Arial 20 bold;color red"#1.g "\Go Fish!"
poolCard=P(noOfCards2,1)call moveCard poolCard
call redealPool noOfCards2
for i = noOfCards3 to1 step -1call RemoveCard hBox, P2(i,1)next i
#1.g "down; color 10 225 127; backcolor 10 225 127"#1.g "place 12 290; boxfilled 600 400"for i =1to noOfCards3
nC=P2(i,1)call DealCard hBox, nC,i*x3Offset,350'why doesn't y2 work?next i
call Pause 20endsubsub moveCard poolCard
'next line for checking in mainwinprint"poolCard is: ";poolCard
'add this card to player's hand
noOfCards3=noOfCards3+1
P2(noOfCards3,1)=P(noOfCards2,1)
P2(noOfCards3,2)=P(noOfCards2,2)call SetCardStatus P2(noOfCards3,1),1
noOfCards2=(noOfCards2-1)'next line for checking in mainwinprint noOfCards2
call removePool noOfCards2
'next lines for checking in mainwinfor i =1to noOfCards3
print i;" - ";P2(i,1), P2(i,2)next i
endsubsub removePool noOfCards2
for i = noOfCards2 to1 step -1call RemoveCard hBox, P(i,1)next#1.g "down; color 10 225 127; backcolor 10 225 127"#1.g "place 12 150; boxfilled 550 248"' #1.g "flush" '<--- why doesn't flush work here?endsubsub redealPool noOfCards2
for i =1to noOfCards2
scan
nC=P(i,1)call DealCard hBox, nC,i*x2Offset,195'y2 'why doesn't y2 work?next#1.g "flush"'<--- why doesn't flush work?endsub[quit]close#qc
close#1end'========================================================'------------------- subs and functions -----------------'========================================================sub indexDeck
j=0for i =1to52
ind(i,1)=i
j=j+1
ind(i,2)=j
if j=13then j=0next i
endsubsub shuffleDeck
for i =1to52
newIndex=int(rnd(0)*52)+1
tempCard=ind(i,1)
tempVal=ind(i,2)
ind(i,1)=ind(newIndex,1)
ind(i,2)=ind(newIndex,2)
ind(newIndex,1)=tempCard
ind(newIndex,2)=tempVal
nextendsubsub Pause ms
'pause ms number of millisecondscalldll#kernel32,"Sleep",_
ms aslong,_
re asvoidendsubSub SetOffSet offset
calldll#qc,"SetOffSet",offset aslong,_
re asvoidendsubSub AdjustCardBlocked nC, bValue
calldll#qc,"AdjustCardBlocked",_
nC aslong, bValue aslong, re asvoidendsubFunction GetCardBlocked(nC)calldll#qc,"GetCardBlocked",nC aslong,_
GetCardBlocked aslongendfunctionsub InitializeDeck hndle
calldll#qc,"InitializeDeck",_
hndle asulong,_
r aslongendsubsub DealCard hndle,nC,x,y
'places cards on window whose handle is hndle at x,y'nC is number of card - 1-52 in first deck and'53-104 in second deck, if usedcalldll#qc,"DealCard",_
hndle asulong,_
nC aslong,_
x aslong,_
y aslong,_
r asvoidendsubsub SetCardStatus nC, face
'nC is number of card - 1-52 in first deck and'53-104 in second deck if used'face: 0=facedown, 1=faceupcalldll#qc,"SetCardStatus",_
nC aslong,_
face aslong,_
r asvoidendsubsub SetCurrentBack nV
calldll#qc,"SetCurrentBack",_
nV aslong,_
r asvoidendsubSub RemoveCard hndle,nC
'removes a card from screen that was'drawn with DealCard, replacing screen backgroundcalldll#qc,"RemoveCard",hndle asulong,_
nC aslong,r asvoidEndSubFunction InitDrag(hndle, x, y)calldll#qc,"InitDrag",_
hndle asulong, x aslong, y aslong,_
InitDrag aslongendfunctionSub AbortDrag
calldll#qc,"AbortDrag",re asvoidendsubFunction GetCardX(nC)calldll#qc,"GetCardX",_
nC aslong,_ 'index of card
GetCardX aslong'x location of upper cornerendfunctionFunction GetCardY(nC)calldll#qc,"GetCardY",_
nC aslong,_ 'index of card
GetCardY aslong'y location of upper cornerendfunctionFunction GetCardStatus(nC)calldll#qc,"GetCardStatus", _
nC aslong, _
GetCardStatus aslongendfunction
THE FOLLOWING PROGRAM IS SUPERCEDED BY THE ONE ABOVE.
Go Fish! card game for Card Contest
A work in progress - not complete
global y1, y3
dim ind(52)'cardIndex 1-52dim P1(25)'computer's hand by cardIndex up to 25 cardsdim P2(25)'player's hand by cardIndex up to 25 cardsdim P(38)'pool hand by cardIndex begins with 38 cardsdim card(52,2)'dbl dim index + face value[varSetup]nomainwinWindowWidth=700:WindowHeight=500UpperLeftX=1:UpperLeftY=1menu#1,"&File","E&xit",[quit]graphicbox#1.g,0,0,700,440open"Card Game"for window_nf as#1#1"trapclose [quit]"'get graphicbox handle
hBox=hWnd(#1.g)'open the dllopen"qcard32.dll "fordllas#qc
'initialize the deckcall InitializeDeck hBox
'back designs: bubbles=1;blue=2;red=3;mountain=4;purple=5;music=6call SetCurrentBack 2'paint background#1.g "down; fill 10 225 127"'This action selects the card in the player's hand that he wishes' to ask opponent for.'If opponent has this card in his hand, all matches are added to' player's hand.'If opponent does not have a matching card, a "Go Fish! message' is issued.#1.g "setfocus; when leftButtonDown [checkIndex]"[fillCardArray]'using the cardIndex from the dll' clubs A - K: 1-13' diamonds A - K: 14-26' hearts A - K: 27-39' spades A - K: 40-52call indexDeck
[shuffleCards]call shuffleDeck
call dblIndexCards
'-------------------------------------------------'NOTE: Since cards have been shuffled, the card() array holds a randomly' organized card index, instead of 1 to 52. So, first 7 cards dealt could' be any card index.'--------------------------------------------------[newDeal]
x1Offset=48
y1=10'[P1]
noOfCards1=7'Computer hand does not need sorted and is dealt face downfor i =1to noOfCards1
scan'place first 7 cards into computer's hand array
P1(i)=card(i,1)'set face down=0call SetCardStatus P1(i),0'<---change to 0 when ready'window handle, card index number, x, ycall DealCard hBox, P1(i),i*x1Offset,y1
call Pause 100'---> print "P1(";i;") = ";card(i,1)next'[P]
x2Offset=12
y2=150
noOfCards2=38'Pool hand is dealt face down, unsortedfor i =1to noOfCards2
scan'place 38 cards into hand array for card pool
P(i)=card(i+7,1)'set face up=1; face down=0call SetCardStatus P(i),0'window handle, card index number, x, ycall DealCard hBox, P(i),i*x2Offset,y2
call Pause 20next'[P2]
x3Offset=48
y3=300
noOfCards3=7'player's hand needs sorted by ranksort card(,46,52,2'create player's hand arrayfor i =1to noOfCards3
P2(i)=card(i+45,1)next i
'deal player's handfor i =1to noOfCards3
scan'set face up=1call SetCardStatus P2(i),1'<---change to 0 for down'window handle, card index number, x, ycall DealCard hBox, P2(i),i*x3Offset,y3
call Pause 100'---> print "P2(";i;") = ";card(i+45,1)next#1.g "flush"wait[checkIndex]'activated by leftButtonDown event
x=0:y=0'reset values
cardClicked=0
mx=MouseX: my=MouseY'mouse x and y location
nCard=InitDrag(hBox, mx, my)'discover index of card under mouseif nCard=0thenwaitfor i =1to noOfCards3
cardEdge=i*x3Offset
if nCard=P2(i)thenexitfornext i
if mx>cardEdge AND mx<(cardEdge+x3Offset)then
cardClicked=P2(i)
ccfv=card(P2(i),2)else
cardClicked=P2(i+1)
ccfv=card(P2(i+1),2)endiffor i =1to52if card(i,1)=cardClicked then
ccfv=card(i,2)exitforendifnext i
'for checking#1.g "place 10 420;\Card Index is ";cardClicked';space$(100)#1.g "place 200 420;\Face value is ";ccfv'cardClicked;space$(100)
x=GetCardX(cardClicked)
y=GetCardY(cardClicked)call AbortDrag 'release DLL mouse capturefor i = noOfCards3 to1 step -1call RemoveCard hBox, P2(i)nextfor i =1to noOfCards3
if P2(i)=cardClicked then y=y3-50else y=y3
call DealCard hBox, P2(i),i*x3Offset,y
next'---> print "Card clicked = ";cardClicked:printgosub[checkComputerHand]#1.g "setfocus; when leftButtonDown"'stop mouse event checkingwait[checkComputerHand]
cnt=0for j=1to noOfCards1
for i=1to52if P1(j)=card(i,1)thenif card(i,2)=ccfv thencall SetCardStatus card(i,1),1'0 '<--- change to 1 for game
cnt=cnt+1endifendifnext i
next j
if cnt=0then'not sure cnt is ever 0?call gofishMsg y1
goto[nomatch]endiffor i = noOfCards1 to1 step -1call RemoveCard hBox, P1(i)nextfor i =1to noOfCards1
if GetCardStatus( card(i,1))=1then y=y1+20else y=y1
call DealCard hBox, card(i,1),i*x1Offset,y'1next i
[nomatch]returnsub gofishMsg y
y=y+80#1.g "place 500 ";y
#1.g "font Arial 20 bold;color red"#1.g "\Go Fish!"#1.g "font 10 bold;color black"endsub[quit]close#qc
close#1end'========================================================'------------------- subs and functions -----------------'========================================================sub indexDeck
for i =1to52
ind(i)=i
nextendsubsub shuffleDeck
for i =1to52
newIndex=int(rnd(0)*52)+1
tempCard=ind(i)
ind(i)=ind(newIndex)
ind(newIndex)=tempCard
nextendsubsub dblIndexCards
for i =1to52
value = ind(i)selectcasecase value <=13
card(i,1)=value
card(i,2)=value
case(value >13)AND(value <=26)
card(i,1)=value
card(i,2)=value-13case(value >26)AND(value <=39)
card(i,1)=value
card(i,2)=value-26case(value >39)
card(i,1)=value
card(i,2)=value-39endselectnext i
'for i=1 to 52' print card(i,1),card(i,2)'next i'print:printendsubsub Pause ms
'pause ms number of millisecondscalldll#kernel32,"Sleep",_
ms aslong,_
re asvoidendsubSub SetOffSet offset
calldll#qc,"SetOffSet",offset aslong,_
re asvoidendsubSub AdjustCardBlocked nC, bValue
calldll#qc,"AdjustCardBlocked",_
nC aslong, bValue aslong, re asvoidendsubFunction GetCardBlocked(nC)calldll#qc,"GetCardBlocked",nC aslong,_
GetCardBlocked aslongendfunctionsub InitializeDeck hndle
calldll#qc,"InitializeDeck",_
hndle asulong,_
r aslongendsubsub DealCard hndle,nC,x,y
'places cards on window whose handle is hndle at x,y'nC is number of card - 1-52 in first deck and'53-104 in second deck, if usedcalldll#qc,"DealCard",_
hndle asulong,_
nC aslong,_
x aslong,_
y aslong,_
r asvoidendsubsub SetCardStatus nC, face
'nC is number of card - 1-52 in first deck and'53-104 in second deck if used'face: 0=facedown, 1=faceupcalldll#qc,"SetCardStatus",_
nC aslong,_
face aslong,_
r asvoidendsubsub SetCurrentBack nV
calldll#qc,"SetCurrentBack",_
nV aslong,_
r asvoidendsubSub RemoveCard hndle,nC
'removes a card from screen that was'drawn with DealCard, replacing screen backgroundcalldll#qc,"RemoveCard",hndle asulong,_
nC aslong,r asvoidEndSubFunction InitDrag(hndle, x, y)calldll#qc,"InitDrag",_
hndle asulong, x aslong, y aslong,_
InitDrag aslongendfunctionSub AbortDrag
calldll#qc,"AbortDrag",re asvoidendsubFunction GetCardX(nC)calldll#qc,"GetCardX",_
nC aslong,_ 'index of card
GetCardX aslong'x location of upper cornerendfunctionFunction GetCardY(nC)calldll#qc,"GetCardY",_
nC aslong,_ 'index of card
GetCardY aslong'y location of upper cornerendfunctionFunction GetCardStatus(nC)calldll#qc,"GetCardStatus", _
nC aslong, _
GetCardStatus aslongendfunction
Additional code for interactive play.
Semi-AI computer play.
Computer's card selected automatically and randomly.
Human player must click card to complete action.
by jaba 11/09/11
GoFish! card game for Card Contest
v.10.1
No artificial intelligence this version.
Player plays both hands.
by jaba 11/07/11
Go Fish! card game for Card Contest
A work in progress - not complete
THE FOLLOWING PROGRAM IS SUPERCEDED BY THE ONE ABOVE.
Go Fish! card game for Card Contest
A work in progress - not complete