This program uses the file simpleGameEngine.m and this png.

**Connect4.m**

clc

clear

close all

% initialize variables

game_over = 0;

% initialize the scene and name variables for the sprites

my_scene = simpleGameEngine(‘ConnectFour.png’,86,101);

empty_sprite = 1;

red_sprite = 2;

black_sprite = 3;

% making empty board

board_display = empty_sprite * ones(6,7);

% setting up the figure

drawScene(my_scene, board_display);

fig = gcf;

fig.Name = ‘Connect 4 like never b4’;

fig.NumberTitle = ‘off’;

fig.ToolBar = ‘none’;

fig.WindowStyle = ‘docked’;

% checks if the player knows how to play the game

rules = 2;

player = randi(1:2);

fprintf(‘Disclaimer: This is a two player only version of connect 4\n’)

name1 = input(‘Enter a name for player 1 ‘,’s’);

name2 = input(‘Enter a name for player 2 ‘,’s’);

while rules ~= 1 && rules ~= 0

rules = input(‘Do you know the rules of connect 4? (1 for yes, 0 for no) ‘);

end

if rules == 1

fprintf(‘Ok. Player 1 is red and player 2 is black.\n’);

elseif rules == 0

fprintf(‘The rules of the game is simple. Choose a row to drop a colored disc in. \nThe goal of the game is to get 4 discs of your color in a row before the opponent does.\nPlayer 1 is red and player 2 is black.\n’);

end

fprintf(‘Please select the column by clicking.\n’);

board = [linspace(0,0,7); linspace(0,0,7); linspace(0,0,7); linspace(0,0,7); linspace(0,0,7); linspace(0,0,7);];

% if game is not over keep playing

while ~game_over

[board, move] = prompt_input(board, player, my_scene,name1,name2);

[board_display, player] = displayMove(move, player, my_scene, board_display);

[game_over, player] = check_game_over(board, move, player);

if game_over == 1

if (player == 1)

fprintf(‘\n%s won!\n’,name1);

else

fprintf(‘\n%s won!\n’,name2);

end

play_again = input(‘Would you like to play again? (1 for yes, 0 for no) ‘);

% if yes reset the board and keep looping

while play_again ~= 1 && play_again ~= 0

fprintf(‘Player %i won!\n’,player);

play_again = input(‘Would you like to play again? (1 for yes, 0 for no) ‘);

end

if play_again == 1

game_over = 0;

board_display = empty_sprite * ones(6,7);

drawScene(my_scene, board_display)

board = [linspace(0,0,7); linspace(0,0,7); linspace(0,0,7); linspace(0,0,7); linspace(0,0,7); linspace(0,0,7);];

else

game_over = 1;

fprintf(‘Goodbye!\n’)

end

end

end

**check_game_over.m**

function [game_over, player] = check_game_over(board, position, player)

% Checks if the game is over and if so who won.

% getting x and y coords of the last move and the color

x = position(2);

y = position(1);

color = board(y,x);

game_over = 0;

% counters for the amount of tokens in a row

in_a_row_y = 1;

in_a_row_x = 1;

in_a_row_z = 1;

% extra z counter because there are 2 different diagonal lines.

in_a_row_z_2 = 1;

%i is how far out it is ie if i is 1 it is checking 1 disc away

% “y” value of the board in the positive direction

for i = 1:3

if y + i > 6

break;

elseif board(y+i,x) == color

in_a_row_y = in_a_row_y + 1;

elseif board(y+i,x) ~= color

break;

end

end

% “x” value of the board in the positive direction

for i = 1:3

if x + i > 7

break;

elseif board(y,x+i) == color

in_a_row_x = in_a_row_x + 1;

elseif board(y,x+i) ~= color

break;

end

end

% “x” value in the negative direction

for i = 1:3

if x – i < 1

break;

elseif board(y,x-i) == color

in_a_row_x = in_a_row_x + 1;

elseif board(y,x-i) ~= color

break;

end

end

% “y” value in the negative direction

for i = 1:3

if y – i < 1

break;

elseif board(y-i,x) == color

in_a_row_y = in_a_row_y + 1;

elseif board(y-i,x) ~= color

break;

end

end

% “z” value in the positive x and positive y direction

for i = 1:3

if y + i > 6 || x + i > 6

break;

elseif board(y+i,x+i) == color

in_a_row_z = in_a_row_z + 1;

elseif board(y+i,x+i) ~= color

break;

end

end

% “z” value in the negative x and negative y direction

for i = 1:3

if y – i < 1 || x – i < 1

break;

elseif board(y-i,x-i) == color

in_a_row_z = in_a_row_z + 1;

elseif board(y-i,x-i) ~= color

break;

end

end

% “z” value in the positive x and negative y direction

for i = 1:3

if y – i < 1 || x + i > 6

break;

elseif board(y-i,x+i) == color

in_a_row_z_2 = in_a_row_z_2 + 1;

elseif board(y-i,x+i) ~= color

break;

end

end

% “z” value in the negative x and positive y direction

for i = 1:3

if y + i > 6 || x – i < 1

break;

elseif board(y+i,x-i) == color

in_a_row_z_2 = in_a_row_z_2 + 1;

elseif board(y+i,x-i) ~= color

break;

end

end

if in_a_row_y >= 4 || in_a_row_x >= 4 || in_a_row_z >= 4 || in_a_row_z_2 >= 4

game_over = 1;

% if they do win, set the player value to the last player that put the disc thingy

% in.

if player == 1

player = 2;

else

player = 1;

end

end

end

**prompt_input.m**

function [column, last_move] = prompt_input(board,player,my_scn,name1,name2)

%prompt_input Prompts user for the input of a row.

% takes the current value of the board, to see if input is valid.

valid = 0;

row = (1:7)’;

while ~valid

%fprintf(‘Enter a row (1-7) player %i’, player);

if player == 1

fprintf(‘Click a square, %s\n’, name1);

else

fprintf(‘Click a square, %s\n’, name2);

end

%in = input(‘: ‘);

[~, c] = getMouseInput(my_scn);

in = c;

% makes sure that input(integer) is between 1 and 7

for i = 1:7

if floor(in) == row(i)

valid = 1;

% check every value in the row inputed and change the value at

% the first available spot.

for k = 1:6

if board(6, in) > 0

% if the last value of the row is taken up then the

% row is full.

fprintf(‘This row is full. Try a different row!\n’);

valid = 0;

break;

elseif board(k,in) > 0

% if there is a value at the spot skip it and move to

% the next one.

else

% depending on the player the number will change: 1 is red

% and 2 is black.

if player == 1

board(k, in) = 1;

else

board(k, in) = 2;

end

break;

end

end

end

end

end

% outputting the current value of the board, and

% outputting where the last disc went.

column = board;

last_move = [k, in];

end

**display_move.m**

function [new_display,player] = displayMove(move,player,scene,display)

% Displays the current move and switches turn.

% Outputs the new board, and switches turn to the other player.

% swap the y value to make gravity.

move_y = 7 – move(1);

move_x = move(2);

% display the new disc at the inputed location

display(move_y,move_x) = player + 1;

drawScene(scene,display)

new_display = display;

% switch the turn

if player == 1

player = 2;

else

player = 1;

end

end