Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-01-2008, 09:41 AM
King8654's Avatar
Member
 
Join Date: Apr 2008
Posts: 9
King8654 is on a distinguished road
Roll 2-Dice "Pig" Game Help
Alright, been working on this game for quite a while, and couple things just cant get straight.

MyApp.java
Quote:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class MyApp extends JFrame
{
private JLabel _aLabel, _comp, _comproll, _comptotal;
private JLabel _user, _userroll, _usertotal;
private JPanel _buttonPanel, _labelPanel1, _labelPanel2, _diePanel;
private JButton roll;
private JButton pass;
private Die _leftDie; // component for one die
private Die _rightDie;
int roll;

public MyApp(String title)
{ super(title);
this.setSize(600,450);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
_aLabel = new JLabel("Pig 2-Dice Java Game");
this.add(_aLabel, java.awt.BorderLayout.NORTH);
_aLabel.setHorizontalAlignment(JLabel.CENTER);

_leftDie = new Die();
_rightDie = new Die();

_diePanel = new JPanel();
_diePanel.setLayout(new GridLayout(0,1));
_diePanel.add(_leftDie);
_diePanel.add(_rightDie);
this.add(_diePanel, java.awt.BorderLayout.CENTER);

_comp = new JLabel("Computer");
_comproll = new JLabel("Round Score: " + roll);
_comptotal = new JLabel("Total Score: ");

_labelPanel1 = new JPanel();
_labelPanel1.setLayout(new GridLayout(0,1));
_labelPanel1.add(_comp);
_labelPanel1.add(_comproll);
_labelPanel1.add(_comptotal);
// put JPanel into ContentPane of Frame
this.add(_labelPanel1, java.awt.BorderLayout.WEST);

_user = new JLabel("User");
_userroll = new JLabel("Round Score: " + roll);
_usertotal = new JLabel("Total Score: " );

// instantiate JPanel and put buttons on JPanel
_labelPanel2 = new JPanel();
_labelPanel2.setLayout(new GridLayout(0,1));
_labelPanel2.add(_user);
_labelPanel2.add(_userroll);
_labelPanel2.add(_usertotal);
// put JPanel into ContentPane of Frame
this.add(_labelPanel2, java.awt.BorderLayout.EAST);


JButton roll = new JButton("Roll");
roll.addActionListener(new RollListener());

JButton pass = new JButton("Pass");

_buttonPanel = new JPanel();
_buttonPanel.setLayout(new GridLayout(1,0));
_buttonPanel.add(roll);
_buttonPanel.add(pass);

this.add(_buttonPanel, java.awt.BorderLayout.SOUTH);


this.setVisible(true);
}

private class RollListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
_leftDie.roll();
_rightDie.roll();


}
}

public static void main(String [] args) {
MyApp myApp = new MyApp("A Game Of Fookin Pig");
}
}
Die.java

Quote:
import java.awt.*;
import javax.swing.*;


public class Die extends JComponent {

private static final int SPOT_DIAM = 12;
private static final int a = 120;
private int _faceValue, die1, die2, roll, score;


public Die() {

setPreferredSize(new Dimension(120,120));

roll();
}


public int roll() {
die = (int)(6*Math.random() + 1);
roll = die1 + die2;
repaint();
return roll;

}


public void paintComponent(Graphics g) {

Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
RenderingHints.VALUE_ANTIALIAS_ON);


g2.setColor(Color.RED);
g2.fillRect(0, 0, a, a);
g2.setColor(Color.BLACK);
g2.drawRect(0, 0, a-1, a-1);

switch (die1) {
case 1:
drawSpot(g2, a/2, a/2);
break;
case 3:
drawSpot(g2, a/2, a/2);
case 2:
drawSpot(g2, a/4, a/4);
drawSpot(g2, 3*a/4, 3*a/4);
break;
case 5:
drawSpot(g2, a/2, a/2);
case 4:
drawSpot(g2, a/4, a/4);
drawSpot(g2, 3*a/4, 3*a/4);
drawSpot(g2, 3*a/4, a/4);
drawSpot(g2, a/4, 3*a/4);
break;
case 6:
drawSpot(g2, a/4, a/4);
drawSpot(g2, 3*a/4, 3*a/4);
drawSpot(g2, 3*a/4, a/4);
drawSpot(g2, a/4, 3*a/4);
drawSpot(g2, a/4, a/2);
drawSpot(g2, 3*a/4, a/2);
break;
}
}



