Download presentation

Presentation is loading. Please wait.

Published byGeorge Leftwich Modified about 1 year ago

1
By Michael Coco

2
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

3
Setting up the board

4
Example of the board (cross)

5
Setting up the Algorithm

6
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

7
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”

8
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];

9
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;

10
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.

11
Examle of board x=6; y=6 xx=0; yy=-1 X2 = 6; y2 = 5 X3 = 6; y2 =

12
Example (continued)

13
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

14
pop Pops the moves off the stack when moves have to be backed up.

15
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

16

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google