E. Final Program with Comments

Connect Four

clc
clear
graphic= simpleGameEngine(‘ConnectFour.png’,86,101);
%Set up variables
empty= 1;
red=2;
black=3;

fprintf(‘\n************************************************\n’)
fprintf(‘* WELCOME TO CONNECT FOUR *\n’)
fprintf(‘************************************************\n’)
fprintf(‘\n’)
fprintf(‘\n*******************RULES**********************\n’)
fprintf(‘ 1. The board contains 6 rows and 7 columns.\n’)
fprintf(‘ 2. Only two players can play this game at a time.\n’)
fprintf(‘ 3. The player must choose a column number from 1 to 7 to place the piece in the board.\n’)
fprintf(‘ 4. Any other input except 1 to 7 display the error message and ask for another input.\n’)
fprintf(‘ 5. Do not enter the column which is full.\n’)
fprintf(‘ 6. This game uses vertical, horizontal and diagonal checks to check if connect four is reached.\n’)
fprintf(‘ 7. Once the connect four is reached, the game will be over. \n’)
fprintf(‘ 8. Player should press run to start the game again.\n’)
fprintf(‘\n’)

fprintf(‘Start’)

%start= imread(‘start-the-game.jpg’);
%imshow(start,’InitialMagnification’,’fit’)
fprintf(‘\n’)
prompt1= ‘Player 1, Enter your name: ‘;
prompt2=’Player 2, Enter your name: ‘;
player1= input(prompt1, ‘s’); %input player 1 name
player2= input(prompt2, ‘s’); % input player 2 name

row= zeros(1,7);   % row vector is created
col= zeros(1,6);    % column vector is created

board= empty* ones(6,7);      %create the board(matrix)
drawScene(graphic, board)    %link the graphic with the board
result= true;
while (result == true)     % Ask for input in given while condition

column= input(‘Player 1, select the column from 1 to 7: ‘ );    %ask the user to input column number

% if column number is not within 1-7 range, ask for another valid input
while(column<1 || column>7)
disp(‘Invalid Entry!! select column from 1 to 7’)
column= input(‘ Player 1, select the column from 1 to 7: ‘ );
end

board(6-row(column), column)= 2;
row(column)= row(column)+1;
disp(board)
drawScene(graphic, board)
test=board==2;

% vertical test
for c= 1:1:7
for r= 1:1:3
if (test(r,c)==1 && test(r+1, c)==1 && test(r+2, c)==1 && test(r+3, c)==1)
result = false;
fprintf(‘%s is winner.\n’, player1)
gameover= imread(‘gameover.jpg’);
imshow(gameover,’InitialMagnification’,’fit’)
fprintf(‘Press run to play the game again.\n’)
break
end
end
end

%horizontal test
for c= 1:1:4
for r= 1:1:6
if test(r,c)==1 && test(r, c+1)==1 && test(r, c+2)==1 && test(r, c+3)==1
result= false;
fprintf(‘%s is winner.\n’, player1)
gameover= imread(‘gameover.jpg’);
imshow(gameover,’InitialMagnification’,’fit’)
fprintf(‘Press run to play the game again.\n’)
break
end
end
end

% diagonal test from column 1 to 4
for c= 1:1:4
for r= 1:1:3
if test(r,c)==1 && test(r+1, c+1)==1 && test(r+2, c+2)==1 && test( r+3, c+3)==1
result= false;
fprintf(‘%s is winner.\n’, player1)
gameover= imread(‘gameover.jpg’);
imshow(gameover,’InitialMagnification’, ‘fit’)
fprintf(‘Press run to play the game again.\n’)
break
end
end
end
% diagonal test from column 4 to 7
for c= 4:1:7
for r= 1:1:3
if test(r, c)==1 && test(r+1, c-1)==1 && test(r+2, c-2)==1 && test(r+3, c-3)==1
result= false;
fprintf(‘%s is winner. \n’, player1)
gameover= imread(‘gameover.jpg’);
imshow(gameover,’InitialMagnification’,’fit’)
fprintf(‘Press run to play the game again.\n’)
break

end
end
end

