Crackerbarrel Peg Game Help!

4 views (last 30 days)
Vinay
Vinay on 6 May 2014
Commented: Geoff Hayes on 14 May 2014
Hey guys, first time posting here so I hope I'm doing it right.
For my assignment, I have to create a MATLAB program in which I can get 1000 solutions from a user selected hole on this triangle solitaire peg board:
A PEG board contains 15 holes, shaped in form of a triangle. Initially 14 of the holes are filled with pegs, while there is one user-prompted empty hole somewhere on the board. During the game it is possible to take a peg, jump over a peg into an empty hole. The peg that was jumped over is then removed from the board. It is important that the jumping peg jumps 2 fields, the destination is an empty hole, and there is a peg in between. The goal of the peg solitaire game is to jump pegs in such a way that only one peg remains on the board eventually.
I need helping looping through if statements to check if a peg is in a random starting hole position, then looping through the pre-stored available moves for each hole, and checking if the conditions allow a move to be made. Then, I check if a move was made and then check if the required number of solutions has been found.
Here is my code so far:
%move {1-15} are row vectors of all the possible moves for each respectable hole
move(1,:) = [2 3 6 10 0 0 0 0]; %hole 1
move(2,:) = [3 4 7 11 0 0 0 0]; %hole 2
move(3,:) = [7 10 4 5 2 1 8 12]; %hole 3
move(4,:) = [3 2 8 11 0 0 0 0]; %hole 4
move(5,:) = [4 3 9 12 0 0 0 0]; %hole 5
move(6,:) = [7 8 10 13 0 0 0 0]; %hole 6
move(7,:) = [8 9 11 14 0 0 0 0]; %hole 7
move(8,:) = [7 6 11 13 0 0 0 0]; %hole 8
move(9,:) = [8 7 12 14 0 0 0 0]; %hole 9
move(10,:) = [6 1 7 3 11 12 13 15]; %hole 10
move(11,:) = [7 2 8 4 0 0 0 0]; %hole 11
move(12,:) = [8 3 9 5 11 10 14 15]; %hole 12
move(13,:) = [11 8 10 6 0 0 0 0]; %hole 13
move(14,:) = [12 9 11 7 0 0 0 0]; %hole 14
move(15,:) = [13 10 14 12 0 0 0 0]; %hole 15
X=input('Which peg hole do you want removed?');
sol = 0;
while sol < 1000;
p_board = ones(15,1);
p_board(X) = 0;
i = 1;
while i < 100;
A = ceil(15*rand());%starts from random hole 1-15
while i == 1;
if A == X;
continue
end
end
sum = 0;%number of potential moves
for j = 1:4;
if move(A,2*j) > 0;
sum = sum+1;
end
nnn = 0;%number of possible moves
for k = 1:sum;
if p_board(A)==1 && p_board(move(A,2*k-1))==1 && p_board(move(A,2*k))==0
nnn = nnn+1;
  11 Comments
Vinay
Vinay on 10 May 2014
Everything checks out ah-okay! Thank you so much for your help and guidance!
Geoff Hayes
Geoff Hayes on 14 May 2014
Anytime. Glad to have been able to help!

Sign in to comment.

Answers (0)

Categories

Find more on Number games 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!