Final Program with Comments

Black Jack:

clc
clear
close all
quit = false;
fprintf(‘Blackjack\nB-Gang\n\nHouse Rules:\nBlackjack is autowin for player\nCannot play an ace high if it exceeds 21\nDealer will never stand while losing\nDealer will always take ties\n\n’);
yeet = input(‘Enter a number to play\n’);
while (~quit)%Play game as many times without killing program
clc
clear
close all

% Initialize scene
my_scene = simpleGameEngine(‘retro_cards.png’,16,16,8,[0,100,0]);

% Set up variables to name the various sprites
empty_sprite = 1;
blank_card_sprite = 2;
card_sprites = 21:72;

card_display = empty_sprite * ones(3,5);
face_display = empty_sprite * ones(3,5);
playerTotal = 0;
dealerTotal = 0;
card_display = empty_sprite * ones(3,5);
ShuffledDeck = randperm(52);%Shuffles deck
playerHand(1) = card_sprites(ShuffledDeck(1));%deals inital cards
dealerHand(1) = card_sprites(ShuffledDeck(2));
playerHand(2) = card_sprites(ShuffledDeck(3));
dealerHand(2) = card_sprites(ShuffledDeck(4));
card_display(1,1) = 3;%shows dealers first card as flipped over
face_display(1,1) = blank_card_sprite;
face_display(1,2) = blank_card_sprite;
face_display(3,1) = blank_card_sprite;
face_display(3,2) = blank_card_sprite;
card_display(1,2) = dealerHand(2);
card_display(3,1) = playerHand(1);
card_display(3,2) = playerHand(2);
deckFlag = 5;%how many cards in the game is
playerFlag = 3;
dealerFlag = 3;
drawScene(my_scene,face_display,card_display)
title(‘Dealer Hand’);
xlabel(‘Player Hand’);
playerTurn = true;
playerWin = false;
while(playerTurn)%loop to see if player holds or hits
playerTotal = total(playerHand);%cal total
if (checkAce(playerHand) && playerTotal+10<22)%players hand if ace and under 21
fprintf(‘Player total: %i or %i\n’, playerTotal, playerTotal+10);
if (playerTotal+10==21 && (playerHand(1)==37 || playerHand(2)==37 || playerHand(1)==50 || playerHand(2)==50))%checks for blackjack
fprintf(‘!!!BLACKJACK!!!\n\n’);
playerWin = true;
break;
end
else
fprintf(‘Player total: %i\n’, playerTotal);
end
if (playerTotal>21)%checks if player busts
fprintf(‘PLAYER BUST\n\n’);
break;
end
choice = input(‘What do you want to do Hit (1) or Stand (2)?\n’);
if (choice == 1)%if player hits
playerHand(playerFlag) = card_sprites(ShuffledDeck(deckFlag));%gets next card in deck to next pos in player hand
face_display(3,playerFlag) = blank_card_sprite;
card_display(3,playerFlag) = playerHand(playerFlag);
drawScene(my_scene,face_display,card_display)%displays it
playerFlag = playerFlag + 1;%moves next flags for deck and hand
deckFlag = deckFlag + 1;
elseif (choice == 2)
playerTurn = false;%checks if player holds then changes turns
if (checkAce(playerHand))%checks if hand has an ace and if its okay to play it high
if (playerTotal+10<22)
playerTotal = playerTotal + 10;
end
end
else
fprintf(‘Choice not valid\n’);
end
end
dealerTurn = true;
while (dealerTurn && ~playerWin)%makes sure player hasnt won yet with a blackjack
dealerTotal = total(dealerHand);
if (dealerTotal>21)%checks if dealer busts
card_display(1,1) = dealerHand(1);
fprintf(‘DEALER BUST\n\n’);
playerWin = true;
drawScene(my_scene,face_display,card_display)
break;
end
if (playerTotal>21)%sees if player already bust
card_display(1,1) = dealerHand(1);%flips first card
dealerTurn = false;
playerWin = false;
elseif (dealerTotal+10<=21 && dealerTotal+10>=playerTotal && checkAce(dealerHand))%checks if hand is better than players with ace
card_display(1,1) = dealerHand(1);%flips first card
dealerTotal = dealerTotal + 10;
dealerTurn = false;
playerWin = false;
elseif (dealerTotal<=21 && dealerTotal>=playerTotal)%checks to see if hand without ace is better than players
card_display(1,1) = dealerHand(1);
dealerTurn = false;
playerWin = false;
else %if player is winning, deal will hit
dealerHand(dealerFlag) = card_sprites(ShuffledDeck(deckFlag));%next card in deck goes to next spot in hand
face_display(1,dealerFlag) = blank_card_sprite;
card_display(1,dealerFlag) = dealerHand(dealerFlag);%displays it
drawScene(my_scene,face_display,card_display)
dealerFlag = dealerFlag + 1;%moves hand and deck flags
deckFlag = deckFlag + 1;
end
drawScene(my_scene,face_display,card_display)
end
if (playerTotal == dealerTotal)%checks for a tie
fprintf(‘TIE, PUSH\nPlayer total: %i\nDealer total: %i\n’, playerTotal, dealerTotal);
elseif(playerWin)%checks if player won
fprintf(‘PLAYER WINS\nPlayer total: %i\nDealer total: %i\n’, playerTotal, dealerTotal);
else %else the dealer wins
fprintf(‘DEALER WINS\nPlayer total: %i\nDealer total: %i\n’, playerTotal, dealerTotal);
end
while (true)%keeps on asking if you want to play again untill a legal response
choice2 = input(‘Do you want to play again? Yes (1) No (2)\n’);
if (choice2==1)
quit = false;
break;
elseif(choice2==2)
quit = true;
break;
else
fprintf(‘Invalid Choice’);
end
end
end

