E. Final Program with Comments

BLACKJACK CODE WITH COMMENTS:

clc

clear

 

%These first fprintf statements explain the rules of the game and allow the

%player to know how to begin the game: Placing a bet

 

fprintf(‘Welcome to Black Jack! Get ready for a game of strategy and risk!\n’)

fprintf(‘********************************************************************\n’)

fprintf (‘Here are the rules to get you started:\n’)

fprintf (‘You will start with 2 cards displayed, each card a random number\n’)

fprintf(‘between 1 and 13. The dealer, your competition, will have one card\n’)

fprintf(‘displayed. It is your job to decide whether or not you want to take\n’)

fprintf(‘another card based on your and the dealers cards. If either you or\n’)

fprintf(‘the dealer have a sum of your cards that equal more than 21, that\n’)

fprintf(‘player loses!\n’)

fprintf(‘If you both you and the dealer bust, you get the money!\n’)

fprintf(‘********************************************************************\n’)

fprintf(‘The object of the game is to have a sum of cards greater than the\n’)

fprintf(‘other player, but less than or equal to 21. The person closest wins!\n’)

fprintf(‘********************************************************************\n’)

fprintf(‘Note: Both you and the dealer only have access to the first four cards\n’)

fprintf(‘of the deck!\n’)

fprintf(‘********************************************************************\n’)

fprintf(‘Hello Player! Please place a bet. (Min $5 Max $100.) \n’)

fprintf(‘Note: Please enter without dollar sign!\n’)

fprintf(‘********************************************************************\n’)

Bet= input(“Input Bet: “)

 

%The cards in the “deck” need to be shuffled in order to ensure that the

%player is receiving a randomized set of cards each time they play. 

%This is done by shuffling each set of 1-13 four times, in order to

%represent the four suits in a deck. 

 

%Dealer’s Deck

D1=[1:13];

D2=[1:13];

D3=[1:13];

D4=[1:13];

ShuffledRow1=randperm(length(D1));

ShuffledRow2=randperm(length(D2));

ShuffledRow3=randperm(length(D3));

ShuffledRow4=randperm(length(D4));

%The shuffled deck is organized into a vector of four rows that can be used

%throughout the play of the game:

ShuffledDeck=[ShuffledRow1;ShuffledRow2;ShuffledRow3;ShuffledRow4];

 

 

%The cards that are in the hand of the player are determined by the first

%card in each of the shuffled rows of the shuffled deck vector. 

%Each “Card” Value displays all cards in the player’s hand up to that point

%in the game. 

 

%Hand of the player:

Hand=ShuffledDeck(1:4,1);

Card1=Hand(1);

Card2=Hand(1:2)

Card3=Hand(1:3);

Card4=Hand(1:4);

 

%The sum of the player’s cards is displayed so the player knows how close

%they are to the ideal value of 21. 

 

Sum_of_Cards=sum(Card2)

 

%Another deck is used for the dealer. It is created and displayed in the

%same way that the player’s deck is.

 

%Dealer Deck Shuffled and Defined:

 

D1b=[1:13];

D2b=[1:13];

D3b=[1:13];

D4b=[1:13];

ShuffledRow1b=randperm(length(D1b));

ShuffledRow2b=randperm(length(D2b));

ShuffledRow3b=randperm(length(D3b));

ShuffledRow4b=randperm(length(D4b));

 

%Deck is Shuffled:

ShuffledDeckb=[ShuffledRow1b;ShuffledRow2b;ShuffledRow3b;ShuffledRow4b];

 

%The hand of the dealer is organized and displayed in the same way that the

%hand of the player is.

 

%Hand of the dealer:

Dealer_Hand=ShuffledDeckb(1:4,1);

Dealer_Card1=Dealer_Hand(1)

Dealer_Card2=Dealer_Hand(1:2);

Dealer_Card3=Dealer_Hand(1:3);

Dealer_Card4=Dealer_Hand(1:4);

 

DealerHand=[Dealer_Card1, Dealer_Card2(2), Dealer_Card3(3), Dealer_Card4(4)];

 

%The first card of the dealer’s hand is displayed so that the player can

%judge their own move based on the card number of the dealer. 

 

Dealer_Sum_of_Cards=sum(DealerHand(1));

 

%In order for the code to run correctly, the following variables need to be

%defined early in the code. Their values are meaningless in the play of the

%game, and are set to low values that will not affect the results. 

 

Dealer_Sum_of_Cards_Hit1=1;

Dealer_Sum_of_Cards_Hit2=1;

 

%There are now a series of loops that the player must go through to take

%their turn. 

 

%Player Plays:

 

%If the sum of the player’s cards is equal to or greater than 21, the

%player cannot take any more hits from their available cards. These outputs

%are expressed in the following loops.

 

%The A and W values are assigned based on the outcome and will be utilized

%in the “results loop” later in the code in order to properly display the

%final result of the game. 

 

if Sum_of_Cards==21

    Player=2;

    W=5;

    A=10;

    %PLAYER WINS (Unless Dealer Ties)

