0 Replies - 5955 Views - Last Post: 17 May 2008 - 05:44 PM Rate Topic: -----

#1 emblem   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 16-April 08

Trying to make a rock paper scissors game.

Post icon  Posted 17 May 2008 - 05:44 PM

I'm trying to make a rock paper scissors game with an applet. I've made one before but that was a text only game. I'm trying to make on in an applet. I'm using a null layout and I"ve made the frame and everything but I'm trying to figure out how to implement the set of rules of the game with the action listeners.

//Import packages
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;

public class PRS
{
	static JFrame frame1;
	static Container pane;
	static JButton btnPaper, btnRock, btnScissors, btnPaper2, btnRock2,btnScissors2;
	static JLabel lblPlayer1, lblPlayer2;
	static Insets insets;
	static String Player1, Player2;
	static String choice1, choice2;

	
	//method for creating a class
	public static void createControls (String P1, String P2)
	{
		 Player1 = P1;
		 Player2 = P2;
		 
		//Create controls
		btnPaper = new JButton ("Paper");
		btnRock = new JButton ("Rock");
		btnScissors = new JButton ("Scissors");
		btnPaper2 = new JButton ("Paper");
		btnRock2 = new JButton ("Rock");
		btnScissors2 = new JButton ("Scissors");
		lblPlayer1 = new JLabel (Player1);
		lblPlayer2 = new JLabel ( Player2);
	   
	}
	
	//Rules for the game
	public static void rules(String choice, String choice2)
	{
		
		
		if (choice1 == "r" && choice2 == choice1 )
		{
			System.out.println ("Tie game.");
		}
		
	
		
		
	
	

	}
	public static void main(String args[])
	{
		Scanner input = new Scanner(System.in);
		 PRS prs = new PRS();
		 
		String P1;
		String P2;
		//Set Look and Feel
		try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
		catch (ClassNotFoundException e) {}
		catch (InstantiationException e) {}
		catch (IllegalAccessException e) {}
		catch (UnsupportedLookAndFeelException e) {}
		
 
		//Create the frame
		frame1 = new JFrame ("Rock!! Paper!! Scissors!!");
		frame1.setSize (800,600);
		pane = frame1.getContentPane();
		insets = pane.getInsets();
		pane.setLayout (null);
		
		System.out.println ("Enter Player one's name");
		P1 = input.next();
		System.out.println ("Enter Player two's name.");
		P2 = input.next();
		
		
		PRS.createControls(P1, P2);
		
		//Add all components to panel
		pane.add (lblPlayer1);
		pane.add (lblPlayer2);
		pane.add (btnRock);
		pane.add (btnPaper);
		pane.add (btnScissors);
		pane.add (btnRock2);
		pane.add (btnPaper2);
		pane.add (btnScissors2);
		
		
		
		//Place all the components
		lblPlayer1.setBounds (insets.left +100,insets.top +5,lblPlayer1.getPreferredSize().width, lblPlayer1.getPreferredSize().height);
		lblPlayer2.setBounds (insets.right +600, insets.top +5,lblPlayer2.getPreferredSize().width,lblPlayer2.getPreferredSize().height);
		btnRock.setBounds 	 (insets.left +25, insets.top + 50, btnRock.getPreferredSize().width,btnRock.getPreferredSize().height);
		btnPaper.setBounds   (btnRock.getX() + btnRock.getWidth()+5,insets.top +50,btnPaper.getPreferredSize().width, btnPaper.getPreferredSize().height);
		btnScissors.setBounds   (btnPaper.getX() + btnPaper.getWidth()+5,insets.top +50,btnScissors.getPreferredSize().width, btnScissors.getPreferredSize().height);
		btnRock2.setBounds 	 (insets.left +525, insets.top + 50, btnRock2.getPreferredSize().width,btnRock2.getPreferredSize().height);
		btnPaper2.setBounds   (btnRock2.getX() + btnRock2.getWidth()+5,insets.top +50,btnPaper2.getPreferredSize().width, btnPaper2.getPreferredSize().height);
		btnScissors2.setBounds   (btnPaper2.getX() + btnPaper2.getWidth()+5,insets.top +50,btnScissors2.getPreferredSize().width, btnScissors2.getPreferredSize().height);
		//Set frame visible.
		frame1.setVisible (true);
		
		//Button's action
		btnRock.addActionListener(new btnRockAction()); //Register action
		btnPaper.addActionListener(new btnPaperAction());
		btnScissors.addActionListener(new btnScissorsAction());
		btnRock2.addActionListener(new btnRock2Action());
		btnScissors2.addActionListener(new btnScissors2Action());
		btnPaper2.addActionListener(new btnPaper2Action());
		
		
		
		
		prs.rules(choice1, choice2);
	   
	}
	
	 
	//Player 1 action listeners
	 public static class btnRockAction implements ActionListener
	 {
		public void actionPerformed (ActionEvent e)
		{
			choice1= "r";
			
		   }
	  }
		   
	 public static class btnPaperAction implements ActionListener
	 {
		public void actionPerformed (ActionEvent e)
		{
			choice1= "p";
			
		   }
		   
	 }
	 
	 public static class btnScissorsAction implements ActionListener
	 {
	 	public void actionPerformed (ActionEvent e)
	 	{
	 		choice1= "s";
	 	}
	 }
	 
	 
	 //player 2 action listeners
	 public static class btnRock2Action implements ActionListener
	 {
		public void actionPerformed (ActionEvent e)
		{
			choice2= "r";
			
		   }
	  }
		   
	 public static class btnPaper2Action implements ActionListener
	 {
		public void actionPerformed (ActionEvent e)
		{
			choice2= "p";
			
		   }
		   
	 }
	 
	 public static class btnScissors2Action implements ActionListener
	 {
	 	public void actionPerformed (ActionEvent e)
	 	{
	 		choice2= "s";
	 	}
	 }
	 
	 
}



I thought of putting the rules as a method and then just calling it in the program.
prs.rules(choice1, choice2);


The thing is don't I have to put that code in after this part?
//Player 1 action listeners
	 public static class btnRockAction implements ActionListener
	 {
		public void actionPerformed (ActionEvent e)
		{
			choice1= "r";
			
		   }
	  }
		   
	 public static class btnPaperAction implements ActionListener
	 {
		public void actionPerformed (ActionEvent e)
		{
			choice1= "p";
			
		   }
		   
	 }
	 
	 public static class btnScissorsAction implements ActionListener
	 {
	 	public void actionPerformed (ActionEvent e)
	 	{
	 		choice1= "s";
	 	}
	 }
	 
	 
	 //player 2 action listeners
	 public static class btnRock2Action implements ActionListener
	 {
		public void actionPerformed (ActionEvent e)
		{
			choice2= "r";
			
		   }
	  }
		   
	 public static class btnPaper2Action implements ActionListener
	 {
		public void actionPerformed (ActionEvent e)
		{
			choice2= "p";
			
		   }
		   
	 }
	 
	 public static class btnScissors2Action implements ActionListener
	 {
	 	public void actionPerformed (ActionEvent e)
	 	{
	 		choice2= "s";
	 	}
	 }
	 
	 
}



But that gives me an error if I try and put the code in that section. I'm just trying to see how to implement the set of rules with the action listeners is my main problem

Is This A Good Question/Topic? 0
  • +

Page 1 of 1