%Player 2 input
column2= input(‘Player 2, select the column from 1 to 7: ‘); % player2 input
while(column2< 1 || column2> 7)
% if column is not within column 1-7 range, ask for another input
disp(‘Invalid entry!! select column from 1 to 7’)
column2= input(‘Player 2 , select the column from 1 to 7: ‘ );
end

board(6-row(column2), column2)=3; % place number 3 (black piece) in matrix
row(column2)= row(column2)+1; % add a piece to row vector
disp(board) % display the matrix
drawScene(graphic, board) % display the graphic
test2= board==3; % display only player2 input

%vertical test
for c= 1:1:7
for r= 1:1:3
if test2(r,c)==1 && test2(r+1, c)==1 && test2(r+2, c)==1 && test2(r+3, c)==1
result = false;
fprintf(‘%s is winner.\n’, player2)
gameover= imread(‘gameover.jpg’);
imshow(gameover,’InitialMagnification’,’fit’)
fprintf(‘Press run to play the game again.\n’)
break
end
end

end

%horizontal test
for c= 1:1:4
for r= 1:1:6
if test2(r,c)==1 && test2(r, c+1)==1 && test2(r, c+2)==1 && test2(r, c+3)==1
result= false ;
fprintf(‘%s is winner.\n’, player2)
gameover= imread(‘gameover.jpg’);
imshow(gameover,’InitialMagnification’,’fit’)
fprintf(‘Press run to play the game again.\n’)
break
end
end
end

% diagonal test from column 1 to 4
for c= 1:1:4
for r= 1:1:3
if test2(r, c)==1 && test2(r+1, c+1)==1 && test2(r+2, c+2)==1 && test2( r+3, c+3)==1
result=false;
fprintf(‘%s is winner.\n’, player2)
gameover= imread(‘gameover.jpg’);
imshow(gameover,’InitialMagnification’,’fit’)
fprintf(‘Press run to play the game again.\n’)
break
end
end
end

% diagonal test from column 4 to 7
for c= 4:1:7
for r= 1:1:3
if test2(r, c)==1 && test2(r+1, c-1)==1 && test2(r+2, c-2)==1 && test2(r+3, c-3)==1
result = false;
fprintf(‘%s is winner.\n’, player2)
gameover= imread(‘gameover.jpg’);
imshow(gameover,’InitialMagnification’,’fit’)
fprintf(‘Press run to play the game again.\n’)
break
end
end
end

end

Tic Tac Toe

% the value 1 in the matrix will display an ‘x’ in the respective space

% the value 2 will display an ‘o’

% the value 3 will be a blank square

fprintf(‘Player 1 will be “Os” and Player 2 will be “Xs”‘)

game_won = 0;

     

my_scn=simpleGameEngine(‘osu_michigan_sprite.png’ , 49,49,5);

Board = [3, 3, 3; 3, 3, 3; 3, 3, 3];

drawScene(my_scn , Board);

%While the board is not full, run through the loop

while ismember(3, Board)

    %Ask for player to input move

    valid = 0;

    %Check to see if the move player makes is valid, if there is already an

    %image in the square, move is NOT valid

while ~valid

    player1_turn = menu(‘Player 1: Select position for your move: ‘ , ‘top left’ , ‘top middle’ , ‘top right’ , ‘middle left’ , ‘middle’ , ‘middle right’ , ‘bottom left’ , ‘bottom middle’ , ‘bottom right’);

if player1_turn == 1 && Board(1,1) == 3

    Board(1,1) = 1;

    valid = 1;

elseif player1_turn == 2 && Board(1, 2) == 3

    Board(1, 2) = 1;

    valid = 1;

elseif player1_turn == 3 && Board(1, 3) == 3

    Board(1, 3) = 1;

    valid = 1;

elseif player1_turn == 4 && Board(2, 1) == 3

    Board(2, 1) = 1;

    valid = 1;

elseif player1_turn == 5 && Board(2, 2) == 3

    Board(2, 2) = 1;

    valid = 1;

elseif player1_turn == 6 && Board(2, 3) == 3

    Board(2, 3) = 1;

    valid = 1;

elseif player1_turn == 7 && Board(3, 1) == 3

    Board(3, 1) = 1;

    valid = 1;