elseif Sum_of_Cards>21

    Player=2;

    W=2;

    A=7;

    %Player will lose unless the dealer also busts.

else

    A=1;

    Player=input(‘Do you want another card? Input 1 for Yes or 2 for No.\n’)

    %Otherwise the game continues, and the player decides whether to take

    %another card from their hand based on the current sum of their cards

    %and the dealer’s card.

end

 

%Player goes through rounds of hit or stand based on their input value.

 

%Round 1:

 

if A==1;

if Player==1

   Card3

   Sum_of_Cards=sum(Card3)

   A=6;

   %The next card is displayed along with the current sum of the player’s

   %hand. 

else 

    Sum_of_Cards=Card2

    Sum_of_Cards=sum(Card2)

    A=5;

    %Otherwise the player keeps the sum that they obtained from their input

    %value. 

end

end

 

%If the player’s sum of cards is still under 21, they will be given the

%option to take the final card from their hand. 

 

%Round 2:

 

if A~=5 && A~=10 && A~=7

    if Sum_of_Cards<21;

        Player=input(‘Do you want to continue? Input 1 for Yes or 2 for No.\n’)

        if Player==1

            Card4

            Sum_of_Cards=sum(Card4)

            %If they choose to take another card, the sum of all four cards

            %in their hand will be displayed. 

        else

        Card3

        Sum_of_Cards=sum(Card3)

        A=5;

        %Otherwise they will keep the sum of their cards from the first

        %round.

        end

    elseif Sum_of_Cards==21

        W=5;

        %If the sum of their cards from the first round is equal to 21, the

        %player will win the game unless the dealer ties with them. 

    else

        W=2;

        A=7;

    end

end

 

%Now the dealer proceeds with their turn and makes decisions based on the

%sum of the player’s cards.

 

%The dealer turns their other card over, and both cards are displayed:

 

Dealer_Card2

 

%The dealer’s hand from the shuffled deck is defined and the sum of their

%cards after the second is turned over is displayed for the player to see:

 

DealerHand=[Dealer_Card1, Dealer_Card2(2), Dealer_Card3(3), Dealer_Card4(4)];

Dealer_Sum_of_Cards=sum(Dealer_Card2)

 

%The following values need to be defined in order for the code to run, but

%their values are meaningless to the play of the game, and will not have an

%affect on the final results:

 

Dealer_Sum_of_Cards_Hit1=30;

Dealer_Sum_of_Cards_Hit2=30;

 

%The dealer now goes through rounds similar to those of the player’s based

%off of the final sum of cards of the player.

 

%Dealer Round 1

 

if A~=7

if Dealer_Sum_of_Cards<Sum_of_Cards && Dealer_Sum_of_Cards<21

    Dealer_Sum_of_Cards_Hit1=sum(DealerHand(1:3))

    A=6;

    W=6;

    %If the dealer’s sum of cards are less than the player’s and still less

    %than 21, they get another card to add to their sum. A and W values are

    %displayed to direct the code to the next round. 

elseif Dealer_Sum_of_Cards>Sum_of_Cards && Dealer_Sum_of_Cards<22

    W=1;

    A=5;

    %If the sum of the dealer’s cards is greater than the player’s and less

    %than 21, the dealer wins, and the A and W values are set to move to

    %the results loop.

elseif Dealer_Sum_of_Cards>21

    W=3;

    %If the dealer’s sum of cards is above 21, they bust and lose the game.

elseif Dealer_Sum_of_Cards==Sum_of_Cards

    W=4;

    A=6;

    %If the dealer and the player have the same sum of cards, there is a

    %tie. 

elseif Dealer_Sum_of_Cards==21

    W=1;

    %If the dealer hits 21 exactly with their sum, they win the game,

    %unless the player also has 21. In that case, it is a tie. 

end

end

 

%Dealer Round 2:

 

if A~=7

    if Dealer_Sum_of_Cards_Hit1<21 && Dealer_Sum_of_Cards_Hit1<=Sum_of_Cards

if Dealer_Sum_of_Cards<Sum_of_Cards && Dealer_Sum_of_Cards<21

    Dealer_Sum_of_Cards_Hit2=sum(DealerHand(1:4))

    A=6;

    W=6;

    %If the dealer’s cards are less than the players and they are still

    %below 21, the dealer will get their final card added to their sum. 

elseif Dealer_Sum_of_Cards>Sum_of_Cards && Dealer_Sum_of_Cards<22

    W=1;

    A=5;

    Dealer_Sum_of_Cards_Hit2=1;

    %In this case, the dealer’s sum of cards is greater than the player’s

    %and less than or equal to 21, meaning that the dealer wins. The sum of

    %cards needs to be redefined as a value less than 21, or it will move

    %to the wrong results loop. 

elseif Dealer_Sum_of_Cards>21

    W=3;

    %Here the dealer goes above 21 and busts.

elseif Dealer_Sum_of_Cards==Sum_of_Cards

    W=4;

    %This W value indicates a tie. 

