function dummy = connect4()
% This file performs the famous game of Connect FOUR
% The rules of the Game are:
% Two players may participate
% The board is shown in the figure window
% Every player has his own tile colour
% Every player drops his tile in his turn by Clicking on the desired Column
% The two players go on trying to connect Four tiles of one Colour
% Horizontally, Vertically or in any diagonal
% The player who manages to do so first in his own Colour
% IS THE WINNER
% Programmed by: Mina Ayman (mina_ayman@yahoo.com)
% Using MATLAB R12 on 14/11/2003
% Enjoy Your Play !!!
% Getting the tiles for the play
tile = imread('btile.bmp');
p1t = imread('p1.bmp');
p2t = imread('p2.bmp');
wint = imread('win.bmp');
% Getting the board for the play
board = [tile tile tile tile tile tile tile;
tile tile tile tile tile tile tile;
tile tile tile tile tile tile tile;
tile tile tile tile tile tile tile;
tile tile tile tile tile tile tile;
tile tile tile tile tile tile tile];
% Sound Generator for the winner
[win,y,z] = wavread('winner.wav');
winning = [win; win; win; win];
% Sound Generator for the hit sound
[hit,b,c] = wavread('hit3.wav');
% Sound Generator for the drop tile sound
[tdrop,bb,cc] = wavread('boing.wav');
% Initialization of the players
fprintf('\n\t\t\t Welcome to The Game Of CONNECT FOUR')
st = 'Your Turn! ';
con = 'Congratulations! ';
wins = ' Wins';
% Player 1
fprintf('\n Entering the Name of Player (1)')
player1 = input('\nYour Name is : ','s');
fprintf('\n Welcome %s',player1)
fprintf('\n You will get the Red Tile')
id1 = [st player1];
con1 = [con player1 wins];
% Player 2
fprintf('\n\n\n Entering the Name of Player (2)')
player2 = input('\nYour Name is : ','s');
fprintf('\n Welcome %s',player2)
fprintf('\n You will get the Yellow Tile')
id2 = [st player2];
con2 = [con player2 wins];
fprintf('\n\n\n Now Let''s Begin Our Game')
fprintf('\n Press (Enter) when READY')
pause;
% Plotting the board
u = figure;
set(gcf,'Color',[0 0.7 0])
imshow(board)
% Introduction
p = title('**Welcome to CONNECT FOUR**');
set(p,'Color',[0.8 0 0.8])
set(p,'FontWeight','bold')
set(p,'FontSize',19)
pause(4);
% Displaying Rules of the game
set(p,'Color',[0.5 0 0.5])
set(p,'String','Drop Your Tile in Your Turn!!!')
set(p,'FontSize',18)
pause(3);
set(p,'String','With a Right Click on the Column')
pause(3);
set(p,'String','Where You Want to Drop Your Tile')
pause(4);
game = zeros(6,7);
% The Beginning of the Play
for n = 1 : 22
p1x = 0;
a = title(id1);
set(a,'FontSize',18)
set(a,'FontWeight','bold')
set(a,'Color','r')
pause(1.2);
% Get the play of Player 1
while(p1x == 0);
[c1,r1,pp1] = impixel(board);
p1y = ceil(c1(1)/50);
col1 = game(:,p1y);
if (all(col1) == 0)
p1x = max(find(col1 == 0));
game(p1x,p1y) = 11;
end
end
board1 = board;
% Perform Play
for m = 1 : p1x
board1((m-1)*50+1 : (m-1)*50+50 , (p1y-1)*50+1 : (p1y-1)*50+50 , :) = p1t;
imshow(board1)
sound(tdrop,bb,cc)
pause(0.1)
board1 = board;
end
sound(hit,b,c)
pause(0.3)
board((p1x-1)*50+1 : (p1x-1)*50+50 , (p1y-1)*50+1 : (p1y-1)*50+50 , :) = p1t;
% Check for a winning condition
if (game(6,1)==11 & game(6,2)==11 & game(6,3)==11 & game(6,4)==11)
pause(1);
board(251:300 , 1:50 , :) = wint;
board(251:300 , 51:100 , :) = wint;
board(251:300 , 101:150 , :) = wint;
board(251:300 , 151:200 , :) = wint;
imshow(board)
a = title(con1);
set(a,'FontSize',18)
set(a,'FontWeight','bold')
set(a,'Color','r')
sound(winning,y,z)
pause(6)
close
break;
end
if (game(6,2)==11 & game(6,3)==11 & game(6,4)==11 & game(6,5)==11)
pause(1);
board(251:300 , 51:100 , :) = wint;
board(251:300 , 101:150 , :) = wint;
board(251:300 , 151:200 , :) = wint;
board(251:300 , 201:250 , :) = wint;
imshow(board)
a = title(con1);
set(a,'FontSize',18)
set(a,'FontWeight','bold')
set(a,'Color','r')
sound(winning,y,z)
pause(6)
close
break;
end
if (game(6,3)==11 & game(6,4)==11 & game(6,5)==11 & game(6,6)==11)
pause(1);
board(251:300 , 101:150 , :) = wint;
board(251:300 , 151:200 , :) = wint;
board(251:300 , 201:250 , :) = wint;
board(251:300 , 251:300 , :) = wint;
imshow(board)
a = title(con1);
set(a,'FontSize',18)
set(a,'FontWeight','bold')
set(a,'Color','r')
sound(winning,y,z)
pause(6)
close
break;
end
if (game(6,4)==11 & game(6,5)==11 & game(6,6)==11 & game(6,7)==11)
pause(1);
board(251:300 , 151:200 , :) = wint;
board(251:300 , 201:250 , :) = wint;
board(251:300 , 251:300 , :) = wint;
board(251:300 , 301:350 , :) = wint;
imshow(board)
a = title(con1);
set(a,'FontSize',18)
set(a,'FontWeight','bold')
set(a,'Color','r')
sound(winning,y,z)
pause(6)
close
break;
end
if (game(5,1)==11 & game(5,2)==11 & game(5,3)==11 & game(5,4)==11)
pause(1);
board(201:250 , 1:50 , :) = wint;
board(201:250 , 51:100 , :) = wint;
board(201:250 , 101:150 , :) = wint;
board(201:250 , 151:200 , :) = wint;
imshow(board)
a = title(con1);
set(a,'FontSize',18)
set(a,'FontWeight','bold')
set(a,'Color','r')
sound(winning,y,z)
pause(6)
close
break;
end
if (game(5,2)==11 & game(5,3)==11 & game(5,4)==11 & game(5,5)==11)
pause(1);
board(201:250 , 51:100 , :) = wint;
board(201:250 , 101:150 , :) = wint;
board(201:250 , 151:200 , :) = wint;
board(201:250 , 201:250 , :) = wint;
imshow(board)
a = title(con1);
set(a,'FontSize',18)
set(a,'FontWeight','bold')
set(a,'Color','r')
sound(winning,y,z)
pause(6)
close
break;
end
if (game(5,3)==11 & game(5,4)==11 & game(5,5)==11 & game(5,6)==11)
pause(1);
board(201:250 , 101:150 , :) = wint;
board(201:250 , 151:200 , :) = wint;
board(201:250 , 201:250 , :) = wint;
board(201:250 , 251:300 , :) = wint;
imshow(board)
a = title(con1);
set(a,'FontSize',18)
set(a,'FontWeight','bold')
set(a,'Color','r')
sound(winning,y,z)
pause(6)
close
break;
end
if (game(5,4)==11 & game(5,5)==11 & game(5,6)==11 & game(5,7)==11)
pause(1);
board(201:250 , 151:200 , :) = wint;
board(201:250 , 201:250 , :) = wint;
board(201:250 , 251:300 , :) = wint;
board(201:250 , 301:350 , :) = wint;
imshow(board)
a = title(con1);
set(a,'FontSize',18)
set(a,'FontWeight','bold')
set(a,'Color','r')
sound(winning,y,z)
pause(6)
close
break;
end
if (game(4,1)==11 & game(4,2)==11 & game(4,3)==11 & game(4,4)==11)
pause(1);
board(151:200 , 1:50 , :) = wint;
board(151:200 , 51:100 , :) = wint;
board(151:200 , 101:150 , :) = wint;
board(151:200 , 151:200 , :) = wint;
imshow(board)
a = title(con1);
set(a,'FontSize',18)
set(a,'FontWeight','bold')
s