Can't get game to work

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1274 Views - Last Post: 22 February 2011 - 10:04 AM Rate Topic: -----

#16 ADK2011  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 20-January 11

Re: Can't get game to work

Posted 17 February 2011 - 11:59 AM

I modified the code, now i am getting this error: Exception in thread "main" java.lang.NullPointerException
at rps.<init>(rps.java:76)
at rps.main(rps.java:45)
Java Result: 1.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*; 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author akr065
 */
public class rpsEngine {
private final String[] strWeapons = {"Paper","Rock","Scissors"};

	private int wins = 0, loses = 0, draws = 0, cpuWeapon = 0;


	public void prsEngine ()
	{
	}

	public String play (int playerWeapon)
	{

		cpuWeapon = getRandomNumber();
		String strDisplay = strWeapons[playerWeapon] + " ";

		if (playerWeapon == cpuWeapon)
		{
			strDisplay += "draws";
		}
		else if (playerWeapon == 0) //Paper
		{

			if (cpuWeapon == 1) //Rock
			{
				strDisplay += "covers";
			}
			else //Scissors
			{
				strDisplay += "is cut by";
			}

		}
		else if (playerWeapon == 1) //Rock
		{

			if (cpuWeapon == 0) //paper
			{
				strDisplay += "is covered by";
			}
			else //Scissors
			{
				strDisplay += "breaks";
			}

		}
		else //Scissors
		{

			if (cpuWeapon == 0) //Paper
			{
				strDisplay += "cuts";
			}
			else //Rock
			{
				strDisplay += "is broken by";
			}

		}

		updateResults(playerWeapon);

		strDisplay += " " + strWeapons[cpuWeapon];
		return strDisplay;


	}

	private int getRandomNumber ()
	{
		return (1 + (int) (Math.random() * 3)) - 1;
	}

	private void updateResults (int playerWeapon)
	{
		int[] paperGrid = {0,1,-1};
		int[] stoneGrid = {-1,0,1};
		int[] scissorGrid = {1,-1,0};
		int[][] prsGrid = {paperGrid, stoneGrid, scissorGrid};
		int result = prsGrid[playerWeapon][cpuWeapon];

		if (result == 0)
		{
			draws++;
		}
		else if (result == -1)
		{
			loses++;
		}
		else
		{
			wins++;
		}

	}

	public void reset ()
	{
		wins = loses = draws = cpuWeapon = 0;
	}

	public String[] getWeapon ()
	{
		return strWeapons;
	}

	public String getWeapon (int index)
	{
		return strWeapons[index];
	}

	public String getStrCPUWeapon ()
	{
		return strWeapons[cpuWeapon];
	}

	public int getCPUWeapon ()
	{
		return cpuWeapon;
	}

	public int getWins ()
	{
		return wins;
	}

	public int getLoses ()
	{
		return loses;
	}

	public int getDraws ()
	{
		return draws;
	}
}

[code] 
Can some help me. almost there
 *
 * @author akr065
 */
public class rps extends JPanel implements ActionListener {

    private static final int FRAME_WIDTH = 1300;
    private static final int FRAME_HEIGHT = 800;
    private static final int AREA_ROWS = 10;
    private static final int AREA_COLUMNS = 30;
    private static final Color clrBackground = new Color(255,255,255);
    private static final Color clrForeground = new Color(0,0,0);
    private JTextArea resultArea;



    private JComboBox cboxWeapon;
    private JTextArea txtCPUWeapon, txtWins,txtLoses,txtDraws;
    private JLabel lblPlayerWeapon, lblCPUWeapon,lblWins,lblLoses,lblDraws,
    lblStatus;
    private JButton cmdPlay, cmdReset, Rock,Paper,Scissor;
    private JPanel panelCounters,panelPlayerWeapon,
    panelCPUWeapon, panelStatusArea,panelGo, panelWins,panelLoses,panelDraws,
    panelRoot, panelPlayerArea, panelCPUArea;

    private rpsEngine engine = new rpsEngine();


