/* Scrabble : An interactive scrabble playing software. Copyright (C) 1999 Amitabh Sinha All Trademarks and Copyrights pertaining to Scrabble, Java, Linux acknowledged. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Contact Amitabh at */ import java.io.*; import java.util.*; import java.awt.*; import java.awt.event.*; import java.lang.*; public class ScrabbleBoard implements ActionListener, MouseListener { int side; Cell[][] board; ScrabbleDictionary d; LetterBag letterBag = new LetterBag(); Frame frame; Screen screen; Button quitButton = new Button("Quit"); HumanPlayer human; ComputerPlayer computer; Button playButton = new Button("Play"); Button changeButton = new Button("Change"); Button finishButton = new Button("Finish"); Button restoreButton = new Button("Restore"); Frame nameFrame; TextField t; LetterScreen letterScreen; ScoreScreen scoreScreen; boolean letterSelected = false; Vector selectedLetters; boolean boardEmpty = true; boolean changedBoard; Frame blankFrame; TextField blankText; String lastWord; int lastX; int lastY; char lastDir; Checkbox computerStarts, showComputer; Checkbox noCaps; CheckboxGroup level; TextField dictName; Choice terminator; String terminatingCondition; int humanMoves = 0; int compMoves = 0; Frame finishFrame; boolean letterPlaced = false; Checkbox modifiedBoard; Vector wordsMade = new Vector(); TextField computerLetters; boolean show; public ScrabbleBoard() { nameFrame = new Frame("Scrabbler"); nameFrame.setLayout(new BorderLayout()); Panel buttonPanel = new Panel(); Panel namePanel = new Panel(); namePanel.setLayout(new GridLayout(5,1)); System.out.println("Scrabbler Version 0.0 Copyright (C) 1999 Amitabh Sinha"); System.out.println("Scrabbler comes with ABSOLUTELY NO WARRANTY."); System.out.println("Scrabbler is free software, and you are welcome to redistribute it under certain conditions; see LICENSE"); Button doneNameButton = new Button("Done"); Button exitButton = new Button("Exit"); doneNameButton.addActionListener(this); exitButton.addActionListener(this); buttonPanel.add(doneNameButton); buttonPanel.add(exitButton); nameFrame.add("South", buttonPanel); Label nameLabel = new Label("Welcome to Scrabble. Enter Name :"); namePanel.add(nameLabel); t = new TextField("Tryst"); t.setEditable(true); namePanel.add(t); namePanel.add(new Label("Enter Dictionary filename :")); dictName = new TextField("dict"); dictName.setEditable(true); namePanel.add(dictName); namePanel.add(new Label("Options :")); Panel optionsPanel = new Panel(); optionsPanel.setLayout(new GridLayout(10,1)); showComputer = new Checkbox("Display computer's letters", true); optionsPanel.add(showComputer); computerStarts = new Checkbox("Computer starts", false); optionsPanel.add(computerStarts); modifiedBoard = new Checkbox("Modified Board", false); optionsPanel.add(modifiedBoard); noCaps = new Checkbox("No capitalized words", true); optionsPanel.add(noCaps); optionsPanel.add(new Label("Difficulty Level :")); level = new CheckboxGroup(); optionsPanel.add(new Checkbox("Easy", level, false)); optionsPanel.add(new Checkbox("Medium", level, false)); optionsPanel.add(new Checkbox("Hard", level, true)); optionsPanel.add(new Label("Terminating Condition")); terminator = new Choice(); terminator.add("10 Moves"); terminator.add("15 Moves"); terminator.add("20 Moves"); terminator.add("25 Moves"); terminator.add("100 Points"); terminator.add("150 Points"); terminator.add("200 Points"); terminator.add("250 Points"); terminator.add("LetterBag Empty"); optionsPanel.add(terminator); nameFrame.add("Center", optionsPanel); nameFrame.add("North", namePanel); nameFrame.pack(); nameFrame.setVisible(true); } public void Scrabbler (boolean humanStarts, String hum, boolean changedBoard) { side = 15; board = new Cell[side][side]; frame = new Frame("Scrabble : "+hum+" vs Moistone"); for (int i=0; i0 && board[x-1][y].isOccupied()) c = 'y'; else if (x<14 && board[x+1][y].isOccupied()) c = 'y'; } lastDir = c; if (boardEmpty) { if (!human.checkStart(c)) { Dialog d = new Dialog("Centre (Start) square must be used."); return false; } Vector v = human.getOtherVector(c); int min = ((Integer)v.elementAt(0)).intValue(); for (int i=1; i((Integer)v2.elementAt(j)).intValue()) min = ((Integer)v2.elementAt(j)).intValue(); if (max<((Integer)v2.elementAt(j)).intValue()) max = ((Integer)v2.elementAt(j)).intValue(); } for (int i=min; i<=max; i++) { Cell cl = (c=='x'?board[x][i]:board[i][x]); if (!cl.isOccupied()) { Dialog d = new Dialog("Gaps should not be left in between."); return false; } } if (c=='y') { x = ((Integer)v1.elementAt(0)).intValue(); min = ((Integer)v2.elementAt(0)).intValue(); max = ((Integer)v2.elementAt(0)).intValue(); if (min != 0) while (board[min-1][x].isOccupied()) { min--; if (min == 0) break; } if (max != 14) while (board[max+1][x].isOccupied()) { max++; if (max == 14) break; } lastX = min; lastY = x; if (max > min) { char[] word = new char[max-min+1]; for (int i=min; i<=max; i++) word[i-min] = board[i][x].getLetter().getValue(); String s = new String(word); lastWord = s; if (!d.checkWord(s)) { Dialog d = new Dialog("Word "+s+" not found in Dictionary"); return false; } if (wordsMade.contains(s)) { Dialog d = new Dialog("Word "+s+" already on board."); return false; } } for (int i=0; imin) { char[] word = new char[max-min+1]; for (int i=min; i<=max; i++) word[i-min] = board[y][i].getLetter().getValue(); String s = new String(word); lastWord = s; if (!d.checkWord(s)) { Dialog d = new Dialog("Word "+s+" not found in Dictionary"); return false; } if (wordsMade.contains(s)) { Dialog d = new Dialog("Word "+s+" already on board."); return false; } } for (int i=0; i15 || y+l*yf>15) return false; // System.out.println("Checking "+x+" "+y+" "+l+" dir "+c); boolean found = false; for (int i=0; i=0 && y1>=0) if (board[x1][y1].isOccupied()) return true; if (x2<=14 && y2<=14) if (board[x2][y2].isOccupied()) return true; } return false; } public boolean computerValid(String wd, char m, int x, int y, char c) { int occ=0; int min, max; if (wordsMade.contains(wd)) return false; int wl = wd.length(); // System.out.print("Validating "+wd+" w/o "+m); if (c=='x') { if (y+wl>15) return false; if (y>0 && board[x][y-1].isOccupied()) return false; if (y+wl<15 && board[x][y+wl].isOccupied()) return false; for (int z=y; z1) return false; for (int i=0; i15) return false; if (x>0 && board[x-1][y].isOccupied()) return false; if (x+wl<15 && board[x+wl][y].isOccupied()) return false; for (int z=x; z1) return false; for (int i=0; i0 && board[x][y-1].isOccupied()) return false; if (y+wl<15 && board[x][y+wl].isOccupied()) return false; for (int i=0; i0 && board[x-1][y].isOccupied()) return false; if (x+wl<15 && board[x+wl][y].isOccupied()) return false; for (int i=0; i=7) potential -= 10; } else if (c=='y' && !(y==0 || y==7 || y==14)) { if (x==0 || x+wl==14) potential -= 10; if ((0=7) potential -= 10; } } // exposure of double words : -3 // for every blank used : -3 int count = 0; for (int i=0; i14 || y+i*yf>14) return; while (board[x+i*xf][y+i*yf].isOccupied()) { lastWord = lastWord.concat(board[x+i*xf][y+i*yf].getLetter().getSValue()); i++; if (x+i*xf>14 || y+i*yf>14) return; } } public void finishWindow() { finishFrame = new Frame("Scrabble - Game Over"); finishFrame.setLayout(new BorderLayout()); Panel panel = new Panel(); Panel buttonPanel = new Panel(); int hu = scoreScreen.getScore("human"); int co = scoreScreen.getScore("computer"); String s; if (hu>co) s = new String(human.getName()+" wins!!!"); else if (hu")); Button closeButton = new Button("Close"); // Button restartButton = new Button("Restart"); closeButton.addActionListener(this); // restartButton.addActionListener(this); buttonPanel.add(closeButton); // buttonPanel.add(restartButton); finishFrame.add("South", buttonPanel); finishFrame.add("North", panel); finishFrame.pack(); finishFrame.setVisible(true); } public boolean finishCheck() { if (terminatingCondition.equals("25 Moves")) { if (humanMoves == 25 && compMoves == 25) { finishWindow(); return true; } else return false; } else if (terminatingCondition.equals("10 Moves")) { if (humanMoves == 10 && compMoves == 10) { finishWindow(); return true; } else return false; } else if (terminatingCondition.equals("15 Moves")) { if (humanMoves == 15 && compMoves == 15) { finishWindow(); return true; } else return false; } else if (terminatingCondition.equals("20 Moves")) { if (humanMoves == 20 && compMoves == 20) { finishWindow(); return true; } else return false; } else if (terminatingCondition.equals("100 Points")) { if (scoreScreen.getScore("human")>=100 || scoreScreen.getScore("computer") >= 100) { finishWindow(); return true; } else return false; } else if (terminatingCondition.equals("150 Points")) { if (scoreScreen.getScore("human")>=150 || scoreScreen.getScore("computer") >= 150) { finishWindow(); return true; } else return false; } else if (terminatingCondition.equals("200 Points")) { if (scoreScreen.getScore("human")>=200 || scoreScreen.getScore("computer") >= 200) { finishWindow(); return true; } else return false; } else if (terminatingCondition.equals("250 Points")) { if (scoreScreen.getScore("human")>=250 || scoreScreen.getScore("computer") >= 250) { finishWindow(); return true; } else return false; } else if (letterBag.getSize() < 1) { finishWindow(); return true; } else return false; } public void askBlank() { disableButtons(); letterScreen.removeMouseListener(this); blankFrame = new Frame("Blank"); blankFrame.setLayout(new GridLayout(3,1)); blankFrame.add(new Label("Enter character")); blankText = new TextField(); blankFrame.add(blankText); Button blankButton = new Button("Enter"); blankFrame.add(blankButton); blankButton.addActionListener(this); blankFrame.pack(); blankFrame.setVisible(true); } public void refresh() { screen.update(screen.getGraphics()); scoreScreen.update(scoreScreen.getGraphics()); letterScreen.update(letterScreen.getGraphics()); } public void actionPerformed(ActionEvent a) { if (a.getActionCommand().equals("Done")) { human = new HumanPlayer(t.getText().trim(), letterBag); String hum = t.getText().trim(); computer = new ComputerPlayer(new String("Moistone"), 5, this, letterBag); computer.setShow(showComputer.getState()); show = showComputer.getState(); boolean humanStarts = !computerStarts.getState(); changedBoard = modifiedBoard.getState(); computer.setDifficulty(level.getSelectedCheckbox().getLabel()); terminatingCondition = terminator.getSelectedItem(); String dn = dictName.getText().trim(); boolean nocaps = noCaps.getState(); // Dialog dd = new Dialog ("Please wait while dictionary is loaded."); nameFrame.dispose(); d = new ScrabbleDictionary(dn, nocaps); Scrabbler(humanStarts, hum, changedBoard); } if (a.getActionCommand().equals("Quit")) { frame.dispose(); System.exit(0); } if (a.getActionCommand().equals("Play")) { if (!letterPlaced) return; letterPlaced = false; if (valid()) { recordMove("human"); human.clear(); screen.update(screen.getGraphics()); scoreScreen.update(scoreScreen.getGraphics()); letterScreen.update(letterScreen.getGraphics()); disableButtons(); humanMoves++; if (!finishCheck()) { if (!computer.play()) { Dialog d = new Dialog("Moistone has nothing to play. Click when done."); } if (show) computerLetters.setText(computer.getSLetters()); compMoves++; if (!finishCheck()) enableButtons(); } } else { restoreHuman(); } screen.update(screen.getGraphics()); scoreScreen.update(scoreScreen.getGraphics()); letterScreen.update(letterScreen.getGraphics()); } if (a.getActionCommand().equals("Change")) { if (letterSelected) return; restoreHuman(); letterPlaced = false; if (human.changeHuman()) { screen.update(screen.getGraphics()); scoreScreen.update(scoreScreen.getGraphics()); letterScreen.update(letterScreen.getGraphics()); disableButtons(); humanMoves++; if (!finishCheck()) { if (!computer.play()) { Dialog d = new Dialog("Moistone has nothing to play. Click when done."); } if (show) computerLetters.setText(computer.getSLetters()); compMoves++; if (!finishCheck()) enableButtons(); } screen.update(screen.getGraphics()); scoreScreen.update(scoreScreen.getGraphics()); letterScreen.update(letterScreen.getGraphics()); } } if (a.getActionCommand().equals("Restore")) { letterPlaced = false; if (letterSelected) human.addLetter((Letter)selectedLetters.lastElement()); selectedLetters.removeElement((Letter)selectedLetters.lastElement()); letterSelected = false; restoreHuman(); screen.update(screen.getGraphics()); scoreScreen.update(scoreScreen.getGraphics()); letterScreen.update(letterScreen.getGraphics()); } if (a.getActionCommand().equals("Help")) { HelpFrame h = new HelpFrame(); } if (a.getActionCommand().equals("Exit")) { nameFrame.dispose(); System.exit(0); } if (a.getActionCommand().equals("Close")) { frame.dispose(); finishFrame.dispose(); System.exit(0); } // if (a.getActionCommand().equals("Restart")) { // frame.dispose(); // finishFrame.dispose(); // Runtime.exec("java Scrabble"); // System.exit(0); // } if (a.getActionCommand().equals("Enter")) { Letter l = (Letter) selectedLetters.lastElement(); String s = blankText.getText().trim().toUpperCase(); if (s.length()==0) return; char c = s.charAt(0); // System.out.println("Setting "+c); l.setValue(c); enableButtons(); letterScreen.addMouseListener(this); blankFrame.dispose(); } if (a.getActionCommand().equals("Finish")) { scoreScreen.addHuman(human.finish()); scoreScreen.addComputer(computer.finish()); scoreScreen.update(scoreScreen.getGraphics()); disableButtons(); finishButton.removeActionListener(this); restoreButton.removeActionListener(this); finishWindow(); } } public void mouseClicked (MouseEvent m) { m.consume(); int x = m.getX(); int y = m.getY(); if (m.getComponent() == screen) { int i = x/40; int j = y/40; Cell c = board[i][j]; if (letterSelected && c.isOccupied()) return; if (letterSelected) { Letter l = (Letter)selectedLetters.lastElement(); c.setLetter(l); human.chooseLetter(l, i, j); letterPlaced = true; if (l.getLetter()==' ') askBlank(); } letterSelected = false; } else if (m.getComponent() == letterScreen) { if (letterSelected) return; if (selectedLetters==null) selectedLetters = new Vector(); if (selectedLetters.size()==0) selectedLetters = new Vector(); letterSelected = true; int i = x/40; if (i>=human.getLetters().size()) return; selectedLetters.addElement((Letter)human.getLetters().elementAt(i)); human.removeLetter((Letter)human.getLetters().elementAt(i)); } screen.update(screen.getGraphics()); scoreScreen.update(scoreScreen.getGraphics()); letterScreen.update(letterScreen.getGraphics()); } public void mousePressed (MouseEvent m) { } public void mouseReleased (MouseEvent m) { } public void mouseEntered (MouseEvent m) { } public void mouseExited (MouseEvent m) { } }