private void drawSpot(Graphics2D g2, int x, int y) {
g2.fillOval(x-SPOT_DIAM/2, y-SPOT_DIAM/2, SPOT_DIAM, SPOT_DIAM);
}
}
So far, the program shows the layout and the dice generated from pressing the Roll button. I still have a long way to go with the game itself, but kinda hit a wall. Im pretty sure its quite simple and im just missing something.

1. Cant seem to figure out how to place the dice next to each other in the center of their panel.

2. Need to add the sum of each roll total, and place the result in the score label, and also keep a running tab of the roll values in the score label.

3. Im pretty sure this program would benefit from more classes instead of squeezing into two, any advice would be awesome.

After this is completed, still have to go back and forth between the user and computer rolling the dice, and the first to 100 wins, BUT if you roll a 1, you lose that rounds score, and if you roll two 1's, you lose your entire score.

Just looking for help with the three objectives i posted little above, any help would be much appreciated. Thanks guys

Last edited by King8654 : 04-07-2008 at 06:25 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-01-2008, 10:56 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class DiceApp extends JFrame { private JLabel _aLabel, _comp, _comproll, _comptotal; private JLabel _user, _userroll, _usertotal; private JPanel _buttonPanel, _labelPanel1, _labelPanel2, _diePanel; private JButton roll; private JButton pass; private Die _leftDie; // component for one die private Die _rightDie; // You have a JButton and an int both named "roll". // int roll; int userScore = 0; int compScore = 0; public DiceApp(String title) { super(title); this.setSize(600,450); this.setDefaultCloseOperation(EXIT_ON_CLOSE ); _aLabel = new JLabel("Pig 2-Dice Java Game"); this.add(_aLabel, BorderLayout.NORTH); _aLabel.setHorizontalAlignment(JLabel.CENTER); _leftDie = new Die(); _rightDie = new Die(); _diePanel = new JPanel(); // Add a border to see what the layout manager is // doing with your components: _diePanel.setBorder(BorderFactory.createEtchedBorder()); // Try a different layout manager for more flexibility: _diePanel.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); // spacing around components: gbc.insets = new Insets(5,5,5,5); // Leave the weight constraints at default zero values // to keep components clumped together. // Or, you could set weighty to a non-zero to allow // components to spread out in vertical dimension: //gbc.weighty = 1.0; // single column: gbc.gridwidth = GridBagConstraints.REMAINDER; _diePanel.add(_leftDie, gbc); _diePanel.add(_rightDie, gbc); this.add(_diePanel, BorderLayout.CENTER); _comp = new JLabel("Computer"); _comproll = new JLabel("Round Score: " + roll); _comptotal = new JLabel("Total Score: "); _labelPanel1 = new JPanel(); // Give labels some breathing room: Dimension d = _labelPanel1.getPreferredSize(); d.width = 200; _labelPanel1.setPreferredSize(d); _labelPanel1.setLayout(new GridLayout(0,1)); _labelPanel1.add(_comp); _labelPanel1.add(_comproll); _labelPanel1.add(_comptotal); // put JPanel into ContentPane of Frame this.add(_labelPanel1, BorderLayout.WEST); _user = new JLabel("User"); _userroll = new JLabel("Round Score: " + roll); _usertotal = new JLabel("Total Score: " ); // instantiate JPanel and put buttons on JPanel _labelPanel2 = new JPanel(); // Need room for labels. _labelPanel2.setPreferredSize(d); _labelPanel2.setLayout(new GridLayout(0,1)); _labelPanel2.add(_user); _labelPanel2.add(_userroll); _labelPanel2.add(_usertotal); // put JPanel into ContentPane of Frame this.add(_labelPanel2, BorderLayout.EAST); JButton roll = new JButton("Roll"); roll.addActionListener(new RollListener()); JButton pass = new JButton("Pass"); _buttonPanel = new JPanel(); _buttonPanel.setLayout(new GridLayout(1,0)); _buttonPanel.add(roll); _buttonPanel.add(pass); this.add(_buttonPanel, BorderLayout.SOUTH); this.setVisible(true); } private class RollListener implements ActionListener { int turn = 0; // computer: 0 user: 1 public void actionPerformed(ActionEvent e) { int left = _leftDie.roll(); int right = _rightDie.roll(); postLabels(left, right, turn); turn = (turn+1) % 2; // toggle turn } } private void postLabels(int left, int right, int turn) { // Figure out what the numbers mean/how scored. // For now: int round = left + right; switch(turn) { case 0: // computer _comproll.setText("Round Score: " + round); compScore += round; _comptotal.setText("Total Score: " + compScore); break; case 1: // user _userroll.setText("Round Score: " + round); userScore += round; _usertotal.setText("Total Score: " + userScore); } if(compScore > 100 || userScore > 100) { System.out.println("Done"); // reset variables, labels and "turn" } } public static void main(String [] args) { new DiceApp("A Game Of Fookin Pig"); } } class Die extends JComponent { private static final int SPOT_DIAM = 12; private static final int a = 120; private int _faceValue, die1, die2, roll, score; public Die() { setPreferredSize(new Dimension(120,120)); roll(); } public int roll() { // die = (int)(6*Math.random() + 1); die1 = (int)(6*Math.random() + 1); die2 = (int)(6*Math.random() + 1); roll = die1 + die2; repaint(); return roll; } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(Color.RED); g2.fillRect(0, 0, a, a); g2.setColor(Color.BLACK); g2.drawRect(0, 0, a-1, a-1); switch (die1) { case 1: drawSpot(g2, a/2, a/2); break; case 3: drawSpot(g2, a/2, a/2); case 2: drawSpot(g2, a/4, a/4); drawSpot(g2, 3*a/4, 3*a/4); break; case 5: drawSpot(g2, a/2, a/2); case 4: drawSpot(g2, a/4, a/4); drawSpot(g2, 3*a/4, 3*a/4); drawSpot(g2, 3*a/4, a/4); drawSpot(g2, a/4, 3*a/4); break; case 6: drawSpot(g2, a/4, a/4); drawSpot(g2, 3*a/4, 3*a/4); drawSpot(g2, 3*a/4, a/4); drawSpot(g2, a/4, 3*a/4); drawSpot(g2, a/4, a/2); drawSpot(g2, 3*a/4, a/2); break; } } private void drawSpot(Graphics2D g2, int x, int y) { g2.fillOval(x-SPOT_DIAM/2, y-SPOT_DIAM/2, SPOT_DIAM, SPOT_DIAM); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-01-2008, 11:44 AM
King8654's Avatar
Member
 
Join Date: Apr 2008
Posts: 9
King8654 is on a distinguished road
awesome
THANKS!!! made the changes, and now the program works great, switching between the computer and the user, keeping track of the score, and upon reaching 100, ending.

One more question for the guru who helped me, if at all possible...

If your playing as the user, you can choose to pass your turn, and click the pass button, and it moves to the computers turn.

Thanks again for the help, i really tried everything, but the gridbaglayout i was scared to even get into on top of the program and complicate everything, but it worked out great! thanks again!

Heres the new MyApp.java with the fixes and some other small bits I added
Quote:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Insets.*;

public class MyApp extends JFrame
{
private JLabel _aLabel, _comp, _comproll, _comptotal;
private JLabel _user, _userroll, _usertotal, _victory, _compWin;
private JPanel _buttonPanel, _labelPanel1, _labelPanel2, _diePanel;
//private JButton _roll;
//private JButton _pass;
//private JButton _quit;
private Die _leftDie; // component for one die
private Die _rightDie;
int userScore = 0;
int compScore = 0;


public MyApp(String title)
{ super(title);
this.setSize(600,450);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
_aLabel = new JLabel("Pig 2-Dice Java Game");
this.add(_aLabel, java.awt.BorderLayout.NORTH);
_aLabel.setHorizontalAlignment(JLabel.CENTER);

_leftDie = new Die();
_rightDie = new Die();

JButton quit = new JButton("Quit");
quit.addActionListener(new QuitListener());

_victory = new JLabel();
_victory.setVisible(false);

_diePanel = new JPanel();
_diePanel.setBorder(BorderFactory.createEtchedBord er());
_diePanel.setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5,5,5,5);
gbc.gridwidth = GridBagConstraints.REMAINDER;
_diePanel.add(_leftDie,gbc);
_diePanel.add(_rightDie,gbc);
_diePanel.add(quit,gbc);
_diePanel.add(_victory,gbc);
_victory.setHorizontalAlignment(JLabel.CENTER);
this.add(_diePanel, BorderLayout.CENTER);

_comp = new JLabel("Computer");
_comp.setFont(new Font("Impact", Font.BOLD, 32));
_comproll = new JLabel();
_comptotal = new JLabel();

_labelPanel1 = new JPanel();
_labelPanel1.setBorder(BorderFactory.createEtchedB order());
Dimension d = _labelPanel1.getPreferredSize();
d.width = 200;
_labelPanel1.setPreferredSize(d);
_labelPanel1.setLayout(new GridLayout(0,1));
_labelPanel1.add(_comp);
_comp.setHorizontalAlignment(JLabel.CENTER);
_comp.setBorder(BorderFactory.createEtchedBorder() );
_labelPanel1.add(_comproll);
_comproll.setHorizontalAlignment(JLabel.CENTER);
_labelPanel1.add(_comptotal);
_comptotal.setHorizontalAlignment(JLabel.CENTER);
this.add(_labelPanel1, BorderLayout.EAST);

_user = new JLabel("User");
_user.setFont(new Font("Impact", Font.BOLD, 32));
_userroll = new JLabel();
_usertotal = new JLabel();

_labelPanel2 = new JPanel();
_labelPanel2.setBorder(BorderFactory.createEtchedB order());
_labelPanel2.setPreferredSize(d);
_labelPanel2.setLayout(new GridLayout(0,1));
_labelPanel2.add(_user);
_user.setHorizontalAlignment(JLabel.CENTER);
_user.setBorder(BorderFactory.createEtchedBorder() );
_labelPanel2.add(_userroll);
_userroll.setHorizontalAlignment(JLabel.CENTER);
_labelPanel2.add(_usertotal);
_usertotal.setHorizontalAlignment(JLabel.CENTER);
this.add(_labelPanel2, BorderLayout.WEST);

JButton roll = new JButton("Roll");
roll.addActionListener(new RollListener());

JButton pass = new JButton("Pass");

_buttonPanel = new JPanel();
_buttonPanel.setLayout(new GridLayout(1,0));
_buttonPanel.add(roll);
_buttonPanel.add(pass);

this.add(_buttonPanel, BorderLayout.SOUTH);


this.setVisible(true);
}

private class RollListener implements ActionListener {
int turn = 0;
public void actionPerformed(ActionEvent e) {
int left = _leftDie.roll();
int right = _rightDie.roll();
postLabels(left, right, turn);
turn = (turn+1) % 2;


}
}

private class QuitListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);


}
}

