import java.applet.*; import java.awt.*; import java.lang.Math; public class Matches extends Applet { Frame window; Image matchbox; AudioClip win; AudioClip lose; Board board; TextField textField; BotNav navigation; int count; public void init() { this.count = 0; matchbox = getImage(getCodeBase(), "matches.gif"); win = getAudioClip(getCodeBase(), "whoopy.au"); lose = getAudioClip(getCodeBase(), "rehot.au"); setLayout(new BorderLayout()); board = new Board(this); textField = new TextField("To start press the Start button"); textField.setEditable(false); navigation = new BotNav(this); add("North", navigation); add("Center", board); add("South", textField); } public void setCount(int value) { this.count = value; } public int getCount() { return this.count; } public int getTurn(int old, int choosen) { int getback; switch((old+choosen)%4) { case 0: if (choosen == 1) { getback = 2; } else { getback = 1; } break; case 1: getback = 4 - choosen; break; case 2: if (choosen == 2) { getback = 3; } else { getback = 1; } break; default: if (choosen == 1) { getback = 1; } else { getback = 3; } break; } return getback; } public void myTurn() { int turn; int select = navigation.getSelected(); if (count == 1) { textField.setText("You WIN !!!"); win.play(); } else { switch(count%4) { case 1: turn = (int)(Math.random()*3+1); break; default: turn = getTurn(count, select); break; } count = count - turn; board.repaint(); if (count == 1) { textField.setText("I took " + turn + " and you LOSE !!!"); lose.play(); } else { textField.setText("Ok...i took " + turn + ", and you ?"); } navigation.counter.setText((String.valueOf(count).toString())); } } } class Board extends Canvas { Matches parent; Board(Matches contr) { super(); parent = contr; } public void paint(Graphics g) { int x = 0; int y = -70; int jump = 0; int p = 0; Color wood = new Color(255, 255, 153); Color bground = new Color(100, 200, 100); Color logo = new Color(100, 250, 100); g.setColor(bground); g.fillRect(0, 0, size().width - 1, size().height - 1); g.setColor(logo); g.drawString("(c)1996 by MK", size().width - 99, size().height - 7); g.drawImage(parent.matchbox, (int)((size().width/5)*4), (int)(size().height/2), this); int rep = parent.getCount(); for (int i = 0; i < rep; i++) { int line = (i%20); if (line == 0) { jump = 20 * p++; y += 90; } x = (i - jump) * 20 + 20; g.setColor(Color.black); g.fillRect(x, y, 6, 10); g.setColor(wood); g.fillRect(x , y+10, 6, 60); } } } class BotNav extends Panel { Matches parent; TextField counter; Label lcounter; Label lchoice; Choice choice; Button submit; Button start; Button accept; int selected = 1; String content; BotNav(Matches contr) { super(); parent = contr; counter = new TextField(3); counter.setEditable(false); lcounter = new Label(" Counter:"); lchoice = new Label(" Your turn:"); choice = new Choice(); choice.addItem("1"); choice.addItem("2"); choice.addItem("3"); start = new Button("Start"); submit = new Button("Submit"); accept = new Button("Accept"); setLayout(new GridLayout(1,7)); add(start); add(lcounter); add(counter); add(accept); add(lchoice); add(choice); add(submit); } public int getSelected() { return this.selected; } public boolean handleEvent(Event e) { if (e.target instanceof Choice) { int result = parent.getCount(); if (result > 1) { selected = choice.getSelectedIndex() + 1; if ((result - selected) < 1) { parent.textField.setText("EEEEK ! Are you crazy ?"); } else { parent.textField.setText("Your choice is " + selected + ", you can submit it !"); } } return true; } if (e.target instanceof Button) { if ((e.arg.toString()) == "Start") { parent.setCount(0); parent.board.repaint(); parent.textField.setText("Ok...now choose count of matches"); counter.setEditable(true); counter.requestFocus(); return true; } if ((e.arg.toString()) == "Accept") { boolean edit = counter.isEditable(); if (edit) { content = counter.getText(); int count = Integer.valueOf(content).intValue(); if ((count > 60) || (count < 2)) { parent.textField.setText("Choose min. 2 and max. 60 matches"); counter.requestFocus(); } else { counter.setEditable(false); counter.nextFocus(); parent.setCount(count); parent.board.repaint(); parent.textField.setText("Now it's your turn"); } } return true; } if ((e.arg.toString()) == "Submit") { int newcount = parent.getCount(); if (newcount > 1) { if ((newcount - selected) < 1) { if (newcount == 3) { parent.textField.setText("Take 2 !!!"); } else { parent.textField.setText("Take 1 !!!"); } } else { newcount -= selected; parent.setCount(newcount); parent.board.repaint(); counter.setText((String.valueOf(newcount).toString())); parent.myTurn(); return true; } } return false; } } return false; } }