// (c) 2001 D. Clayton, all rights reserved import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class Go extends JFrame implements MouseListener { Vector whites, blacks; int turn, winner; boolean gameOver; public static void main(String args[]) { new Go().show(); } public void reset() { whites = new Vector(); blacks = new Vector(); whites.addElement(new Piece(8, 8, 0)); whites.addElement(new Piece(9, 9, 0)); blacks.addElement(new Piece(9, 8, 1)); blacks.addElement(new Piece(8, 9, 1)); turn = 0; gameOver = false; winner = -1; } public Go() { // 17 x 17 setSize(600,400); reset(); addMouseListener(this); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); repaint(); } public void paint(Graphics g) { g.setColor(Color.lightGray); g.fillRect(0, 0, 600, 400); g.setColor(Color.black); for (int y = 20; y <= 380; y+=20) { g.drawLine(0, y, 380, y); } for (int x = 20; x <= 380; x+=20) { g.drawLine(x, 0, x, 380); } for (int b = 0; b < blacks.size(); b++) { ((Piece)blacks.elementAt(b)).paint(g); } for (int w = 0; w < whites.size(); w++) { ((Piece)whites.elementAt(w)).paint(g); } g.setColor(Color.black); if (gameOver) { g.drawString((winner == 0 ? "WHITE" : "BLACK") + " WINS!! Game over.", 440, 80); } else { int tot = whites.size() + blacks.size(); int wp = (int)((double) whites.size() / tot * 100); int bp = (int)((double) blacks.size() / tot * 100); g.drawString("White: " + whites.size() + " (" + wp + "%)", 430, 50); g.drawString("Black: " + blacks.size() + " (" + bp + "%)", 430, 80); g.drawString("It is " + (turn == 0 ? "white" : "black") + "'s turn.", 430, 110); } } public void mouseClicked(MouseEvent e) { int x, y; boolean free = true; if (gameOver) { reset(); repaint(); return; } x = e.getX(); y = e.getY(); x-=10; y-=10; x/=20; y/=20; if (x > 18 || y > 18) return; // check if position is free for (int i = 0; i < blacks.size(); i++) { if (((Piece)blacks.elementAt(i)).isAt(x, y)) { free = false; break; } } for (int i = 0; i < whites.size(); i++) { if (((Piece)whites.elementAt(i)).isAt(x, y)) { free = false; break; } } if (!free) return; // assume it's free; put a piece (seed) there... Piece seed = new Piece(x, y, turn); Piece nearW=null, nearE=null, nearN=null, nearS=null, nearNW=null, nearNE=null, nearSW=null, nearSE=null; Vector fr, en; if (turn == 0) { fr = whites; en = blacks; } else { fr = blacks; en = whites; } fr.addElement(seed); for (int i = 0; i < fr.size(); i++) { Piece p = (Piece)fr.elementAt(i); if (p.isNearer(0, nearN, seed)) { nearN = p; } if (p.isNearer(1, nearNE, seed)) { nearNE = p; } if (p.isNearer(2, nearE, seed)) { nearE = p; } if (p.isNearer(3, nearSE, seed)) { nearSE = p; } if (p.isNearer(4, nearS, seed)) { nearS = p; } if (p.isNearer(5, nearSW, seed)) { nearSW = p; } if (p.isNearer(6, nearW, seed)) { nearW = p; } if (p.isNearer(7, nearNW, seed)) { nearNW = p; } } boolean line = true; if (nearN != null) { x = seed.x; for (y = nearN.y + 1; y < seed.y; y++) { if (!anyAt(en, x, y)) line = false; } if (line) { x = seed.x; for (y = nearN.y + 1; y < seed.y; y++) { if (remAny(en, x, y)) { fr.addElement(new Piece(x, y, turn)); } } } } line = true; if (nearNE != null) { x = nearNE.x - 1; for (y = nearNE.y + 1; y < seed.y; y++) { if (!anyAt(en, x, y)) line = false; x--; } if (line) { x = nearNE.x - 1; for (y = nearNE.y + 1; y < seed.y; y++) { if (remAny(en, x, y)) { fr.addElement(new Piece(x, y, turn)); } x--; } } } line = true; if (nearE != null) { y = seed.y; for (x = nearE.x - 1; x > seed.x; x--) { if (!anyAt(en, x, y)) line = false; } if (line) { y = seed.y; for (x = nearE.x - 1; x > seed.x; x--) { if (remAny(en, x, y)) { fr.addElement(new Piece(x, y, turn)); } } } } line = true; if (nearSE != null) { x = nearSE.x - 1; for (y = nearSE.y - 1; y > seed.y; y--) { if (!anyAt(en, x, y)) line = false; x--; } if (line) { x = nearSE.x - 1; for (y = nearSE.y - 1; y > seed.y; y--) { if (remAny(en, x, y)) { fr.addElement(new Piece(x, y, turn)); } x--; } } } line = true; if (nearS != null) { x = seed.x; for (y = nearS.y - 1; y > seed.y; y--) { if (!anyAt(en, x, y)) line = false; } if (line) { x = seed.x; for (y = nearS.y - 1; y > seed.y; y--) { if (remAny(en, x, y)) { fr.addElement(new Piece(x, y, turn)); } } } } line = true; if (nearSW != null) { x = nearSW.x + 1; for (y = nearSW.y - 1; y > seed.y; y--) { if (!anyAt(en, x, y)) line = false; x++; } if (line) { x = nearSW.x + 1; for (y = nearSW.y - 1; y > seed.y; y--) { if (remAny(en, x, y)) { fr.addElement(new Piece(x, y, turn)); } x++; } } } line = true; if (nearW != null) { y = nearW.y; for (x = nearW.x+1; x < seed.x; x++) { if (!anyAt(en, x, y)) line = false; } if (line) { y = nearW.y; for (x = nearW.x+1; x < seed.x; x++) { if (remAny(en, x, y)) { fr.addElement(new Piece(x, y, turn)); } } } } line = true; if (nearNW != null) { x = nearNW.x + 1; for (y = nearNW.y+1; y < seed.y; y++) { if (!anyAt(en, x, y)) line = false; x++; } if (line) { x = nearNW.x + 1; for (y = nearNW.y+1; y < seed.y; y++) { if (remAny(en, x, y)) { fr.addElement(new Piece(x, y, turn)); } x++; } } } if (turn == 0) { turn = 1; } else { turn = 0; } if (whites.size() == 0 || blacks.size() == 0) { gameOver = true; winner = (whites.size() == 0 ? 1 : 0); } if (whites.size() + blacks.size() == 361) { gameOver = true; winner = -1; if (whites.size() > blacks.size()) winner = 0; if (blacks.size() > whites.size()) winner = 1; } repaint(); } public boolean remAny(Vector v, int x, int y) { for (int i = 0; i < v.size(); i++) { if (((Piece)v.elementAt(i)).isAt(x, y)) { v.removeElementAt(i); return true; } } return false; } public boolean anyAt(Vector v, int x, int y) { for (int i = 0; i < v.size(); i++) { if (((Piece)v.elementAt(i)).isAt(x, y)) { return true; } } return false; } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public class Piece { protected int x, y, side; public Piece(int xx, int yy, int s) { x = xx; y = yy; side = s; } public boolean isNearer(int dir, Piece p, Piece s) { if (p == null) { if (dir == 0) { if (x == s.x && y < s.y) return true; } else if (dir == 1) { if (x - s.x == s.y - y && y < s.y) return true; } else if (dir == 2) { if (y == s.y && x > s.x) return true; } else if (dir == 3) { if (x - s.x == y - s.y && y > s.y) return true; } else if (dir == 4) { if (x == s.x && y > s.y) return true; } else if (dir == 5) { if (x - s.x == s.y - y && x < s.x) return true; } else if (dir == 6) { if (y == s.y && x < s.x) return true; } else if (dir == 7) { if (x - s.x == y - s.y && x < s.x) return true; } return false; } if (dir == 0) { // n if (x == s.x && y < s.y && y > p.y) { return true; } } else if (dir == 1) { // ne if (x - s.x == s.y - y && x > s.x && x < p.x) { return true; } } else if (dir == 2) { // e if (y == s.y && x > s.x && x < p.x) { return true; } } else if (dir == 3) { // se if (x - s.x == y - s.y && x > s.x && x < p.x) { return true; } } else if (dir == 4) { // s if (x == s.x && y > s.y && y < p.y) { return true; } } else if (dir == 5) { // sw if (x - s.x == s.y - y && x < s.x && x > p.x) { return true; } } else if (dir == 6) { // w if (y == s.y && x < s.x && x > p.x) { return true; } } else if (dir == 7) { // nw if (x - s.x == y - s.y && x < s.x && x > p.x) { return true; } } return false; } public boolean isAt(int i, int j) { if (x == i && y == j) { return true; } return false; } public void paint(Graphics g) { if (side == 0) { g.setColor(Color.white); } else { g.setColor(Color.black); } g.fillOval(12 + 20*x, 12 + 20*y, 16, 16); } } }