private class PassListener implements ActionListener {
public void actionPerformed(ActionEvent e) {

}
}
private void postLabels(int left, int right, int turn) {

int round = left + right;
switch(turn) {
case 0: // user
_userroll.setText("Round Score: " + round);
userScore += round;
_usertotal.setText("Total Score: " + userScore);
break;
case 1: // computer
_comproll.setText("Round Score: " + round);
compScore += round;
_comptotal.setText("Total Score: " + compScore);
}
if(userScore > 100) {
_victory.setText(" User Wins " + userScore + " to " + compScore);
_victory.setVisible(true);

}
else if(compScore > 100) {
_victory.setText("Computer Wins " + compScore + " to " + userScore);
_victory.setVisible(true);

}
// reset variables, labels and "turn"
}



public static void main(String [] args) {
MyApp myApp = new MyApp("A Game Of Fookin Pig");
}
}

Last edited by King8654 : 04-01-2008 at 01:32 PM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-01-2008, 08:39 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Some more ideas:
Code:
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class DiceApp2 extends JFrame { private JLabel _aLabel, _comp, _comproll, _comptotal; private JLabel _user, _userroll, _usertotal, _victory, _compWin; private JPanel _buttonPanel, _labelPanel1, _labelPanel2, _diePanel; private Die _leftDie; // component for one die private Die _rightDie; int userScore = 0; int compScore = 0; TurnManager turnManager = new TurnManager(); public DiceApp2(String title) { super(title); this.setSize(600,450); this.setDefaultCloseOperation(EXIT_ON_CLOSE ); _aLabel = new JLabel("Pig 2-Dice Java Game"); this.add(_aLabel, java.awt.BorderLayout.NORTH); _aLabel.setHorizontalAlignment(JLabel.CENTER); _leftDie = new Die(); _rightDie = new Die(); JButton quit = new JButton("Quit"); quit.addActionListener(new QuitListener()); _victory = new JLabel(); _victory.setVisible(false); _diePanel = new JPanel(); // This was for diagnostic visibility; not needed. _diePanel.setBorder(BorderFactory.createEtchedBorder()); _diePanel.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(5,5,5,5); gbc.gridwidth = GridBagConstraints.REMAINDER; _diePanel.add(_leftDie,gbc); _diePanel.add(_rightDie,gbc); _diePanel.add(quit,gbc); _diePanel.add(_victory,gbc); _victory.setHorizontalAlignment(JLabel.CENTER); this.add(_diePanel, BorderLayout.CENTER); _comp = new JLabel("Computer"); _comp.setFont(new Font("Impact", Font.BOLD, 32)); _comproll = new JLabel(); _comptotal = new JLabel(); _labelPanel1 = new JPanel(); _labelPanel1.setBorder(BorderFactory.createEtchedBorder()); Dimension d = _labelPanel1.getPreferredSize(); d.width = 200; _labelPanel1.setPreferredSize(d); _labelPanel1.setLayout(new GridLayout(0,1)); _labelPanel1.add(_comp); _comp.setHorizontalAlignment(JLabel.CENTER); _comp.setBorder(BorderFactory.createEtchedBorder() ); _labelPanel1.add(_comproll); _comproll.setHorizontalAlignment(JLabel.CENTER); _labelPanel1.add(_comptotal); _comptotal.setHorizontalAlignment(JLabel.CENTER); this.add(_labelPanel1, BorderLayout.EAST); _user = new JLabel("User"); _user.setFont(new Font("Impact", Font.BOLD, 32)); _userroll = new JLabel(); _usertotal = new JLabel(); _labelPanel2 = new JPanel(); _labelPanel2.setBorder(BorderFactory.createEtchedBorder()); _labelPanel2.setPreferredSize(d); _labelPanel2.setLayout(new GridLayout(0,1)); _labelPanel2.add(_user); _user.setHorizontalAlignment(JLabel.CENTER); _user.setBorder(BorderFactory.createEtchedBorder() ); _labelPanel2.add(_userroll); _userroll.setHorizontalAlignment(JLabel.CENTER); _labelPanel2.add(_usertotal); _usertotal.setHorizontalAlignment(JLabel.CENTER); this.add(_labelPanel2, BorderLayout.WEST); JButton roll = new JButton("Roll"); roll.addActionListener(new RollListener()); JButton pass = new JButton("Pass"); pass.addActionListener(new PassListener()); _buttonPanel = new JPanel(); _buttonPanel.setLayout(new GridLayout(1,0)); _buttonPanel.add(roll); _buttonPanel.add(pass); this.add(_buttonPanel, BorderLayout.SOUTH); this.setVisible(true); } private class RollListener implements ActionListener { public void actionPerformed(ActionEvent e) { int left = _leftDie.roll(); int right = _rightDie.roll(); postLabels(left, right); turnManager.next(); } } private class QuitListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } private class PassListener implements ActionListener { public void actionPerformed(ActionEvent e) { turnManager.next(); } } private class TurnManager { final static int USER = 0; final static int COMPUTER = 1; int turn = USER; public void next() { turn = (turn+1) % 2; } public int getTurn() { return turn; }; public void reset() { turn = USER; }; } private void postLabels(int left, int right) { int round = left + right; int turn = turnManager.getTurn(); switch(turn) { case TurnManager.USER: _userroll.setText("Round Score: " + round); userScore += round; _usertotal.setText("Total Score: " + userScore); break; case TurnManager.COMPUTER: _comproll.setText("Round Score: " + round); compScore += round; _comptotal.setText("Total Score: " + compScore); } if(userScore > 100 || compScore > 100) gameOver(); } private void gameOver() { if(userScore > 100) { _victory.setText(" User Wins " + userScore + " to " + compScore); _victory.setVisible(true); } else if(compScore > 100) { _victory.setText("Computer Wins " + compScore + " to " + userScore); _victory.setVisible(true); } // reset variables, labels and "turn" } public static void main(String [] args) { new DiceApp2("A Game Of Fookin Pig"); } }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-01-2008, 09:06 PM
King8654's Avatar
Member
 
Join Date: Apr 2008
Posts: 9
King8654 is on a distinguished road
About turnmanager
Thanks once again for the help wired, but I seem to still be having a problem, maybe it was my mis explaining the objective.

After adding the revised code, and running, clicking on roll multiple times keeps switching between the user and computer, instead of staying with the same player and adding the sum of the rolls until the pass button is pressed, which changes the turn over to the computer.

The pass button now at least functions, but instead of switching to the other players turn, it actually passes over their turn, like skipping them in line, so with successive clicks of the pass button and roll button the turn can stay with one person until 100, instead of switching.

Sorry to keep bothering you, and thanks for all the awesome help you've been dishing out.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-01-2008, 09:09 PM
King8654's Avatar
Member
 
Join Date: Apr 2008
Posts: 9
King8654 is on a distinguished road
ACTUALLY, just fixed that problem by removing:

Quote:
public void actionPerformed(ActionEvent e) {
int left = _leftDie.roll();
int right = _rightDie.roll();
postLabels(left, right);
//turnManager.next();
}
}
the turnmanager.next(); from the roll function, so indeed it does work good. Im going to go through this, and try now to implement if a one is rolled, the round score is set to 0, and if two ones are rolled, the total score is set to 0. Both of these switch turns, but with the manager now, should be alot easier. Thanks!
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-07-2008, 07:13 AM
King8654's Avatar
Member
 
