function varargout = Connect4(varargin)
% CONNECT4 M-file for Connect4.fig
% CONNECT4, by itself, creates a new CONNECT4 or raises the existing
% singleton*.
%
% H = CONNECT4 returns the handle to a new CONNECT4 or the handle to
% the existing singleton*.
%
% CONNECT4('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in CONNECT4.M with the given input arguments.
%
% CONNECT4('Property','Value',...) creates a new CONNECT4 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Connect4_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Connect4_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help Connect4
% Last Modified by GUIDE v2.5 14-Apr-2010 01:46:13
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Connect4_OpeningFcn, ...
'gui_OutputFcn', @Connect4_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before Connect4 is made visible.
function Connect4_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Connect4 (see VARARGIN)
% Choose default command line output for Connect4
handles.output = hObject;
%axis([0 c 0 r],'equal','off')
% UIWAIT makes Connect4 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Connect4_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonDownFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global board t currentPlayer computerSwitch;
r = get(hObject, 'currentPoint');
j = (r(1)-20)/9.5;
if (scanBoard_Connect4(board) ~= 0)
set(handles.text1, 'String', 'please restart game');
return;
end
if (j < 0 || j > 7)
return;
else
column = ceil(j);
end
newBoard = makeMove_Connect4(board, column, currentPlayer); % outputs matrix
if (newBoard == false)
set(handles.text1, 'String', 'invalid move');
return;
else
p = find(newBoard(:,column) ~= board(:,column)); % returns row number
ii = 6.5-p;
jj = column - 0.5;
x = 0.35*sin(t);
y = 0.35*cos(t);
%area(x+jj, y+6.5, 'FaceColor', [1 0 0], 'EdgeColor', 'none'); axis([0 7 0 6]); set(handles.axes1, 'Color','yellow'); hold on;
for i = 5.5:-1:ii
area(x+jj, y+i+1, 'FaceColor', [0.75 0.75 0.75], 'EdgeColor','none');axis([0 7 0 6]); set(handles.axes1, 'Color','yellow');
if (currentPlayer == 1)
area(x+jj, y+i, 'FaceColor', [1 0 0], 'EdgeColor','none');axis([0 7 0 6]); set(handles.axes1, 'Color','yellow');
else
area(x+jj, y+i, 'FaceColor', [0 0 0], 'EdgeColor','none');axis([0 7 0 6]); set(handles.axes1, 'Color','yellow');
end
pause(0.02);
end
board = newBoard;
checkWin = scanBoard_Connect4(board);
if(checkWin ~= 0)
if (checkWin == 1)
set(handles.text1, 'String', 'Player 1 Won');
return;
else
set(handles.text1, 'String', 'Player 2 Won');
return;
end
end
% change who's turn it is.
if (currentPlayer == 1)
currentPlayer = 2;
elseif (currentPlayer == 2)
currentPlayer = 1;
end
% call ai move
if (currentPlayer == 2 && computerSwitch == 1)
set(handles.text1,'String','Computer Turn');
column = aiAnalyze_Connect4(board);
newBoard = makeMove_Connect4(board, column, 2);
p = find(newBoard(:,column) ~= board(:,column)); % returns row number
ii = 6.5-p;
jj = column - 0.5;
x = 0.35*sin(t);
y = 0.35*cos(t);
%area(x+jj, y+6.5, 'FaceColor', [1 0 0], 'EdgeColor', 'none'); axis([0 7 0 6]); set(handles.axes1, 'Color','yellow'); hold on;
for i = 5.5:-1:ii
area(x+jj, y+i+1, 'FaceColor', [0.75 0.75 0.75], 'EdgeColor','none');axis([0 7 0 6]); set(handles.axes1, 'Color','yellow');hold on;
if (currentPlayer == 1)
area(x+jj, y+i, 'FaceColor', [1 0 0], 'EdgeColor','none');axis([0 7 0 6]); set(handles.axes1, 'Color','yellow');hold on;
elseif (currentPlayer == 2)
area(x+jj, y+i, 'FaceColor', [0 0 0], 'EdgeColor','none');axis([0 7 0 6]); set(handles.axes1, 'Color','yellow');hold on;
end
pause(0.02);
end
board = newBoard;
checkWin = scanBoard_Connect4(board);
if(checkWin ~= 0)
if (checkWin == 1)
set(handles.text1, 'String', 'Player 1 Won');
return;
else
set(handles.text1, 'String', 'Player 2 Won');
return;
end
end
% change who's turn it is.
if (currentPlayer == 1)
currentPlayer = 2;
elseif (currentPlayer == 2)
currentPlayer = 1;
end
end
if (currentPlayer == 1)
set(handles.text1, 'String', 'Player 1 Turn');
elseif (currentPlayer == 2)
set(handles.text1, 'String', 'Player 2 Turn');
end
end
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Update handles structure
set(handles.pushbutton1, 'Visible', 'Off');
set(handles.pushbutton2, 'Visible', 'Off');
guidata(hObject, handles);
global board t currentPlayer computerSwitch;
board=zeros(6,7);
computerSwitch = 1;
radius=0.35;
t=0:0.01:2*pi;
x = radius.*sin(t);
y = radius.*cos(t);
for i = 0.5:5.5
for j = 0.5:6.5
area(x+j, y+i, 'facecolor', [0.75 0.75 0.75], 'edgecolor','none');
axis([0 7 0 6]);
axis equal;
set(handles.axes1, 'Color','yellow');
hold on;
end
end
currentPlayer = 1;
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Update handles structure
set(handles.pushbutton1, 'Visible', 'Off');
set(handles.pushbutton2, 'Visible', 'Off');
guidata(hObject, handles);
global board t currentPlayer computerSwitch;
board=zeros(6,7);
computerSwitch