A few old code files from my undergraduate CS122 class; these are all written as Java applets (as was the fashion at the time), so I’m just making the code available. They do still run though (thanks Java!)
Infix to PostFix Converter & Calculator
Supports (a) variable, negation, parentheses, functions, and multi-digit numbers.
MyPanel.java
// Shawn O'Neil
// Assignment 3d - Multiple Digit RPN Calculator
import java.awt.*;
import java.awt.event.*;
public class MyPanel extends Panel
{
private TextField infix, x, postfix, value;
private Label infixlabel, xlabel, postfixlabel, valuelabel;
private Manager output;
public MyPanel()
{
setLayout(null);
infixlabel = new Label("InFix:");
infixlabel.setLocation(10,10);
infixlabel.setSize(50, 25);
add(infixlabel);
infix = new TextField();
infix.setLocation(10, 40);
infix.setSize(200, 25);
add(infix);
xlabel = new Label("x:");
xlabel.setLocation(10,80);
xlabel.setSize(50, 25);
add(xlabel);
x = new TextField();
x.setLocation(10, 110);
x.setSize(200, 25);
add(x);
postfixlabel = new Label("PostFix:");
postfixlabel.setLocation(10,150);
postfixlabel.setSize(50, 25);
add(postfixlabel);
postfix = new TextField();
postfix.setLocation(10, 180);
postfix.setSize(200, 25);
add(postfix);
valuelabel = new Label("Value:");
valuelabel.setLocation(10, 220);
valuelabel.setSize(50,25);
add(valuelabel);
value = new TextField();
value.setLocation(10, 250);
value.setSize(200, 25);
add(value);
output = new Manager(infix, x, postfix, value);
infix.addActionListener(output);
x.addActionListener(output);
}
}
PanelApplet.java
/*
applet that runs a panel
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class PanelApplet extends Applet
{
Panel panel;
public void init()
{
setLayout(null);
panel = new MyPanel();
add(panel);
panel.setSize(getSize().width, getSize().height);
panel.setLocation(0,0);
panel.setVisible(true);
panel.requestFocus();
if (panel instanceof KeyListener)
addKeyListener((KeyListener)panel);
}
}
Manager.java
// Shawn O'Neil
// Assignment 3d - Multiple Digit RPN Calculator
import java.awt.*;
import java.awt.event.*;
public class Manager implements ActionListener
{
private String poststring, instring;
private DStack ds;
private TextField infix, x, postfix, value;
private double valuedouble, xdouble;
private double numbers[];
public Manager(TextField i, TextField y, TextField p, TextField v)
{
instring = "";
poststring = "";
valuedouble = 0;
xdouble = 0;
infix = i;
postfix = p;
value = v;
x = y;
DStack ds = new DStack();
numbers = new double[100];
}
public void actionPerformed(ActionEvent e)
{
xdouble = convert(x.getText());
instring = infix.getText();
poststring = convertfix(instring);
valuedouble = calculate(poststring);
postfix.setText(poststring);
value.setText(""+valuedouble);
}
public double convert(String s)
{
String error = "";
double d = -99999;
boolean ok = true;
s = s.trim();
try
{
d = (new Double(s)).doubleValue();
}
catch (Throwable err)
{
error += err;
ok = false;
}
return d;
}
public String convertfix(String in)
{
int number = 0;
instring = in;
instring += ' ';
CStack cs = new CStack();
poststring = "";
boolean firstTime = true;
for(int n = 0; n<instring.length(); n++)
{
char ch = instring.charAt(n);
if(firstTime && ch == '-')
ch = '$';
firstTime = false;
switch(ch)
{
case 'x':
{
poststring += ch;
break;
}
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '.':
{
poststring += '#';
int m = n+1;
while(isADigit(instring.charAt(m))) m++;
numbers[number++] = convert(instring.substring(n,m));
n = m-1;
break;
}
case '+': case '-': case '/': case '*': case '^': case '$':
{
while(leftFirst(cs.peek(), ch))
poststring += cs.pop();
cs.push(ch);
break;
}
case '(':
{
cs.push(ch);
firstTime = true;
break;
}
case ')':
{
while(cs.peek() != '(')
poststring += cs.pop();
cs.pop();
break;
}
case 'r': case 'e': case 'l': case 's': case 'c': case 'a':
{
cs.push('(');
cs.push(ch);
n++;
firstTime = true;
break;
}
}
}
while(!cs.empty())
poststring += cs.pop();
return poststring;
}
private boolean leftFirst(char a, char b)
{
if( a == '^' && b == '^')
return false;
else if(rank(a) >= rank(b))
return true;
return false;
}
private int rank(char c)
{
switch(c)
{
case '+': case '-':
return 1;
case '*': case '/':
return 2;
case '$':
return 3;
case '^':
return 4;
default: return 0;
}
}
private boolean isADigit(char c)
{
if('0' <= c && c <= '9') return true;
if(c == '.') return true;
return false;
}
public double calculate(String t)
{
int number = 0;
DStack ds = new DStack();
for(int n=0; n<t.length(); n++)
{
char ch = t.charAt(n);
switch(ch)
{
case '#':
{
ds.push(numbers[number++]);
break;
}
case 'x':
ds.push(xdouble);
break;
case '+':
{
double a = ds.pop();
double b = ds.pop();
ds.push(b+a);
break;
}
case '-':
{
double a = ds.pop();
double b = ds.pop();
ds.push(b-a);
break;
}
case '*':
{
double a = ds.pop();
double b = ds.pop();
ds.push(b*a);
break;
}
case '/':
{
double a = ds.pop();
double b = ds.pop();
ds.push(b/a);
break;
}
case '$':
{
double a = ds.pop();
ds.push(-a);
break;
}
case '^':
{
double a = ds.pop();
double b = ds.pop();
ds.push(Math.pow(b,a));
break;
}
case 'r':
{
double a = ds.pop();
ds.push(Math.sqrt(a));
break;
}
case 's':
{
double a = ds.pop();
ds.push(Math.sin(a));
break;
}
case 'c':
{
double a = ds.pop();
ds.push(Math.cos(a));
break;
}
case 'a':
{
double a = ds.pop();
ds.push(Math.abs(a));
break;
}
case 'l':
{
double a = ds.pop();
ds.push(Math.log(a));
break;
}
}
}
return ds.pop();
}
}
CStack.java
// Shawn O'Neil
// Assignment 3d - Multiple Digit RPN Calculator
import java.awt.*;
import java.awt.event.*;
public class Manager implements ActionListener
{
private String poststring, instring;
private DStack ds;
private TextField infix, x, postfix, value;
private double valuedouble, xdouble;
private double numbers[];
public Manager(TextField i, TextField y, TextField p, TextField v)
{
instring = "";
poststring = "";
valuedouble = 0;
xdouble = 0;
infix = i;
postfix = p;
value = v;
x = y;
DStack ds = new DStack();
numbers = new double[100];
}
public void actionPerformed(ActionEvent e)
{
xdouble = convert(x.getText());
instring = infix.getText();
poststring = convertfix(instring);
valuedouble = calculate(poststring);
postfix.setText(poststring);
value.setText(""+valuedouble);
}
public double convert(String s)
{
String error = "";
double d = -99999;
boolean ok = true;
s = s.trim();
try
{
d = (new Double(s)).doubleValue();
}
catch (Throwable err)
{
error += err;
ok = false;
}
return d;
}
public String convertfix(String in)
{
int number = 0;
instring = in;
instring += ' ';
CStack cs = new CStack();
poststring = "";
boolean firstTime = true;
for(int n = 0; n<instring.length(); n++)
{
char ch = instring.charAt(n);
if(firstTime && ch == '-')
ch = '$';
firstTime = false;
switch(ch)
{
case 'x':
{
poststring += ch;
break;
}
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '.':
{
poststring += '#';
int m = n+1;
while(isADigit(instring.charAt(m))) m++;
numbers[number++] = convert(instring.substring(n,m));
n = m-1;
break;
}
case '+': case '-': case '/': case '*': case '^': case '$':
{
while(leftFirst(cs.peek(), ch))
poststring += cs.pop();
cs.push(ch);
break;
}
case '(':
{
cs.push(ch);
firstTime = true;
break;
}
case ')':
{
while(cs.peek() != '(')
poststring += cs.pop();
cs.pop();
break;
}
case 'r': case 'e': case 'l': case 's': case 'c': case 'a':
{
cs.push('(');
cs.push(ch);
n++;
firstTime = true;
break;
}
}
}
while(!cs.empty())
poststring += cs.pop();
return poststring;
}
private boolean leftFirst(char a, char b)
{
if( a == '^' && b == '^')
return false;
else if(rank(a) >= rank(b))
return true;
return false;
}
private int rank(char c)
{
switch(c)
{
case '+': case '-':
return 1;
case '*': case '/':
return 2;
case '$':
return 3;
case '^':
return 4;
default: return 0;
}
}
private boolean isADigit(char c)
{
if('0' <= c && c <= '9') return true;
if(c == '.') return true;
return false;
}
public double calculate(String t)
{
int number = 0;
DStack ds = new DStack();
for(int n=0; n<t.length(); n++)
{
char ch = t.charAt(n);
switch(ch)
{
case '#':
{
ds.push(numbers[number++]);
break;
}
case 'x':
ds.push(xdouble);
break;
case '+':
{
double a = ds.pop();
double b = ds.pop();
ds.push(b+a);
break;
}
case '-':
{
double a = ds.pop();
double b = ds.pop();
ds.push(b-a);
break;
}
case '*':
{
double a = ds.pop();
double b = ds.pop();
ds.push(b*a);
break;
}
case '/':
{
double a = ds.pop();
double b = ds.pop();
ds.push(b/a);
break;
}
case '$':
{
double a = ds.pop();
ds.push(-a);
break;
}
case '^':
{
double a = ds.pop();
double b = ds.pop();
ds.push(Math.pow(b,a));
break;
}
case 'r':
{
double a = ds.pop();
ds.push(Math.sqrt(a));
break;
}
case 's':
{
double a = ds.pop();
ds.push(Math.sin(a));
break;
}
case 'c':
{
double a = ds.pop();
ds.push(Math.cos(a));
break;
}
case 'a':
{
double a = ds.pop();
ds.push(Math.abs(a));
break;
}
case 'l':
{
double a = ds.pop();
ds.push(Math.log(a));
break;
}
}
}
return ds.pop();
}
}
DStack.java
public class DStack
{
private double data[];
private int top;
public DStack()
{
data = new double[100];
top = -1;
}
public void push(double c)
{
if(top<100)
{
top++;
data[top] = c;
}
}
public double pop()
{
if(top < 0)
return -99999.99;
return data[top--];
}
public boolean empty()
{
return(top<0);
}
}
“Queens” Problem Solver
Supports various board sizes, displays types of solutions, and removes duplicate solutions.
MyPanel.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MyPanel extends Panel
{
private Label rowsLabel;
private TextField rowsField;
private BoardList list1, list2, list3, list4;
private ListCanvas listCanvas;
private Manager manager;
private Button go, shownext, showprev, all, unique, symetric, elegant;
private StatsCanvas statsCanvas;
public MyPanel()
{
setLayout(null);
rowsLabel = new Label("Rows:");
rowsLabel.setLocation(300, 10);
rowsLabel.setSize(90, 20);
add(rowsLabel);
rowsField = new TextField();
rowsField.setLocation(300, 35);
rowsField.setSize(90, 25);
add(rowsField);
go = new Button("Go");
go.setLocation(300, 70);
go.setSize(90,25);
add(go);
shownext = new Button("Show Next");
shownext.setLocation(300, 95);
shownext.setSize(90,25);
add(shownext);
showprev = new Button("Show Prev");
showprev.setLocation(300, 120);
showprev.setSize(90,25);
add(showprev);
all = new Button("A");
all.setLocation(300, 145);
all.setSize(20, 25);
add(all);
all.setBackground(new Color(100, 100, 100));
unique = new Button("U");
unique.setLocation(322, 145);
unique.setSize(20, 25);
add(unique);
unique.setBackground(new Color(100, 255, 100));
symetric = new Button("S");
symetric.setLocation(344, 145);
symetric.setSize(20, 25);
add(symetric);
symetric.setBackground(new Color(100, 100, 255));
elegant = new Button("E");
elegant.setLocation(366, 145);
elegant.setSize(20, 25);
add(elegant);
elegant.setBackground(new Color(255, 100, 100));
list1 = new BoardList();
list2 = new BoardList();
list3 = new BoardList();
list4 = new BoardList();
listCanvas = new ListCanvas(list1, list2, list3, list4);
listCanvas.setLocation(10, 10);
listCanvas.setSize(280, 280);
add(listCanvas);
listCanvas.setBackground(new Color(200, 200, 255));
statsCanvas = new StatsCanvas(list1, list2, list3, list4);
statsCanvas.setLocation(300, 180);
statsCanvas.setSize(90, 110);
add(statsCanvas);
statsCanvas.setBackground(new Color(200, 200, 255));
manager = new Manager(rowsField, list1, list2, list3, list4, listCanvas, statsCanvas, shownext, showprev, all, unique, symetric, elegant);
rowsField.addActionListener(manager);
go.addActionListener(manager);
shownext.addActionListener(manager);
showprev.addActionListener(manager);
all.addActionListener(manager);
unique.addActionListener(manager);
symetric.addActionListener(manager);
elegant.addActionListener(manager);
}
}
PanelApplication.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class PanelApplication implements WindowListener
{
public static final int PANEL_WIDTH = 400; // <-- put window size here
public static final int PANEL_HEIGHT = 300;
private static PanelApplication panelApplication;
private Panel panel;
private Frame frame;
public static void main(String args[])
{
panelApplication = new PanelApplication();
}
private PanelApplication()
{
panel = new MyPanel(); // <-- your program name here
panel.setSize(PANEL_WIDTH, PANEL_HEIGHT);
panel.setLocation(0, 0);
panel.setVisible(true);
panel.requestFocus();
frame = new AdjustingFrame("My Program", panel);
frame.addWindowListener(this);
frame.setSize(PANEL_WIDTH, PANEL_HEIGHT);
frame.setLocation(20, 20);
frame.setVisible(true);
frame.setLayout(null);
if (panel instanceof KeyListener)
frame.addKeyListener((KeyListener)panel);
}
public void windowActivated (WindowEvent e) {}
public void windowClosed (WindowEvent e) {}
public void windowClosing (WindowEvent e)
{
frame.dispose();
System.exit(0);
}
public void windowDeactivated (WindowEvent e) {}
public void windowDeiconified (WindowEvent e) {}
public void windowIconified (WindowEvent e) {}
public void windowOpened (WindowEvent e) {}
}
Manager.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Manager implements ActionListener
{
private int count1, count2, count3, count4, size, showing, showtype;
private BoardList list1, list2, list3, list4;
private TextField rowsField;
private ListCanvas listCanvas;
private StatsCanvas statsCanvas;
private Button shownext, showprev, all, unique, symetric, elegant;
public Manager(TextField rf, BoardList lst1, BoardList lst2, BoardList lst3, BoardList lst4, ListCanvas can, StatsCanvas sc, Button sn, Button sp, Button al, Button uq, Button sy, Button el)
{
showtype = 1;
list1 = lst1;
list2=lst2;
list3 = lst3;
list4 = lst4;
shownext = sn;
showprev = sp;
statsCanvas = sc;
count1 = 0;
count2 = 0;
count3 = 0;
count4 = 0;
size = 0;
rowsField = rf;
listCanvas = can;
all = al;
unique = uq;
symetric = sy;
elegant = el;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == shownext)
{
if(showtype == 1)
{
if(list1.showing < list1.getHowMany())
{
listCanvas.showtype = showtype;
listCanvas.godirection = 1;
listCanvas.repaint();
statsCanvas.repaint();
}
}
else if(showtype == 2)
{
if(list2.showing < list2.getHowMany())
{
listCanvas.showtype = showtype;
listCanvas.godirection = 1;
listCanvas.repaint();
statsCanvas.repaint();
}
}
else if(showtype == 3)
{
if(list3.showing < list3.getHowMany())
{
listCanvas.showtype = showtype;
listCanvas.godirection = 1;
listCanvas.repaint();
statsCanvas.repaint();
}
}
else if(showtype == 4)
{
if(list4.showing < list4.getHowMany())
{
listCanvas.showtype = showtype;
listCanvas.godirection = 1;
listCanvas.repaint();
statsCanvas.repaint();
}
}
}
else if(e.getSource() == showprev)
{
if(showtype == 1)
{
if(list1.showing > 1)
{
listCanvas.showtype = showtype;
listCanvas.godirection = -1;
listCanvas.repaint();
statsCanvas.repaint();
}
}
else if(showtype == 2)
{
if(list2.showing > 1)
{
listCanvas.showtype = showtype;
listCanvas.godirection = -1;
listCanvas.repaint();
statsCanvas.repaint();
}
}
else if(showtype == 3)
{
if(list3.showing > 1)
{
listCanvas.showtype = showtype;
listCanvas.godirection = -1;
listCanvas.repaint();
statsCanvas.repaint();
}
}
else if(showtype == 4)
{
if(list4.showing > 1)
{
listCanvas.showtype = showtype;
listCanvas.godirection = -1;
listCanvas.repaint();
statsCanvas.repaint();
}
}
}
else if(e.getSource() == all)
{
showtype = 1;
count1 = 0;
count2 = 0;
count3 = 0;
count4 = 0;
list1.head = null;
list2.head = null;
list3.head = null;
list4.head = null;
size = convert(rowsField.getText());
makeFuller(new Board(size));
statsCanvas.showtype = showtype;
listCanvas.showtype = showtype;
listCanvas.godirection = 1;
listCanvas.resetShowing();
listCanvas.repaint();
statsCanvas.repaint();
}
else if(e.getSource() == unique)
{
showtype = 2;
count1 = 0;
count2 = 0;
count3 = 0;
count4 = 0;
list1.head = null;
list2.head = null;
list3.head = null;
list4.head = null;
size = convert(rowsField.getText());
makeFuller(new Board(size));
statsCanvas.showtype = showtype;
listCanvas.showtype = showtype;
listCanvas.godirection = 1;
listCanvas.resetShowing();
listCanvas.repaint();
statsCanvas.repaint();
}
else if(e.getSource() == symetric)
{
showtype = 3;
count1 = 0;
count2 = 0;
count3 = 0;
count4 = 0;
list1.head = null;
list2.head = null;
list3.head = null;
list4.head = null;
size = convert(rowsField.getText());
makeFuller(new Board(size));
statsCanvas.showtype = showtype;
listCanvas.showtype = showtype;
listCanvas.godirection = 1;
listCanvas.resetShowing();
listCanvas.repaint();
statsCanvas.repaint();
}
else if(e.getSource() == elegant)
{
showtype = 4;
count1 = 0;
count2 = 0;
count3 = 0;
count4 = 0;
list1.head = null;
list2.head = null;
list3.head = null;
list4.head = null;
size = convert(rowsField.getText());
makeFuller(new Board(size));
statsCanvas.showtype = showtype;
listCanvas.showtype = showtype;
listCanvas.godirection = 1;
listCanvas.resetShowing();
listCanvas.repaint();
statsCanvas.repaint();
}
else
{
count1 = 0;
count2 = 0;
count3 = 0;
count4 = 0;
list1.head = null;
list2.head = null;
list3.head = null;
list4.head = null;
size = convert(rowsField.getText());
makeFuller(new Board(size));
statsCanvas.showtype = showtype;
listCanvas.showtype = showtype;
listCanvas.godirection = 1;
listCanvas.resetShowing();
listCanvas.repaint();
statsCanvas.repaint();
}
}
public void makeFuller(Board b)
{
if(b == null)
return;
if(b.full())
{
int type = 2;
if(b.toString().compareTo(b.reverse().upsideDown().toString()) == 0)
type = 3;
if(b.toString().compareTo(b.inverse().reverse().toString()) == 0)
type = 4;
if(b.toString().compareTo(b.reverse().toString()) > 0)
type = 1;
if(b.toString().compareTo(b.upsideDown().toString()) > 0)
type = 1;
if(b.toString().compareTo(b.inverse().toString()) > 0)
type = 1;
if(b.toString().compareTo(b.reverse().upsideDown().toString()) > 0)
type = 1;
if(b.toString().compareTo(b.inverse().reverse().toString()) > 0)
type = 1;
if(b.toString().compareTo(b.inverse().upsideDown().toString()) > 0)
type = 1;
if(b.toString().compareTo(b.inverse().reverse().upsideDown().toString()) > 0)
type = 1;
//count++;
if(type == 1)
{
count1++;
list1.addToEnd(count1, b, type);
}
else if(type == 2)
{
count1++;
count2++;
list1.addToEnd(count1, b, type);
list2.addToEnd(count2, b, type);
}
else if(type == 3)
{
count1++;
count2++;
count3++;
list1.addToEnd(count1, b, type);
list2.addToEnd(count2, b, type);
list3.addToEnd(count3, b, type);
}
else if(type == 4)
{
count1++;
count2++;
count3++;
count4++;
list1.addToEnd(count1, b, type);
list2.addToEnd(count2, b, type);
list3.addToEnd(count3, b, type);
list4.addToEnd(count4, b, type);
}
}
else
for(int r = 0; r<size; r++)
makeFuller(b.makeAFullerBoard(r));
}
private int convert(String s)
{
try
{
return (new Integer(s)).intValue();
}
catch (Throwable t)
{
return -9999;
}
}
}
StatsCanvas.java
import java.awt.*;
import java.awt.event.*;
public class StatsCanvas extends Canvas
{
private BoardList list1, list2, list3, list4;
public int showtype;
public StatsCanvas(BoardList lst1, BoardList lst2, BoardList lst3, BoardList lst4)
{
showtype = 1;
list1 = lst1;
list2 = lst2;
list3 = lst3;
list4 = lst4;
}
public void paint(Graphics g)
{
int total = list1.getHowMany();
g.drawString("Total = "+total, 10, 10);
if(showtype == 1)
{
g.drawString(list1.showing+" of "+list1.getHowMany(), 10, 22);
}
if(showtype == 2)
{
g.drawString(list2.showing+" of "+list2.getHowMany(), 10, 22);
}
if(showtype == 3)
{
g.drawString(list3.showing+" of "+list3.getHowMany(), 10, 22);
}
if(showtype == 4)
{
g.drawString(list4.showing+" of "+list4.getHowMany(), 10, 22);
}
}
}
Node.java
import java.awt.*;
import java.awt.event.*;
public class Node
{
private Board board;
private int number, type;
private Node link;
public Node(int i, Board b, int t, Node n)
{
type = t;
number = i;
board = b;
link = n;
}
public void paint(Graphics g, int showing, int showtype)
{
Color color = Color.black;
if(number == showing)
{
if(type == 2)
color = Color.green;
if(type == 3)
color = Color.blue;
if(type == 4)
color = Color.red;
board.paint(g, color);
//g.drawString(""+number, 20, 20);
//g.drawString(""+board.toString(), 100, 20);
}
else if(link != null)
link.paint(g, showing, showtype);
}
public Node getLink()
{
return link;
}
public int getNumber()
{
return number;
}
public Board getBoard()
{
return board;
}
public void addToEnd(int i, Board b, int t)
{
if(link != null)
link.addToEnd(i, b, t);
else
link = new Node(i, b, t, null);
}
public int getHowMany()
{
int me = 1;
if(link == null)
return me;
else
return link.getHowMany() + me;
}
}
Board.java
import java.awt.*;
import java.awt.event.*;
public class Board
{
private int howMany, size;
private int rows[];
public Board(int s)
{
size = s;
howMany = 0;
rows = new int[size];
}
public boolean full()
{
return howMany == size;
}
public void paint(Graphics g, Color color)
{
for(int l = 1; l < size+2; l++)
{
g.drawLine(20*l, 20, 20*l, 20 + 20*size);
g.drawLine(20, 20*l, 20 + 20*size, 20*l);
}
g.setColor(color);
for(int c = 0; c < howMany; c++)
g.fillOval(20*c+2+20, 20*rows[c]+2+20, 15, 15);
}
public Board makeAFullerBoard(int row)
{
if(full())
return null;
if(ewMatch(row))
return null;
if(diagMatch(row))
return null;
Board b = new Board(size);
b.howMany = howMany +1;
for(int c = 0; c < howMany; c++)
b.rows[c] = rows[c];
b.rows[howMany] = row;
return b;
}
public Board reverse()
{
if(!full()) return null;
Board b = new Board(size);
b.howMany = howMany;
for(int c = 0, cc = howMany-1; c<howMany; c++, cc--)
b.rows[cc] = rows[c];
return b;
}
public Board upsideDown()
{
if(!full()) return null;
Board b = new Board(size);
b.howMany = howMany;
for(int c = 0; c<howMany; c++)
b.rows[c] = howMany - 1 - rows[c];
return b;
}
public Board inverse()
{
if(!full()) return null;
Board b = new Board(size);
b.howMany = howMany;
for(int c = 0; c<howMany; c++)
b.rows[rows[c]] = c;
return b;
}
private boolean ewMatch(int r)
{
for(int c = 0; c < howMany; c++)
if(r == rows[c])
return true;
return false;
}
public String toString()
{
String s = "";
for(int c = 0; c < howMany; c++)
s += rows[c];
return s;
}
private boolean diagMatch(int r)
{
for(int c = 0; c < howMany; c++)
if(howMany - c == Math.abs(r - rows[c]))
return true;
return false;
}
}
BoardList.java
import java.awt.*;
import java.awt.event.*;
public class BoardList
{
public Node head;
public int showing, showtype;
public BoardList()
{
showtype = 1;
showing = 0;
head = null;
}
public void paint(Graphics g)
{
if (head == null)
g.drawString("No Solutions.", 20, 20);
else
head.paint(g, showing, showtype);
}
public void resetShowing()
{
showing = 0;
}
public void showNext(Graphics g)
{
if (head == null)
{
g.drawString("No Solutions.", 20, 20);
return;
}
else
{
showing++;
head.paint(g, showing, showtype);
}
}
public void showPrev(Graphics g)
{
if (head == null)
g.drawString("No Solutions.", 20, 20);
else
{
showing--;
head.paint(g, showing, showtype);
}
}
public void addToEnd(int i, Board b, int t)
{
if(head == null)
head = new Node(i, b, t, null);
else
head.addToEnd(i, b, t);
}
public int getHowMany()
{
return head.getHowMany();
}
public int getShowType()
{
return showtype;
}
}
ListCanvas.java
import java.awt.*;
import java.awt.event.*;
public class ListCanvas extends Canvas
{
private BoardList list1, list2, list3, list4;
public int godirection;
public int showtype;
public ListCanvas(BoardList lst1, BoardList lst2, BoardList lst3, BoardList lst4)
{
list1 = lst1;
list2 = lst2;
list3 = lst3;
list4 = lst4;
showtype = 1;
godirection = 1;
}
public void paint(Graphics g)
{
if(godirection == 1)
{
if(showtype == 1)
list1.showNext(g);
else if(showtype == 2)
list2.showNext(g);
else if(showtype == 3)
list3.showNext(g);
else if(showtype == 4)
list4.showNext(g);
}
else if(godirection == -1)
{
if(showtype == 1)
list1.showPrev(g);
else if(showtype == 2)
list2.showPrev(g);
else if(showtype == 3)
list3.showPrev(g);
else if(showtype == 4)
list4.showPrev(g);
}
}
public void resetShowing()
{
list1.resetShowing();
list2.resetShowing();
list3.resetShowing();
list4.resetShowing();
}
}
“Pegs on a Board” Problem Solver
Two board size options.
MyPanel.java
import java.awt.*;
import java.awt.event.*;
public class MyPanel extends Panel implements ActionListener
{
private Board list[];
private int howMany;
public int showing, size;
private Button shownext, showprev, size5, size6;
private Label sizeLabel, showLabel;
public MyPanel()
{
setLayout(null);
showLabel = new Label("Show Step:");
showLabel.setLocation(300, 10);
showLabel.setSize(90, 20);
add(showLabel);
shownext = new Button("Next");
shownext.setLocation(300, 40);
shownext.setSize(90, 25);
add(shownext);
showprev = new Button("Prev");
showprev.setLocation(300, 70);
showprev.setSize(90, 25);
add(showprev);
sizeLabel = new Label("Board Size:");
sizeLabel.setLocation(300, 100);
sizeLabel.setSize(90, 20);
add(sizeLabel);
size5 = new Button("5");
size5.setLocation(300, 130);
size5.setSize(40, 25);
add(size5);
size6 = new Button("6");
size6.setLocation(350, 130);
size6.setSize(40, 25);
add(size6);
shownext.addActionListener(this);
showprev.addActionListener(this);
size5.addActionListener(this);
size6.addActionListener(this);
size = 5;
howMany = 0;
showing = 0;
list = new Board[7*7];
try
{
search(new Board(size));
}
catch(FoundItException e)
{
repaint();
}
}
public void paint(Graphics g)
{
list[showing].paint(g);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == shownext)
{
if(showing < howMany-1)
showing++;
repaint();
}
if(e.getSource() == showprev)
{
if(showing > 0)
showing--;
repaint();
}
if(e.getSource() == size5)
{
size = 5;
try
{
howMany = 0;
showing = 0;
search(new Board(size));
}
catch(FoundItException x)
{
repaint();
}
}
if(e.getSource() == size6)
{
size = 6;
try
{
howMany = 0;
showing = 0;
search(new Board(size));
}
catch(FoundItException x)
{
repaint();
}
}
}
private void search(Board b) throws FoundItException
{
if(b == null)
return;
list[howMany++] = b;
if(b.solved())
{
throw new FoundItException();
}
else
{
for(int x=0; x<size; x++)
{
for(int y=0; y<size; y++)
{
search(b.jump(x,y, 1, -1));
search(b.jump(x,y, -1, 1));
search(b.jump(x,y, 1, 0));
search(b.jump(x,y, -1, 0));
search(b.jump(x,y, 0, 1));
search(b.jump(x,y, 0, -1));
}
}
}
howMany--;
}
public void keyPressed(KeyEvent e)
{
repaint();
}
}
class FoundItException extends Exception
{
}
PanelApplet.java
/*
applet that runs a panel
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class PanelApplet extends Applet
{
Panel panel;
public void init()
{
setLayout(null);
panel = new MyPanel();
add(panel);
panel.setSize(getSize().width, getSize().height);
panel.setLocation(0,0);
panel.setVisible(true);
panel.requestFocus();
if (panel instanceof KeyListener)
addKeyListener((KeyListener)panel);
}
}
Board.java
import java.awt.*;
import java.awt.event.*;
public class Board
{
private static final int HOLE=0;
private static final int PEG=1;
private static final int NO_HOLE=2;
private int board[][];
private int pegs;
private int size;
public Board(int size)
{
this.size = size;
board = new int[size][size];
for(int x=0; x<size; x++)
for(int y=0; y<size; y++)
if(x+y<size)
board[x][y]=PEG;
else
board[x][y] = NO_HOLE;
board[0][0] = HOLE;
pegs = (size*(size + 1))/2 -1;
}
public Board jump(int row, int col, int dr, int dc)
{
if(board[row][col] != PEG) return null;
if(row+dr<0 || row +dr > (size -1) || col+dc <0 || col+dc >(size -1) || board[row+dr][col+dc] != PEG) return null;
if(row+2*dr<0 || row + 2*dr > (size -1) || col + 2* dc < 0 ||col + 2*dc >(size -1) ||board[row+2*dr][col+2*dc] != HOLE) return null;
Board b = new Board(size);
for(int x = 0; x< size; x++)
for(int y = 0; y<size; y++)
b.board[x][y] = board[x][y];
b.pegs = pegs;
b.board[row][col] = HOLE;
b.board[row + dr][col + dc] = HOLE;
b.board[row + 2*dr][col + 2*dc] = PEG;
b.pegs--;
return b;
}
public boolean solved()
{
if(pegs == 1)
return true;
return false;
}
public String toString()
{
String s = "";
for(int r = (size -1); r>= 0; r--)
{
for(int n = 0; n<r; n++)
s+=" ";
for(int n = 0; n< size-r; n++)
s+=board[r][n] + " ";
s+="\n";
}
return s;
}
public void paint(Graphics g)
{
int b = 0;
for(int r = 0; r < size; r++)
{
for(int n = 0; n< size-r; n++)
{
if(board[r][n] == HOLE)
g.drawOval(21*n+b, 18*r, 15, 15);
if(board[r][n] == PEG)
g.fillOval(21*n+b, 18*r, 15, 15);
}
b+=10;
}
}
}