Go Fish: Difference between revisions

355 bytes added ,  1 month ago
m
mNo edit summary
 
(6 intermediate revisions by 2 users not shown)
Line 245:
 
=={{header|FutureBasic}}==
Translated from FreeBasic May 5, 2024
<syntaxhighlight lang="futurebasic">
 
May 22 Minor update replaces || with or in the instructions.
 
<syntaxhighlight lang="futurebasic">
/*
 
Go Fish
-- Rich Love --
 
FutureBasic app forFor Macintosh
Get the latest FutureBasic here
http://www.brilorsoftware.com/fb/pages/home.html
 
*/
 
 
_window = 1
Line 269 ⟶ 275:
cards = "A234567890JQK"
short play(13), Computer(13), deck(13), guess(13), poss(13), asked(13)
str255 YourName, Someone,RequestedCard
//bool gNeedToClearScreen
short Points(2) : Points(0) = 0 : Points(1) = 0
short i, k, j, CardNumber
Line 281 ⟶ 287:
str255 WantsCard
WantsCard = str$(TheCard)
WantsCard = Mid$(cards,k,1)
if TheCard = 0 then WantsCard = "10"
if TheCard = 11 then WantsCard = "jack"
Line 292 ⟶ 298:
void local fn PrintViewScrollToBottom( printView as ViewRef )
BeginCCode
NSScrollView *scrollView = [printView enclosingScrollView];
NSClipView *clipView = [scrollView contentView];
[clipView scrollToPoint:NSMakePoint(0,printView.frame.size.height-scrollView.contentSize.height + 20)];
[scrollView reflectScrolledClipView:clipView];
EndC
end fn
 
Line 304 ⟶ 310:
RemainingCards -= 1
short sc
sc = rnd(RemainingCards) + 1 // 5/2/24 Rich added + 1
For k = 1 To 13
Line 327 ⟶ 333:
End fn
 
 
void local fn cpuGoFishing
Line 332 ⟶ 339:
fn DealCards
Print " a card from the deck."
if k > 13 then k = 13
deck(k) -= 1
Computer(k) += 1
End fn
 
 
local fn InitCards
cards = "A234567890JQK"
RemainingCards = 4 * len$(cards) // the length of cards is 13. There are 4 suits of cards. so RemainingCards is 52
i = 0:k = 0:j = 0:CardNumber = 0
for i = 0 to 1
Points(i) = 0
next i
FOR i = 1 TO 13 ' Reset each element to 0
play(i) = 0
Computer(i) = 0
deck(i) = 0
guess(i) = 0
poss(i) = 0
asked(i) = 0
NEXT i
For i = 1 To 13
deck(i) = 4
Next i
For i = 1 To 9
fn DealCards
deck(k) -= 1
Computer(k) += 1
fn DealCards
deck(k) -= 1
play(k) += 1
Next i
end fn
 
 
Line 384 ⟶ 356:
WantsCard = Mid$(cards,i,1)
if WantsCard = "j" or|| WantsCard = "J" then WantsCard = "Jack"
if WantsCard = "q" or|| WantsCard = "Q" then WantsCard = "Queen"
if WantsCard = "k" or|| WantsCard = "K" then WantsCard = "King"
if WantsCard = "a" or|| WantsCard = "A" then WantsCard = "Ace"
if WantsCard = "0" then WantsCard = "10"
Line 410 ⟶ 382:
WantsCard = Mid$(cards,i,1)
if WantsCard = "j" or|| WantsCard = "J" then WantsCard = "Jack"
if WantsCard = "q" or|| WantsCard = "Q" then WantsCard = "Queen"
if WantsCard = "k" or|| WantsCard = "K" then WantsCard = "King"
if WantsCard = "a" or|| WantsCard = "A" then WantsCard = "Ace"
if WantsCard = "0" then WantsCard = "10"
Line 426 ⟶ 398:
End fn
 
 
local fn InitCards
cards = "A234567890JQK"
RemainingCards = 4 * len$(cards) // the length of cards is 13. There are 4 suits of cards. so RemainingCards is 52
i = 0:k = 0:j = 0:CardNumber = 0
For i = 0 to 1
Points(i) = 0
next i
For i = 1 TO 13 // Reset each element to 0
play(i) = 0
Computer(i) = 0
deck(i) = 0
guess(i) = 0
poss(i) = 0
asked(i) = 0
NEXT i
For i = 1 To 13
deck(i) = 4
Next i
For i = 1 To 9
fn DealCards
deck(k) -= 1
Computer(k) += 1
fn DealCards
deck(k) -= 1
play(k) += 1
Next i
fn CheckForCompletedBook // Rich added 5/1/24
fn CheckCPUForCompletedBook // Rich added 5/5/24
end fn
 
 
local fn QuitOrPlayAlert(GameResult as CFStringRef)
Line 456 ⟶ 467:
Next i
If RemainingCards = <0 Or|| np = 0 Or|| nc = 0
text ,,fn colorRed
Line 492 ⟶ 503:
Print " __ _ _ "
Print " __ _ ___ / _(_)___| |__ "
Print " / ` |/ _ \ | |_| / __| '//_ \ "
Print "| (_) | (_) | | _| \__ \ | | | "
Print " \__, |\___/ |_| |_|___/_| |_| "
Line 508 ⟶ 519:
print " The remaining cards are placed face down in the center of the table"
print " to form the draw pile (the fish pond)."
print " On your turn, you ask the CPU forFor a card."
print " You must already have at least one card of a given rank to ask forFor more."
print " (A rank is one or more of any card.)"
print " If the CPU has any cards of the named rank, it must hand over all such cards,"
Line 524 ⟶ 535:
CFStringRef UserInput
"InputYourName"
UserInput = input % (300, 70), @"What's your name?: "
if ( UserInput == NULL ) then "InputYourName" // Rich added this 5/1/24
fn CFStringGetPascalString (UserInput, @YourName, 256, _kCFStringEncodingMacRoman)
gNeedToClearScreen = _True
cls
if YourName = "X" or YourName = "x" then fn QuitOrResumeAlert(@"EXIT")
if YourName = "X" || YourName = "x" || YourName = chr$(127) then fn QuitOrResumeAlert(@"EXIT")
End fn
Line 554 ⟶ 567:
end fn
 
