ProjsHowTo - Starter Yahtzee prototype Project Outlines:...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Starter Yahtzee prototype Project Outlines: Yahtzee and Checkers CSC 113 Fall 2010 Checkers prototype Starter Yahtzee prototype Checkers prototype Gaphic user interfaces can be built a couple of ways in Matlab: Manual coding; Using GUIDE (Graphic User Interface Development Environment): Starter Yahtzee prototype Checkers prototype Graphic User Elements The basic building block is the uicontrol function. Here’s a simple example: function main figure ; clf ; h . button = uicontrol ( ' Style ' , ' Pushbutton ' ,... ' String ' , ' Push me ! ' ,... ' Position ' ,[10 10 350 30] , ' Callback ' , @fname ); end function fname ( src , event ) cstring = get ( src , ' String ' ); if strcmp ( cstring , ' Push me ! ') set ( src , ' String ' , ' Push me again ! ' ); else set ( src , ' String ' , ' Push me ! ' ); end end Starter Yahtzee prototype Checkers prototype Not quite “Hello world” but . . . First this. . . then this again. . . . . . then this . . . and so on Starter 11/26/10 2:05 PM Yahtzee prototype Checkers prototype /Users/phillipregalia/Documents/EECS/CSC113/Pr.../yp.m 1 of 2 function yp global h h.fig = figure; set(h.fig,’ToolBar’,’none’); set(h.fig,’MenuBar’,’none’); set(h.fig,’Units’,’Normalized’); h.axes = axes(’Position’,[0 0 1 1],... ’Color’,[0.5 0.5 0.5],’Units’,’Normalized’,... ’Xtick’,,’Ytick’,); axis([0 1 0 1]); hold on; h.dice = zeros(1,5); h.nums = zeros(1,5); h.vals = zeros(1,5); h.chkbox = zeros(1,5); h.rollcount = 0; xdice = [0 0 0.15 0.15]; ydice = [0 0.15 0.15 0]; xoffset = 0.75; for kk=1:5 yoffset = 0.025 + (kk 1) * 0.2; h.dice(kk) = fill(xoffset+xdice,yoffset+ydice,[1 1 1]); h.nums(kk) = text(0.825,yoffset+0.075,num2str(kk),... ’HorizontalAlignment’,’Center’,... ’Fontsize’,30); h.chkbox(kk) = uicontrol(’Style’,’Checkbox’,... ’Units’,’Normalized’,... ’Enable’,’off’,... ’Position’,[0.7 yoffset+0.05 0.05 0.05]); end h.roll = uicontrol(’Style’,’Pushbutton’,’String’,’Roll’,... ’Units’,’Normalized’,... ’Position’,[0.55 0.05 0.1 0.1],... ’Callback’,@rollfcn); % % Example scoring button: h.score = uicontrol(’Style’,’Pushbutton’,’Units’,’Normalized’,... ’String’,’Push me’,’Position’,[0.05 0.8 0.15 0.1],... ’Callback’,@score); end function rollfcn(src,event) global h; Yahtzee prototype Starter Yahtzee prototype Checkers prototype One step at a time 11/26/10 1:40 PM /Users/phillipregalia/Documents/EECS/CSC113/Pr.../yp.m 1 of 2 function yp global h h.fig = figure; set(h.fig,’ToolBar’,’none’); set(h.fig,’MenuBar’,’none’); set(h.fig,’Units’,’Normalized’); h.axes = axes(’Position’,[0 0 1 1],... ’Color’,[0.5 0.5 0.5],’Units’,’Normalized’,... ’Xtick’,,’Ytick’,); axis([0 1 0 1]); hold on; h.dice = zeros(1,5); h.nums = zeros(1,5); h.vals = zeros(1,5); h.chkbox = zeros(1,5); h.rollcount = 0; h.scorelock = 0; xdice = [0 0 0.15 0.15]; ydice = [0 0.15 0.15 0]; xoffset = 0.75; for kk=1:5 yoffset = 0.025 + (kk 1) * 0.2; h.dice(kk) = fill(xoffset+xdice,yoffset+ydice,[1 1 1]); h.nums(kk) = text(0.825,yoffset+0.075,num2str(kk),... ’HorizontalAlignment’,’Center’,... ’Fontsize’,30); h.chkbox(kk) = uicontrol(’Style’,’Checkbox’,... ’Units’,’Normalized’,... This merely sets up a canvas using figure and axes, along with normalized units, for positioning subsequent uicontrol elements. Starter function yp Yahtzee prototype global h Next block of code h.fig = figure; set(h.fig,’ToolBar’,’none’); set(h.fig,’MenuBar’,’none’); set(h.fig,’Units’,’Normalized’); h.axes = axes(’Position’,[0 0 1 1],... ’Color’,[0.5 0.5 0.5],’Units’,’Normalized’,... ’Xtick’,,’Ytick’,); axis([0 1 0 1]); hold on; h.dice = zeros(1,5); h.nums = zeros(1,5); h.vals = zeros(1,5); h.chkbox = zeros(1,5); h.rollcount = 0; xdice = [0 0 0.15 0.15]; ydice = [0 0.15 0.15 0]; xoffset = 0.75; for kk=1:5 yoffset = 0.025 + (kk 1) * 0.2; h.dice(kk) = fill(xoffset+xdice,yoffset+ydice,[1 1 1]); h.nums(kk) = text(0.825,yoffset+0.075,num2str(kk),... ’HorizontalAlignment’,’Center’,... ’Fontsize’,30); h.chkbox(kk) = uicontrol(’Style’,’Checkbox’,... ’Units’,’Normalized’,... ’Enable’,’off’,... ’Position’,[0.7 yoffset+0.05 0.05 0.05]); end h.roll = uicontrol(’Style’,’Pushbutton’,’String’,’Roll’,... ’Units’,’Normalized’,... ’Position’,[0.55 0.05 0.1 0.1],... ’Callback’,@rollfcn); % % Example scoring button: h.score = uicontrol(’Style’,’Pushbutton’,’Units’,’Normalized’,... ’String’,’Push me’,’Position’,[0.05 0.8 0.15 0.1],... ’Callback’,@score); end This sets up the dice and check boxes. function rollfcn(src,event) global h; Checkers prototype h.nums = zeros(1,5); Starter Yahtzee prototype h.vals = zeros(1,5); h.chkbox = zeros(1,5); h.rollcount = 0; xdice = [0 0 0.15 0.15]; ydice = [0 0.15 0.15 0]; xoffset = 0.75; for kk=1:5 yoffset = 0.025 + (kk 1) * 0.2; h.dice(kk) = fill(xoffset+xdice,yoffset+ydice,[1 1 1]); h.nums(kk) = text(0.825,yoffset+0.075,num2str(kk),... ’HorizontalAlignment’,’Center’,... ’Fontsize’,30); h.chkbox(kk) = uicontrol(’Style’,’Checkbox’,... ’Units’,’Normalized’,... ’Enable’,’off’,... ’Position’,[0.7 yoffset+0.05 0.05 0.05]); end Checkers prototype And the buttons h.roll = uicontrol(’Style’,’Pushbutton’,’String’,’Roll’,... ’Units’,’Normalized’,... ’Position’,[0.55 0.05 0.1 0.1],... ’Callback’,@rollfcn); % % Example scoring button: h.score = uicontrol(’Style’,’Pushbutton’,’Units’,’Normalized’,... ’String’,’Push me’,’Position’,[0.05 0.8 0.15 0.1],... ’Callback’,@score); end function rollfcn(src,event) global h; if h.rollcount==3 return; end This adds two buttons. When a button is pushed, its “callback” function is called (here, either rollfcn or score). These are developed next. for jj=1:10 for kk=1:5 if h.rollcount==0 h.vals(kk) = randi(6); Starter Yahtzee prototype Checkers prototype Callback functions function rollfcn(src,event) global h; if h.rollcount==3 return; end for jj=1:10 for kk=1:5 if h.rollcount==0 h.vals(kk) = randi(6); set(h.nums(kk),'String',num2str(h.vals(kk))); else if (~get(h.chkbox(kk),'Value')) h.vals(kk) = randi(6); set(h.nums(kk),'String',num2str(h.vals(kk))); end end pause(0.01); end end h.rollcount = h.rollcount+1; if h.rollcount==1 for kk=1:5 set(h.chkbox(kk),'Enable','on'); end elseif h.rollcount==3; for kk=1:5 set(h.chkbox(kk),'Enable','off'); end end end % end rollfcn This rolls the dice, and counts how many times they’ve been rolled. Starter Yahtzee prototype Callback functions Scoring function (to be expanded!) function score(src,event) global h; set(src,'Enable','off'); h.rollcount = 0; for kk=1:5 set(h.chkbox(kk),'Value',0); end % % More code here: % end Checkers prototype Starter Yahtzee prototype Checkers prototype Checkers First draw the checkerboard: 11/26/10 10:29 PM /Users/phillipregalia/Documents/EECS/CSC113/P.../cp.m 1 of 3 function cp global h h.fig = figure; set(h.fig,’ToolBar’,’none’); set(h.fig,’MenuBar’,’none’); set(h.fig,’Units’,’Normalized’); h.axes = axes(’Position’,[0 0 1 1],... ’Color’,[0.5 0.5 0.5],’Units’,’Normalized’,... ’Xtick’,,’Ytick’,); axis([0 1.2 0 1]); hold on h.chkbd = zeros(8,8); h.occupied = zeros(8,8); % hold handles of pieces xbox = [0 0 0.125 0.125]; % to draw checkerboard squares ybox = [0 0.125 0.125 0]; % to draw checkerboard squares theta = linspace(0,2*pi,50); h.xcirc = 0.0625 + 0.06 * cos(theta); % to draw pieces h.ycirc = 0.0625 + 0.06 * sin(theta); % to draw pieces for jj=1:8 % draw checkerboard xoffset = (jj 1)* 0.125; for kk=1:8 yoffset = (kk 1) * 0.125; if (mod(jj+kk,2)) h.chkbd(jj,kk) = fill(xoffset+xbox,yoffset+ybox,[0.5 0.5 0.5]); else h.chkbd(jj,kk) = fill(xoffset+xbox,yoffset+ybox,[1 1 1]); end end end for jj=1:8 % set up initial pieces xoffset = (jj 1)* 0.125; for kk=1:8 yoffset = (kk 1) * 0.125; if (mod(jj+kk,2)) if (kk < 4) h.occupied(jj,kk) = ... Starter Yahtzee prototype Checkers prototype Then set up the pieces: for jj=1:8 % set up initial pieces xoffset = (jj-1)* 0.125; for kk=1:8 yoffset = (kk-1) * 0.125; if (mod(jj+kk,2)) if (kk < 4) h.occupied(jj,kk) = ... patch(xoffset+h.xcirc,yoffset+h.ycirc,'r'); set(h.occupied(jj,kk),'EdgeColor','r',... 'Linewidth',2); elseif (kk > 5) h.occupied(jj,kk) = ... patch(xoffset+h.xcirc,yoffset+h.ycirc,'b'); set(h.occupied(jj,kk),'EdgeColor','b',... 'Linewidth',2); end end end end h.turn = 0; h.indicator = text(1.05,0.5,'Red plays'); set(h.indicator,'Color',[1 1 1]); while 1 gamefnc; end end % end of cp function The final while loop sets the game in motion, by calling the gamefcn function. Starter Yahtzee prototype Checkers prototype The gamefcn function allows us to choose a piece, using the ginput function. function gamefnc global h getpiece = 1; while getpiece click = ginput(1); coords = ceil(8 * click); if (coords(1) <= 8) if (h.occupied(coords(1),coords(2))); colors = get(h.occupied(coords(1),coords(2)),'FaceColor'); switch h.turn case 0 if (colors(1) == 1) set(h.occupied(coords(1),coords(2)),'EdgeColor',[0 0 0]) getpiece = 0; end case 1 if (colors(3) == 1) set(h.occupied(coords(1),coords(2)),'EdgeColor',[0 0 0]) getpiece = 0; end end end end end The ginput function returns the coordinates of where the mouse was clicked. Starter Yahtzee prototype Once a piece is selected, we can then move it: setpiece = 1; while setpiece click = ginput(1); newc = ceil(8 * click); if(coords(1) <= 8) fcolor = get(h.occupied(coords(1),coords(2)),'FaceColor'); if h.turn if (newc(2)==coords(2)-1) && (abs(newc(1) - coords(1))==1) if (~h.occupied(newc(1),newc(2))) h.occupied(newc(1),newc(2)) = h.occupied(coords(1),coords(2)); h.occupied(coords(1),coords(2)) = 0; xdata = 0.125 * (newc(1)-1) + h.xcirc; ydata = 0.125 * (newc(2)-1) + h.ycirc; set(h.occupied(newc(1),newc(2)),'Xdata',xdata,... 'Ydata',ydata,'EdgeColor',fcolor); setpiece = 0; end end else if (newc(2)==coords(2)+1) && (abs(newc(1) - coords(1))==1) if (~h.occupied(newc(1),newc(2))) h.occupied(newc(1),newc(2)) = h.occupied(coords(1),coords(2)); h.occupied(coords(1),coords(2)) = 0; xdata = 0.125 * (newc(1)-1) + h.xcirc; ydata = 0.125 * (newc(2)-1) + h.ycirc; set(h.occupied(newc(1),newc(2)),'Xdata',xdata,... 'Ydata',ydata,'EdgeColor',fcolor); setpiece = 0; end end end end end if h.turn set(h.indicator,'String','Red Plays'); h.turn = 0; else set(h.indicator,'String','Blue Plays'); h.turn = 1; end end % end gamefcn Actual game: We need a more detailed rule set! Checkers prototype Starter Yahtzee prototype Once a piece is selected, we can then move it: setpiece = 1; while setpiece click = ginput(1); newc = ceil(8 * click); if(coords(1) <= 8) fcolor = get(h.occupied(coords(1),coords(2)),'FaceColor'); if h.turn if (newc(2)==coords(2)-1) && (abs(newc(1) - coords(1))==1) if (~h.occupied(newc(1),newc(2))) h.occupied(newc(1),newc(2)) = h.occupied(coords(1),coords(2)); h.occupied(coords(1),coords(2)) = 0; xdata = 0.125 * (newc(1)-1) + h.xcirc; ydata = 0.125 * (newc(2)-1) + h.ycirc; set(h.occupied(newc(1),newc(2)),'Xdata',xdata,... 'Ydata',ydata,'EdgeColor',fcolor); setpiece = 0; end end else if (newc(2)==coords(2)+1) && (abs(newc(1) - coords(1))==1) if (~h.occupied(newc(1),newc(2))) h.occupied(newc(1),newc(2)) = h.occupied(coords(1),coords(2)); h.occupied(coords(1),coords(2)) = 0; xdata = 0.125 * (newc(1)-1) + h.xcirc; ydata = 0.125 * (newc(2)-1) + h.ycirc; set(h.occupied(newc(1),newc(2)),'Xdata',xdata,... 'Ydata',ydata,'EdgeColor',fcolor); setpiece = 0; end end end end end if h.turn set(h.indicator,'String','Red Plays'); h.turn = 0; else set(h.indicator,'String','Blue Plays'); h.turn = 1; end end % end gamefcn Actual game: We need a more detailed rule set! Checkers prototype ...
View Full Document

This note was uploaded on 02/29/2012 for the course CSC 113 taught by Professor Phillipregali during the Fall '10 term at Catholic University of America.

Ask a homework question - tutors are online