/** This class is the main GUI class which calls all other GUI classes * * @author Kristof Friedrichsen * @author Ryan Mann * @author Bob Free * @author Pat O'Neill * @version December 15, 2006 */ package chutesladder; import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JOptionPane; import javax.swing.ImageIcon; import javax.swing.border.EtchedBorder; import javax.swing.border.TitledBorder; public class CaL extends JFrame implements ActionListener, Runnable { static BackgroundImage board; static ImageIcon blue = new ImageIcon("src/Resources/Icons/blue.jpg"); static ImageIcon green = new ImageIcon("src/Resources/Icons/green.jpg"); static ImageIcon red = new ImageIcon("src/Resources/Icons/red.jpg"); static ImageIcon yellow = new ImageIcon("src/Resources/Icons/yellow.jpg"); static ImageIcon GB= new ImageIcon("src/Resources/Icons/greenblue.jpg"); static ImageIcon BR = new ImageIcon("src/Resources/Icons/bluered.jpg"); static ImageIcon YB = new ImageIcon("src/Resources/Icons/yellowblue.jpg"); static ImageIcon BRG = new ImageIcon("src/Resources/Icons/blueredgreen.jpg"); static ImageIcon YBG = new ImageIcon("src/Resources/Icons/yellowbluegreen.jpg"); static ImageIcon YBR = new ImageIcon("src/Resources/Icons/yellowbluered.jpg"); static ImageIcon GR = new ImageIcon("src/Resources/Icons/greenred.jpg"); static ImageIcon YG = new ImageIcon("src/Resources/Icons/yellowgreen.jpg"); static ImageIcon YRG = new ImageIcon("src/Resources/Icons/yellowredgreen.jpg"); static ImageIcon YR = new ImageIcon("src/Resources/Icons/yellowred.jpg"); static ImageIcon all = new ImageIcon("src/Resources/Icons/all.jpg"); static int player[] = new int[4]; // cell player is located static int playerRow[] = new int[4]; // row player is located static ImageIcon color[] = new ImageIcon[4]; // player's icon static String playerName[] = new String[4]; // player's name String inputLine = ""; static public int myNumber = 0; //set by server static public int numberOfPlayers = 0; int me; // number to be assigned by server public static String host = "moxie.oswego.edu"; public static Integer port = 2699; public static String Ghost = "moxie.oswego.edu"; public static Integer Gport = 2698; public static Integer valueSpun = 0; public static BufferedReader Gin; public static PrintWriter Gout; public static int playerTurn=0; protected Thread Glistener; static JOptionPane Turn = new JOptionPane(); static public void setPlayerInformation(int pn, String p1, String p2, String p3, String p4, int nump) { myNumber = pn; playerName[0]=p1; playerName[1]=p2; playerName[2]=p3; playerName[3]=p4; numberOfPlayers = nump; System.out.println("Info"+myNumber+" "+playerName[0]+" "+playerName[1]+" "+playerName[2]+" "+playerName[3]+" "+numberOfPlayers); GameHistory.gameHistory.append("\nWelcome to the game " +playerName[pn]+"!"); GameHistory.gameHistory.setCaretPosition( GameHistory.gameHistory.getDocument().getLength() ); if (myNumber == 0) { GamePlayPanel.SetButtonTrue(); Turn.setSize(100,100); Turn.setVisible(true); Turn.showMessageDialog(null,"Go ahead and start the game, "+playerName[0]); } else { GamePlayPanel.SetButtonFalse(); } } public CaL(String title, InputStream Giin, OutputStream Giout, int colorNum) throws UnknownHostException, IOException { super(title); this.Gin = new BufferedReader(new InputStreamReader(Giin)); this.Gout = new PrintWriter( new OutputStreamWriter(Giout)); Glistener = new Thread(this); Glistener.start(); color[0]=blue; color[1]=green; color[2]=red; color[3]=yellow; player[0]=-1; player[1]=-1; player[2]=-1; player[3]=-1; playerRow[0]=9; playerRow[1]=9; playerRow[2]=9; playerRow[3]=9; /* Creating the JFrame and setting its layout */ setTitle("Chutes and Ladders"); setSize(1024,768); setLocationRelativeTo(null); setLayout(new BorderLayout()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /* The code for creating a simple menu bar */ JMenuBar menuBar = new JMenuBar(); JMenu game = new JMenu("Help"); JMenuItem quit = new JMenuItem("Quit"); JMenuItem manual = new JMenuItem("Manual"); quit.addActionListener(this); manual.addActionListener(this); menuBar.add(game); game.add(manual); game.addSeparator(); game.add(quit); setJMenuBar(menuBar); /* Creating four JPanels containing the board image, the chat box, * the game history box and the spinner */ JPanel boardPanel = new JPanel(); board = new BackgroundImage(); boardPanel.add(BorderLayout.WEST,board); GameHistory gh = new GameHistory(); Socket s; s = new Socket(host, port); Chat ch = new Chat(title+": ", s.getInputStream(), s.getOutputStream()); GamePlayPanel gp = new GamePlayPanel(); /* Creating borders around the chat box, the game history box and the spinner */ TitledBorder chatBorder = new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED), "Chat"); TitledBorder ghBorder = new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED), "Game History"); TitledBorder gpBorder = new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED), "Spinner"); TitledBorder pBorder = new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED),"Information"); TitledBorder boardBorder = new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED),"Your Player Color:"); gh.setBorder(ghBorder); gp.setBorder(gpBorder); ch.setBorder(chatBorder); //set color for player boardPanel.setBorder(boardBorder); if ( colorNum == 0 ) { boardPanel.setBackground(Color.BLUE); } // end if ( colorNum == 0 ) if ( colorNum == 1 ) { boardPanel.setBackground(Color.GREEN); } // end if ( colorNum == 1 ) if ( colorNum == 2 ) { boardPanel.setBackground(Color.RED); } // end if ( colorNum == 3 ) if ( colorNum == 3 ) { boardPanel.setBackground(Color.YELLOW); } // end if ( colorNum == 3 ) /* Creating a JPanel, adding all three non-board JPanels to it and setting its layout */ JPanel peripherals = new JPanel(); peripherals.setLayout(new BorderLayout()); peripherals.add(BorderLayout.NORTH, gh); peripherals.add(BorderLayout.CENTER, gp); peripherals.add(BorderLayout.SOUTH, ch); peripherals.setBorder(pBorder); /* Adding all JPanels to the JFrame */ add(BorderLayout.WEST, boardPanel); add(BorderLayout.EAST, peripherals); pack(); setVisible(true); } // this method either sets the moved icon or removes your icon // and allows for any number of players to be on one spot // it looks crazy but it's a great set of nested ifs static void setIcons(int flag, int me) { for ( int x=0; x<4; x++ ) { if ( player[x]>-1) { if ( player[0]==player[x] && playerRow[0]==playerRow[x] ) { if ( player[1]==player[x] && playerRow[1]==playerRow[x] ) { if ( player[2]==player[x] && playerRow[2]==playerRow[x] ) { if ( player[3]==player[x] && playerRow[3]==playerRow[x] ) { board.LabelArray[playerRow[x]][player[x]].setIcon(all); } else board.LabelArray[playerRow[x]][player[x]].setIcon(BRG); } else if ( player[3]==player[x] && playerRow[3]==playerRow[x] ) { board.LabelArray[playerRow[x]][player[x]].setIcon(YBG); } else board.LabelArray[playerRow[x]][player[x]].setIcon(GB); } else if ( player[2]==player[x] && playerRow[2]==playerRow[x] ) { if ( player[3]==player[x] && playerRow[3]==playerRow[x] ) { board.LabelArray[playerRow[x]][player[x]].setIcon(YBR); } else board.LabelArray[playerRow[x]][player[x]].setIcon(BR); } else if ( player[3]==player[x] && playerRow[3]==playerRow[x] ) { board.LabelArray[playerRow[x]][player[x]].setIcon(YB); } else if (flag==0) board.LabelArray[playerRow[x]][player[x]].setIcon(blue); else board.LabelArray[playerRow[me]][player[me]].setIcon(null); } else if ( player[1]==player[x] && playerRow[1]==playerRow[x] ) { if ( player[2]==player[x] && playerRow[2]==playerRow[x] ) { if ( player[3]==player[x] && playerRow[3]==playerRow[x] ) { board.LabelArray[playerRow[x]][player[x]].setIcon(YRG); } else board.LabelArray[playerRow[x]][player[x]].setIcon(GR); } else if ( player[3]==player[x] && playerRow[3]==playerRow[x] ) { board.LabelArray[playerRow[x]][player[x]].setIcon(YG); } else if (flag==0) board.LabelArray[playerRow[x]][player[x]].setIcon(green); else board.LabelArray[playerRow[me]][player[me]].setIcon(null); } else if ( player[2]==player[x] && playerRow[2]==playerRow[x] ) { if ( player[3]==player[x] && playerRow[3]==playerRow[x] ) { board.LabelArray[playerRow[x]][player[x]].setIcon(YR); } else if (flag==0) board.LabelArray[playerRow[x]][player[x]].setIcon(red); else board.LabelArray[playerRow[me]][player[me]].setIcon(null); } else if ( player[3]==player[x] && playerRow[3]==playerRow[x] ) { if (flag==0) board.LabelArray[playerRow[x]][player[x]].setIcon(yellow); else board.LabelArray[playerRow[me]][player[me]].setIcon(null); } } } } static void movePlayer(int x, int me) { valueSpun = x; GameHistory.showMove(playerName[me],x); int check = player[me] + x; if ( player[me] >= 0 ) setIcons(1, me); if ( playerRow[me]==9 || playerRow[me]==7 || playerRow[me]==5 || playerRow[me]==3 || playerRow[me]==1) { if ( check==10 ) { player[me] = 9; playerRow[me] --; checkChuteOrLadder(me); } else if ( check ==11 ) { player[me] = 8; playerRow[me] --; checkChuteOrLadder(me); } else if ( check ==12) { player[me] = 7; playerRow[me] --; checkChuteOrLadder(me); } else if ( check ==13 ) { player[me] = 6; playerRow[me] --; checkChuteOrLadder(me); } else if ( check ==14) { player[me] = 5; playerRow[me] --; checkChuteOrLadder(me); } else if ( check ==15 ) { player[me] = 4; playerRow[me] --; checkChuteOrLadder(me); } else { player[me] = player[me] + x; checkChuteOrLadder(me); } } else if ( playerRow[me] == 0 ) { if ( player[me] == x) { playerRow[me] = 0; player[me] = 0; setIcons(0, me); GameHistory.gameHistory.append("\n" + playerName[me] + " wins the game!!"); GameHistory.gameHistory.setCaretPosition( GameHistory.gameHistory.getDocument().getLength() ); GamePlayPanel.SetButtonFalse(); checkChuteOrLadder(me); startNewGameFunc(me); } else if (player[me]-x<0) { checkChuteOrLadder(me); } else { player[me] = player[me] - x; checkChuteOrLadder(me); } } else { check = player[me] - x; if ( check == -1 ) { player[me] = 0; playerRow[me] --; checkChuteOrLadder(me); } else if ( check == -2 ) { player[me] = 1; playerRow[me] --; checkChuteOrLadder(me); } else if ( check == -3) { player[me] = 2; playerRow[me] --; checkChuteOrLadder(me); } else if ( check == -4 ) { player[me] = 3; playerRow[me] --; checkChuteOrLadder(me); } else if ( check == -5) { player[me] = 4; playerRow[me] --; checkChuteOrLadder(me); } else if ( check == -6 ) { player[me] = 5; playerRow[me] --; checkChuteOrLadder(me); } else { player[me] = player[me] - x; checkChuteOrLadder(me); } } } static public void checkChuteOrLadder(int me) { GameHistory.gameHistory.setLineWrap(true); if ( playerRow[me]==9 ) { if ( player[me]==0 ) { playerRow[me] = 6; player[me] = 2; GameHistory.gameHistory.append("\n" + playerName[me] + " planted flowers and they grew and look so pretty, and moves up to 38"); GameHistory.gameHistory.setCaretPosition( GameHistory.gameHistory.getDocument().getLength() ); } else if ( player[me]==3 ) { playerRow[me]=8; player[me]=6; GameHistory.gameHistory.append("\n" + playerName[me] + " baked a cake and got to eat it, and moves up to 14"); GameHistory.gameHistory.setCaretPosition( GameHistory.gameHistory.getDocument().getLength() ); } else if ( player[me]==8 ) { playerRow[me]=6; player[me]=9; GameHistory.gameHistory.append("\n" + playerName[me] + " mows the lawn and gets to go to the carnival, and moves up to 31"); GameHistory.gameHistory.setCaretPosition( GameHistory.gameHistory.getDocument().getLength() ); } } else if ( playerRow[me]==8 ) { if ( player[me]==4 ) { playerRow[me]=9; player[me]=5; GameHistory.gameHistory.append("\n" + playerName[me] + " reads a comic book in class and gets punished, and moves down to 6"); GameHistory.gameHistory.setCaretPosition( GameHistory.gameHistory.getDocument().getLength() ); } } else if ( playerRow[me]==7 ) { if ( player[me]==0 ) { playerRow[me]=5; player[me]=1; GameHistory.gameHistory.append("\n" + playerName[me] + " helped a hurt puppy, and moves up to spot 42"); GameHistory.gameHistory.setCaretPosition( GameHistory.gameHistory.getDocument().getLength() ); } else if ( player[me]==7 ) { playerRow[me]=1; player[me]=3; GameHistory.gameHistory.append("\n" + playerName[me] + " rescued a cat from a tree and made a new friend, and moves up to spot 84"); GameHistory.gameHistory.setCaretPosition( GameHistory.gameHistory.getDocument().getLength() ); } } else if ( playerRow[me]==6) { if ( player[me]==4 ) { playerRow[me]=5; player[me]=3; GameHistory.gameHistory.append("\n" + playerName[me] + " finally ate his vegetables and grew tall, and moves up to spot 44"); GameHistory.gameHistory.setCaretPosition( GameHistory.gameHistory.getDocument().getLength() ); } } else if ( playerRow[me]==5) { if ( player[me]==8 ) { playerRow[me]=8; player[me]=9; GameHistory.gameHistory.append("\n" + playerName[me] + " ate too much chocolate and got a stomach ache, and moves down to spot 11"); GameHistory.gameHistory.setCaretPosition( GameHistory.gameHistory.getDocument().getLength() ); } else if ( player[me]==6 ) { playerRow[me]=7; player[me]=5; GameHistory.gameHistory.append("\n" + playerName[me] + " skated on thin ice and fell through, and moves down to spot 26"); GameHistory.gameHistory.setCaretPosition( GameHistory.gameHistory.getDocument().getLength() ); } } else if ( playerRow[me]==4 ) { if ( player[me]==4 ) { playerRow[me]=4; player[me]=7; GameHistory.gameHistory.append("\n" + playerName[me] + " played outside in the rain and got sick, and moves down to spot 53"); GameHistory.gameHistory.setCaretPosition( GameHistory.gameHistory.getDocument().getLength() ); } else if ( player[me]==9 ) { playerRow[me]=3; player[me]=6; GameHistory.gameHistory.append("\n" + playerName[me] + " swept the floor and got to go to the movies, and moves up to spot 67"); GameHistory.gameHistory.setCaretPosition( GameHistory.gameHistory.getDocument().getLength() ); } } else if ( playerRow[me]==3 ) { if ( player[me]==3 ) { playerRow[me]=4; player[me]=0; GameHistory.gameHistory.append("\n" + playerName[me] + " rode his bike with no hands and hurt his arm, and moves down to spot 60"); GameHistory.gameHistory.setCaretPosition( GameHistory.gameHistory.getDocument().getLength() ); } else if ( player[me]==1 ) { playerRow[me]=8; player[me]=1; GameHistory.gameHistory.append("\n" + playerName[me] + " stacked the dishes too high and they all broke, and moves down to spot 19"); GameHistory.gameHistory.setCaretPosition( GameHistory.gameHistory.getDocument().getLength() ); } } else if ( playerRow[me]==2 ) { if ( player[me]==0 ) { playerRow[me]=0; player[me]=0; GameHistory.gameHistory.append("\n" + playerName[me] + " entered his dog in a pet show and won, and moves to spot 100 and wins the game"); GameHistory.gameHistory.setCaretPosition( GameHistory.gameHistory.getDocument().getLength() ); GamePlayPanel.SetButtonFalse(); //added to set button to false String update = ""+me+valueSpun+myNumber+numberOfPlayers; if (me == myNumber) { sendUpdateServer(update); } setIcons(0,me); startNewGameFunc(me); } else if ( player[me]==9 ) { playerRow[me]=0; player[me]=9; GameHistory.gameHistory.append("\n" + playerName[me] + " found a lost purse and got to eat ice cream, and moves up to spot 91"); GameHistory.gameHistory.setCaretPosition( GameHistory.gameHistory.getDocument().getLength() ); } } else if ( playerRow[me]==1 ) { if (player[me]==6 ) { playerRow[me]=7; player[me]=3; GameHistory.gameHistory.append("\n" + " Who tried to steal a cookie from the cookie jar? " + playerName[me] + " that's who, and falls down to spot 24"); GameHistory.gameHistory.setCaretPosition( GameHistory.gameHistory.getDocument().getLength() ); } } else if ( playerRow[me]==0 ) { if ( player[me]==7 ) { playerRow[me]=2; player[me]=7; GameHistory.gameHistory.append("\n" + playerName[me] + " drew on the wall and had to clean it, and moves down to spot 73"); GameHistory.gameHistory.setCaretPosition( GameHistory.gameHistory.getDocument().getLength() ); } else if ( player[me]==5 ) { playerRow[me]=2; player[me]=5; GameHistory.gameHistory.append("\n" + playerName[me] + " broke a window and had to pay for it, and moves down to spot 75"); GameHistory.gameHistory.setCaretPosition( GameHistory.gameHistory.getDocument().getLength() ); } else if ( player[me]==2 ) { playerRow[me]=2; player[me]=2; GameHistory.gameHistory.append("\n" + playerName[me] + " pulled his cat's tail and it scratched him all over, and moves down to spot 78"); GameHistory.gameHistory.setCaretPosition( GameHistory.gameHistory.getDocument().getLength() ); } } String update = ""+me+valueSpun+myNumber+numberOfPlayers; if (me == myNumber) { sendUpdateServer(update); } setIcons(0,me); } public void run() { try { while (true) { String line = Gin.readLine(); inputLine = line; int playerNum = Integer.parseInt(inputLine.substring(0,1)); int num = Integer.parseInt(inputLine.substring(1,2)); playerTurn = Integer.parseInt(inputLine.substring(2,3)); System.out.println("Player Turn "+playerTurn); System.out.println("My number "+myNumber); if (playerNum == 6 && playerTurn == myNumber) { GamePlayPanel.SetButtonTrue(); Turn.setSize(100,100); Turn.setVisible(true); Turn.showMessageDialog(null,"Your turn, "+playerName[myNumber]); } else { GameHistory.gameHistory.append("\n"+playerName[playerTurn]+" it is your turn."); GameHistory.gameHistory.setCaretPosition( GameHistory.gameHistory.getDocument().getLength() ); try { Thread.sleep(1000); } catch (InterruptedException ex) { ex.printStackTrace(); } if (playerTurn == myNumber) { GamePlayPanel.SetButtonTrue(); } System.out.println("Recieved update from server. "+line); if (playerNum != myNumber) { movePlayer(num, playerNum); startTimer(); } } } } catch (IOException ex) { ex.printStackTrace(); } finally { Glistener = null; validate(); Gout.close(); } } static public void sendUpdateServer(String up) { Gout.println(up); Gout.flush(); System.out.println("Sending update to server. "+up); GamePlayPanel.SetButtonFalse(); } public void actionPerformed(ActionEvent e) { try { String s = e.getActionCommand(); if(s.equals("Quit")) { System.exit(0); } else if(s.equals("Manual")) { Manual manu = new Manual(); } } catch (NullPointerException ex) {} } public static void startTimer() { int i = 0; while(GamePlayPanel.btSpin.isEnabled()) { if (i == 30) { GamePlayPanel.forceSpin(); GameHistory.gameHistory.append("\n"+playerName[playerTurn]+" it has taken too long to spin!!!"); GameHistory.gameHistory.setCaretPosition( GameHistory.gameHistory.getDocument().getLength() ); } try { Thread.sleep(1000); } catch(Exception e) {} i++; } } public static void startNewGameFunc(int me) { JOptionPane OPT = new JOptionPane(); OPT.setSize(100,100); OPT.setVisible(true); int opt = OPT.showConfirmDialog(null,"Would you like to play again?","Player "+playerName[me]+" has won the game!!!", JOptionPane.YES_NO_OPTION); if(opt==OPT.YES_OPTION) { try { StartFrame.StartNewGame(); } catch (UnknownHostException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } StartFrame.frame.setVisible(false); Gout.close(); } else { System.exit(0); } } }