Go Fish/Mathematica: Difference between revisions
< Go Fish
Content added Content deleted
(Added Mathematica) |
(Fixed Mathematica + Description) |
||
Line 1: | Line 1: | ||
{{collection|Go Fish}} |
{{collection|Go Fish}} |
||
The AI simply picks the rank that it has the most of. |
|||
<lang Mathematica>ranks = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", |
<lang Mathematica>ranks = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", |
||
"Eight", "Nine", "Ten", "Jack", "Queen", "King"}; |
"Eight", "Nine", "Ten", "Jack", "Queen", "King"}; |
||
Line 22: | Line 23: | ||
playerTurn = True; |
playerTurn = True; |
||
While[player != {} || opp != {} || deck != {}, |
While[player != {} || opp != {} || deck != {}, |
||
If[player == {}, drawCards[ |
If[player == {}, drawCards[1, deck, player]]; |
||
If[opp == {}, drawCards[ |
If[opp == {}, drawCards[1, deck, opp]]; |
||
If[playerTurn, |
If[playerTurn, |
||
If[player == {}, |
|||
DialogInput[ |
|||
⚫ | |||
⚫ | |||
" |
"You have no cards and the deck is empty!", |
||
⚫ | |||
⚫ | |||
Module[{choice = |
|||
⚫ | |||
ChoiceDialog[ |
|||
⚫ | |||
"Your opponent has " <> IntegerString[oppBooks] <> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
ranks[[#]] -> # & /@ Union[First /@ player]]}, |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
playerTurn = False]], |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
DialogNotebook[{TextCell["Go fish! You gained a card.", |
|||
⚫ | |||
FontFamily -> "Arial"], DefaultButton[]}]]; |
|||
⚫ | |||
playerTurn = |
playerTurn = False]]], |
||
If[opp != {}, |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
playerTurn = True]]]]; {player, playerBooks} = |
|||
fixHand[player, playerBooks]; {opp, oppBooks} = |
fixHand[player, playerBooks]; {opp, oppBooks} = |
||
fixHand[opp, oppBooks]]; MessageDialog[ |
fixHand[opp, oppBooks]]; MessageDialog[ |
Revision as of 21:05, 1 May 2015
Go Fish/Mathematica is part of Go Fish. You may find other members of Go Fish at Category:Go Fish.
The AI simply picks the rank that it has the most of. <lang Mathematica>ranks = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King"};
suits = {"Clubs", "Diamonds", "Hearts", "Spades"}; deck = Tuples[{Range[13], Range[4]}]; cardName[{rank_, suit_}] := ranksrank <> " of " <> suitssuit; Attributes[drawCards] = {HoldRest}; drawCards[num_, src_, dest_] :=
Module[{cards = RandomSample[src, Min[num, Length[src]]]}, dest = Join[dest, cards]; src = Fold[DeleteCases[#, #2, 1, Count[cards, #2]] &, src, Union[cards]];];
fixHand[{a___, {rank_, _}, b___, {rank_, _}, c___, {rank_, _},
d___, {rank_, _}, e___}, prev_] := fixHand[{a, b, c, d, e}, prev + 1];
fixHand[hand_, prev_] := {hand, prev}; player = opp = {}; drawCards[9, deck, player]; drawCards[9, deck, opp]; {player, playerBooks} = fixHand[player, 0]; {opp, oppBooks} = fixHand[opp, 0]; playerTurn = True; While[player != {} || opp != {} || deck != {},
If[player == {}, drawCards[1, deck, player]]; If[opp == {}, drawCards[1, deck, opp]]; If[playerTurn, If[player == {}, DialogInput[ DialogNotebook[{TextCell[ "You have no cards and the deck is empty!", FontFamily -> "Arial"], DefaultButton[]}]], Module[{choice = ChoiceDialog[ "Your opponent has " <> IntegerString[oppBooks] <> " books, and you have " <> IntegerString[playerBooks] <> " books and these cards: " <> StringRiffle[cardName /@ SortBy[player, #1 + #2/10 &], ", "] <> "\nWhich rank will you call?", ranks# -> # & /@ Union[First /@ player]]}, If[MemberQ[opp, {choice, _}], player = Join[player, Cases[opp, {choice, _}]]; opp = DeleteCases[opp, {choice, _}], drawCards[1, deck, player]; DialogInput[ DialogNotebook[{TextCell["Go fish! You gained a card.", FontFamily -> "Arial"], DefaultButton[]}]]; playerTurn = False]]], If[opp != {}, Module[{choice = Commonest[First /@ opp, 1]1}, If[MemberQ[player, {choice, _}], opp = Join[opp, Cases[player, {choice, _}]]; player = DeleteCases[player, {choice, _}], playerTurn = True]]]]; {player, playerBooks} = fixHand[player, playerBooks]; {opp, oppBooks} = fixHand[opp, oppBooks]]; MessageDialog[ Which[oppBooks > playerBooks, "Your opponent wins with " <> IntegerString[oppBooks] <> " books!", playerBooks > oppBooks, "You win with " <> IntegerString[playerBooks] <> " books!", True, "Tie!"]];</lang>