Join Date: Apr 2008
Posts: 9
King8654 is on a distinguished road
About rolling 1's, and changing comp's turn after one roll
Sorry, but im back. Just got over catching something bad, havent been able to do much work since last post, with other exams eating me alive. feel bad for me...jk lol

wondering where to start for a method to handle if a one or two ones is rolled, and the actions taken, for a one changes turn and doesnt count the score from that roll, and two rolls of a one wipes out the total score and changes turns. Guessing this would use a switch loop, where the cases would be a sum of 2(roll 2 ones), and a roll 1 || roll 2 equal one, then call the turn manager, but just dont know where to put it.

Also, wondering for hint on how to use the turn manager next turn call after one roll for the computers turn. thanks a million for all the other help received, just a tad bit more and the prog will work beautifully. thanks again!

bryan
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
- FREE COLLEGE EXAM BANK
CASH giveaway first month - Live Within WEEK
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-07-2008, 08:58 PM
King8654's Avatar
Member
 
Join Date: Apr 2008
Posts: 9
King8654 is on a distinguished road
Finished!!!!!!!
Guess i doubted myself too much, so went ahead, wrote a few methods, messed around with some loops and function calls, and bam, all done!

Wrote two new functions, for rolling the single one and double ones, and works!

heres the updated code if anyone needs it in the future, or for some help!

