2
I have a program that simulates a game of dice, where I have two "dice" that, when the sum of the two gives 7, the user is winner, if not, he loses.
Well, I made a simple interface on swing
, where I created two buttons, a launches the first value using the class Random
, and another button that throws a second value, and one more that brings the result.
I wonder if it is possible, and if it is, how to make the user click on only one button, to trigger the two values, he clicks once, triggers the value of the first dice, and he clicks on another, and triggers the value of the second dice. Thank you!
Follows Code from the Screen:
public class JDialogJogar extends javax.swing.JDialog {
/**
* Creates new form Jogo
*/
public JDialogJogar(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}
int numero1;
int numero2;
public void jogarDado1() {
try {
Random gerador1 = new Random();
numero1 = gerador1.nextInt(7) + 1;
JOptionPane.showMessageDialog(this, "Dado 1: " + numero1);
int dado1 = numero1;
txtDado1.setText(String.valueOf(numero1));
travarDado1();
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Erro" + e.getMessage());
}
}
public void jogarDado2() {
try {
Random gerador = new Random();
numero2 = gerador.nextInt(7 + 1);
JOptionPane.showMessageDialog(this, "Dado 2: " + numero2);
int dado2 = numero2;
txtDado2.setText(String.valueOf(numero2));
travarDado2();
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Erro" + e.getMessage());
}
}
public void mostrarResultado() {
int resultado = numero1 + numero2;
if ((numero1 + numero2) == 7) {
JOptionPane.showMessageDialog(this, "Resultado: " + resultado + " Parabéns! Você Ganhou!");
destravar();
limpar();
} else {
JOptionPane.showMessageDialog(this, "Resultado: " + resultado + " Você Perdeu!");
destravar();
limpar();
}
}
Of the action of the button:
private void btnResultadoActionPerformed(java.awt.event.ActionEvent evt) {
mostrarResultado();
}
private void btnJogarDado1ActionPerformed(java.awt.event.ActionEvent evt) {
jogarDado1();
}
private void btnJogarDado2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jogarDado2();
}
You want with one click, fire dice 1 and 2 or with different clicks?
– user28595
I want you to click, fire dice 1, and with another click, fire dice 2.
– Donna Brown
The data can continue to be fired freely after they have already been fired once each?
– user28595
Can Yes!------
– Donna Brown