    public static void main(String[] args)
	{

	JFrame frame = new JFrame("Paper Rock Scissors ");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        JComponent paneMain = new rps();
        paneMain.setOpaque(true);
	paneMain.setPreferredSize(new Dimension(420,350));
        frame.setContentPane(paneMain);
        frame.pack();
        frame.setVisible(true);

    }
public rps(){



    Rock = new JButton("Rock");
    Paper = new JButton("Paper");
    Scissor = new JButton("Scissor");
    setSize(FRAME_WIDTH, FRAME_HEIGHT);
   


        


    resultArea = new JTextArea(AREA_ROWS, AREA_COLUMNS);

    cboxWeapon = new JComboBox(engine.getWeapon());
    

    txtWins = new JTextArea();
		txtLoses = new JTextArea();
		txtDraws = new JTextArea();

		txtCPUWeapon.setEditable(false);
		txtWins.setEditable(false);
		txtLoses.setEditable(false);
		txtDraws.setEditable(false);


lblPlayerWeapon = new JLabel("Choose Your Weapon:", JLabel.CENTER);
		lblCPUWeapon = new JLabel("PC's Weapon:", JLabel.CENTER);
		lblWins = new JLabel("Amount of Wins:", JLabel.RIGHT);
		lblLoses = new JLabel("Amount of Loses:", JLabel.RIGHT);
		lblDraws = new JLabel("Amount of Draws:", JLabel.RIGHT);
		lblStatus = new JLabel("", JLabel.CENTER);

		cmdPlay = new JButton("Go!");
		cmdReset = new JButton("Restart");
                Rock = new JButton("Rock");
                Paper = new JButton("Paper");
                Scissor= new JButton("Scissor");

		cmdPlay.addActionListener(this);
		cmdReset.addActionListener(this);
                Rock.addActionListener(this);
                Paper.addActionListener(this);
                Scissor.addActionListener(this);
setLayout(new BorderLayout());
		panelRoot = new JPanel(new BorderLayout());
		panelPlayerArea = new JPanel(new BorderLayout());
		panelPlayerWeapon= new JPanel(new BorderLayout());
		panelCPUArea = new JPanel(new BorderLayout());
		panelCPUWeapon = new JPanel(new BorderLayout());
		panelStatusArea = new JPanel(new BorderLayout());
		panelGo = new JPanel();
		panelCounters = new JPanel(new GridLayout(3,1,2,2));
		panelWins = new JPanel();
		panelLoses = new JPanel();
		panelDraws = new JPanel();

		add(panelRoot, BorderLayout.CENTER);
		panelRoot.add(panelPlayerArea, BorderLayout.WEST);
		panelPlayerArea.add(panelPlayerWeapon, BorderLayout.NORTH);
		panelPlayerWeapon.add(lblPlayerWeapon, BorderLayout.NORTH);
		panelPlayerWeapon.add(cboxWeapon, BorderLayout.SOUTH);
                panelRoot.add(panelCPUArea, BorderLayout.EAST);
		panelCPUArea.add(panelCPUWeapon, BorderLayout.NORTH);
		panelCPUWeapon.add(lblCPUWeapon, BorderLayout.NORTH);
		panelCPUWeapon.add(txtCPUWeapon, BorderLayout.SOUTH);
		panelRoot.add(panelStatusArea, BorderLayout.SOUTH);
		panelStatusArea.add(panelGo, BorderLayout.NORTH);
		panelGo.add(cmdPlay);
		panelGo.add(cmdReset);
                panelGo.add(Rock);
                panelGo.add(Paper);
                panelGo.add(Scissor);
		panelGo.add(lblStatus);
		panelStatusArea.add(panelCounters, BorderLayout.SOUTH);
		panelCounters.add(panelWins);
		panelWins.add(lblWins);
		panelWins.add(txtWins);
		panelCounters.add(panelLoses);
		panelLoses.add(lblLoses);
		panelLoses.add(txtLoses);
		panelCounters.add(panelDraws);
		panelDraws.add(lblDraws);
		panelDraws.add(txtDraws);

		panelRoot.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

		setBackground(clrBackground);
		panelRoot.setBackground(clrBackground);
		panelPlayerArea.setBackground(clrBackground);
		panelPlayerWeapon.setBackground(clrBackground);
		panelCPUArea.setBackground(clrBackground);
		panelCPUWeapon.setBackground(clrBackground);
		panelStatusArea.setBackground(clrBackground);
		panelGo.setBackground(clrBackground);
		panelCounters.setBackground(clrBackground);
		panelWins.setBackground(clrBackground);
		panelLoses.setBackground(clrBackground);
		panelDraws.setBackground(clrBackground);

		lblPlayerWeapon.setForeground(clrForeground);
		lblCPUWeapon.setForeground(clrForeground);
		lblWins.setForeground(clrForeground);
		lblLoses.setForeground(clrForeground);
		lblDraws.setForeground(clrForeground);
		txtWins.setForeground(clrForeground);
		txtLoses.setForeground(clrForeground);
		txtDraws.setForeground(clrForeground);
		txtCPUWeapon.setForeground(clrForeground);



}
public void reset ()
	{

		cboxWeapon.setSelectedIndex(0);
		lblStatus.setText("");

		engine.reset();

	}

