I am currently building a connect 4 game in matlab, I have a working script file
ID: 3664350 • Letter: I
Question
I am currently building a connect 4 game in matlab, I have a working script file that allows me to play the game, but I need to add a graphic "board" provided by the teacher and I cannot figure out how to combine the two. If anyone could help that would be amazing. The script and graphic are below.
Script File
function main
clear all; close all; clc;
global g;
setupgame;
for n=1:g.numgames
initboard;
showboard;
while ~g.done
getmove;
checkwin;
showboard;
end
pause(1)
end
function setupgame
global g;
prompts = {'Player 1''s name','Player 2''s name','# rows','# columns','connect # to win','# games'};
inp = {'*red','*Black','6','7','4','1'};
inp = inputdlg(prompts,'Connect X',1,inp);
if isempty(inp); error('User canceled input'); end;
g.name{1}=inp{1};
g.name{2}=inp{2};
g.nr=str2double(inp{3});
g.nc=str2double(inp{4});
g.n=str2double(inp{5});
g.numgames=str2double(inp{6});
g.playercolors={[1 0 0],[0 0 0]}; % red,black
g.results = [0 0 0];
function initboard
global g;
g.board = zeros(g.nr,g.nc); % playing board
g.player = 1; % current player
g.done = 0;
g.winner = 0;
g.lastmove = 0;
function showboard
global g;
if g.lastmove==0 % board initialization
clf reset
axis([0 g.nc+1 0 g.nr+1])
hold on
bc='k-';
plot([.5 g.nc+.5],[.5 .5],bc); % bottom
for i=0:g.nc
plot([i+.5 i+.5],[.5 g.nr+.5],bc); % vertical lines
end
else % plot last move
[r,c]=ind2sub(size(g.board),g.lastmove);
i = g.board(r,c);
plot(c,r,'o','markersize',250/max(g.nr,g.nc),'markeredgecolor','black','markerfacecolor',g.playercolors{i})
end
if g.winner>0
plot(g.win_x,g.win_y,'k-','linewidth',5,'color','green');
txt = sprintf('%s wins',g.name{g.winner});
updateresults(g.winner);
playdonesound;
elseif g.done>0
txt = sprintf('Tie game!');
updateresults(3);
playdonesound;
else
txt = sprintf('%s''s turn',g.name{g.player});
end
title(sprintf('Connect %u : %s',g.n,txt),'fontsize',16);
drawnow
function getmove
global g;
goodmove = 0;
while ~goodmove
if strncmp(g.name{g.player},'*',1)
c = getcomputermove;
else
[x,y] = ginput(1);
c = round(x);
end
if c<1 || c>g.nc || g.board(g.nr,c)>0
beep;
continue;
end
goodmove=1;
r=find(g.board(:,c)==0);
g.board(r(1),c) = g.player;
g.lastmove = sub2ind(size(g.board),r(1),c);
g.player=mod(g.player,2)+1;
end
function checkwin
global g;
offs(1) = g.nr; % right
offs(2) = 1; % up
offs(3) = g.nr+1; %up, right
offs(4) = -g.nr+1; %up, left
for s=1:g.nr*g.nc
if g.board(s)>0
[r,c]=ind2sub(size(g.board),s);
for o=1:length(offs)
if o>1 && r>(g.nr-g.n+1); continue; end; % don't do upward check
if (o==1 || o==3) && c>(g.nc-g.n+1); continue; end; % don't do rightward check
if o==4 && c<g.n; continue; end; % don't do leftward check
i=(0:g.n-1)*offs(o)+ s;
if isempty(find(i<1, 1)) && isempty(find(i>g.nr*g.nc, 1))
t=g.board(i);
if t(:)==t(1)
g.winner=t(1);
g.done=1;
[r1 c1] = ind2sub(size(g.board),i(1));
[r2 c2] = ind2sub(size(g.board),i(g.n));
g.win_x = [c1 c2];
g.win_y = [r1 r2];
return
end
end
end
end
end
g.done = ( nnz(g.board)==(g.nr*g.nc) );
Graphic
load Connect
% Loads Board (6x7 cell array), a redchip block, and a blackchip block
imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:}])
% Shows the initial board – 6 rows and 7 columns – initially empty (yellow)
Explanation / Answer
function main
clear all; close all; clc;
global g;
setupgame;
for n=1:g.numgames
initboard;
showboard;
while ~g.done
getmove;
checkwin;
showboard;
end
pause(1)
end
function setupgame
global g;
prompts = {'Player 1''s name','Player 2''s name','# rows','# columns','connect # to win','# games'};
inp = {'*Orange','*Black','6','7','4','1'};
inp = inputdlg(prompts,'Connect X',1,inp);
if isempty(inp); error('User canceled input'); end;
g.name{1}=inp{1};
g.name{2}=inp{2};
g.nr=str2double(inp{3});
g.nc=str2double(inp{4});
g.n=str2double(inp{5});
g.numgames=str2double(inp{6});
g.playercolors={[247/255 127/255 0],[0 0 0]}; % orange,black
g.results = [0 0 0];
function initboard
global g;
g.board = zeros(g.nr,g.nc); % playing board
g.player = 1; % current player
g.done = 0;
g.winner = 0;
g.lastmove = 0;
function showboard
global g;
if g.lastmove==0 % board initialization
clf reset
axis([0 g.nc+1 0 g.nr+1])
hold on
bc='k-';
plot([.5 g.nc+.5],[.5 .5],bc); % bottom
for i=0:g.nc
plot([i+.5 i+.5],[.5 g.nr+.5],bc); % vertical lines
end
else % plot last move
[r,c]=ind2sub(size(g.board),g.lastmove);
i = g.board(r,c);
plot(c,r,'o','markersize',250/max(g.nr,g.nc),'markeredgecolor','black','markerfacecolor',g.playercolors{i})
end
if g.winner>0
plot(g.win_x,g.win_y,'k-','linewidth',5,'color','green');
txt = sprintf('%s wins',g.name{g.winner});
updateresults(g.winner);
playdonesound;
elseif g.done>0
txt = sprintf('Tie game!');
updateresults(3);
playdonesound;
else
txt = sprintf('%s''s turn',g.name{g.player});
end
title(sprintf('Connect %u : %s',g.n,txt),'fontsize',16);
drawnow
function getmove
global g;
goodmove = 0;
while ~goodmove
if strncmp(g.name{g.player},'*',1)
c = getcomputermove;
else
[x,y] = ginput(1);
c = round(x);
end
if c<1 || c>g.nc || g.board(g.nr,c)>0
beep;
continue;
end
goodmove=1;
r=find(g.board(:,c)==0);
g.board(r(1),c) = g.player;
g.lastmove = sub2ind(size(g.board),r(1),c);
g.player=mod(g.player,2)+1;
end
function checkwin
global g;
offs(1) = g.nr; % right
offs(2) = 1; % up
offs(3) = g.nr+1; %up, right
offs(4) = -g.nr+1; %up, left
for s=1:g.nr*g.nc
if g.board(s)>0
[r,c]=ind2sub(size(g.board),s);
for o=1:length(offs)
if o>1 && r>(g.nr-g.n+1); continue; end; % don't do upward check
if (o==1 || o==3) && c>(g.nc-g.n+1); continue; end; % don't do rightward check
if o==4 && c<g.n; continue; end; % don't do leftward check
i=(0:g.n-1)*offs(o)+ s;
if isempty(find(i<1, 1)) && isempty(find(i>g.nr*g.nc, 1))
t=g.board(i);
if t(:)==t(1)
g.winner=t(1);
g.done=1;
[r1 c1] = ind2sub(size(g.board),i(1));
[r2 c2] = ind2sub(size(g.board),i(g.n));
g.win_x = [c1 c2];
g.win_y = [r1 r2];
return
end
end
end
end
end
g.done = ( nnz(g.board)==(g.nr*g.nc) );
function playdonesound
global g;
if sum(g.results)<g.numgames; return; end
builtinsounds = {'train', 'gong', 'chirp', 'handel', 'laughter', 'splat'};
i=randi(length(builtinsounds));
load(builtinsounds{i});
wavplay(y,Fs,'async')
function updateresults(p)
global g;
g.results(p)=g.results(p)+1;
fprintf('%s wins: %u %s wins: %u Ties:%u ',g.name{1},g.results(1),g.name{2},g.results(2),g.results(3));
function c = getcomputermove
global g;
o = find(g.board(g.nr,:)==0);
i = randi(length(o));
c = o(i);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.