/* Eight Queens $Id: EightQueens.java,v 1.2 1996/04/21 10:55:34 ryo2 Exp ryo2 $ */ import java.applet.Applet; import java.awt.*; import java.util.StringTokenizer; import java.net.URL; import java.net.MalformedURLException; public class EightQueens extends Applet { Panel slides; int sArray[]; public int blockSize = 40, initOfs = 4; public int boardSize; Image image[], OffScreen; Graphics OffGraph; Dimension dim; Color bg; Panel signBoard = new Panel(); NamePanel namePanel, finalPanel; Label topLine = new Label(); Panel mainBoard, finalBoard; Panel buttonMain; ChessPanel currPanel = null; String lang = "eng"; String imagedir = new String(); public void init() { int i, j; String s; String imgs = null; String sBg; s = getParameter("blocksize"); if (s != null) blockSize = Integer.valueOf(s).intValue(); boardSize = blockSize*8 + initOfs*2; dim = size(); sBg = getParameter("bg"); sArray = new int[3]; if (sBg == null || (new StringTokenizer(sBg,"| ,")).countTokens() != 3) { sBg = "d8|d8|b4"; } StringTokenizer asBg = new StringTokenizer(sBg,"| ,"); for(i = 0; asBg.hasMoreTokens() == true; i++) { sArray[i] = Integer.parseInt(asBg.nextToken(),16); } bg = new java.awt.Color(sArray[0],sArray[1],sArray[2]); setBackground(bg); namePanel = new NamePanel(dim,bg,this); finalPanel = new NamePanel(dim,bg,this,1); s = getParameter("imagedir"); if (s != null) imagedir = s; s = getParameter("images"); if (s != null) imgs = s; s = getParameter("lang"); if (s != null) lang = s; OffScreen = createImage(boardSize,boardSize); OffGraph = OffScreen.getGraphics(); setLayout(new CardLayout()); signBoard.setLayout(new FlowLayout()); signBoard.add(new Label("Fill Your Name in the Follwing Blank")); signBoard.add(namePanel); mainBoard = new Panel(); mainBoard.setLayout(new BorderLayout()); finalBoard = new Panel(); finalBoard.setLayout(new FlowLayout()); finalBoard.add(new Label("You've Got It !")); finalBoard.add(finalPanel); mainBoard.add("North", topLine); buttonMain = new Panel(); buttonMain.setLayout(new FlowLayout()); buttonMain.add(new Button(" QUIT ")); buttonMain.add(new Button("CLEAR BOARD")); mainBoard.add("South", buttonMain); add("signBoard", signBoard); add("mainBoard", mainBoard); add("finalBoard", finalBoard); } public static void main(String args[]) { Frame f = new Frame("EightQueens"); EightQueens eightQueens = new EightQueens(); eightQueens.init(); eightQueens.start(); f.add("Center", eightQueens); f.resize(360, 430); f.show(); } public boolean action(Event e, Object arg) { if (e.target instanceof Button) { if ("START GAME".equals(arg)) { if (currPanel == null) { currPanel = new ChessPanel(this, imagedir, OffScreen, OffGraph, blockSize, initOfs, boardSize, bg); mainBoard.add("Center", currPanel); } ((CardLayout)getLayout()).show(this, "mainBoard"); return true; } else if ("CLEAR BOARD".equals(arg)) { currPanel.cboard.InitPosData(); currPanel.cboard.repaint(); currPanel.label.setText(currPanel.topMsg[0]+currPanel.topMsg2); return true; } else if (" QUIT ".equals(arg) || "CANCEL".equals(arg)) { ((CardLayout)getLayout()).show(this, "signBoard"); mainBoard.getLayout().removeLayoutComponent(currPanel); currPanel = null; return true; } } return false; } } class ChessPanel extends Panel implements Runnable { EightQueens parent; CardLayout mainPanel; String topMsg[] = { "No queen is", "One queen is", "Two queens are", "Three queens are", "Four queens are", "Five queens are", "Six queens are", "Seven queens are", "Eight Queens are" }; String topMsg2 = " put on the board"; Thread mainThread = null; ChessBoard cboard; Label label; Color bg; MediaTracker mt = new MediaTracker(this); Image image[]; public ChessPanel(EightQueens parent, String imagedir, Image OffScreen, Graphics OffGraph, int blockSize, int initOfs, int boardSize, Color bg) { int i; this.parent = parent; setLayout(new BorderLayout()); this.bg = bg; image = new Image[8]; for(i = 0; i < 8; i++) { image[i] = parent.getImage(parent.getDocumentBase(),imagedir + "/" + "queen" + Integer.toString(i) + ".gif"); mt.addImage(image[i], 0); } cboard = new ChessBoard(parent, imagedir, OffScreen, OffGraph, blockSize, initOfs, boardSize, bg); label = new Label("Please try to put EIGHT QUEENS on the board !", Label.CENTER); add("Center", cboard); add("North", label); start(); } public void start() { if (mainThread == null) { mainThread = new Thread(this); mainThread.start(); } } public void stop() { mainThread.stop(); mainThread = null; } public void run() { Thread.currentThread().setPriority(Thread.NORM_PRIORITY-3); try { mt.waitForID(0); } catch (InterruptedException e) { return; } while (mainThread != null) { if (cboard.gCount == 0 && cboard.count == 8) { try { parent.showStatus(" Congratulations!!"); Thread.sleep(15); cboard.repaint(); if (cboard.strXofs < -200) { mainPanel = (CardLayout)parent.getLayout(); mainPanel.show(parent, "finalBoard"); stop(); } } catch (InterruptedException e){} } else { try { Thread.sleep(60); cboard.repaint(); } catch (InterruptedException e){} } } mainThread = null; } } class ChessBoard extends Canvas { EightQueens parent; Image offScr; Graphics Off; Font font = new Font("TimesRoman",Font.BOLD,32); int blSize, iOfs, boSize; int filledPos[][]; int count = 0, gCount = 0; double time = 0.1; int startHeight = 80, decHeight, strSpeed = 5, strXofs = 0; String finalmsg = "Congratulations"; int prev_count = -1; public ChessBoard(EightQueens parent, String imagedir, Image OffScreen, Graphics OffGraphics, int blockSize, int initOfs, int boardSize, Color bg) { int i, j; this.parent = parent; blSize = blockSize; iOfs = initOfs; boSize = boardSize; filledPos = new int[8][]; offScr = OffScreen; Off = OffGraphics; InitPosData(); setBackground(bg); resize(boSize,boSize); } public void InitPosData() { int i, j = 0; for(i = 0; i < 8; i++) { filledPos[i] = new int [8]; for(j = 0; j < 8; j++) if (i == j) { filledPos[i][j] = 0; } } count = 0; } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { int i = 0, j = 0, k; if (!parent.currPanel.mt.checkID(0)) { g.setColor(Color.orange); g.fillRect(0, 0, boSize, boSize); g.setColor(Color.black); g.drawString("Loading GIF files...", 100, 40); } else { Off.setColor(Color.orange); Off.fillRect(0, 0, boSize, boSize); Off.setColor(Color.white); Off.fillRect(iOfs, iOfs, boSize-2*iOfs, boSize-2*iOfs); Off.setColor(Color.black); for(i = 0; i < 8; i++) { for(j = 0; j < 8; j++) { if (((i+j) % 2) == 0) { Off.fillRect(i*blSize+iOfs, j*blSize+iOfs, blSize, blSize); } if (filledPos[i][j] != 0) { k = filledPos[i][j]; if (k == 8 || k > 23) { // not animate Off.drawImage(parent.currPanel.image[7], i*blSize+iOfs,j*blSize+iOfs,this); filledPos[i][j] = 8; } else { // animate k = (filledPos[i][j] & 15) -1; // mask 0xf; Off.drawImage(parent.currPanel.image[k], i*blSize+iOfs,j*blSize+iOfs,this); if (filledPos[i][j] > 15) { ++filledPos[i][j]; } else { --filledPos[i][j]; } } } } } if (gCount != 0) { --gCount; } if (count == 7) { Off.setFont(font); strXofs = boSize; } if (count == 8) { time += 0.8; strXofs -= strSpeed; decHeight = (int)(startHeight*time - 5.0*time*time); if (decHeight < 0) { decHeight = 0; time=0.1; startHeight -= 2; } Off.drawString(finalmsg, strXofs+2, boSize - decHeight+2); Off.setColor(new Color(40,255,255-decHeight/2)); Off.drawString(finalmsg, strXofs, boSize - decHeight); } g.drawImage(offScr, 0, 0, null); } } public void checkPos(int x, int y) { int i, j; for(i = 0; i < 8; i++) { for(j = 0; j < 8; j++) { if (filledPos[i][j] == 8 && (i != x || j != y)) { if ((i == x) || (j == y) || (j-i) == (y-x) || (i+j) == (y+x)) { filledPos[i][j] = 7; } } } } } public boolean handleEvent(Event e) { int i, j; switch (e.id) { case Event.MOUSE_DOWN: if (gCount == 0) { if (e.x>iOfs && e.y>iOfs && e.x<(boSize+iOfs) && e.y<(boSize+iOfs)) { i = (int)((e.x-iOfs)/blSize); j = (int)((e.y-iOfs)/blSize); switch (filledPos[i][j]) { case 8: filledPos[i][j] = 7; break; case 0: filledPos[i][j] = 17; // 1 + 0x10 checkPos(i,j); break; } gCount = 7; count = 0; for(i = 0; i < 8; i++) { for(j = 0; j < 8; j++) if (filledPos[i][j] > 7) ++count; } parent.currPanel.label.setText(parent.currPanel.topMsg[count] + parent.currPanel.topMsg2); repaint(); return false; // tricky double handling } } return true; case Event.WINDOW_EXPOSE: repaint(); return true; } return false; } } class NamePanel extends Panel { Label msg, msg2, msg3; TextField blank, em; int dum = 0; String s; String warningStr[] = {"You can't start without name.", "Are you sure that you will be anonymous?"}; EightQueens thisApp; URL scoreURL; public NamePanel(Dimension dim, Color bgColor, EightQueens App) { thisApp = App; s = new String(); setBackground(bgColor); setLayout(new BorderLayout()); add("North", blank = new TextField("", 20)); Panel startPanel = new Panel(); msg = new Label("", Label.CENTER); startPanel.add(new Button("START GAME")); startPanel.add(new Button(" RESET ")); add("Center", startPanel); add("South", msg); } public NamePanel(Dimension dim, Color bgColor, EightQueens App, int dum) { this.dum = dum; thisApp = App; s = new String(); setBackground(bgColor); Panel buttonPanel = new Panel(); add(msg2 = new Label("Please fill your e-mail address in the ", Label.CENTER)); add(msg3 = new Label("following blank to place on the ranking record.", Label.CENTER)); add(new Label("", Label.CENTER)); add(em = new TextField("", 32)); buttonPanel.add(new Button("FINISH")); buttonPanel.add(new Button("RESET BLANK")); buttonPanel.add(new Button("CANCEL")); add(buttonPanel); setLayout(new GridLayout(5, 1)); } public void showDocument(URL u) { thisApp.getAppletContext().showDocument(u); } public void handleArg(String arg) { try { scoreURL = new URL(thisApp.getDocumentBase(), arg); } catch (MalformedURLException e) { scoreURL = null; } } public void showStatus(String arg) { thisApp.showStatus(arg); } public boolean action(Event e, Object arg) { String data = ""; int i, j; if (e.target instanceof Button) { String label = (String)arg; if (" RESET ".equals(arg)) { blank.setText(""); showStatus("What's your name ?"); return true; } else if ("START GAME".equals(arg)) { s = blank.getText().trim(); blank.setText(s); if ("".equals(s)) { msg.setText(warningStr[dum]); return true; } msg.setText(""); return false; } else if ("FINISH".equals(arg)) { String name = thisApp.namePanel.s.replace(' ','+'); s = em.getText().trim(); if ("".equals(s)) { s = "anonymous"; } for(i = 0; i < 8; i++) { for(j = 0; j < 8; j++) { data += Integer.toString(thisApp.currPanel.cboard.filledPos[i][j]); } data += "+"; } handleArg("../cgi-bin/java8queen.cgi?lang="+thisApp.lang+"&ad="+s+"&nm="+name+"&data="+data); if (scoreURL == null) { msg2.setText("Sorry that it's disable to regist the record, now"); msg3.setText("Try later again."); } else { showDocument(scoreURL); } return true; } else if ("RESET BLANK".equals(arg)) { String name = thisApp.namePanel.s.replace(' ','+'); em.setText(""); return true; } return false; } else if (e.target instanceof TextField) { s = blank.getText().trim(); if ("".equals(arg)) { msg.setText(warningStr[dum]); return true; } else { msg.setText(""); } return false; } return false; } }