MyApp2.java - handles actual game

Quote:
// MyApp.java

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

public class DiceApp2 extends JFrame
{
private JLabel _aLabel, _comp, _comproll, _comptotal;
private JLabel _user, _userroll, _usertotal, _victory, _compWin;
private JPanel _buttonPanel, _labelPanel1, _labelPanel2, _diePanel;
private Die _leftDie;
private Die _rightDie;
int userScore = 0;
int compScore = 0;
TurnManager turnManager = new TurnManager();


public DiceApp2(String title)
{ super(title);
this.setSize(600,450);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
_aLabel = new JLabel("Pig 2-Dice Java Game");
this.add(_aLabel, java.awt.BorderLayout.NORTH);
_aLabel.setHorizontalAlignment(JLabel.CENTER);

_leftDie = new Die();
_rightDie = new Die();

JButton quit = new JButton("Quit");
quit.addActionListener(new QuitListener());

_victory = new JLabel();
_victory.setVisible(false);

_diePanel = new JPanel();
_diePanel.setBorder(BorderFactory.createEtchedBord er());
_diePanel.setBackground(Color.DARK_GRAY);
_diePanel.setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5,5,5,5);
gbc.gridwidth = GridBagConstraints.REMAINDER;
_diePanel.add(_leftDie,gbc);
_diePanel.add(_rightDie,gbc);
_diePanel.add(quit,gbc);
_diePanel.add(_victory,gbc);
_victory.setHorizontalAlignment(JLabel.CENTER);
this.add(_diePanel, BorderLayout.CENTER);

_comp = new JLabel("Computer");
_comp.setFont(new Font("Impact", Font.BOLD, 32));
_comproll = new JLabel();
_comptotal = new JLabel();

_labelPanel1 = new JPanel();
_labelPanel1.setBorder(BorderFactory.createEtchedB order());
Dimension d = _labelPanel1.getPreferredSize();
d.width = 200;
_labelPanel1.setPreferredSize(d);
_labelPanel1.setLayout(new GridLayout(0,1));
_labelPanel1.add(_comp);
_comp.setHorizontalAlignment(JLabel.CENTER);
_comp.setBorder(BorderFactory.createEtchedBorder() );
_labelPanel1.add(_comproll);
_comproll.setHorizontalAlignment(JLabel.CENTER);
_labelPanel1.add(_comptotal);
_comptotal.setHorizontalAlignment(JLabel.CENTER);
this.add(_labelPanel1, BorderLayout.EAST);

_user = new JLabel("User");
_user.setFont(new Font("Impact", Font.BOLD, 32));
_userroll = new JLabel();
_usertotal = new JLabel();

_labelPanel2 = new JPanel();
_labelPanel2.setBorder(BorderFactory.createEtchedB order());
_labelPanel2.setPreferredSize(d);
_labelPanel2.setLayout(new GridLayout(0,1));
_labelPanel2.add(_user);
_user.setHorizontalAlignment(JLabel.CENTER);
_user.setBorder(BorderFactory.createEtchedBorder() );
_labelPanel2.add(_userroll);
_userroll.setHorizontalAlignment(JLabel.CENTER);
_labelPanel2.add(_usertotal);
_usertotal.setHorizontalAlignment(JLabel.CENTER);
this.add(_labelPanel2, BorderLayout.WEST);

JButton roll = new JButton("Roll");
roll.addActionListener(new RollListener());

JButton pass = new JButton("Pass");
pass.addActionListener(new PassListener());

_buttonPanel = new JPanel();
_buttonPanel.setLayout(new GridLayout(1,0));
_buttonPanel.add(roll);
_buttonPanel.add(pass);

this.add(_buttonPanel, BorderLayout.SOUTH);

this.setVisible(true);
}

