Reversi 64

Reversi 64 is a very basic Reversi-Implementation with artificial intelligence in 64 lines written in Java.

Source Code

    1 import java.awt.*; import java.awt.event.*;
    2 import javax.swing.*; import javax.swing.event.*;
    3 
    4 public class Reversi extends JPanel {
    5   private static final long serialVersionUID = 8210537589686946439L;
    6   Color field[] = new Color[64], p1 = Color.RED, p2 = Color.BLUE;
    7   public static JLabel status = new JLabel("Please click on a field.");
    8   public static void main(String[] args) {
    9     JFrame f = new JFrame("Reversi 64");
   10     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   11     f.add(new Reversi()); f.add(status,"South");
   12     f.setSize(200, 220); f.setVisible(true);
   13   }
   14   public Reversi() {
   15     field[27] = p1; field[28] = p2; field[35] = p2; field[36] = p1;
   16     addMouseListener(new MouseInputAdapter() {
   17       public synchronized void mouseClicked(MouseEvent e) {
   18         int c = 0, pos = e.getX()*8/getWidth()+e.getY()*8/getHeight()*8;
   19         if (colorize(pos%8, pos/8, p1, false) != 0) bestVal(5, true, p2);
   20         while(bestVal(1, false, p1) == -64) {
   21           bestVal(4, true, p2);
   22           if (bestVal(1, false, p2) == -64) {
   23             for(int i=0;i<64;i++) c+=field[i]==p1?1:field[i]==p2?-1:0;
   24             status.setText("You have " + ((c>0)?"won.":"didn't win."));
   25             break;
   26         }  }
   27         repaint();
   28       }});
   29   }
   30   public synchronized int bestVal(int depth, boolean doIt, Color c) {
   31     int kiZug = -1, best = -64;
   32     Color[] oldField = new Color[64];
   33     System.arraycopy(field, 0, oldField, 0, 64);
   34     for (int i = 0; i < 64; i++) {
   35       int val = colorize(i%8,i/8,c,false);
   36       if (val != 0) {
   37         val -= depth>1?bestVal(depth - 1, false, c==p1?p2:p1):0;
   38         if (val > best) { best = val; kiZug = i; }
   39         System.arraycopy(oldField, 0, field, 0, 64);
   40     }  }
   41     if (doIt && (kiZug!=-1)) colorize(kiZug%8, kiZug/8, p2, false);
   42     return best;
   43   }
   44   public synchronized int colorize(int x, int y, Color color, boolean test) {
   45     if (field[x+y*8] != null) return 0;
   46     int i, c=0, vs[][]={{y*8+7,1},{y*8,-1},{x,-8},{x+56,8},{x>y?x-y:y*8-x*8,-9},
   47     {x<7-y?x+y:8*x+8*y-49,-7},{x<y?x-y+63:8*y-8*x+63,9},{x<7-y?(y+x)*8:49+x+y,7}};
   48     for(int[] v:vs) for (i=x+y*8+v[1];(i!=v[0]+v[1])&&(field[i]!=null);i+=v[1])
   49         if (field[i] == color) {
   50           for (i-=v[1]; i!=x+y*8; i-=v[1]) if ((c++ < 65) && !test)field[i]=color;
   51           break;
   52         }
   53     if ((c != 0) && !test) field[x+y*8] = color;
   54     return c;
   55   }
   56   public void paintComponent(Graphics g) {
   57     for (int x = 0, h = getHeight(), w = getWidth(); x < 64; x++) {
   58       g.setColor(((x%8) + x/8) % 2 == 0 ? Color.DARK_GRAY : Color.WHITE);
   59       g.fillRect(w*(x/8)/8, h*(x%8)/8, w/8, h/8);
   60       g.setColor(field[x%8 * 8 + x/8]);
   61       g.fillOval(w*(x/8)/8, h*(x%8)/8, w/8, h/8);
   62     }
   63   }
   64 }

Download


�2008 Max Nagl
Web-design, development and programming by Max Nagl