Go Fish/Mathematica: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Mathematica)
 
m (Fixed syntax highlighting.)
 
(One intermediate revision by one other user not shown)
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",
<syntaxhighlight lang="mathematica">ranks = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King"};
"Eight", "Nine", "Ten", "Jack", "Queen", "King"};
suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
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[
Line 51: Line 58:
playerBooks > oppBooks,
playerBooks > oppBooks,
"You win with " <> IntegerString[playerBooks] <> " books!", True,
"You win with " <> IntegerString[playerBooks] <> " books!", True,
"Tie!"]];</lang>
"Tie!"]];</syntaxhighlight>

Latest revision as of 08:27, 31 August 2022

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.

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_}] := ranks[[rank]] <> " of " <> suits[[suit]];
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!"]];