private class RollListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
int left = _leftDie.roll();
int right = _rightDie.roll();
rollDoubleOnes(left,right);



}
}

private class QuitListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}

private class PassListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
turnManager.next();

}
}

private class TurnManager {
final static int USER = 0;
final static int COMPUTER = 1;
int turn = USER;

public void next() {
turn = (turn+1) %2;
}

public int getTurn() { return turn; };

public void reset() { turn = USER; };
}

private void postLabels(int left, int right) {

int round = left + right;
int turn = turnManager.getTurn();
switch(turn) {
case TurnManager.USER: // user

_userroll.setText("Round Score: " + round);
userScore += round;
_usertotal.setText("Total Score: " + userScore);
//turnManager.next();
break;
case TurnManager.COMPUTER: // computer

_comproll.setText("Round Score: " + round);
compScore += round;
_comptotal.setText("Total Score: " + compScore);
turnManager.next();
}



if(userScore > 100 || compScore > 100)
gameOver();


}

private void rollDoubleOnes(int left, int right) {

int round = left + right;

int turn = turnManager.getTurn();

if (turn == turnManager.USER && round == 2) {

round = round *0;
_userroll.setText("Round Score: " + round);
userScore = userScore *0;
_usertotal.setText("Total Score: " + userScore);
turnManager.next();


}
else if(turn == turnManager.COMPUTER && round == 2) {

round = round *0;
_comproll.setText("Round Score: " + round);
compScore = compScore *0;
_comptotal.setText("Total Score: " + compScore);
turnManager.next();


} else {
rollOnes(left,right); }
}

