E. Final Program with Comments

Connect 4 Final Program

%%Connect 4 Game Program

%Load the Connect 4 data
load(‘Connect.mat’);

%Display a blank Connect 4 board and dock the image to play game
imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}])
fprintf(‘\nWelcome to Connect 4!’)
fprintf(‘\n’)

%Start the game by inputing which player goes first, Player 1 will be the
%redchip, Player 2 will be the blackchip
player=input(‘Type 2 to start playing;’);

%Initialize a while loop that goes until the board is full or a player has
%won. Also create a zeros matrix for both players that stores their moves.

%Within the while loop, create an if-else statement that prompts the player
%to input their turn, displays their chip on the board, and checks the
%board for all possible win combinations after every turn.

%Create another if-else statement within the first if-else statement to
%display the winner if the situation occurs.

Player1=zeros(6:7);
Player2=zeros(6:7);
turn=42;
sumPlayer1=0;
sumPlayer2=0;
while turn~=0
%sumPlayer# is the zeroes board, turn is counting down the amount of
%turns left until the board fills up
sumPlayer1=0
sumPlayer2=0
turn=turn-1;
%The if/else statement assigns a color chip based on whether the turn is
%even or odd.
if mod(player,2)==0
player=1
%This is where the player will enter their move, make sure you read
%the directions well!
fprintf(‘\nRow 6 refers to the bottommost row and row 1 refers to the top-most row.’)
fprintf(‘\n’)
fprintf(‘\nColumn 1 refers to the left-most column and column 7 refers to the right-most column.’)
fprintf(‘\n’)
I=input(‘Input the row you want to place your chip in as # note that the chip should be placed in the bottommost row, or in the row above another chip:’)
J=input(‘Input the column you want to place your chip in as #:’)
Board{I,J}=redchip
imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}]);
%After displaying their turn onto the actual board, the program
%inputs their move into the zeros board we created in the
%beginning and assigns it a value of 1
positionx=I;
positiony=J;
Player2(positionx,positiony)=1
else
player=2
fprintf(‘\nRow 6 refers to the bottommost row and row 1 refers to the top-most row.’)
fprintf(‘\n’)
fprintf(‘\nColumn 1 refers to the left-most column and column 7 refers to the right-most column.’)
fprintf(‘\n’)
x=input(‘Input the row you want to place your chip in as # note that the chip should be placed in the bottommost row, or in the row above another chip:’)
y=input(‘Input the column you want to place your chip in as #:’)
Board{x,y}=blackchip
imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}]);
postionx=x;
positiony=y;
Player1(positionx,positiony)=1
end

%%Player 2 Winning Check
%Horizontal Win
for k=1:6 %The rows
for j=1:4 %The columns
sumPlayer2=0
for l=0:3
%This sums the rows of Player2’s zeros board
sumPlayer2=sumPlayer2+Player2(k,j+l)
end
%If the sum is 4, the player has won
if sumPlayer2==4
break
end
end
end

%Vertical Win
for m=1:7 %The columns
for n=1:3 %The rows
sumPlayer2=0
for b=0:3
%This sums the columns of Player2’s zeros board
sumPlayer2=sumPlayer2+Player2(n+b,m)
end
%If the sum is 4, the player has won
if sumPlayer2==4
break
end
end
end

%Diagonal Right Win
for u=1:4 %The rows
for o=4:6 %The columns
sumPlayer2=0
for g=0:3
%This sums the right diagonal win combinations of Player2’s
%zeros board
sumPlayer2=sumPlayer2+Player2(o-g,u+g)
end
%If the sum is 4, the player has won
if sumPlayer2==4
break
end
end
end

%Diagonal Left Win
for t=4:7 %The columns
for c=4:6 %The rows
sumPlayer2=0
for q=0:3
%This sums the left diagonal win combinations of Player2’s
%zeros board
sumPlayer2=sumPlayer2+Player2(c-q,t-q)
end
%If the sum is 4, the player has won
if sumPlayer2==4
break
end
end
end
%Repeat the win check with Player1’s zeros board and just add a 1 to
%every variable used to limit confusion.

%%Player 1 Winning Check
%Horizontal Win
for k1=1:6 %The rows
for j1=1:4 %The columns
sumPlayer1=0
for l1=0:3
sumPlayer1=sumPlayer1+Player1(k1,j1+l1)
end
if sumPlayer1==4
break
end
end

end

%Vertical Win
for m1=1:7 %The rows
for n1=1:3 %The columns
sumPlayer1=0
for b1=0:3
sumPlayer1=sumPlayer1+Player1(n1+b1,m1)
end
if sumPlayer1==4
break
end
end
end

%Diagonal Right Win
for u1=1:4
for o1=4:6
sumPlayer1=0
for g1=0:3
sumPlayer1=sumPlayer1+Player1(o1-g1,u1+g1)
end
if sumPlayer1==4
break
end
end
end

%Diagonal Left Win Check
for t1=4:7
for c1=4:6
sumPlayer1=0
for q1=0:3
sumPlayer1=sumPlayer1+Player1(c1-q1,t1-q1)
end
if sumPlayer1==4
break
end
end
end

end
%A separate if/else statement is needed to print to the screen that a
%player has won
if sumPlayer1==4
fprintf(“Player1 wins!”)
else sumPlayer2==4
fprintf(“Player2 wins!”)
end

 

 

