Skip to content

Instantly share code, notes, and snippets.

@paulerickson
Created April 26, 2009 05:31
Show Gist options
  • Save paulerickson/101895 to your computer and use it in GitHub Desktop.
Save paulerickson/101895 to your computer and use it in GitHub Desktop.
//
//I compulsively reformat as I read, so don't expect the line numbers to match yours
//
1 import java.awt.*;
2 import java.awt.event.*;
3
4 public class ThePanel extends Panel{
5 private int boardsInList;
6 private PegBoard[] list;
7
8 public ThePanel(){
9
10 setLayout(null);
11 setBackground(new Color(200, 225, 255));
12
13 boardsInList = 0;
14 list = new PegBoard[14]; //*** You make an array of size 14, but have no checks to make sure it doesn't exceed this ***
15
16 PegBoard b = new PegBoard();
17
18 int r = 0, c = 0;
19 do{
20 r = (int)(Math.random() * 5);
21 c = (int)(Math.random() * 5);
22 }
23 while (r + c >= 5);
24
25 b.removePeg(r, c); //have it so that one can enter choice of initial peg to move
26
27 try{
28 findSolution(b);
29 }
30 catch(FoundSolutionException e){
31 System.out.println("we got thus far");
32 }
33 }
34
35 public void paint(Graphics g){
36 System.out.println("we got thus far");
37
38 //Put tester with System.out.println("painting") here to see if method is working
39 for(int n = 0; n < boardsInList; n++)
40 list[n].paintingPegBoards(g, 12 + (108 * (n % 7)), 12 + (108 * (n / 7)));
41 }
42
43 private void findSolution(PegBoard b) throws FoundSolutionException{
44 if (b == null)
45 return;
46
47 list[boardsInList++] = b; //*** I'd expect it to break on this line
48
49 if (b.pegsLeft() == 1)
50 throw new FoundSolutionException();
51
52 for (int row = 0; row < 5; row++)
53 for (int col = 0; col < 5; col++)
54 for (int direction = 0; direction < 6; direction++)
55 findSolution(b.boardAfterJump(row, col, direction));
56 boardsInList--;
57 }
58
59 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment