Commit 2d892cd5 authored by lchem's avatar lchem

Final Dice Game

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-12">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>dicegame1</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=12
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=12
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=12
package dicegame1;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author lchem
*/
//import util for random number generator
import java.util.*;
public class dice1 extends javax.swing.JFrame {
int player1 = 0; //player1 initial score
int player2 = 0; //player2 initial score
int round = 0; //initial round
int whichplayer = 1; //first player is player1
public static double randomnumbercreator (double first,double last) {
//random number maker between 1 and 6
//developed from this address
//https://dzone.com/articles/random-number-generation-in-java
first = 1;
last = 6;
double a;
a = (int) (Math.random()*((last - first)+1))+first;
return a;
}
/**
* Creates new form dice1
*/
public dice1() {
initComponents();
//show round
currentround.setText(String.valueOf(round));
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
currentround = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
player1_total = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
player2_total = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
turn = new javax.swing.JLabel();
roll_dice = new javax.swing.JButton();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
die1 = new javax.swing.JLabel();
die2 = new javax.swing.JLabel();
display = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
total = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
currentround.setFont(new java.awt.Font("Segoe UI Black", 0, 24)); // NOI18N
jLabel1.setFont(new java.awt.Font("Segoe UI Black", 0, 24)); // NOI18N
jLabel1.setForeground(new java.awt.Color(229, 0, 0));
jLabel1.setText("ROUND:");
jLabel2.setFont(new java.awt.Font("Segoe UI Black", 0, 18)); // NOI18N
jLabel2.setText("Player 2 Total:");
player1_total.setFont(new java.awt.Font("Tahoma", 0, 20));
jLabel3.setFont(new java.awt.Font("Segoe UI Black", 0, 18)); // NOI18N
jLabel3.setText("Player 1 Total:");
player2_total.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
jLabel4.setFont(new java.awt.Font("Segoe UI Black", 0, 24)); // NOI18N
jLabel4.setText("Who Just rolled:");
turn.setFont(new java.awt.Font("Segoe UI Black", 0, 18)); // NOI18N
roll_dice.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
roll_dice.setForeground(new java.awt.Color(229, 0, 0));
roll_dice.setText("ROLL DICE");
roll_dice.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
roll_diceActionPerformed(evt);
}
});
jLabel5.setFont(new java.awt.Font("Segoe UI Black", 0, 18)); // NOI18N
jLabel5.setText("First die:");
jLabel6.setFont(new java.awt.Font("Segoe UI Black", 0, 18)); // NOI18N
jLabel6.setText("Second die:");
die1.setFont(new java.awt.Font("Segoe UI Black", 0, 18)); // NOI18N
die2.setFont(new java.awt.Font("Segoe UI Black", 0, 18)); // NOI18N
display.setFont(new java.awt.Font("Segoe UI Black", 0, 18)); // NOI18N
jLabel7.setFont(new java.awt.Font("Segoe UI Black", 0, 18)); // NOI18N
jLabel7.setText("Total:");
total.setFont(new java.awt.Font("Segoe UI Black", 0, 18)); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(400, 400, 400)
.addComponent(jLabel7))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(turn, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(currentround, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(roll_dice))
.addGroup(layout.createSequentialGroup()
.addGap(43, 43, 43)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(player2_total, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(player1_total, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addGap(48, 48, 48)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(14, 14, 14)
.addComponent(jLabel6))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel5))))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(display, javax.swing.GroupLayout.PREFERRED_SIZE, 371, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(total, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 43, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(die1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(die2, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(currentround, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(20, 20, 20)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(turn, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(16, 16, 16)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE)
.addComponent(die1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
.addComponent(die2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel4)
.addGap(30, 30, 30)
.addComponent(roll_dice, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(11, 11, 11)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7)
.addComponent(total, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(display, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel3)
.addComponent(player1_total, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(player2_total, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void roll_diceActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//use rndm number generator to get number for both dice.
int rand1 = (int) dice1.randomnumbercreator(6,1);
//value to string
//https://www.tutorialspoint.com/java/java_string_valueof.htm
die1.setText(String.valueOf(rand1)); // die1 box set to rand1 value
int rand2 = (int) dice1.randomnumbercreator(6,1); //second generated number
die2.setText(String.valueOf(rand2)); //die2 box set to rand1 value
total.setText(""); //total to be empty to begin
// total of adding the two dice
double sum = rand1 + rand2;
//show the sum of the two random numbers in total box
total.setText(String.valueOf(sum));
// create a variable to save the sum if it is challenging
double challengenumber = 13;
//if statement for possible outcomes starting with what to do if theres
//a challenge number
if( sum > challengenumber ){ //if challenge number was changed,
//the sum could be bigger and a win could have been made
if (whichplayer ==1){
turn.setText(String.valueOf(whichplayer));
round = round +1;
player1 = player1 + 1; //player gets a point for sum being higher
player1_total.setText(String.valueOf(player1));
currentround.setText(String.valueOf(round));
}
else{
turn.setText(String.valueOf(whichplayer));
player2 = player2 + 1; //player wins + 1 point
player2_total.setText(String.valueOf(player2));
}
display.setText(" congrats! you scored higher + 1 pt"); //show win
}
else if (sum == 7 || sum ==11){ //player +1 point if 7 or 11 thrown
if (whichplayer == 1){ //for player 1, they get a point
turn.setText(String.valueOf(whichplayer));
round = round+1;
currentround.setText(String.valueOf(round));
player1 = player1 + 1;
player1_total.setText(String.valueOf(player1));
}// end of if within first if
else { //for player2 they get a point.
turn.setText(String.valueOf(whichplayer));
player2 = player2 + 1;
player2_total.setText(String.valueOf(player2));
} //end of else within first if
display.setText("Great throw! + 1 point"); //show win
}
else if( sum == 2 || sum == 3 || sum ==12){ //a draw if these numbers created
if(whichplayer==1){ //both players gain a point each
turn.setText(String.valueOf(whichplayer));
round = round+ 1;
currentround.setText(String.valueOf(round));
player1 = player1 +1; //one point added
player1_total.setText(String.valueOf(player1));
player2 = player2 +1; // one point added
player2_total.setText(String.valueOf(player2));
} //end of if within else if
else{ //both players +1 point for draw
turn.setText(String.valueOf(whichplayer));
player1 = player1 +1; //one point added
player1_total.setText(String.valueOf(player1));
player2 = player2 +1; //1 point added
player2_total.setText(String.valueOf(player2));
} //end of else within else if
display.setText("A Draw!"); //show it was a draw
} //end of else if
else { //challenge number is rolled.
if (whichplayer == 1){
turn.setText(String.valueOf(whichplayer));
round= round+1;
currentround.setText(String.valueOf(round));
display.setText("if player 2 rolls higher than " + sum + " you lose!");
challengenumber = sum; //create variable for the sum scored.
}// end of if within else
else {
turn.setText(String.valueOf(whichplayer));
display.setText("if player 1 rolls higher than " + sum + " you lose!");
challengenumber = sum;
} // end of else within else
} //end of else for last else.
if(whichplayer ==1){
whichplayer = 2; }
else{
whichplayer = 1;
}
challengenumber = 13; //change number back to 13 if it was used.
// if(sum > challengenumber){
// if (whichplayer== 1){
//
// round = round +1;
// currentround.setText(String.valueOf(round));
// turn.setText(String.valueOf(whichplayer));
// player1 = player1 + 1;
// player1_total.setText(String.valueOf(player1));
// }
// else{
// turn.setText(String.valueOf(whichplayer));
// player2 = player2 +1;
// player2_total.setText(String.valueOf(player2));
// }
// display.setText("You scored higher! + 1 point");
// }
// else if(sum <= challengenumber){
// if (whichplayer== 1){
// round = round +1;
// currentround.setText(String.valueOf(round));
// turn.setText(String.valueOf(whichplayer));
// player2 = player2 + 1;
// player2_total.setText(String.valueOf(player2));
// display.setText("Unlucky! One point for player 2 ");
// }
// else{
// turn.setText(String.valueOf(whichplayer));
// player1 = player1 +1;
// player1_total.setText(String.valueOf(player1));
// display.setText("Unlucky! One point for Player 1 ");
// }
// }
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
//initialise scores and round to zero.
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(dice1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(dice1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(dice1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(dice1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new dice1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel currentround;
private javax.swing.JLabel die1;
private javax.swing.JLabel die2;
private javax.swing.JLabel display;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel player1_total;
private javax.swing.JLabel player2_total;
private javax.swing.JButton roll_dice;
private javax.swing.JLabel total;
private javax.swing.JLabel turn;
// End of variables declaration
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment