Peg Solitaire

# Peg Solitaire

Télécharger la présentation

## Peg Solitaire

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. By Michael Coco Peg Solitaire

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 00 01 02 03 04 05 06 07 08 10 11 12 13 14 15 16 17 18 20 21 22 23 23 25 26 27 28 30 31 32 33 34 35 36 37 38 40 41 42 43 44 45 46 47 48 50 51 52 53 54 55 56 57 58 60 61 62 63 64 65 66 67 68 70 71 72 73 74 75 76 77 78 80 81 82 83 84 85 86 87 88

4. Example of the board (cross) 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 2 2 2 2 2 2 1 1 1 2 2 2 2 1 1 1 1 1 1 1 2 2 1 1 1 0 1 1 1 2 2 1 1 1 1 1 1 1 2 2 2 2 1 1 1 2 2 2 2 2 2 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2

5. Setting up the Algorithm

6. Initial Declarations Free 0; Moves 0; Pegs 0; Occupied 1; Wall 2; 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 = 4 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 2 2 2 2 2 2 1 1 1 2 2 2 2 1 1 1 1 1 1 1 2 2 1 1 1 011 1 2 2 1 1 1 1 1 1 1 2 2 2 2 1 1 1 2 2 2 2 2 2 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2

12. Example (continued) 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 2 2 2 2 2 2 1 1 1 2 2 2 2 2 2 1 1 1 2 2 2 2 2 2 1 1 1 2 2 2 2 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 2 2 1 1 1 0 1 1 1 2 2 1 1 1 1 0 0 1 2 2 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 2 2 2 2 1 1 1 2 2 2 2 2 2 1 1 1 2 2 2 2 2 2 1 1 1 2 2 2 2 2 2 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

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