	public void actionPerformed (ActionEvent e)
	{

		if (e.getSource() == cmdReset)
		{
			reset();
		}
		else
		{
			lblStatus.setText(engine.play(cboxWeapon.getSelectedIndex()));
		}

		txtCPUWeapon.setText(engine.getStrCPUWeapon());
		txtWins.setText(Integer.toString(engine.getWins()));
		txtLoses.setText(Integer.toString(engine.getLoses()));
		txtDraws.setText(Integer.toString(engine.getDraws()));


}




}



Edited by macosxnerd101: PLEASE, :code:!!!
Was This Post Helpful? 0
  • +
  • -

#17 ADK2011  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 20-January 11

Re: Can't get game to work

Posted 17 February 2011 - 01:17 PM

Nevermind i figured it out i forgot to initialize txtCPUWeapon in my text area
Was This Post Helpful? 0
  • +
  • -

#18 ADK2011  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 20-January 11

Re: Can't get game to work

Posted 17 February 2011 - 05:42 PM

I have a question on my code, i can't seemed to get my action JButtons to work accordingly. For example, the user pushes the rock button, the code is saying its the paper button on all three buttons. where am i going wrong? help would be great.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*; 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author akr065
 */
public class rps extends JPanel implements ActionListener {

    private static final int FRAME_WIDTH = 1300;
    private static final int FRAME_HEIGHT = 800;
    private static final int AREA_ROWS = 10;
    private static final int AREA_COLUMNS = 30;
    private static final Color clrBackground = new Color(255,255,255);
    private static final Color clrForeground = new Color(0,0,0);
    private JTextArea resultArea;



    private JComboBox cboxWeapon;
    private JTextArea txtCPUWeapon, txtWins,txtLoses,txtDraws;
    private JLabel lblPlayerWeapon, lblCPUWeapon,lblWins,lblLoses,lblDraws,
    lblStatus;
    private JButton cmdPlay, cmdReset, cmdRock,cmdPaper,cmdScissor;
    private JPanel panelCounters,panelPlayerWeapon,buttonPanel,
    panelCPUWeapon, panelStatusArea,panelGo, panelWins,panelLoses,panelDraws,
    panelRoot, panelPlayerArea, panelCPUArea;

    private rpsEngine engine = new rpsEngine();


