// Fred's graphical display of the PEGGED Programmer Of The Month // (POTM) solution for the 1Q/1997 PEGGED puzzle. Details at: // http://www.cs.washington.edu/homes/corin/POTM.PAGES/ for the USofA // http://www.lbi.ro/potm/ in Europe // Adapted from the original by John Cottrell at jcottrel@ix.netcom.com // The original plays an interactive remove the pegs game. Try it at: // http://ddi.digital.net/~cottrell/Peg2.html // Beware: this is my first Java program ... the fact that it works at // all is a tribute to the language (since I'm not much of a programmer). // I've tried to comment it pretty well so that I can figure out what // it does, but there may be remnants of the original stuff from John's // program that I missed ... use or steal or whatever as you wish! // Assumes we start on a 490x250 display with an additional 30 pixels // above the display area for the buttons - fit pegs to the display // called with: // *********************************************************************** import java.awt.*; public class applet4 extends java.applet.Applet { final boolean DEBUG = false; int spacing, holedia, pegdia, hoffset, voffset; int jump_peg, diadiff; int x_temp, y_temp; int index=0; int row,column; long millis; Button reset, slow, fast, step, counter; int PEG='X' ; int HOLE='O' ; int SPACE='_' ; // This is the board map ... it is generated from a preprocessing // program that reads the problem file and the solution file // ################### BEGIN DATA INPUT ##################### int rowlim=9; int collim=15; int moves=70; // 'O' is an open hole, 'X' is a filled hole, '_' is not available int board_table[][] = { { 'X','X','X','X','O','O','X','X','O','O','X','X','O','X','X','0'}, { 'O','_','O','O','X','O','X','X','X','O','X','O','O','_','O','0'}, { 'O','_','X','X','X','O','X','X','X','O','X','X','X','_','O','0'}, { '_','_','X','O','X','_','_','_','_','_','X','X','X','_','O','0'}, { '_','_','X','O','X','X','X','O','X','X','X','X','X','_','_','0'}, { '_','_','X','O','X','X','X','X','X','O','O','X','X','_','_','0'}, { 'X','X','X','O','O','O','X','X','X','O','X','X','X','_','_','0'}, { 'X','O','O','X','X','X','X','X','O','O','X','X','X','_','X','0'}, { '_','_','_','_','_','O','O','O','O','O','X','X','X','X','X','0'}, }; // zero is a walk, one is a jump ... the other four numbers are // the starting row/column and the ending row/column. // unlike the puzzle, the first row and column are ZERO. int moves_table[][] = { { 1 ,0,2,0,4 }, { 1 ,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8,14,8,12 }, { 1 ,8,11,8,13 }, { 1 ,6,12,8,12 }, { 1 ,8,13,8,11 }, { 1 ,8,11,8,9 }, }; // ################### END DATA INPUT ##################### // ########################################################################## public void init() { // Assumes we start on a 490x250 display with an additional 30 pixels // above the display area for the buttons - fit pegs to the display // called with: spacing = (245/collim)*2; // space between holes (even #pixels) if (spacing > (125/rowlim)*2) spacing = (125/rowlim)*2; holedia = spacing-2; // size of holes (pixels) pegdia = holedia-2; // size of pegs (pixels) diadiff=(holedia-pegdia)/2; hoffset=(490-collim*spacing)/2; // horizontal offset (to center pic) voffset=30+(250-rowlim*spacing)/2;// vertical offset (to center pic) // x increases left to right - like the column number // y increases top to bottom - like the row - add 30 because of title this.setLayout(new FlowLayout(1,5,5)); slow = new Button("SLOWER"); reset = new Button("SOLVE BOARD"); step = new Button("STEP BY STEP"); fast = new Button("FASTER"); counter = new Button(String.valueOf(moves)); this.add(slow); this.add(reset); this.add(step); this.add(fast); this.add(counter); millis=10000; // Start off very fast Graphics g = getGraphics(); this.showStatus("THIS IS THE STARTING POSITION."); } // end init // ########################################################################## public void rest() { int jj, timer=0; for(jj=0; jj-->