All pastes #960324 Raw Edit

Miscellany

public text v1 · immutable
#960324 ·published 2008-03-28 00:40 UTC
rendered paste body
import java.util.Random;
import java.awt.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.text.DecimalFormat;

/**
 * fen
 * 
 * 
 */
public class pig extends JApplet
{
   int WIDTH = 700, HEIGHT = 1000;
   int die1, die2, total, roundTotal, yourScore, compScore, compDecide, counting;
   JLabel d1, d2, totalr, urscore, cscore, directions, result, intheway, in2;
   JButton Roll, Pass, CompRoll;
   PairoDice rolled = new PairoDice(); 
   Random generator = new Random();
   
   //add the initial items to the gui
   public void addItems(Container cp)
   {	
	   Color me = new Color(107, 142, 35);
       cp.setBackground(me);
       cp.setLayout(new FlowLayout());
       cp.add(directions);
       cp.setLayout(new GridLayout(0,2));
       cp.add(intheway);
       cp.add(Roll);
       cp.add(Pass);
       cp.add(d1);
       cp.add(d2);
       cp.add(result);
       cp.add(totalr);
       cp.add(urscore);
       cp.add(cscore);
   }
   
   //setup computer gui
   public void switchToComputer(Container cp)
   {
       cp.remove(directions);
       cp.remove(intheway);
       cp.remove(Roll);
       cp.remove(Pass);
       cp.remove(d1);
       cp.remove(d2);
       cp.remove(result);
       cp.remove(totalr);
       cp.remove(urscore);
       cp.remove(cscore);
       cp.add(directions);
       cp.add(intheway);
       cp.add(in2);
       cp.add(CompRoll);
       cp.add(d1);
       cp.add(d2);
       cp.add(result);
       cp.add(totalr);
       cp.add(urscore);
       cp.add(cscore);
       repaint();
   }
   
   //setup player gui
   public void switchToPlayer(Container cp)
   {          
       cp.remove(directions);
       cp.remove(intheway);
       cp.remove(in2);
       cp.remove(CompRoll);
       cp.remove(d1);
       cp.remove(d2);
       cp.remove(result);
       cp.remove(totalr);
       cp.remove(urscore);
       cp.remove(cscore);
       cp.add(directions);
       cp.add(intheway);
       cp.add(Roll);
       cp.add(Pass);
       cp.add(d1);
       cp.add(d2);
       cp.add(result);
       cp.add(totalr);
       cp.add(urscore);
       cp.add(cscore);     
       repaint();
   }
   
   //initialize the gui
    public void init()
    {
    	//Setup the window messages to be displayed
        directions 		= new JLabel ("a game called pig");
        intheway		= new JLabel(" ");
        in2			= new JLabel(" ");
        d1			= new JLabel ("----");
        d2			= new JLabel ("----");
        totalr			= new JLabel ("----");
        urscore			= new JLabel ("----");
        cscore			= new JLabel("----");
        result			= new JLabel("--------");
        Roll 			= new JButton("Roll");
        Pass 			= new JButton("Pass");
        CompRoll		= new JButton("Computer rolls");
        
        //link the classes with the listeners
        Roll.addActionListener(new RollListener());
        Pass.addActionListener(new PassListener());
        CompRoll.addActionListener(new CompRollListener());
        
        //initialize colors and variables
        roundTotal=0;
        yourScore=0;
        compScore=0;
        
        //add the display objects to the current frame
        addItems(getContentPane());

        setSize(WIDTH, HEIGHT);
    }
    

//**************************************************/
//**************************************************/
//class that monitors for a mouse press on the roll button
private class RollListener implements ActionListener
{
	//the action that was performed in the button
   public void actionPerformed(ActionEvent event)
    {
        die1=0;
        die2=0;
        PairoDice rolled = new PairoDice();
        die1 = rolled.getDie1();
        die2 = rolled.getDie2();
        total= die1 + die2;
        roundTotal = total + roundTotal;
        d1.setText("die1 " + die1);
        d2.setText("die2 " + die2);
        totalr.setText("your round total " + roundTotal);
        urscore.setText("your total score now " + yourScore);
        cscore.setText("computer's score " + compScore);
        repaint();
        
		if(die1 == 1 && die2 != 1 || die2 == 1 && die1 != 1)
		{
			roundTotal=0;
			yourScore= yourScore + roundTotal;
			totalr.setText("your round total " + roundTotal );
			directions.setText("you lose your round score you cannot roll again");
			cscore.setText("computer's score " + compScore);
			urscore.setText("your total score now " + yourScore);
			cscore.setText("computer's score " + compScore);
			switchToComputer(getContentPane());
		}
		if(die1==1 && die2==1)
		{
			yourScore=0;
			roundTotal=0;
			totalr.setText("your round total " + roundTotal);
			directions.setText("you lose your round score you cannot roll again");
			cscore.setText("computer's score " + compScore);
			urscore.setText("your total score now " + yourScore);
			cscore.setText("computer's score " + compScore);
			switchToComputer(getContentPane());
		}
	}   
}



    
//**************************************************/
//**************************************************/
//class that monitors for a mouse press on the pass button   
class PassListener implements ActionListener
{
	//the action that was performed in the button
	public void actionPerformed (ActionEvent event)
	{
		yourScore=yourScore+roundTotal;
		roundTotal=0;	
		totalr.setText("your round total "+Integer.toString (roundTotal));
		directions.setText("you lose your round score you cannot roll again");
		cscore.setText("computer's score " + compScore);
		urscore.setText("your total score now " + yourScore);
		cscore.setText("computer's score " + compScore);    
		switchToComputer(getContentPane());
	}
}

//**************************************************/
//**************************************************/
//class that monitors for a mouse press on the Computer roll button   
class CompRollListener implements ActionListener
{
	//the action that was performed in the button
       public void actionPerformed (ActionEvent event)
       {
             roundTotal	= 0;
             compDecide	= 1;
             counting	= 12;
             while (roundTotal<23 && compDecide!=0)
             {
                PairoDice rolled=new PairoDice();
                die1=rolled.getDie1();
                die2=rolled.getDie2();
                total=die1+die2;
                roundTotal=total+roundTotal;
                Random generator = new Random();
                compDecide=generator.nextInt(counting);
                counting--;
                d1.setText ("die1 " + die1);
				d2.setText ("die2 " + die2);
				totalr.setText("computer's round total " + roundTotal);
				urscore.setText("your total score now " + yourScore);
				cscore.setText("computer's score " + compScore);
				repaint();
		        
				if (die1==1 || die2 ==1)
				{ 
				    compDecide	= 0;
				    roundTotal		= 0;
			        if (die1==1 && die2 ==1)
			        {
			            compScore=0;
			        }
					totalr.setText("computer's round total "+ roundTotal);
					urscore.setText("your total score now " + yourScore);
					cscore.setText("computer's score " + compScore);
					urscore.setText("your total score now " + yourScore);
					cscore.setText("computer's score " + compScore);
					repaint();
				}
				             
             }
	        compScore=roundTotal+compScore;
	        roundTotal=0;
	        compScore=roundTotal+compScore;
	        switchToPlayer(getContentPane());   
       }
}

}