Re: need help at assignment
Hi,
Post the code you've written so far please then someone can help you much better.
Re: need help at assignment
Here
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package assignment;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.ImageIcon;
/**
*
* @author Ng Min Teck
*/
public class AssignmentFrame extends JFrame implements ActionListener {
private AssignmentGame gamecontrol;
//North Panel
private JPanel internorthpanel, northpanel;
private JLabel myfirstjavagame, nameofplayer, selectacounter;
private JTextField nameofplayertextfield;
private ButtonGroup group;
private JRadioButton redcounter;
private JRadioButton bluecounter;
//
//South Panel
private JPanel southpanel;
private JButton startgamebtn;
private JButton rollthedicebtn;
private JButton viewhistorybtn;
private JButton exitgamebtn;
//
//East Panel
private JPanel eastpanel;
private JTextArea textarea;
//
//West Panel
private JPanel westpanel;
//
//Center Panel
private JPanel centerpanel;
private JButton[] number;
private void north() {
northpanel = new JPanel(new BorderLayout());
myfirstjavagame = new JLabel("My First Java Game", JLabel.CENTER);
internorthpanel = new JPanel(new FlowLayout());
nameofplayer = new JLabel("Name of Player:");
nameofplayertextfield = new JTextField(15);
selectacounter = new JLabel("Select a Counter:");
redcounter = new JRadioButton();
bluecounter = new JRadioButton();
group = new ButtonGroup();
//radio button must group
group.add(redcounter);
group.add(bluecounter);
redcounter.setSelected(true);
internorthpanel.add(nameofplayer);
internorthpanel.add(nameofplayertextfield);
internorthpanel.add(selectacounter);
internorthpanel.add(redcounter);
internorthpanel.add(new JLabel(new ImageIcon("red.gif")));
internorthpanel.add(bluecounter);
internorthpanel.add(new JLabel(new ImageIcon("blue.gif")));
northpanel.add(myfirstjavagame, BorderLayout.NORTH);
northpanel.add(internorthpanel, BorderLayout.CENTER);
}
private void south() {
southpanel = new JPanel(new FlowLayout());
startgamebtn = new JButton("Start Game");
rollthedicebtn = new JButton("Roll The Dice");
rollthedicebtn.setEnabled(false);
viewhistorybtn = new JButton("View History");
exitgamebtn = new JButton("Exit Game");
southpanel.add(startgamebtn);
southpanel.add(rollthedicebtn);
southpanel.add(viewhistorybtn);
southpanel.add(exitgamebtn);
}
private void east() {
eastpanel = new JPanel(new BorderLayout());
textarea = new JTextArea(5, 10);
eastpanel.add(textarea);
}
private void west() {
westpanel = new JPanel(new FlowLayout());
}
private void center() {
centerpanel = new JPanel(new GridLayout(0, 8));
number = new JButton[33];
//33 become we only need 32 so 32>33
for (int i = 1; i < number.length; i++) {
number[i] = new JButton("" + i);
}
// The min to max vlaue of array
//Board game GUL
for (int i = 32; i >= 25; i--) {
centerpanel.add(number[i]);
}
for (int i = 17; i <= 24; i++) {
centerpanel.add(number[i]);
}
for (int i = 16; i >= 9; i--) {
centerpanel.add(number[i]);
}
for (int i = 1; i <= 8; i++) {
centerpanel.add(number[i]);
}
//End of Boad game GUI
}
public AssignmentFrame() {
north();
south();
east();
west();
center();
setLayout(new BorderLayout());
add(northpanel, BorderLayout.NORTH);
add(southpanel, BorderLayout.SOUTH);
add(eastpanel, BorderLayout.EAST);
add(westpanel, BorderLayout.WEST);
add(centerpanel, BorderLayout.CENTER);
initactionlistener();
//gamecontrol = new AssignmentGame();
}
public void initactionlistener() {
startgamebtn.addActionListener(this);
rollthedicebtn.addActionListener(this);
viewhistorybtn.addActionListener(this);
exitgamebtn.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == rollthedicebtn) {
dice();
} else if (e.getSource() == startgamebtn) {
start();
} else if (e.getSource() == viewhistorybtn) {
viewhistory();
} else {
exitgame();
}
}
public void start() {
AssignmentPlayer name= new AssignmentPlayer();
String playername = nameofplayertextfield.getText().trim();
if (playername.length() > 0) {
if (redcounter.isSelected() == true) {
name.setPlayerImage(new ImageIcon("red.gif"));
} else {
name.setPlayerImage(new ImageIcon("blue.gif"));
}
name.setName(playername);
startgamebtn.setEnabled(false);
rollthedicebtn.setEnabled(true);
}
}
public void dice() {
int rollNumber = gamecontrol.getPlayer().rollDice();
//rollDice from get player from gamecontrol set it here for using rollnumber alot of time
String Msg = getName() + "rolled number:" + rollNumber;
textarea.append(Msg);
}
public void viewhistory() {
}
public void exitgame() {
}
}
Player
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package assignment;
import javax.swing.ImageIcon;
/**
*
* @author Ng Min Teck
*/
public class AssignmentPlayer {
private String name;
private ImageIcon playerImage;
private int position;
private int numofroll;
public ImageIcon getPlayerImage() {
return playerImage;
}
public int getPosition() {
return position;
}
public int getNumOfRolls(){
return numofroll;
}
public String getName(){
return this.name;
}
public void setPlayerImage(ImageIcon newplayerimage){
this.playerImage = newplayerimage;
}
public void setPosition(int newposition){
this.position = newposition;
}
public void setNumOfRolls(int newnumofroll){
this.numofroll = newnumofroll;
}
public void setName(String newname){
this.name = newname;
}
public int rollDice(){
return(int)(Math.random() * 6 + 1);
}
// public boolean resetPlayer(maxiumgameboard){
// }
}
Game
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package assignment;
/**
*
* @author Ng Min Teck
*/
public class AssignmentGame {
private AssignmentPlayer player;
private int boardSize;
//private int currentplayer;
private String history;
private int gamenumber;
public AssignmentGame(int boardSize){
this.boardSize = 32;
}
public AssignmentPlayer getPlayer(){
return player;
}
public boolean advanceCurrentPlayer(int numberofsteps){
}
// public void nextGame()
//getHistory
//setHistory
}
Re: need help at assignment
Can you explain your problem creating the "roll dice" method?
The code you posted needs a main() method to be able to execute it for testing.
Re: need help at assignment
My problem is need to make player can move at board game when the roll dice button have been clicked
My lecture ask me create 4 class game,player,frame & the run program class, in assignment my lecture give me hint that using gamecontrol at frame to link the game class & player class together and he also giving all the method
eh how come i can't reply?
Re: need help at assignment
How do you execute the code? There is no main() method.
Re: need help at assignment
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package assignment;
import javax.swing.JFrame;
/**
*
* @author Ng Min Teck
*/
public class Assignment {
public static void main(String[] args) {
AssignmentFrame f = new AssignmentFrame();
f.setTitle("JPRG Assignment");
f.setSize(800, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Re: need help at assignment
Do you get any errors when you execute the code?
What are the steps for executing the program?
Re: need help at assignment
you can excute the code by playing assignment class
there is a error at assignmentframe
Re: need help at assignment
Quote:
Originally Posted by
ng min teck
there is a error at assignmentframe line 146 //gamecontrol = new AssignmentGame();
What type of error are you receiving?
Re: need help at assignment
The main problem is what code should i use in java to move the player of number of step determine by what i rolled from the dice
Re: need help at assignment
Re: need help at assignment
What are the steps for executing the program?
When I start the code, the roll the dice button is not enabled. What is the used supposed to do to use the program?
Re: need help at assignment
solved can you remove the code, because my shool just warn me not to post the code thank you, i can't edit the top post
Re: need help at assignment
Quote:
Originally Posted by
ng min teck
solved can you remove the code, because my shool just warn me not to post the code thank you, i can't edit the top post
This seems to be an ongoing problem. The ability for regular members to edit their posts expires after about 24 hours.
Re: need help at assignment
Nor should we have the code deleted or concealed. It was posted publicly and your school should be able to know this if it so desires.
Re: need help at assignment
i mean can i delete my code post as the request?
Re: need help at assignment
If it wasn't to be posted, you shouldn't have posted it. And no, it won't be deleted.
db
Re: need help at assignment
firstly i don't know i can't post the code, so my school ask me to remove. so i don't have no right request to delete my own post? i am very disappointed the service you provided
Re: need help at assignment
Quote:
Originally Posted by
ng min teck
i am very disappointed the service you provided
Demand your money back
db