Peg game code help
Show older comments
This is the first time posting here, and I need some help with my coding.
p_game=ones(1,15); %initialize the array that contain 15 holes, and all of them have pegs.
remove=input('select a hole you want to remove.'); p_game(remove)=0; %select one hole,and remove the peg of that hole.
solution=0; solution_num =input('how many solution you want?'); %promote user for number of solutions to find
move_hole=cell(15); %access by calling move_hole{num}(inx,:)
move_hole{1}=[2 3; 6 10; 0 0; 0 0];
move_hole{2}=[3 4; 7 11; 0 0; 0 0];
move_hole{3}=[2 1; 4 5; 7 10; 8 12];
move_hole{4}=[3 2; 8 11; 0 0; 0 0];
move_hole{5}=[4 3; 9 12; 0 0; 0 0];
move_hole{6}=[7 8; 10 13; 0 0; 0 0];
move_hole{7}=[8 9; 11 14; 0 0; 0 0];
move_hole{8}=[11 13; 7 6; 0 0; 0 0];
move_hole{9}=[12 14; 8 7; 0 0; 0 0];
move_hole{10}=[6 1; 13 15; 7 3; 11 12];
move_hole{11}=[8 4; 7 2; 0 0; 0 0];
move_hole{12}=[9 5; 8 3; 11 10; 14 15];
move_hole{13}=[10 6; 11 8; 0 0; 0 0];
move_hole{14}=[12 9; 11 7; 0 0; 0 0];
move_hole{15}=[14 12; 13 10; 0 0; 0 0];
%initialize the cell array for solution sol=cell(1,13); court=1;
%total number of possible move Move=0;
while solution <= solution_num;
sum = 14;
while sum > 1
pick=ceil(15*rand()); % random for hole 1-15.
disp('moved hole');
disp(pick);
solution=solution+1; %may not be correct to place here
if p_game(pick)==0 % if picked hole has no peg,repick again
continue;
end
for j=1:15
if p_game(j)~=0
for g=1:4
ck= move_hole{j}(g,:);
c=ck(1);
d=ck(2);
if c~=0 && p_game(c)==1 && p_game(d)==0
Move=Move+1;
end
end
end
end
if p_game(pick)==1 % if picked hole has peg, continue program and random one out of four protential move.
pick1 = ceil(4*rand());
disp('move method');
disp(pick1);
replace = move_hole{pick}(pick1,:);%%make the move cell array correspond to rand hole
check=replace(2); % use check to represent the hole two step away
disp(check);
removed= replace(1); %use remove to represent the hole between original hole and protential moved hole
if check == 0 %in case the random number for hole is zero, random again
continue;
elseif check~=0 && p_game(check)==0 && p_game(removed)==1
% if random number is nonzero, hole two steps away has no peg,
% and hole between oringinal hole and protential hole has peg;
% made move and pre-store the move.
disp('allowed move');
p_game(removed)=0;
p_game(check)=1;
p_game(pick)=0;
% move made
sol{1,court}=[pick check];% 1 will be changed to solution_num after fixing the problem of summation.
%store move into solution cell array.
disp(p_game);
court=court+1;
sum= sum-1;
elseif Move==0
break;
end
end
end
end
I try to use available move to reset my p_game, but somehow it doesn't work as expect. Please Help!
Answers (0)
Categories
Find more on Strategy & Logic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!