private void rollOnes(int left, int right) {


int turn = turnManager.getTurn();
int round = left + right;

if (turn == turnManager.USER && left == 1 || right == 1) {

round = round *0;
_userroll.setText("Round Score: " + round);
userScore = userScore + 0;
_usertotal.setText("Total Score: " + userScore);
turnManager.next();
}

else if (turn == turnManager.COMPUTER && left == 1 || right == 1) {

round = round *0;
_comproll.setText("Round Score: " + round);
compScore = compScore + 0;
_comptotal.setText("Total Score: " + compScore);
turnManager.next();
}
else {
postLabels(left, right);

}

}

private void gameOver() {
if(userScore > 100) {
_victory.setText(" User Wins " + userScore + " to " + compScore);
_victory.setFont(new Font("Ariel", Font.BOLD, 16));
_victory.setForeground(new Color(0xffffff));
_victory.setVisible(true);

}
else if(compScore > 100) {
_victory.setText(" Comp Wins " + compScore + " to " + userScore);
_victory.setFont(new Font("Ariel", Font.BOLD, 16));
_victory.setForeground(new Color(0xffffff));
_victory.setVisible(true);

}
// reset variables, labels and "turn"
}



public static void main(String [] args) {
new DiceApp2("A Game Of Fookin Pig");
}
}
And Die.java - handles the drawing of die and defines some functions