elseif player1_turn == 8 && Board(3, 2) == 3

    Board(3, 2) = 1;

    valid = 1;

elseif player1_turn == 9 && Board(3, 3) == 3

    Board(3, 3) = 1;

    valid = 1

else

    valid = 0;

    disp(‘That square has already been taken’)

end

drawScene(my_scn , Board);

end

%Check to see if player 1 wins

if Board(1, 1) == 1 && Board(2, 1) == 1 && Board(3, 1) == 1

    disp(‘Player 1 is the winner!’

player1win=simpleGameEngine(‘player_1_wins.png’ , 150,150,5);

winner_board = 1;

drawScene(player1win, winner_board)

game_won = 1;

    break

elseif Board(1, 2) == 1 && Board(2, 2) == 1 && Board(3, 2) == 1

    disp(‘Player 1 is the winner!’

player1win=simpleGameEngine(‘player_1_wins.png’ , 150,150,5);

winner_board = 1;

drawScene(player1win, winner_board)

game_won = 1;

    break

elseif Board(1, 3) == 1 && Board(2, 3) == 1 && Board(3, 3) == 1

     disp(‘Player 1 is the winner!’)   

player1win=simpleGameEngine(‘player_1_wins.png’ , 150,150,5);

winner_board = 1;

drawScene(player1win, winner_board)

game_won = 1;

     break

elseif Board(1, 1) == 1 && Board(1, 2) == 1 && Board(1, 3) == 1

     disp(‘Player 1 is the winner!’)

player1win=simpleGameEngine(‘player_1_wins.png’ , 150,150,5);

winner_board = 1;

drawScene(player1win, winner_board)

game_won = 1;

     break

elseif Board(2, 1) == 1 && Board(2, 2) == 1 && Board(2, 3) == 1

     disp(‘Player 1 is the winner!’)

player1win=simpleGameEngine(‘player_1_wins.png’ , 150,150,5);

winner_board = 1;

drawScene(player1win, winner_board)

game_won = 1;

     break

elseif Board(3, 1) == 1 && Board(3, 2) == 1 && Board(3, 3) == 1

     disp(‘Player 1 is the winner!’)

player1win=simpleGameEngine(‘player_1_wins.png’ , 150,150,5);

winner_board = 1;

drawScene(player1win, winner_board)

game_won = 1;

     break

elseif Board(1, 1) == 1 && Board(2, 2) == 1 && Board(3, 3) == 1

     disp(‘Player 1 is the winner!’)

player1win=simpleGameEngine(‘player_1_wins.png’ , 150,150,5);

winner_board = 1;

drawScene(player1win, winner_board)

game_won = 1;

     break

elseif Board(1, 3) == 1 && Board(2, 2) == 1 && Board(3, 1) == 1

     disp(‘Player 1 is the winner!’)

player1win=simpleGameEngine(‘player_1_wins.png’ , 150,150,5);

winner_board = 1;

drawScene(player1win, winner_board)

game_won = 1;

     break

end

%Ask for player input for move

valid = 0;

%Check to see if the move player makes is valid, if there is already an

    %image in the square, move is NOT valid

while ~valid

player2_turn = menu(‘Player 2: Select position for your move: ‘ , ‘top left’ , ‘top middle’ , ‘top right’ , ‘middle left’ , ‘middle’ , ‘middle right’ , ‘bottom left’ , ‘bottom middle’ , ‘bottom right’);

if player2_turn == 1 && Board(1,1) == 3

    Board(1,1) = 2;

    valid = 1;

elseif player2_turn == 2 && Board(1, 2) == 3

    Board(1, 2) = 2;

    valid = 1;

elseif player2_turn == 3 && Board(1, 3) == 3

    Board(1, 3) = 2;

    valid = 1;

elseif player2_turn == 4 && Board(2, 1) == 3

    Board(2, 1) = 2;

    valid = 1;

elseif player2_turn == 5 && Board(2, 2) == 3

    Board(2, 2) = 2;

    valid = 1;

elseif player2_turn == 6 && Board(2, 3) == 3

    Board(2, 3) = 2;

    valid = 1;

elseif player2_turn == 7 && Board(3, 1) == 3

    Board(3, 1) = 2;

    valid = 1;

elseif player2_turn == 8 && Board(3, 2) == 3

    Board(3, 2) = 2;

    valid = 1;

elseif player2_turn == 9 && Board(3, 3) == 3

    Board(3, 3) = 2;

    valid = 1;

else

    valid = 0;

    disp(‘That square has already been taken!’)

end

drawScene(my_scn , Board);

end

%determine if player 2 won

%Check to see if player 2 wins

if Board(1, 1) == 2 && Board(2, 1) == 2 && Board(3, 1) == 2

    disp(‘Player 2 is the winner!’)

player2win=simpleGameEngine(‘player_2_wins.png’ , 150,150,5);

winner_board = 1;

drawScene(player2win, winner_board)

game_won = 1;

    break

elseif Board(1, 2) == 2 && Board(2, 2) == 2 && Board(3, 2) == 2

    disp(‘Player 2 is the winner!’)

player2win=simpleGameEngine(‘player_2_wins.png’ , 150,150,5);

winner_board = 1;

drawScene(player2win, winner_board)

game_won = 1;

    break

elseif Board(1, 3) == 2 && Board(2, 3) == 2 && Board(3, 3) == 2

     disp(‘Player 2 is the winner!’

player2win=simpleGameEngine(‘player_2_wins.png’ , 150,150,5);

winner_board = 1;

drawScene(player2win, winner_board)

game_won = 1;

     break

elseif Board(1, 1) == 2 && Board(1, 2) == 2 && Board(1, 3) == 2

     disp(‘Player 2 is the winner!’)

player2win=simpleGameEngine(‘player_2_wins.png’ , 150,150,5);

winner_board = 1;

drawScene(player2win, winner_board)

game_won = 1;

     break

elseif Board(2, 1) == 2 && Board(2, 2) == 2 && Board(2, 3) == 2

     disp(‘Player 2 is the winner!’)

player2win=simpleGameEngine(‘player_2_wins.png’ , 150,150,5);

winner_board = 1;

drawScene(player2win, winner_board)

game_won = 1;

     break

elseif Board(3, 1) == 2 && Board(3, 2) == 2 && Board(3, 3) == 2

     disp(‘Player 2 is the winner!’)

player2win=simpleGameEngine(‘player_2_wins.png’ , 150,150,5);

winner_board = 1;

drawScene(player2win, winner_board)

game_won = 1;

     break

elseif Board(1, 1) == 2 && Board(2, 2) == 2 && Board(3, 3) == 2

     disp(‘Player 2 is the winner!’)

player2win=simpleGameEngine(‘player_2_wins.png’ , 150,150,5);

winner_board = 1;

drawScene(player2win, winner_board)

game_won = 1;

     break

elseif Board(1, 3) == 2 && Board(2, 2) == 2 && Board(3, 1) == 2

     disp(‘Player 2 is the winner!’)

player2win=simpleGameEngine(‘player_2_wins.png’ , 150,150,5);

winner_board = 1;

drawScene(player2win, winner_board)

game_won = 1;

     break

end

    end

% if game_won ~= 1

%     draw_game=simpleGameEngine(‘game_draw.png’ , 180,180,5);

%     draw_game_board = [1];

%     drawScene(draw_game, draw_game_board)

% end

Over Under

% Print statement explaining how the game works
fprintf(‘ A number one through ten will be generated, guess if the number will be Over, Under or Equal to 5!’)
% Prompt asks player to guess if the number will be over, under, or equal to 5
prompt= ‘\n Enter 0 for Over, 1 for Under, and 2 for Equal to 5’;
str = input(prompt,‘s’);

% Picks a random number from 1 to 10
number = randi(10,1)

% If statement with print statements to tell the player if they won or lost

if number > 5 && str == ‘0’
fprintf(‘You Win! 🙂 ‘)
elseif number < 5 && str == ‘1’
fprintf(‘You Win! 🙂 ‘)
elseif number == 5 && str == ‘2’
fprintf(‘You Win! 🙂 ‘)

else fprintf(‘You Lose! :(‘)
end

fprintf(‘ Hit the run arrow to play again.\n’)