/* * Created on Jan 25, 2005 * Modified on Feb 26, 2005 * *@author John * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class HurryGame extends JApplet implements ActionListener, MouseListener { HurryLogic player1 = new HurryLogic(); HurryLogic player2 = new HurryLogic(); Choice choice = new Choice(); JButton submitButton = new JButton("Submit Color"); JButton rollButton = new JButton("Roll"); String selected = null; private String rollColor; private String prevColor; private int currentSpace; boolean begin = true; boolean again = false; boolean p1 = true; boolean p2 = false; boolean wasWhite = false; int prevRollPic = -1; int rollPic = -1; int cardPic = -1; Image pics[] = new Image[4]; Image dice[] = new Image[6]; Image cards[] = new Image[12]; EasyReader reader = new EasyReader(); int numGamePieces = 60; String card; public void init() { Container c = getContentPane(); JPanel topPanel = new JPanel(); JPanel bottomPanel = new JPanel(); JPanel anotherPanel = new JPanel(); choice.add("Yellow"); choice.add("Red"); choice.add("Orange"); choice.add("Blue"); choice.add("Green"); topPanel.add(choice); bottomPanel.add(submitButton); anotherPanel.add(rollButton); c.add(topPanel, BorderLayout.NORTH); c.add(bottomPanel, BorderLayout.SOUTH); c.add(anotherPanel, BorderLayout.EAST); c.addMouseListener(this); submitButton.addActionListener(this); rollButton.addActionListener(this); pics[0] = getImage(getCodeBase(),"board.jpg"); pics[1] = getImage(getCodeBase(),"gamepiece1.jpg"); pics[2] = getImage(getCodeBase(),"gamepiece2.jpg"); pics[3] = getImage(getCodeBase(),"gamepiece3.jpg"); dice[0] = getImage(getCodeBase(),"yellowdie.jpg"); dice[1] = getImage(getCodeBase(),"greendie.jpg"); dice[2] = getImage(getCodeBase(),"orangedie.jpg"); dice[3] = getImage(getCodeBase(),"bluedie.jpg"); dice[4] = getImage(getCodeBase(),"reddie.jpg"); dice[5] = getImage(getCodeBase(),"whitedie.jpg"); cards[0] = getImage(getCodeBase(),"busstop.jpg"); cards[1] = getImage(getCodeBase(),"postoffice.jpg"); cards[2] = getImage(getCodeBase(),"petstore.jpg"); cards[3] = getImage(getCodeBase(),"firestation.jpg"); cards[4] = getImage(getCodeBase(),"painter.jpg"); cards[5] = getImage(getCodeBase(),"vegetablestand.jpg"); cards[6] = getImage(getCodeBase(),"policelady.jpg"); cards[7] = getImage(getCodeBase(),"petstore.jpg"); cards[8] = getImage(getCodeBase(),"farm.jpg"); cards[9] = getImage(getCodeBase(),"fisherman.jpg"); cards[10] = getImage(getCodeBase(),"train.jpg"); cards[11] = getImage(getCodeBase(),"icecream.jpg"); } public void paint(Graphics g) { System.out.println("p1 is " + p1); System.out.println("p2 is " + p2); super.paint(g); if(!again) { begin = true; } if(begin) { g.drawImage(pics[0],80,100,this); //the board g.setColor(Color.darkGray); g.drawString("Your previous roll:",582,480); g.drawString("Your current roll: ",582,110); g.setColor(Color.red); g.drawString("You're on a " + prevColor, 50, 40); g.drawString("You rolled a " + rollColor, 50, 60); g.setColor(Color.green); g.drawString("Player 1 space number " + player1.getCurrentSpace(), 250, 40); g.drawString("Player 2 space number " + player2.getCurrentSpace(), 250, 60); if (prevRollPic != -1) { g.drawImage(dice[prevRollPic],600,520,this); } if (rollPic != -1) { g.drawImage(dice[rollPic],600,150,this); } if (cardPic != -1) { g.drawImage(cards[cardPic],580,600,this); cardPic = -1; } g.setColor(Color.orange); g.drawString("Last card drawn was the " + card, 550, 50); if (p1) { g.setColor(Color.blue); g.drawString("Roll Player 1", 600, 300); } else { g.setColor(Color.blue); g.drawString("Roll Player 2", 600, 300); } } if (again) { if(player1.getCurrentSpace() >= 60 || player2.getCurrentSpace() >= 60) { if(p2) { g.drawString("YOU WIN PLAYER 1",200,300); } if(p1) { g.drawString("YOU WIN PLAYER 2",200,300); } } else { if(p1) { player1.rollDie(); System.out.println("I have rolled player1's die"); player1.rollToColor(); System.out.println("Player1 roll converted to color"); rollColor = player1.getRollColor(); System.out.println("Player1's roll color is" + rollColor); } else { player2.rollDie(); System.out.println("I have rolled player2's die"); player2.rollToColor(); System.out.println("Player2 roll converted to color"); rollColor = player2.getRollColor(); System.out.println("Player2's roll color is" + rollColor); } System.out.println("before if rollcolor equals color is " + rollColor); if (rollColor.equals("yellow")) //These if statements plot the corresponding { //...color on the current roll die rollPic = 0; } else if (rollColor.equals("green")) { rollPic = 1; } else if (rollColor.equals("orange")) { rollPic = 2; } else if (rollColor.equals("blue")) { rollPic = 3; } else if (rollColor.equals("red")) { rollPic = 4; } else if (rollColor.equals("white")) { rollPic = 5; if(p1) { wasWhite = true; player1.changeWhite(); //roll color and card name updated card = player1.getCardName(); currentSpace = player1.getCurrentSpace(); System.out.println("Currentspace before moveWhite is" + currentSpace); player1.moveWhite(); currentSpace = player1.getCurrentSpace(); System.out.println("Currentspace after moveWhite is" + currentSpace); } else { wasWhite = true; player2.changeWhite(); //roll color and card name updated card = player2.getCardName(); currentSpace = player2.getCurrentSpace(); System.out.println("Currentspace before moveWhite is" + currentSpace); player2.moveWhite(); currentSpace = player2.getCurrentSpace(); System.out.println("Currentspace after moveWhite is" + currentSpace); } System.out.println(card); if (card.equals("busstop")) { cardPic = 0; } else if (card.equals("postoffice")) { cardPic = 1; } else if (card.equals("petstore")) { cardPic = 2; } else if (card.equals("firestation")) { cardPic = 3; } else if (card.equals("painter")) { cardPic = 4; } else if (card.equals("vegetablestand")) { cardPic = 5; } else if (card.equals("policelady")) { cardPic = 6; } else if (card.equals("toystore")) { cardPic = 7; } else if (card.equals("farm")) { cardPic = 8; } else if (card.equals("fisherman")) { cardPic = 9; } else if (card.equals("train")) { cardPic = 10; } else if (card.equals("icecreamstand")) { cardPic = 11; } } if(p1) { System.out.println("previous color before change is" + prevColor); prevColor = player1.getPreviousRollColor(); System.out.println("prev color after change is " + prevColor); if (wasWhite) { } else { if (prevColor.equals("yellow")) //plots the previous roll color and... { //...updates player's space on the board player1.moveYellow(); prevRollPic = 0; } else if (prevColor.equals("green")) { player1.moveGreen(); prevRollPic = 1; } else if (prevColor.equals("orange")) { player1.moveOrange(); prevRollPic = 2; } else if (prevColor.equals("blue")) { player1.moveBlue(); prevRollPic = 3; } else if (prevColor.equals("red")) { player1.moveRed(); prevRollPic = 4; } else if (prevColor.equals("white")) { prevRollPic = 5; } } wasWhite = false; player1.setToPreviousRoll(); } else if (p2) { prevColor = player2.getPreviousRollColor(); if (wasWhite) { } else { if (prevColor.equals("yellow")) //plots the previous roll color and... { //...updates player's space on the board player2.moveYellow(); prevRollPic = 0; } else if (prevColor.equals("green")) { player2.moveGreen(); prevRollPic = 1; } else if (prevColor.equals("orange")) { player2.moveOrange(); prevRollPic = 2; } else if (prevColor.equals("blue")) { player2.moveBlue(); prevRollPic = 3; } else if (prevColor.equals("red")) { player2.moveRed(); prevRollPic = 4; } else if (prevColor.equals("white")) { prevRollPic = 5; } } wasWhite = false; player2.setToPreviousRoll(); } g.setColor(Color.darkGray); g.drawString("Your previous roll:",582,480); g.drawString("Your current roll: ",582,110); g.drawImage(pics[0],80,100,this); //the board g.setColor(Color.green); System.out.println("P1 Space number " + player1.getCurrentSpace()); System.out.println("P2 Space number " + player2.getCurrentSpace()); g.setColor(Color.red); g.drawString("You're on a " + prevColor, 50, 40); g.drawString("You rolled a " + rollColor, 50, 60); g.setColor(Color.green); g.drawString("Player 1 space number " + player1.getCurrentSpace(), 250, 40); g.drawString("Player 2 space number " + player2.getCurrentSpace(), 250, 60); if (prevRollPic != -1) { g.drawImage(dice[prevRollPic],600,520,this); } if (rollPic != -1) { g.drawImage(dice[rollPic],600,150,this); } if (cardPic != -1) { g.drawImage(cards[cardPic],580,600,this); cardPic = -1; } g.setColor(Color.orange); g.drawString("Last card drawn was the " + card, 550, 50); again = false; if (p1) { g.setColor(Color.blue); g.drawString("Roll Player 2", 600, 300); p1 = false; p2 = true; } else { g.setColor(Color.blue); g.drawString("Roll Player 1", 600, 300); p1 = true; p2 = false; } } } } public void cardDraw() { } public void actionPerformed(ActionEvent e) { JButton button = (JButton)e.getSource(); if (button == submitButton) { selected = choice.getSelectedItem(); } else if (button == rollButton) { begin = false; again = true; } repaint(); } public void mouseClicked(MouseEvent e) { int x = e.getX(); int y = e.getY(); System.out.println(x + " " + y); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } }