import java.awt.*; import java.util.Vector; import java.awt.image.*; import java.awt.event.*; import java.applet.*; public class BattleCards extends Applet implements Runnable { Thread myThread; Image offImage; Graphics offGraphics, g; Dimension d; int rows = 10, cols = 13, frame = 75, numPlayers = 2, currPlayer = 0; Vector field; Label labelWidth, labelHeight, labelNumP; TextField fieldWidth, fieldHeight, fieldNumP; Button reset; Color playerColors [] = {Color.blue, Color.red, Color.green, Color.orange, Color.yellow, new Color(0.0f, 0.333f, 0.333f)}; int moveMatrix[][] = {{0, 0, 0},{0, 0, 0},{0, 0, 0}}; Font normal, won; public void init() { int x,y; myThread = new Thread(this); addKeyListener(new KeyHandler()); field = new Vector(); for (x = 0; x < cols; x++) { field.addElement(new Vector()); for (y = 0; y < rows; y++) { ((Vector)field.elementAt(x)).addElement(new BCard()); } } addMouseListener(new MouseHandler()); g = getGraphics(); labelWidth = new Label("# rows:"); add(labelWidth); fieldWidth = new TextField("10", 3); add(fieldWidth); labelHeight = new Label("# cols:"); add(labelHeight); fieldHeight = new TextField("13", 3); add(fieldHeight); labelNumP = new Label("# players:"); add(labelNumP); fieldNumP = new TextField("2", 2); add(fieldNumP); reset = new Button("New Game"); add(reset); reset.addActionListener(new PressButton()); for (x = 0; x < 3; x++) { for (y = 0; y < 3; y++) { moveMatrix[x][y] = (int)(Math.random() * 2); } } moveMatrix[1][1] = 1; setBackground(Color.white); normal = new Font("Helvetica", Font.PLAIN, 12); won = new Font("Courier", Font.BOLD, 28); myThread.run(); } public void start() { myThread = new Thread(this); myThread.start(); } public void stop() { myThread.stop(); myThread = null; } public void run() { while (Thread.currentThread() == myThread) { repaint(); try { Thread.sleep(3); } catch (InterruptedException e) { break; } } } public void paint(Graphics g) { if (offImage == null) { offImage = createImage(800,600); offGraphics = offImage.getGraphics(); } else { offGraphics.setColor(Color.white); offGraphics.fillRect(0,0,800,600); } drawFrame(offGraphics); g.drawImage(offImage, 0, 0, this); } public void update(Graphics g) { paint(g); } public void drawFrame(Graphics g) { int count, x, y, numFilled = 0, largest = -1, largestNum = 0; int totals[]; double x1; d = size(); float colsize = (d.width - 2 * frame) / cols; float rowsize = (d.height - 2 * frame) / rows; g.setColor(Color.black); for (count = frame; count <= d.width - frame; count += colsize) { g.drawLine(count, frame, count, (int)(frame + rows * rowsize)); } for (count = frame; count <= d.height - frame; count += rowsize) { g.drawLine(frame, count, (int)(frame + cols * colsize), count); } g.setColor(playerColors[currPlayer]); for (x = 0; x < 3; x++) { for (y = 0; y < 3; y++) { if (moveMatrix[x][y] == 1) { g.fillRect(d.width - frame + x * frame / 3, y * frame / 3 + 1, frame / 3, frame / 3); } } } g.setColor(Color.black); for (x1 = 1; x1 <= frame; x1 += (double)(frame - 1) / 3) { g.drawLine(d.width - frame, (int)x1, d.width - 1, (int)x1); } for (x1 = d.width - frame; x1 <= d.width; x1 += (double)(frame - 1) / 3) { g.drawLine((int)x1, 1, (int)x1, frame); } for (x = 0; x < cols; x++) { for (y = 0; y < rows; y++) { if (getCell(x,y) != -1) { g.setColor(playerColors[getCell(x,y)]); g.fillRect((int)(frame + x * colsize + 1), (int)(frame + y * rowsize + 1), (int)(colsize - 1), (int)(rowsize - 1)); } } } g.setFont(normal); totals = calcTotals(); for (x = 0; x < numPlayers; x++) { g.setColor(playerColors[x]); g.drawString("Player " + (x + 1) + ": " + totals[x], frame + 85 * x, d.height - frame / 2); numFilled += totals[x]; if (largestNum < totals[x]) { largest = x; largestNum = totals[x]; } } if (numFilled == rows * cols) { g.setColor(Color.black); g.setFont(won); g.drawString("GAME OVER, PLAYER " + (largest + 1) + " WINS!", 250, 300); } } public class KeyHandler implements KeyListener { public void keyPressed(KeyEvent e) { char key = e.getKeyChar(); } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } } public class MouseHandler extends MouseAdapter { public void mousePressed(MouseEvent e) { int x, y, a, b; float colsize = (d.width - 2 * frame) / cols; float rowsize = (d.height - 2 * frame) / rows; x = (int)((e.getX() - frame) / colsize); y = (int)((e.getY() - frame) / rowsize); //System.out.println(x + " " + y + " " + field.size() + " " + ((Vector)field.elementAt(0)).size()); if (x >= 0 && x < cols && y >= 0 && y < rows) { if (getCell(x,y) == -1) { for (a = 0; a < 3; a++) { for (b = 0; b < 3; b++) { if (moveMatrix[a][b] == 1) { if (x + a - 1 >= 0 && x + a - 1 < cols && y + b - 1 >= 0 && y + b - 1 < rows) { setCell(x + (a - 1), y + (b - 1), 1); } } } } currPlayer++; if (currPlayer >= numPlayers) { currPlayer = 0; } for (x = 0; x < 3; x++) { for (y = 0; y < 3; y++) { moveMatrix[x][y] = (int)(Math.random() * 2); } } moveMatrix[1][1] = 1; } } } public void mouseReleased(MouseEvent e) { //This line left blank. } public void mouseClicked(MouseEvent e) { //This line left blank. } } public void setCell(int x, int y, int playerNum) { ((BCard)((Vector)field.elementAt(x)).elementAt(y)).owned = currPlayer; } public int getCell(int x, int y) { return ((BCard)((Vector)field.elementAt(x)).elementAt(y)).owned; } public int[] calcTotals() { int p, x, y; int totals[] = new int[numPlayers]; for (p = 0; p < numPlayers; p++) { totals[p] = 0; } for (x = 0; x < cols; x++) { for (y = 0; y < rows; y++) { if (((BCard)((Vector)field.elementAt(x)).elementAt(y)).owned != -1) { totals[((BCard)((Vector)field.elementAt(x)).elementAt(y)).owned]++; } } } return totals; } public class BCard { float atks[][] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; int owned; public BCard() { owned = -1; } } private class PressButton implements ActionListener { public void actionPerformed(ActionEvent e) { Integer tempInt; int x, y; if (e.getActionCommand().equals("New Game")) { currPlayer = 0; tempInt = new Integer(fieldWidth.getText()); rows = tempInt.intValue(); tempInt = new Integer(fieldHeight.getText()); cols = tempInt.intValue(); tempInt = new Integer(fieldNumP.getText()); numPlayers = tempInt.intValue(); field = new Vector(); for (x = 0; x < cols; x++) { field.addElement(new Vector()); for (y = 0; y < rows; y++) { ((Vector)field.elementAt(x)).addElement(new BCard()); } } } } } }