'//--- Start ---
 
fn BuildWindow
Line 572 ⟶ 585:
 
ShowHand = _false
 
short loopcounter = 0
str255 RequestedCard
 
While ShowHand = _false
Line 588 ⟶ 602:
Print Chr$(10) + " " + str$(RemainingCards) + " remaining cards"
text ,,fn colorWhite
/*
// Uncomment this to see the CPU'sCPUs cards forFor testing
Print Chr$(10) + "CPU Cards: ";
For i = 1 To 13
Line 628 ⟶ 643:
if fn CheckForEndGame = _True then cls:fn InitCards:goto "Loop"
loopcounter ++
Someone = YourName
if gNeedToClearScreen = _False then fn PrintViewScrollToBottom( fn WindowPrintView(1) ):gNeedToClearScreen = _False
CFStringRef UserInput = 0
"InputCard"
UserInput = input % (20, fn WhatCardInputHeight),@"What card do you want? "
if ( UserInput == NULL ) then "InputCard" // Rich added this 5/1/24
fn CFStringGetPascalString (UserInput, @RequestedCard, 256, _kCFStringEncodingMacRoman)
if RequestedCard = "10" then RequestedCard = "0"// card zero is a 10
Line 642 ⟶ 656:
WantsCard = RequestedCard
if WantsCard = "j" or|| WantsCard = "J" then WantsCard = "Jack"
if WantsCard = "q" or|| WantsCard = "Q" then WantsCard = "Queen"
if WantsCard = "k" or|| WantsCard = "K" then WantsCard = "King"
if WantsCard = "a" or|| WantsCard = "A" then WantsCard = "Ace"
if WantsCard = "0" then WantsCard = "10"
Line 653 ⟶ 667:
AorAn = "a"
if WantsCard = "Ace" then AorAn = "an"
print YourName + " asked forFor " + AorAn + " " + WantsCard
print
text ,,fn ColorWhite
Line 659 ⟶ 673:
fn PrintViewScrollToBottom( fn WindowPrintView(1))
if RequestedCard = "X" or|| RequestedCard = "x" then fn QuitOrResumeAlert(@"EXIT")
If RequestedCard <> "" Then CardNumber = Instr$(1,cards, Ucase$(RequestedCard)): RequestCard = _true
Line 668 ⟶ 682:
print
fn PrintViewScrollToBottom( fn WindowPrintView(1))
Else if play(CardNumber) = 0 Then text ,,fn colorRed: Print "You don'//t have that card!": text ,,fn colorRed: RequestCard = _false
fn PrintViewScrollToBottom( fn WindowPrintView(1))
text,,fn ColorWhite
End If
// loopcounter should never reach 100 but just in case, prevent hangs
if loopcounter > 100 then loopcounter = 0:exit while
Wend
Line 698 ⟶ 711:
ShowHand = _false
End If
// loopcounter should never reach 100 but just in case, prevent hangs
if loopcounter > 100 then loopcounter = 0:exit while
Wend
 
Line 716 ⟶ 728:
po = 0
For i = 1 To 13
If (Computer(i) > 0) And&& (guess(i) > 0) Then poss(i) = 1: po += 1
Next i
short whilecounter
WhileCounter = 0
If po = 0
// this k is the card the CPU gets after go fish card.
while computer(k) = 0 Or asked(k)
k = rnd(12) +1
while Computer(k) = 0 || asked(k)
whilecounter ++
k = rnd(12) +1
if WhileCounter > 100 then k = 0: exit while //5/5/24 Rich added this to prevent hangs
wend
Else
// this k is the card you get after go fish
k = rnd(12) + 1
while poss(k) = 0
k = rnd(12) + 1
if WhileCounter > 100 then k = 0: exit while //5/5/24 Rich added this to prevent hangs
wend
Line 735 ⟶ 758:
end if
if k = 0 then "Loop" //5/5/24 Rich added this to prevent hangs
WantsCard = fn CheckForFaceCard(k)
if WantsCard = "j" or|| WantsCard = "J" then WantsCard = "Jack"
if WantsCard = "q" or|| WantsCard = "Q" then WantsCard = "Queen"
if WantsCard = "k" or|| WantsCard = "K" then WantsCard = "King"
if WantsCard = "a" or|| WantsCard = "A" then WantsCard = "Ace"
if WantsCard = "0" then WantsCard = "10"
Line 747 ⟶ 772:
print "-------------------------------------"
 
Print:Print Someone + " wants your " + wantsCard + "'s."
print
Line 754 ⟶ 781:
asked(k) = 1
If play(k) = 0
Line 759 ⟶ 787:
text ,,fn colorRed: Print "go fish!"
text ,,fn colorWhite:Print Someone + " got";: fn cpuGoFishing
fn CheckCPUForCompletedBook
CPUsTurn = _true
Line 778 ⟶ 807:
"Loop"
goto "Main"
 
 
handleevents
 
 
</syntaxhighlight>
 
44

edits