4 Replies - 425 Views - Last Post: 23 August 2012 - 09:45 PM Rate Topic: -----

#1 ric1989  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 28-February 12

When i end my turn, the game does not show computer turn and score ?

Posted 22 August 2012 - 09:10 PM

I'm also trying to figure how to implement all my coding into a a JFrame along with JTextboxes
to look nicer as a major college project?

i tried it but had no luck adding it to a JFrame?

it does not show computer's turn event though i assign it as a generator?


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

import java.util.Random.*;
import java.util.Scanner;

//add library tools on the classic pig game
	 
	public class ClassicPigGame extends JFrame{  //name of the game
	     //** added
	    private static final int WIDTH = 500; //frame size
	    private static final int HEIGHT = 400; //frame size
	    private JButton RollDiceR, SkipTurnR; //instantiating my 2 buttons for user
	    
	    //updated **added
	    public ClassicPigGame(){  //my game constructor
	        
	        RollDiceR = new JButton("ROLL DICE");  //roll dice message button will be display on jframe window
	        RollDiceR.addActionListener (new RollDiceHandler()); //execute the action for the dice roll button
	        
	        SkipTurnR = new JButton("SKIP TURN"); //skip turn message button will be display on jframe window
	        SkipTurnR.addActionListener (new SkipTurnHandler()); //execute the action for the skip turn button
	       
	        
	       //get the content pant (CP) 
	       Container pane = getContentPane();
	        
	        //sets the layout of 4 rows and 2 column
	       pane.setLayout(new GridLayout (4, 2));
	       
	       //two buttons thrown into the Container window
	       pane.add(RollDiceR); 
	       //user will roll dice and will gain points
	       pane.add(SkipTurnR); 
	       //computer will take turn and try to catch up
	       //and get points
	       
	       //makes the window title
	       setTitle("Ricardo's classic pig game project");
	       
	       //windows size, visibility and can close down
	       setSize(WIDTH, HEIGHT);
	       setVisible(true);
	       setDefaultCloseOperation(EXIT_ON_CLOSE);
	       
	       }
	//
	       
	public static void main(String[] args)  
	    {  int UserPoint = 0, CompPoint = 0;
	       boolean rollAgain = false, rollAgainCP = false;
	           
	       Scanner scan = new Scanner(System.in);
	       System.out.println("Welcome to Ric Classic pig Simulation game!");
	   
	     do
	    {System.out.println("Player's turn roll the dice. \n Press 0 to roll Die");
	     String roll = scan.next();
	     if(roll.equalsIgnoreCase("0"))
	     UserPoint = changeTurn(UserPoint, CompPoint, true);
	     rollAgain = rollAgain(UserPoint, CompPoint, true);
	   while (rollAgain)
	   {
	       UserPoint = changeTurn(UserPoint, CompPoint, true);
	       rollAgain = rollAgain(UserPoint, CompPoint, true);
	   }
	   
	   while (rollAgain == false)
			 {System.out.println("Computers turn");
			 UserPoint = changeTurn(UserPoint, CompPoint, false);
			  rollAgainCP = rollAgain(UserPoint, CompPoint, false);}
			 while (rollAgainCP == true)
				 {UserPoint = changeTurn(UserPoint, CompPoint, false);
			 		rollAgain = rollAgain(UserPoint, CompPoint, false);}
			 
	

	       	}
	       	
		while (UserPoint<100 || CompPoint<100);
		
		boolean pwinner = isWinner(UserPoint, CompPoint);   //computer winning
		if (pwinner == true)
			System.out.println("Congratulation, you won the game !!!");
		else 
			System.out.println("Game Over, you lost the game, Try Again !!!");
          }
	
	   public static int rollDie()
		{
			Random generator = new Random();
			int die1 = generator.nextInt(6)+1;
			return die1;
		}
		
		public static boolean isWinner(int UserPoint, int CompPoint)
		{
			boolean Uwinner;        //human winning 
			if (UserPoint > CompPoint & UserPoint>=100)
				{Uwinner= true;}
			else if (UserPoint < CompPoint & CompPoint>=100)
			Uwinner = false;
			else
				Uwinner = false;
			return Uwinner;					
		}
		
	    public static int changeTurn(int UserPoint, int CompPoint, boolean humanType){
	        int UserTotal = 0, CompTotal = 0;
	        int points = 0, CompPoints = 0;
	        int type;
	        boolean rollAgain = false;
	        boolean endTurn = false;
	        if (humanType == true)
	        {type = UserPoint;
	        UserTotal = UserPoint;}
	        else
	        {type = CompPoint;
	        CompTotal = CompPoints;}
	       do
		{			
			int die1 = rollDie();
				int die2 = rollDie();
				System.out.println(die1+" "+die2);
			if (die1 ==1 && die2==1)
				{
				if (humanType == false)
				{System.out.println("Computer Total points = 0");
				endTurn = true;
				CompTotal = 0;}
				if (humanType == true)
					{endTurn = true;
				UserTotal = 0;
				System.out.println("you had two 1's so it is the end of your turn.");
				System.out.println("Your Total points = 0");}
				
			else if (die1 ==1 || die2==1)
		{
				if (humanType == false)
				{CompPoints = (die1+die2);
				endTurn = true;
				CompTotal += CompPoints;  //computer final points add as total score at end of game
				System.out.println("Computer ends the turn.");
				System.out.println("Computer Total points = "+ CompTotal);}
				if (humanType == true)
				{	UserTotal+= points;  //player final total points at end of game
					System.out.println("you had a 1 so it is the end of your turn.");
					System.out.println("Your ends the turn.");  //statements
					System.out.println("Your Total points = "+UserTotal);}
				}
		}
			else
			{

				if (humanType == false)
				{CompPoints = (die1+die2);
				System.out.println("Points for computer's turn"+CompPoints);
				CompTotal += CompPoints;   //computer goes points add as total score at end of game
				System.out.println("Total Points so far for the computer = " + CompTotal);
				
				}
				if (humanType == true)
				{	points = (die1+die2); //diplay points on each turn, roll and total points for user so far
					System.out.println("Points for your turn"+points);
					System.out.println("Another roll");
					UserTotal += points;
					System.out.println("Total Points so far you = " + UserTotal);
				}
				}
			if (humanType == true && (!(die1 ==1 || die2==1)||(die1 ==1 && die2==1)))
			{
				type = UserPoint;   //computer score
				rollAgain = rollAgain(UserPoint, CompPoint, true); //user and computer score change when rolling die
			}
		}
		
		while(UserTotal!=0 || endTurn == false || rollAgain == true );
	
			
				UserTotal = (type+points);
				points = 0;
				System.out.println("total points "+ UserTotal);  //points and player final score adds up
		
				return UserTotal;  //final score for player
		}
		
	           //updated project    *added
	           class RollDiceHandler implements ActionListener{
	               public void actionPerformed(ActionEvent e){
	                   System.exit(0);
	             }
	           }
	           
	           class SkipTurnHandler implements ActionListener{
	               public void actionPerformed(ActionEvent e){
	                  System.exit(0);
	               }           
	           }
	           
	    public static boolean rollAgain(int UserPoint, int CompPoint, boolean humanType)
		{
			Scanner scan = new Scanner(System.in);
			boolean rollAgain = false;
			if (humanType)
			{
				System.out.println("press 0 to roll again / press c to end turn");
				
				String roll = scan.next();
				if (roll.equalsIgnoreCase("0"))
						rollAgain = true;
				if (roll.equalsIgnoreCase("c"))
						rollAgain = false;
					
			}
			else
			{
				if (CompPoint>=20)
					rollAgain = false;
				else
					rollAgain = true;
			}
			return rollAgain;
		}
	}