elseif Dealer_Sum_of_Cards==21

    W=1;

    %This indicates that the dealer reached 21 and won the game, unless

    %there was a tie. 

end

    end

end

 

%This loop sets the W values equal to numbers that correspond with

%the results loop in order to properly display the results based off of the

%sum of the dealer’s cards.

 

if A==6

    if Dealer_Sum_of_Cards_Hit1>21

        W=3;

    else

    end

end

 

if A==6

    if Dealer_Sum_of_Cards_Hit2>21

        W=3;

    else

    end

end

 

if A==6

    if Dealer_Sum_of_Cards_Hit2<21 && Dealer_Sum_of_Cards<Sum_of_Cards

        W=5;

    else

    end

end

 

%Based on the W values set throughout the code, the results loop will

%display the final results of the game.

 

%RESULTS LOOP:

 

if W==5

    %This is if player=21, or player>dealer but <21

     fprintf(‘Player Wins!! \n’)

     %Bet results shown:

     Final_Money_Value=2*sum(Bet)

     %The player’s money doubles

elseif W==4

    %This is if player=dealer

        fprintf(‘There has been a tie! \n’)

        fprintf(‘You kept your bet, but you did not make any moo-la! \n’)

        %Bet results shown:

        Final_Money_Value=sum(Bet)

        %The player’s money stays the same

elseif W==3

    %This is if the dealer busts

        fprintf(‘Dealer Busts! Player Wins! You Doubled your money!\n’)

        %Bet results shown:

        Final_Money_Value=2*sum(Bet)

        %The player’s money doubles

elseif W==2

    %This is if player busts

    fprintf(‘Bust! Player loses 🙁 \n’)

    %Bet results shown:

    Final_Money_Value=0-sum(Bet)

    %The player loses all of their money

else

    %This is if the dealer>player but <21

         fprintf(‘Dealer Wins! Better luck next time!\n’)

         %Bet results shown:

         Final_Money_Value=0-sum(Bet)

         %The player loses all of their money

end

 

Connect Four code:

%Connect Four

clc

clear

fprintf(‘Welcome Players!\n\n’)

x=input(‘Does all players know the instructions\n of Connect4:Group O Edition? and how to play?\n 1=YES 2=NO ‘)

if x==1;

    fprintf(‘Let the Games Begin Players!\n\n’)

else fprintf(‘Each player has a number of “circle”-type playing pieces.\n Players must double click on selected spot twice in order to input their color piece. Player1 piece is any other color than Player2; the same is for Player2.\n The two players take turns putting a playing piece into a vertical column of their choice.\n The pieces must first be placed in the bottom row before stacking,one above the other, as traditional connect four.\n In this way, the columns start to fill up. The object is to be the first player to connect four of your pieces.\n The four pieces may be lined up horizontally, vertically, or diagonally.\n When more than one game is played, players alternate taking the first turn. Players must keep track of their wins as well.\n\n\n’)

fprintf(‘Let the Games Begin Players!\n\n’)

end

%Turns the graph into a circle grid

circle=linspace(0,2*pi,50);

circle1=0.431*cos(circle);

circle2=0.431*sin(circle);

%Dimensions of the board

RW=6;

%RW=Row

ClM=7;

%ClM=column

grid=zeros(ClM,RW);

a=1:100;

b=1:100;

l=1;

abs=zeros(ClM,RW);

%Moving the pieces

end

[a,b] = meshgrid(a,b);

for x=1:ClM;

    for y=1:RW;

        grid(x,y)=patch(x+circle1,y+circle2,[1 1 0]);

    end

end

%Title of figure 1

title(‘”Connect Four: Wingineers Edition”‘)

%This functions removes axises numbers

set(gca,’YTick’,[])

set(gca,’XTick’,[])

grid on

z=100;

for x=1:z;

    disp(‘Player1 Move’)

    [a,b]=ginput(1);

    disp(‘Player2 Move’)

    [a,b]=ginput(1);

    a=round(a);

    b=round(b);

    abs(a,b)==1;

%Allows User to Change their Color Piece

    ColorOD=get(gca,’ColorOrder’);

    customColor=uisetcolor;

    NC=[‘b’;’r’];

    for s=1:50;

     if abs(a,b)>=-1000;

           set(grid(a,b),’FaceColor’,[customColor]);

            colororder(NC)

     end

       

            break

            set(grid(a,b),’FaceColor’,[0 0 1]);

            if abs(a,b)>=-1000;

               continue

             if abs(a,b)>=-1000

                w=[‘r’;’b’]

                set(grid(a,b),’color’,’w’);

            abs(a,b)>=0;

            if abs(a,b)>=0;

                set(grid(a,b),’color’,’w’);

                set(grid(a,b),’FaceColor’,[0 0 1]);

                abs(a,b)>=-1000;

            set(grid(a,b),’FaceColor’,[1 0 0]);

            abs(a,b)=1;

            end

            end

            end

     end

    end