By Michael Coco
Setting up the board Two dimensional array board[x][y] First column second row is 10 Walls will be labeled with 2 Open positions will be labeled with 0 Occupied positions with label 1
Setting up the board
Example of the board (cross)
Setting up the Algorithm
Initial Declarations Free0; Moves0; Pegs0; Occupied1; Wall2; Board[][]; xx[]={0,1,0,-1} yy[]={-1,0,1,0} dirSouthdirEast Are both defined by the size of the array
Main Function for x=0 to N for t=0 to N if board[x][t] == 1 pegs++; end if if (moves(pegs)) finished(); else “No solution yet”
moves(pegs) If pegs==1 Return True; for x=0 to N for t=0 to N if board[x][t] == 1 for q=dirSouth to dirEast x2 = x+xx[q]; y2 = y+yy[q]; if board[x2][y2] == 1 x3 = x2+xx[q]; y3 = y2+yy[q];
moves(pegs) continued if board[x3][y3]==0 board[x][y]=0; board[x2][y2]=0; board[x3][y3]=1; pegs--; push(board,x,y,q) if pegs==1 return True;
push(board,x,y,q) The stack will hold the board[x][y] values and the positions that it was switched with incase moves must be backed up.
Examle of board x=6; y=6 xx=0; yy=-1 X2 = 6; y2 = 5 X3 = 6; y2 =
Example (continued)
Moves(peg) continued… The Algorithm continues, checking up, down and side to side. When it comes to a single occupied position it will backup and try another angle pop(); board[x][y]=1; board[x2][y2] = 1; board[x3][y3] = 0; pegs++; Return False
pop Pops the moves off the stack when moves have to be backed up.
The moves with q value are the moves that you must make to solve the algorithm. However this is not the only solution, it is simply the first one this algorithm has found