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
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
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)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started