Crackerbarrel Peg Game Help!
Show older comments
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
Geoff Hayes
on 6 May 2014
Vinay - I'm not to clear on your move vector. In the first row, you have
move(1,:) = [2 3 6 10]; %hole 1
What exactly does this mean? Looking at the attached image, if the user moves the peg from position 1 then it can only move to 4 (jumping 2) or move to 6 (jumping 3). How can a peg move from position 1 to 10 in one jump? Same is true for the remaining pegs too. Most should have only two possible moves as they can only jump two different neighbours (4 and 6 may be the only exceptions with three possible jumps).
Vinay
on 6 May 2014
Geoff Hayes
on 6 May 2014
Vinay - you realize that the code you have posted above doesn't run, right? Even if you ignore everything after the while loop (which is incomplete) the assignment of moves to your move array fails because most of the rows have four elements while other rows have eight elements. In a matrix, the number of columns in each row has to be identical. You may want to consider using a cell array which will allow each element to be a variable size. And instead of using a single row vector, you can have a matrix with each row being two columns long with the first column being the adjacent peg, and the subsequent being the destination. This will get you started:
moves = {};
moves{1} = [2 3; 6 10];
You can do something similar for the remaining 14 pegs.
Geoff Hayes
on 7 May 2014
No Vinay, that won't work because you are assigning a 4x2 matrix to one row only of matrix move. If you were to try the above, you should get the Subscripted assignment dimension mismatch error.
Geoff Hayes
on 8 May 2014
Edited: Geoff Hayes
on 9 May 2014
You have your p_board of all ones with the exception of the X (could make this lower case and rename to something that informs the reader what the variable is for) that was removed (note that you should have some sort of check on X in case the user enters something that is not numeric or is not an integer within the interval 1:15).
Of the 14 remaining holes that have a peg in them, you have to choose one peg such that it can jump another and land in that zero hole. Your move array has that information but is not that easy to get to. You may want to consider reconstructing this array so that it is a nx3 matrix where the first column is the starting peg hole, the second column is the peg hole that is being jumped, and the third column is the destination peg hole:
pegHoleMoves = [
1 2 3;
1 6 10;
2 3 4;
2 7 11;
% etc.
];
That way, given an initial empty peg hole of X, you could find all the possible jumps from a starting peg to land in that empty peg hole:
idcs = find(pegHoleMoves(:,3)==X);
idcs is a list of row indices into pegHoleMoves that allow for a jump to end up in peg hole X. If the list has one index, then there is only one possible move from pegHoleMoves(idcs,1). If the list has more than one element, they you can randomly pick a starting peg from idcs. Once you have that starting peg it gets a little easier. You make the jump, and clear out the jumped peg hole from your p_board and you could do the following: suppose that you jumped peg hole 5. It can never be jumped again, so remove it from your list of possible jumps:
pegHoleMoves(pegHoleMoves(:,2)==5) = [];
In the above, the second column represents the middle peg holes of the jump. Since peg hole 5 has been cleared, then we want to find all such moves where 5 is in the middle and remove them from the list (and you can do this by setting the row to []). Try it out and verify that it does what it should. You should also do this for the initial peg (X) that has been removed, since that peg hole can never be jumped either. This way, your list of possible moves decreases over time...
Vinay
on 8 May 2014
Geoff Hayes
on 9 May 2014
The 21 is expected, as it must be that the 21rst row of pegHoleMoves as the value X in its third column. find returns the indices of the rows where the third column has a value that matches X. For example if:
A = [1;1;2;2;3;3;4;4;3;3;2;2];
find(A==1)
ans =
[1 2]
since 1 is at A(1) and A(2). Type help find for details.
Vinay
on 10 May 2014
Geoff Hayes
on 14 May 2014
Anytime. Glad to have been able to help!
Answers (0)
Categories
Find more on Just for fun 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!