    public static void main(String[] args)
	{

	JFrame frame = new JFrame("Paper Rock Scissors ");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        JComponent paneMain = new rps();
        paneMain.setOpaque(true);
	paneMain.setPreferredSize(new Dimension(420,350));
        frame.setContentPane(paneMain);
        frame.pack();
        frame.setVisible(true);

    }
    // Constructor sets up the GUI
public rps(){


    setSize(FRAME_WIDTH, FRAME_HEIGHT);
   


        
//Sets up Rock Paper Scissor

    resultArea = new JTextArea(AREA_ROWS, AREA_COLUMNS);

    cboxWeapon = new JComboBox(engine.getWeapon());
    
            //Sets the text area
    txtWins = new JTextArea();
		txtLoses = new JTextArea();
		txtDraws = new JTextArea();
                txtCPUWeapon= new JTextArea();
                JScrollPane scrollPane = new JScrollPane(resultArea);

		txtCPUWeapon.setEditable(false);
		txtWins.setEditable(false);
		txtLoses.setEditable(false);
		txtDraws.setEditable(false);

                //sets up  JLabels
lblPlayerWeapon = new JLabel("Choose Your Weapon:", JLabel.CENTER);
		lblCPUWeapon = new JLabel("PC's Weapon:", JLabel.CENTER);
		lblWins = new JLabel("Amount of Wins:", JLabel.RIGHT);
		lblLoses = new JLabel("Amount of Loses:", JLabel.RIGHT);
		lblDraws = new JLabel("Amount of Draws:", JLabel.RIGHT);
		lblStatus = new JLabel("", JLabel.CENTER);
                //Sets up buttons
		cmdPlay = new JButton("Go!");
		cmdReset = new JButton("Restart");
                cmdRock = new JButton("Rock");
                cmdPaper = new JButton("Paper");
                cmdScissor= new JButton("Scissor");
                //assigns buttons to listeners
		cmdPlay.addActionListener(this);
		cmdReset.addActionListener(this);
                cmdRock.addActionListener(this);
                cmdPaper.addActionListener(this);
                cmdScissor.addActionListener(this);

                buttonPanel = new JPanel();
                buttonPanel.setBorder(BorderFactory.createTitledBorder("Buttons"));
                buttonPanel.add(cmdPaper);
                buttonPanel.add(cmdRock);
                buttonPanel.add(cmdScissor);
                //Sets and organize the GUI lay-out

setLayout(new BorderLayout());
		panelRoot = new JPanel(new BorderLayout());
		panelPlayerArea = new JPanel(new BorderLayout());
		panelPlayerWeapon= new JPanel(new BorderLayout());
		panelCPUArea = new JPanel(new BorderLayout());
		panelCPUWeapon = new JPanel(new BorderLayout());
		panelStatusArea = new JPanel(new BorderLayout());
		panelGo = new JPanel();
		panelCounters = new JPanel(new GridLayout(3,1,2,2));
		panelWins = new JPanel();
		panelLoses = new JPanel();
		panelDraws = new JPanel();

		add(panelRoot, BorderLayout.CENTER);
		panelRoot.add(panelPlayerArea, BorderLayout.WEST);
		panelPlayerArea.add(panelPlayerWeapon, BorderLayout.NORTH);
		panelPlayerWeapon.add(lblPlayerWeapon, BorderLayout.NORTH);
		panelPlayerWeapon.add(buttonPanel, BorderLayout.SOUTH);
                panelRoot.add(panelCPUArea, BorderLayout.EAST);
		panelCPUArea.add(panelCPUWeapon, BorderLayout.NORTH);
		panelCPUWeapon.add(lblCPUWeapon, BorderLayout.NORTH);
		panelCPUWeapon.add(txtCPUWeapon, BorderLayout.SOUTH);
		panelRoot.add(panelStatusArea, BorderLayout.SOUTH);
		panelStatusArea.add(panelGo, BorderLayout.NORTH);
		panelGo.add(cmdPlay);
		panelGo.add(cmdReset);
		panelGo.add(lblStatus);
		panelStatusArea.add(panelCounters, BorderLayout.SOUTH);
		panelCounters.add(panelWins);
		panelWins.add(lblWins);
		panelWins.add(txtWins);
		panelCounters.add(panelLoses);
		panelLoses.add(lblLoses);
		panelLoses.add(txtLoses);
		panelCounters.add(panelDraws);
		panelDraws.add(lblDraws);
		panelDraws.add(txtDraws);

		panelRoot.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

		setBackground(clrBackground);
		panelRoot.setBackground(clrBackground);
		panelPlayerArea.setBackground(clrBackground);
		panelPlayerWeapon.setBackground(clrBackground);
		panelCPUArea.setBackground(clrBackground);
		panelCPUWeapon.setBackground(clrBackground);
		panelStatusArea.setBackground(clrBackground);
		panelGo.setBackground(clrBackground);
		panelCounters.setBackground(clrBackground);
		panelWins.setBackground(clrBackground);
		panelLoses.setBackground(clrBackground);
		panelDraws.setBackground(clrBackground);
                buttonPanel.setBackground(clrBackground);

		lblPlayerWeapon.setForeground(clrForeground);
		lblCPUWeapon.setForeground(clrForeground);
		lblWins.setForeground(clrForeground);
		lblLoses.setForeground(clrForeground);
		lblDraws.setForeground(clrForeground);
		txtWins.setForeground(clrForeground);
		txtLoses.setForeground(clrForeground);
		txtDraws.setForeground(clrForeground);
		txtCPUWeapon.setForeground(clrForeground);


//sets up the reset button
}
public void reset ()
	{

		cboxWeapon.setSelectedIndex(0);
		lblStatus.setText("");

		engine.reset();

	}

    


	public void actionPerformed (ActionEvent e)
	{

		if (e.getSource() == cmdReset)
		{
			reset();
		}
		else
		{
		lblStatus.setText(engine.play(cboxWeapon.getSelectedIndex()));

}
               

     



		txtCPUWeapon.setText(engine.getStrCPUWeapon());
		txtWins.setText(Integer.toString(engine.getWins()));
		txtLoses.setText(Integer.toString(engine.getLoses()));
		txtDraws.setText(Integer.toString(engine.getDraws()));


}

}


[code/]

[code]
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author akr065
 */
public class rpsEngine {
private final String[] strWeapons = {"Paper","Rock","Scissors"};

