import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; import java.util.*; public class mancala extends java.applet.Applet implements ActionListener { // Server-related data String servername; int port; Socket server; BufferedReader in; PrintWriter out; boolean reading; // Talks to the server Thread serverThread; // Data for drawing the board Image boardImage; // Control panel items Button userButton, loginButton, resignButton; LED led; Label count, lastMove; // For fast access to the pits Pit FarMancala, NearMancala, FarPits[], NearPits[]; Vector PitCircle; // Ruleset data public boolean leftOk = true; // Status information public boolean inGame = false; public boolean moveOk = false; public void init() { setBackground(Color.black); setLayout(null); Panel Controls = new Panel(); Controls.setLayout(new FlowLayout(FlowLayout.LEFT)); add(Controls); Controls.setBounds(0,175,500,50); Label temp; Controls.add(led = new LED(5, Color.red)); Controls.add(temp = new Label("Stones:", Label.CENTER)); temp.setForeground(Color.white); Controls.add(count = new Label(" ", Label.CENTER)); count.setForeground(Color.white); Controls.add(temp = new Label("Last Move:", Label.CENTER)); temp.setForeground(Color.white); Controls.add(lastMove = new Label(" ", Label.CENTER)); lastMove.setForeground(Color.white); Controls.add(resignButton = new Button("Resign")); resignButton.setActionCommand("Resign"); resignButton.addActionListener(this); resignButton.setEnabled(false); resignButton.setVisible(false); Controls.add(userButton = new Button("List other players")); userButton.setActionCommand("UserList"); userButton.addActionListener(this); userButton.setEnabled(false); Controls.add(loginButton = new Button("Log in")); loginButton.setActionCommand("LogIn"); loginButton.addActionListener(this); PitCircle = new Vector(13); FarMancala = new Pit(this, 24, 46, 42, 84, 0, false); add(FarMancala); NearMancala = new Pit(this, 433, 46, 42, 84, 0, false); add(NearMancala); FarPits = new Pit[6]; for(int i=0; i<6; i++) { FarPits[i] = new Pit(this, (int)(83+(58.5*i)), 35, 41, 43, 0, false); add(FarPits[i]); } NearPits = new Pit[6]; for(int i=0; i<6; i++) { NearPits[i] = new Pit(this, (int)(83+(58.5*i)), 98, 41, 43, 0, true); add(NearPits[i]); PitCircle.addElement(NearPits[i]); } PitCircle.addElement(NearMancala); for(int i=5; i>=0; i--) PitCircle.addElement(FarPits[i]); try { boardImage = getImage(new URL(getCodeBase()+"mancala.gif")); MediaTracker mt = new MediaTracker(this); mt.addImage(boardImage, 0); mt.waitForAll(); } catch(Exception e) { System.err.println("Exception "+e);return; } } public void start() { userButton.setEnabled(false); loginButton.setEnabled(true); loginButton.setVisible(true); try { servername = getParameter("server"); port = Integer.parseInt(getParameter("port")); if(servername=="") servername = "moo.midgard.org"; if(port==0) port=4848; } catch(Exception e) { System.err.println("Exception "+e);return; } } public void stop() { try { sendLine("! QUIT"); readLine(); server.close(); serverThread.stop(); } catch(Exception e) { System.err.println("Exception "+e);return; } } public void paint(Graphics g) { try { g.drawImage(boardImage, 0, 0, this); } catch(Exception e) { System.err.println("Exception "+e);return; } super.paint(g); } public String [] explode(String s) { return explode(s, " "); } public String [] explode(String s, String delim) { String [] e; StringTokenizer st = new StringTokenizer(s, delim); int j=st.countTokens(); e = new String [j]; for(int i=0;i0;s--) { // pit = (Pit)PitCircle.elementAt(i % 13); // pit.setStones(pit.getStones() + 1); // i++; // } } public void moveLeft(Pit startingpit) { Pit pit = startingpit; int i = PitCircle.indexOf(pit)-1, s = pit.getStones(); if(!checkMove(i+2, "LEFT")) return; // pit.setStones(0); // for(;s>0;s--) { // pit = (Pit)PitCircle.elementAt((i + 52) % 13); // pit.setStones(pit.getStones() + 1); // i--; // } } public boolean checkMove(int pit, String dir) { sendLine("! MOVE "+pit+" "+dir); String s, words[]; words = explode(s = readLine()); if(words[0].equals("+")) { moveOk = false; led.setColor(Color.red); return true; } return false; } public String readLine() { String s; reading = true; try { s = in.readLine(); } catch(Exception e) { System.err.println("Exception "+e);s="- I/O Error: "+e; } reading = false; System.err.println("READ: "+s); return s; } public boolean readReady() { try { return in.ready(); } catch(Exception e) { System.err.println("Exception "+e);return false; } } public void sendLine(String s) { System.err.println("SEND: "+s); out.println(s); } public void endGame(String words[]) { String result; moveOk = false; inGame = false; led.setColor(Color.red); led.setOn(false); result = words[2].equalsIgnoreCase("TIE") ? "It's a tie." : (words[2].equalsIgnoreCase("WIN") ? "You win!" : "You lose."); MsgBox mb = new MsgBox(result, "Game Over"); center(mb,this); mb.show(); userButton.setEnabled(true); userButton.setVisible(true); resignButton.setEnabled(false); resignButton.setVisible(false); serverThread = new WaitThread(this); serverThread.start(); } public void center(Component a, Component b) { // Centers a on b. Dimension ad = a.getSize(), bd = b.getSize(); Point l = b.getLocationOnScreen(); l.translate(((bd.width/2)-(ad.width/2)), ((bd.height/2)-(ad.height/2))); a.setLocation(l); } }