Generalization of 3x3 tic toe tac game

조회 수: 1 (최근 30일)
taitai ho
taitai ho 2015년 5월 3일
댓글: taitai ho 2015년 5월 4일
I want to generalize a 3x3 tic toe tac game to a 3x3x3 tic toe tac game(it has 27 boxes in total).Can someone tell me how should i begin or what commands should i use?
a 3x3 tic toe tac game code is given here (which come from my teacher) function game_start2 %GAME_START2 Summary of this function goes here % Detailed explanation goes here close all clc S.fh = figure('units','pixels',... 'position',[50 40 1024 768],... 'menubar','none',... 'name','gui',... 'numbertitle','off',... 'resize','off'); S.pb = uicontrol('style','push',... 'units','pix',... 'position',[10 55 180 40],... 'fontsize',14,... 'string','Restart'); S.tx = uicontrol('style','text',... 'units','pix',... 'position',[10 10 360 40],... 'string','Player 1,your turn..',... 'fontsize',23); global stage stage=zeros(3,3); global current_pos current_pos=[1 1]; global lock lock=[1 1]; global player player=1; global box_handles box_handles=zeros(3,3); global lock_handles lock_handles=zeros(3,3); global flag flag=0;
set(S.pb,'callback' ,{@pb_call,S}) set(S.pb,'KeyPressFcn',{@pb_kpf ,S}); set(S.fh,'KeyPressFcn',{@pb_kpf ,S});
visualization3(stage,lock) set(lock_handles(lock(1),lock(2)),'FaceColor','flat')
function pb_call(varargin) set(lock_handles(lock(1),lock(2)),'FaceColor','none') stage=zeros(3,3); lock=[1 1]; for i=1:3 for j=1:3 set(box_handles(i,j),'CData',get(box_handles(i,j),'CData')*0+0.51);
end
end
set(lock_handles(1,1),'FaceColor','flat')
player=1;
set(S.tx,'String', ['Player ' num2str(1) ', your turn..'])
flag=0;
end
function pb_kpf(varargin)
update_lock_prosition(varargin{1,2}.Character)
if flag==0
if varargin{1,2}.Key=='return'
if stage(lock(1),lock(2))==0
stage(lock(1),lock(2))=round((player-0.5)*2);
if player==1
v=get(box_handles(lock(1),lock(2)),'CData');
set(box_handles(lock(1),lock(2)),'CData',v*0+0.2)
else
v=get(box_handles(lock(1),lock(2)),'CData');
set(box_handles(lock(1),lock(2)),'CData',v*0+0.8)
end
player=mod(player+1,2);
set(S.tx,'String', ['Player ' num2str(round(2-player)) ', your turn..'])
flag=isGameEnd;
if flag==1
set(S.tx,'String', 'Player 1, you win !')
elseif flag==-1
set(S.tx,'String', 'Player 2, you win !')
end
end
end
end
end
function flag=isGameEnd flag=0; if max(find(sum(stage,1)==3))>0 flag=1; return elseif max(find(sum(stage,1)==-3))>0 flag=-1; return end if max(find(sum(stage,2)==3))>0 flag=1; return elseif max(find(sum(stage,2)==-3))>0 flag=-1; return end
if sum(diag(stage))==3
flag=1;
elseif sum(diag(stage))==-3
flag=-1;
end
if sum(diag(rot90(stage)))==3
flag=1;
elseif sum(diag(rot90(stage)))==-3
flag=-1;
end
end
function update_lock_prosition(input_key)
switch input_key
case 'w'
if lock(2)<3
set(lock_handles(lock(1),lock(2)),'FaceColor','none')
lock(2)=lock(2)+1;
set(lock_handles(lock(1),lock(2)),'FaceColor','flat')
set(lock_handles(lock(1),lock(2)),'edgecolor','none')
end
case 's'
if lock(2)>1
set(lock_handles(lock(1),lock(2)),'FaceColor','none')
lock(2)=lock(2)-1;
set(lock_handles(lock(1),lock(2)),'FaceColor','flat')
set(lock_handles(lock(1),lock(2)),'edgecolor','none')
end
case 'a'
if lock(1)>1
set(lock_handles(lock(1),lock(2)),'FaceColor','none')
lock(1)=lock(1)-1;
set(lock_handles(lock(1),lock(2)),'FaceColor','flat')
set(lock_handles(lock(1),lock(2)),'edgecolor','none')
end
case 'd'
if lock(1)<3
set(lock_handles(lock(1),lock(2)),'FaceColor','none')
lock(1)=lock(1)+1;
set(lock_handles(lock(1),lock(2)),'FaceColor','flat')
set(lock_handles(lock(1),lock(2)),'edgecolor','none')
end
otherwise;
end
end
function visualization3(stage,lock) hold off surf(zeros(2,2),zeros(2,2),zeros(2,2),zeros(2,2)) A=[hsv;hot]; colormap(A) hold on surf(zeros(2,2),zeros(2,2),zeros(2,2),ones(2,2))
for i=1:3
for j=1:3
box_handles(i,j)=surf([i-2.5 i-1.5;i-2.5 i-1.5],[j-2.5 j-2.5;j-1.5 j-1.5],[0 0;0 0],ones(2,2)*0.51);
lock_handles(i,j)=surf([i-2.1 i-1.9;i-2.1 i-1.9],[j-2.1 j-2.1;j-1.9 j-1.9],[1 1;1 1],ones(2,2)*0.42);
set(lock_handles(i,j),'FaceColor','none')
set(lock_handles(i,j),'edgecolor','none')
end
end
axis off
colorbar off
axis equal
axis tight
view([0 90])
plot([-1.5 1.5],[-1.5 -1.5],'w','linewidth',2)
plot([-1.5 1.5],[-0.5 -0.5],'w','linewidth',2)
plot([-1.5 1.5],[0.5 0.5],'w','linewidth',2)
plot([-1.5 1.5],[1.5 1.5],'w','linewidth',2)
plot([-1.5 -1.5],[-1.5 1.5],'w','linewidth',2)
plot([-0.5 -0.5],[-1.5 1.5],'w','linewidth',2)
plot([0.5 0.5],[-1.5 1.5],'w','linewidth',2)
plot([1.5 1.5],[-1.5 1.5],'w','linewidth',2)
end
end
Thank toy very much!!
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2015년 5월 3일
taitai - rather than posting all of the code in the question, why not just attach it using the paperclip button? I would think the first step in putting together a 3x3x3 tic-tac-toe game is to define the rules/logic. Have you written this logic out in the form of pseudo code?
Alice Yuen
Alice Yuen 2015년 5월 4일
The hints provided in that homework guidelines plus F1 in matlab are sufficient lah. This 3x3x3 shares the exactly same structure with the code of that 3x3 version. and the functions themselves are as well quite similar, except for the function visualization3, you have to rewrite it. 利申:spent ten hours in matlab yesterday and I have nearly k.o. it. :o)

댓글을 달려면 로그인하십시오.

답변 (1개)

taitai ho
taitai ho 2015년 5월 4일
Thanks for your answer ,Alice.I am keep trying T.T.By the way,what do u mean F1?
  댓글 수: 2
Alice Yuen
Alice Yuen 2015년 5월 4일
편집: Alice Yuen 2015년 5월 4일
F1 help XD Or http://www.mathworks.com/help/ Endless searching, looking up the hints given, and trial and error lol But first try plotting out the .mat file given lol then you should have a clue lah lol
taitai ho
taitai ho 2015년 5월 4일
okay.Thank you. I am trying.Hope you get a perfect mark in this assignment. :)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Strategy & Logic에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by