	private int wins = 0, loses = 0, draws = 0, cpuWeapon = 0;


	public void prsEngine ()
	{
	}

	public String play (int playerWeapon)
	{

		cpuWeapon = getRandomNumber();
		String strDisplay = strWeapons[playerWeapon] + " ";

		if (playerWeapon == cpuWeapon)
		{
			strDisplay += "draws";
                       
		}
		else if (playerWeapon == 0) //Paper
		{

			if (cpuWeapon == 1) //Rock
			{
				strDisplay += "covers";
			}
			else //Scissors
			{
				strDisplay += "is cut by";
			}

		}
		else if (playerWeapon == 1) //Rock
		{

			if (cpuWeapon == 0) //paper
			{
				strDisplay += "is covered by";
			}
			else //Scissors
			{
				strDisplay += "breaks";
			}

		}
		else //Scissors
		{

			if (cpuWeapon == 0) //Paper
			{
				strDisplay += "cuts";
			}
			else //Rock
			{
				strDisplay += "is broken by";
			}

		}

		updateResults(playerWeapon);

		strDisplay += " " + strWeapons[cpuWeapon];
		return strDisplay;


	}

	private int getRandomNumber ()
	{
		return (1 + (int) (Math.random() * 3)) - 1;
	}

	private void updateResults (int playerWeapon)
	{
		int[] paperGrid = {0,1,-1};
		int[] stoneGrid = {-1,0,1};
		int[] scissorGrid = {1,-1,0};
		int[][] prsGrid = {paperGrid, stoneGrid, scissorGrid};
		int result = prsGrid[playerWeapon][cpuWeapon];

		if (result == 0)
		{
			draws++;
		}
		else if (result == -1)
		{
			loses++;
		}
		else
		{
			wins++;
		}

	}

	public void reset ()
	{
		wins = loses = draws = cpuWeapon = 0;
	}
        
	public String[] getWeapon ()
	{
		return strWeapons;
	}

	public String getWeapon (int index)
	{
		return strWeapons[index];
	}

	public String getStrCPUWeapon ()
	{
		return strWeapons[cpuWeapon];
	}

	public int getCPUWeapon ()
	{
		return cpuWeapon;
	}

	public int getWins ()
	{
		return wins;
	}

	public int getLoses ()
	{
		return loses;
	}

	public int getDraws ()
	{
		return draws;
	}
}




Edited by macosxnerd101: This is getting ridiculous. This is the fourth time we've had to fix your code tags. There is a BIG RED BOX and WATERMARK when you go to post that illustrates how to use code tags. Please, :code:!!!
Was This Post Helpful? 0
  • +
  • -

#19 ADK2011  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 20-January 11

Re: Can't get game to work

Posted 21 February 2011 - 07:17 AM

Sorry i thought I used code tags. Can you someone guide me on my code though.
Was This Post Helpful? 0
  • +
  • -

#20 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10461
  • View blog
  • Posts: 38,753
  • Joined: 27-December 08

Re: Can't get game to work

Posted 21 February 2011 - 09:14 AM

Take a look at this portion of your actionPerformed() method. The else statement deals with anything that isn't the cmdReset JButton. You don't have any cases for which the rock, paper, or scissors buttons are hit.
 public void actionPerformed (ActionEvent e)  {  
   if (e.getSource() == cmdReset)  
   {  
       reset();  
   }  
   else {  
         lblStatus.setText(engine.play(cboxWeapon.getSelectedIndex()));  
   }  



Also, you really need to work on your coding indentations to make your code more readable. :)
Was This Post Helpful? 0
  • +
  • -

#21 ADK2011  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 20-January 11

Re: Can't get game to work

Posted 21 February 2011 - 09:21 AM

Oh ok i was looking at that.
Was This Post Helpful? 0
  • +
  • -

#22 ADK2011  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 20-January 11

Re: Can't get game to work

Posted 22 February 2011 - 10:04 AM

My action button are stiill not working, can someone look at my code to see what i am doing incorrectly?

public void actionPerformed (ActionEvent e)
	{

		if (e.getSource() == cmdReset)
		{
			reset();
		}
		else
		{
		lblStatus.setText(engine.play(cboxWeapon.getSelectedIndex()));

                if(e.getSource() == cmdRock)
                    lblStatus.setText(engine.play(playerWeapon));
                else if (e.getSource() == cmdPaper)
                    lblStatus.setText(engine.play(playerWeapon));
                else
                    lblStatus.setText(engine.play(playerWeapon));
                {


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2