function x = total(hand)%calculates total from an array of cards
x = 0;
for i=1:size(hand,2)
if (rem((hand(i)-21), 13) < 10)
x = x + rem((hand(i)-21),13) + 1;
else
x = x + 10;
end
end
end

function y = checkAce(hand)%checks hand if theres any aces
y = false;
for i = 1:size(hand,2)
if (rem((hand(i)-21), 13) == 0)
y = true;
end
end
end

Connect 4:

clc
clear
%sets up board and variables
board = simpleGameEngine(‘ConnectFour.png’,86,101);

empty = 1;
red = 2;
black = 3;

board_display = empty * ones(6,7);
drawScene(board,board_display)

winner = false;
first = -1;
fprintf(‘Connect Four\nB-Gang\n\n\nWelcome to Connect Four! Choose what color goes first!\n’);
while (first == -1)%asks who goes first till legal response
first = input(‘Red (1) or Black (2): ‘);
if (first==1)
redTurn = true;
elseif (first==2)
redTurn = false;
else
fprintf(‘\nInvalid Choice\n’);
first = -1;
end
end
while (~winner)%loops through each players turn till winner
if (redTurn)
fprintf(‘Player Red turn\n’);
current = 2;%sets current chip to red
redTurn = false;
else
fprintf(‘Player Black turn\n’);
current = 3;%sets current chip to black
redTurn = true;
end
choice = input(‘What column would you like to put your piece? (1-7): ‘);
while(true)
if ((choice < 1 || choice > 7) || board_display(1,choice) ~= 1 || floor(choice) ~= choice)
if (choice < 1 || choice > 7)%checks if collum is between 1 and 7
fprintf(‘Invalid Input\n’);
else %checks if collum is full
fprintf(‘Column Full\n’);
end
choice = input(‘What column would you like to put your piece? (1-7): ‘);
else
break;
end
end
for i=1:7 %checks collum selected for lowest open spot
if (board_display(7-i,choice)==1)
board_display(7-i,choice) = current;
drawScene(board,board_display)
break;
end
end
temp = 0;%sets in a row to zero
for j=7-i:10-i%checks for vertical win from last spot
if (10-i>6)%stops if too low
break;
end
if(board_display(j, choice)==current)
temp=temp+1;
else
temp=0;%sets in a row to zero
end
if (temp==4)%four in a row vertically
winner = true;
break;
end
end
temp = 0;%sets in a row to zero
for k=choice-3:choice+3%checks if win horizontally
if (k<1 || k>7)%if out of bounds skip
continue;
end
if (board_display(7-i,k)==current)
temp=temp + 1;
else
temp = 0;%sets in a row to zero
end
if (temp==4)%four in a row horizonally
winner=true;
break;
end
end
temp = 0;%sets in a row to zero
for l=choice-3:choice+3%checks for win vertically top right to bottom left
if (l<1 || l>7)%if out of bounds skip
continue;
end
if (7-i-(l-choice)>6 || 7-i-(l-choice)<1)%if out skip
continue;
end
if (board_display(7-i-(l-choice),l)==current)
temp = temp + 1;
else
temp = 0;%sets in a row to zero
end
if (temp==4)%four in a row vertically
winner=true;
break;
end
end
temp = 0;%sets in a row to zero
for m=choice-3:choice+3%checks for win vertically bottom right to top left
if (m<1 || m>7)%if out skip
continue;
end
if (7-i+(m-choice)>6 || 7-i+(m-choice)<1)%if out skip
continue;
end
if (board_display(7-i+(m-choice),m)==current)
temp = temp + 1;
else
temp = 0;%sets in a row to zero
end
if (temp==4)%four in a row vertically
winner=true;
break;
end
end

for n=1:7%checks if entire board if full for tie
if (board_display(1,n)==empty)
break;
end
if (n==7)
winner=true;
current=-1;
end
end

if (winner == true)
if (current == 2)%checks if winner is red
redWinner = true;
else%else is black
redWinner = false;
end
end
end
if (current==-1)%checks for tie
fprintf(‘Tie game\n’);
else
if (redWinner)%else whoever won above
fprintf(‘Red wins\n’);
else
fprintf(‘Black wins\n’);
end
end