Go Fish/Mathematica: Difference between revisions

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[deck, player, 1]];
If[player == {}, drawCards[1, deck, player]];
If[opp == {}, drawCards[deck, opp, 1]];
If[opp == {}, drawCards[1, deck, opp]];
If[playerTurn,
If[playerTurn,
Module[{choice =
If[player == {},
ChoiceDialog[
DialogInput[
DialogNotebook[{TextCell[
"Your opponent has " <> IntegerString[oppBooks] <>
" books, and you have " <> IntegerString[playerBooks] <>
"You have no cards and the deck is empty!",
FontFamily -> "Arial"], DefaultButton[]}]],
" books and these cards: " <>
Module[{choice =
StringRiffle[cardName /@ SortBy[player, #[[1]] + #[[2]]/10 &],
ChoiceDialog[
", "] <> "\nWhich rank will you call?",
ranks[[#]] -> # & /@ Range[13]]},
"Your opponent has " <> IntegerString[oppBooks] <>
" books, and you have " <> IntegerString[playerBooks] <>
If[MemberQ[opp, {choice, _}],
" books and these cards: " <>
player = Join[player, Cases[opp, {choice, _}]];
StringRiffle[cardName /@ SortBy[player, #[[1]] + #[[2]]/10 &],
opp = DeleteCases[opp, {choice, _}], drawCards[1, deck, player];
", "] <> "\nWhich rank will you call?",
DialogInput[
ranks[[#]] -> # & /@ Union[First /@ player]]},
DialogNotebook[{TextCell["Go fish! You gained a card.",
If[MemberQ[opp, {choice, _}],
FontFamily -> "Arial"], DefaultButton[]}]];
player = Join[player, Cases[opp, {choice, _}]];
playerTurn = False]],
opp = DeleteCases[opp, {choice, _}], drawCards[1, deck, player];
Module[{choice = Commonest[First /@ opp, 1][[1]]},
DialogInput[
If[MemberQ[player, {choice, _}],
DialogNotebook[{TextCell["Go fish! You gained a card.",
opp = Join[opp, Cases[player, {choice, _}]];
FontFamily -> "Arial"], DefaultButton[]}]];
player = DeleteCases[player, {choice, _}],
playerTurn = True]]]; {player, playerBooks} =
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[player, playerBooks]; {opp, oppBooks} =
fixHand[opp, oppBooks]]; MessageDialog[
fixHand[opp, oppBooks]]; MessageDialog[