Quote:
import java.awt.*;
import javax.swing.*;


public class Die extends JComponent {

private static final int SPOT_DIAM = 12;
private static final int a = 120;
private int die, _faceValue, _roll = 0, _sum = 0, roll;


public Die() {

setPreferredSize(new Dimension(120,120));
roll();

}


public int roll() {
die = (int)(6*Math.random() + 1);
setValue(die);
return die;

}


/** Returns result of last roll.*/
public int getValue() {
return _faceValue;

}

/** Sets the value of the Die. Causes repaint().
* @param spots Number from 1-6.
*/
public void setValue(int spots) {
_faceValue = spots;
repaint();
}


/** Draws spots of die face. */
public void paintComponent(Graphics g) {

Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
RenderingHints.VALUE_ANTIALIAS_ON);


g2.setColor(Color.RED);
g2.fillRect(0, 0, a, a);
g2.setColor(Color.BLACK);

g2.drawRect(0, 0, a-1, a-1);

switch (die) {
case 1:
drawSpot(g2, a/2, a/2);
break;
case 3:
drawSpot(g2, a/2, a/2);
case 2:
drawSpot(g2, a/4, a/4);
drawSpot(g2, 3*a/4, 3*a/4);
break;
case 5:
drawSpot(g2, a/2, a/2);
case 4:
drawSpot(g2, a/4, a/4);
drawSpot(g2, 3*a/4, 3*a/4);
drawSpot(g2, 3*a/4, a/4);
drawSpot(g2, a/4, 3*a/4);
break;
case 6:
drawSpot(g2, a/4, a/4);
drawSpot(g2, 3*a/4, 3*a/4);
drawSpot(g2, 3*a/4, a/4);
drawSpot(g2, a/4, 3*a/4);
drawSpot(g2, a/4, a/2);
drawSpot(g2, 3*a/4, a/2);
break;
}
}


/** Utility method used by paintComponent(). */
private void drawSpot(Graphics2D g2, int x, int y) {
g2.fillOval(x-SPOT_DIAM/2, y-SPOT_DIAM/2, SPOT_DIAM, SPOT_DIAM);
}
}
thanks everyone for the snippets of help, and for actually giving me WAY more confidence in programming having to figure most of it on my own. thanks again guys!!
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
- FREE COLLEGE EXAM BANK
CASH giveaway first month - Live Within WEEK
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
failure at Class.forName("oracle.jdbc.driver.OracleDriver"); RonNYC Eclipse 1 03-14-2008 04:51 PM
Implementing "Game Over" in Minesweeper game based on Gridworld framework. JFlash New To Java 0 11-16-2007 01:02 AM
Hwlp with "Open", "Save", "Save as..." trill New To Java 1 07-31-2007 09:53 AM
Exception in thread "main" java.net.ConnectException: Connection timed out osval Advanced Java 1 07-28-2007 12:59 AM
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException romina New To Java 1 07-26-2007 12:55 AM


All times are GMT +3. The time now is 12:53 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org