Is This A Good Question/Topic? 0
  • +

Replies To: When i end my turn, the game does not show computer turn and score ?

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: When i end my turn, the game does not show computer turn and score ?

Posted 22 August 2012 - 10:39 PM

So are you using JFrame or console?
Also indent your code, it makes it easy to follow.

Quote

it does not show computer's turn event though i assign it as a generator?

At which line did you assign it?
Was This Post Helpful? 2
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: When i end my turn, the game does not show computer turn and score ?

Posted 23 August 2012 - 02:25 PM

The only static method should be main() all the other methods shouldn't be

Your main() method is reaklly to long. Most of it's code should be in a method of the class. The main() method should only be used to instantiate a ClassicPigGame object. Where did you call your ClassiPgGame constructor ?
Was This Post Helpful? 1
  • +
  • -

#4 ric1989  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 28-February 12

Re: When i end my turn, the game does not show computer turn and score ?

Posted 23 August 2012 - 07:05 PM

View Postsmohd, on 23 August 2012 - 12:39 AM, said:

So are you using JFrame or console?
Also indent your code, it makes it easy to follow.

Quote

it does not show computer's turn event though i assign it as a generator?

At which line did you assign it?


i would like to use JFrame instead of console,
ibasically want it to be with Jtexboxes and JFrame that way
you can see computer score and human score and can visually
see everything i code in a window.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10446
  • View blog
  • Posts: 38,684
  • Joined: 27-December 08

Re: When i end my turn, the game does not show computer turn and score ?

Posted 23 August 2012 - 09:45 PM

Unlike with console programming, GUI Programming relies on events instead of loops. You will want to deal with listeners to update variables rather than focusing on a "game looP" to choose the next turn. Check out my tutorial on Event-Driven Programming for a good starting point.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1