# help coding the games



## makarov (Nov 14, 2009)

*help with the codes!*

This applet should generate 3 random numbers, in the 3 coloured boxes to simulate the rolling of 3 dice.  The player enters 3 in the boxes below the dice as guesses for what will be thrown.
When the button is is pressed, the 3 die are "rolled" and their scores are matched with the guesses in the boxes. The player is told if there are 0, 1, 2 or 3 matches.


----------



## TheLaughingMan (Nov 14, 2009)

Well I am not sure what you are having trouble with here.  I understand what you are trying to do, but you don't say what is the problem or what you can't get working.  You may also want to post your code so people can download and test it themselves for better help.


----------



## Kreij (Nov 14, 2009)

Welcome to TPU Makarov : toast:

As laughingMan stated, we need more information on what is not working for you and it really helps if you post the code you have for your projects. We will be happy to help you get your code working.


----------



## Easy Rhino (Nov 15, 2009)

makarov? from COD: MW2 ? and you want us to write your code for you?


----------



## makarov (Nov 15, 2009)

*help with the codes!*



Kreij said:


> Welcome to TPU Makarov : toast:
> 
> As laughingMan stated, we need more information on what is not working for you and it really helps if you post the code you have for your projects. We will be happy to help you get your code working.



Thanks mate, I just posted what I've alredy done. I would appreciate it, if you could help.

Cheers


----------



## makarov (Nov 15, 2009)

*help with the codes!*



Easy Rhino said:


> makarov? from COD: MW2 ? and you want us to write your code for you?





Thanks mate, I just posted what I've alredy done. I would appreciate it, if you could help.

Cheers


----------



## makarov (Nov 15, 2009)

Easy Rhino said:


> makarov? from COD: MW2 ? and you want us to write your code for you?



Am not lazy & am not asking 4 anybody 2 do the entire work 4 me.Am simple asking 4 help with ideas & suggestions regarding what I've already done.


----------



## Kreij (Nov 15, 2009)

Makarov, copy and paste your code into the thread so we can look at it here and not have to download a file to help you. Use the code tags ...

[code]
 Included code here
[/code]


We don't think you're lazy, we just don't know how to help you if you don't post your code.


----------



## makarov (Nov 16, 2009)

Kreij said:


> Makarov, copy and paste your code into the thread so we can look at it here and not have to download a file to help you. Use the code tags ...
> 
> [code]
> Included code here
> ...




```
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DiceRoller extends Applet implements ActionListener{
	
	
	private Die d1,d2;
	private Button btnRoll;
	
	
	public void init(){
		setSize(220,200);
		setBackground(Color.green);
		
		
		d1 = new Die(20,40);
		d2 = new Die(120,40);
		
		
		btnRoll = new Button("ROLL DICE");
		btnRoll.addActionListener(this);
		add(btnRoll);
	}
	
	/**
	 * Display the current values of the 2 dice
	 */
	public void paint(Graphics g){
		d1.display(g);
		d2.display(g);
	}

	/**
	 * When the button is clicked roll the dice.
	 * 
	 */
	public void actionPerformed(ActionEvent ae) {
		d1.roll();
		d2.roll();
		repaint();		
	}
}
```


```
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

/**
 * Simple class to demonstrate a Die
 * 
 * @author Peter lager
 *
 */
public class Die {

	private int value;
	private int posX, posY;
	private Font font;
	
	/**
	 * Create a new die at the given position and 
	 * set the initial value to 1
	 * 
	 * @param posX
	 * @param posY
	 */
	public Die(int posX, int posY) {
		this.posX = posX;
		this.posY = posY;
		value = 1;
		font = new Font("Courier", Font.BOLD, 60);
	}
	
	/**
	 * Set the top left position to display the die
	 * @param posX
	 * @param posY
	 */
	public void setPos(int posX, int posY){
		this.posX = posX;
		this.posY = posY;		
	}

	/**
	 * @return the value
	 */
	public int getValue() {
		return value;
	}

	/**
	 * @param value the value to set
	 */
	public void setValue(int value) {
		this.value = value;
	}

	/**
	 * @param font the font to set
	 */
	public void setFont(Font font) {
		this.font = font;
	}
	
	/**
	 * Roll the dice to get a random value between 1& 6
	 */
	public void roll(){
		value = (int)(Math.random()*6 + 1);
	}
	
	public void display(Graphics g){
		g.setColor(Color.yellow);
		g.fillRect(posX, posY, 80, 80);
		g.setColor(Color.black);
		g.drawRect(posX, posY, 80, 80);
		g.setFont(font);
		g.drawString(""+value, posX + 20, posY+60);	
	}
}
```


----------



## Easy Rhino (Nov 16, 2009)

let me get this straight...

1. you want to create 3 dice 
2. the user enters what they think the dice will roll in a textfield
3. each die generates(rolls) a random number between 1 and 6
4. display the output of the number 
5. check and see if the value in the textfield matches the value of the dice

do i get what you are trying to do?

if so, if you are struggling with the actual mechanics of the code i always find it helpful to work out the mechanics simply using the console. that way you can see the values of the die in a simply println and see if it is actually getting the values you enter. then build the graphics around each object.


----------

