Sample hand:
Script uses sprite file:
Code:
clc
clear
% Create the scene using simpleGameEngine
gameScene = simpleGameEngine(‘retro_cards.png’, 16, 16, 15, [255, 255, 255]);
% Create the cards based on their locations in the sprite file
cards = zeros(1, 52);
for i=1:length(cards)
cards(i) = i + 20;
end
% Create the deck
nums = [1 2 3 4 5 6 7 8 9 10 11 12 13];
deck = [nums nums nums nums];
% Random order of cards that players will draw from
shuffled_cards = randperm(52) + 20;
% Deal hands to player and opponent
player_hand = shuffled_cards(1:7);
computer_hand = shuffled_cards(8:15);
% Keep track of next card to be drawn
next_card = 16;
% Display the scene
drawScene(gameScene, player_hand);
% Keep track of points
playerpoints = 0;
computerpoints = 0;
% Check if player has 4 of a kind and remove from hand if so
for i = 1:length(player_hand)-1
count = 0;
for j = i+1:length(player_hand)
if (count ~= 3)
if (deck(player_hand(i) – 20) == deck(player_hand(j) – 20))
count = count + 1;
if count == 3
num = deck(player_hand(i)-20);
indexofnum = find(deck(player_hand-20) == num);
player_hand(indexofnum) = [];
playerpoints = playerpoints + 1;
fprintf(‘\n You got 4 of a kind of %d!\n’, num);
end
end
end
end
end
% Check if opponent has 4 of a kind and remove from hand if so
for i = 1:length(computer_hand)-1
count = 0;
for j = i+1:length(computer_hand)
if (deck(computer_hand(i) – 20) == deck(computer_hand(j) – 20))
count = count + 1;
if count == 3
num = deck(computer_hand(i)-20);
indexofnum = find(deck(computer_hand-20) == num);
computer_hand(indexofnum) = [];
computerpoints = computerpoints + 1;
fprintf(‘\n Computer got 4 of a kind of %d! \n’, num);
end
end
end
end
% Run game
while (~isempty(player_hand) && ~isempty(computer_hand))
% Verify that player hand is not empty
if ~isempty(player_hand)
turn = true;
while turn && ~isempty(computer_hand) && ~isempty(player_hand)
guess = input(‘\nWhat card do you want to ask for (represented as a number)? ‘);
if guess ~= deck(computer_hand – 20)
fprintf(‘\n Player Go Fish!\n’);
pause(1);
% Deal card to player
player_hand(length(player_hand) + 1) = shuffled_cards(next_card);
next_card = next_card + 1;
turn = false;
else
fprintf(‘\n You got cards from your opponent! \n’)
pause(1);
for i = 1 : length(computer_hand)
if guess == deck(computer_hand(i) – 20)
player_hand(length(player_hand) + 1) = computer_hand(i);
end
end
x = find((deck(computer_hand-20)) == guess);
computer_hand(x) = [];
drawScene(gameScene, player_hand);
end
drawScene(gameScene, player_hand);
% Check for 4 of a kind
for i = 1:length(player_hand)-1
count = 0;
for j = i + 1:length(player_hand)
if (deck(player_hand(i)-20) == deck(player_hand(j)-20))
count = count + 1;
if count == 3
num = deck(player_hand(i)-20);
indexofnum = find(deck(player_hand-20) == num);
player_hand(indexofnum) = [];
playerpoints = playerpoints + 1;
fprintf(‘\n You got 4 of a kind of %d!\n’, num);
drawScene(gameScene, player_hand);
end
end
end
end
end
fprintf(‘\nYour score is: %d\nComputer score is: %d\n’, playerpoints, computerpoints);
end
pause(1);
% Verify that computer hand is not empty
if ~isempty(computer_hand)
turn = true;
while turn && ~isempty(computer_hand) && ~isempty(player_hand)
computerguess = deck(computer_hand(randi(length(computer_hand), 1))-20);
fprintf(‘\nComputer asked for %d\n’, computerguess);
pause(1);
if computerguess ~= deck(player_hand-20)
fprintf(‘\n Computer Go Fish!\n’)
pause(1);
computer_hand(length(computer_hand)+1) = shuffled_cards(next_card);
next_card = next_card+1;
turn = false;
else
fprintf(‘\n Your opponent got cards from you! \n’)
pause(1);
for i = 1 : length(player_hand)
if computerguess == deck(player_hand(i) – 20)
computer_hand(length(computer_hand) + 1) = player_hand(i);
end
end
x = find((deck(player_hand-20)) == computerguess);
player_hand(x) = [];
drawScene(gameScene, player_hand);
end
% Check for 4 of a kind
for i = 1:length(computer_hand)-1
count = 0;
for j = i+1:length(computer_hand)
if (deck(computer_hand(i)-20) == deck(computer_hand(j)-20))
count = count + 1;
if count == 3
num = deck(computer_hand(i) – 20);
indexofnum = find((deck(computer_hand – 20)) == num);
computer_hand(indexofnum) = [];
computerpoints = computerpoints + 1;
fprintf(‘\n Computer got 4 of a kind of %d!\n’, num);
end
end
end
end
end
end
drawScene(gameScene, player_hand);
fprintf(‘Your score is: %d\nComputer score is: %d\n’, playerpoints, computerpoints);
end
if playerpoints > computerpoints
fprintf(‘\nYou won!\n’);
elseif playerpoints < computerpoints
fprintf(‘\nYou lost!\n’);
else
fprintf(‘You tied!’);
end