Blackjack Final Program

%% Load the card and choose the deck

clc

clear

load(‘CardDeck.mat’)

C = cell(BlueDeck);

% To translate the cards to the the value they present

num_BlueDeck=[1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10];

%% Shuffle the cards

ShuffledDeck=randperm(52);

% Shuffle the card into a random deck

%% Print the rule to the player

fprintf(“Game Rules\n”)

fprintf(“1.In this game there are no jacks and blank cards.\n”)

fprintf(“2.Please follow the instructions to ensure the game keeps going.\n”)

fprintf(“3.If you have aces in your card,you can choose whether to consider a ace to be 1 or 11. The dealer can also choose to consider a ace to be 1 or 11.\n”)

fprintf(“4.The player and the dealer will have two cards at first. The player will then choose whether to have another cards. \n”)

fprintf(“5.When the player stop adding cards, the dealer will start to get cards from the pile.\n”)

fprintf(“6.The game ends when the player wants to stay or they bust, then the dealer will go. Whoever does not exceed 21 and is closer to the value 21 wins.\n”)

fprintf(“7.The player can see all the cards the player has and one card the dealer has before the game ends.\n”)

%% Make sure the player know how to play the game

Play=0;

% Decision from the player: whether to play or not

Play=input(‘Please press 1 if you have read the rule and want to play the game, press 0 to exit the game.’)

if Play==1

%% Distribute the cards to the player

hand1=ShuffledDeck(1:2)% The cards hold by the player

hand2=ShuffledDeck(3:4)% The card hold by the dealer

figure(1)

imshow([BlueDeck{hand1}])% Show the player’s cards to the player

figure(2)

imshow([BlueDeck{hand2(2)}])% Show the dealer’s cards to the dealer

%% Count the number of cards that has been handed out

card_handout=4% The total number of cards has been handed out

num_hand1=2% The number of cards the player has

num_hand2=2% The number of cards the dealer has

%% Player’s round

decision=input(‘Do you want another card? Please enter 1 for yes or 2 for no.’)% Prompt and read in the player’s decision

while decision==1

hand1(num_hand1+1)=ShuffledDeck(card_handout+1)

num_hand1=num_hand1+1

card_handout=card_handout+1

figure(1)

imshow([BlueDeck{hand1}])

decision=input(‘Do you want another card? Please enter 1 for yes or 2 for no.(If your score is over 21, please press 2)’)

end

%% Dealer’s round

sum_temp=num_BlueDeck(hand2(1))+num_BlueDeck(hand2(2))% Temporary sum of the cards from the dealer to make the decision

num_ace=0  % The number of aces the dealer has

DealerFrom1to11=0 % Whether the dealer has changed an ace from 1 to 11 during the period

while sum_temp<17

l=length(hand2)

% Find the number of aces in the dealers’ hand

for k= 1:l %

if num_BlueDeck(hand2(k))==1

num_ace=num_ace+1

end

end

if sum_temp<=21-10 & num_ace~=0

sum_temp=sum_temp+10

DealerFrom1to11=1

else

sum_temp=sum_temp

end

card_handout=card_handout+1

num_hand2=num_hand2+1

hand2(num_hand2)=ShuffledDeck(card_handout)

sum_temp=sum_temp+num_BlueDeck(hand2(num_hand2))

end

%% End of the game

figure(2) % Show the dealer’s hand to the players

imshow([BlueDeck{hand2}])

%% Count the sum of the player and dealer

sumPlayer=0% The sum of hand of the player

Player_decision=0%The player’s decision whether to consider an ace as 11

numfrom1to11=0% The number of aces the player want to change to 11

% Calculate the sum of the player

for k=1:length(hand1)

if num_BlueDeck(hand1(k))==1

Player_decision=input(‘Do you want consider this ace as 11? 1 for yes and 2 for no.’)

if Player_decision==1

numfrom1to11=numfrom1to11+1

Player_decision=0

end

end

end

for k=1:length(hand1)

sumPlayer=sumPlayer+num_BlueDeck(hand1(k))

end

%% Calculate the sum of the dealer

sumDealer=0% The sum of dealer’s hand

if sum_temp>21 & DealerFrom1to11==1

sumDealer=sum_temp-10

else

sumDealer=sum_temp

end

%% Compare the score of dealer and score of the player

scorePlayer=sumPlayer-21 % Show how close the sum of card the player have is to 21

scoreDealer=sumDealer-21 % Show how close the sum of card the dealer have is to 21

%Calculate who busted

if scorePlayer>0

fprintf(“The player busted :(\n”)

end

if scoreDealer>0

fprintf(“The dealer busted :)\n”)

end

% Decide who win the game

if scorePlayer>0 & scoreDealer<=0

fprintf(“The dealer win! :(\n”)

elseif scorePlayer<=0 & scoreDealer>0

fprintf(“The player win! :)\n”)

elseif scorePlayer>0 & scoreDealer>0

fprintf(“No one win. Maybe try again next time!\n”)

else

if scorePlayer<scoreDealer

fprintf(“The dealer win! :(\n”)

elseif scorePlayer>scoreDealer

fprintf(“The player win! :)\n”)

else

fprintf(“The score of the player and the score of the player are the same.\n”)

end

end

else

fprintf(“You have successfully exited the game!\n”)

end