/* * Turnablock.java * The turnablock game * Usage: 8) { x=8; xf.setText("8"); } if (y<3) { y=3; yf.setText("3"); } else if (y>8) { y=8; yf.setText("8"); } data = new int[x][y]; board.restart(x,y,data); strategy = new Strategy(data,x,y); strategy.setSkill(skill.getSelectedIndex()); layout(); toppanel.layout(); if (thread != null) { thread.stop(); } thread = new Thread(this); thread.start(); } // Helper function for GridBagLayout public void constrain(GridBagLayout gridbag, GridBagConstraints gc, Container parent, Component c, int w) { gc.gridwidth = w; gridbag.setConstraints(c,gc); parent.add(c); } // Test if the board is in a winning position public boolean win() { for (int i=0;imove.x+1 || move.height<1 || move.height>move.y+1) { System.out.println("Error: bad move region"); ok = false; } return ok; } // Flip the specified rectangle void flip(Rectangle mv) { for (int i=0;imaxh/y0) { sz = maxh/y0; } if (sz>50) { sz=50; } xsize = x0; ysize = y0; xpix = xsize*sz+2*margin; ypix = ysize*sz+3*margin; resize(xpix,ypix); back = null; data = indata; hilite_who=0; win_who=0; for (int i=0;ihilite.x) { xleft = hilite.x; } if (xleft <0) { xleft = 0; } if (ytop <0) { ytop = 0; } if (ytop >hilite.y) { ytop = hilite.y; } hilite.width=hilite.x-xleft+1; hilite.height=hilite.y-ytop+1; repaint(); } return true; } public boolean mouseUp(Event evt, int xm, int ym) { if (mousemode==2) { synchronized(waitthread) { waitthread.notify(); } } return true; } public void highlight(int who) { hilite_who = who; repaint(); } public void highlight(int who, Rectangle rect) { hilite = rect; hilite_who = who; repaint(); } public void update(Graphics g) { if (back == null) { back = createImage(xpix,ypix); backgr = back.getGraphics(); backgr.setColor(getBackground()); backgr.fillRect(0,0,xpix,ypix); } paint(backgr); g.drawImage(back,0,0,this); if (hilite_who>0) { g.setColor(hilite_who==1?Color.green:Color.red); g.drawRect(margin+(hilite.x-hilite.width+1)*sz, margin+(hilite.y-hilite.height+1)*sz, hilite.width*sz-1,hilite.height*sz-1); g.drawRect(margin+(hilite.x-hilite.width+1)*sz+2, margin+(hilite.y-hilite.height+1)*sz+2, hilite.width*sz-5,hilite.height*sz-5); g.drawRect(margin+(hilite.x-hilite.width+1)*sz+4, margin+(hilite.y-hilite.height+1)*sz+4, hilite.width*sz-9,hilite.height*sz-9); } } public void paint(Graphics g) { for (int i=0;i