Commit b58d44a4 authored by Oliw2's avatar Oliw2

Dice game (almost) working.

parents
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>DiceGame</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>13</maven.compiler.source>
<maven.compiler.target>13</maven.compiler.target>
</properties>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8" ?>
<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
<Properties>
<Property name="defaultCloseOperation" type="int" value="3"/>
<Property name="title" type="java.lang.String" value="Dice game"/>
<Property name="background" type="java.awt.Color" editor="org.netbeans.beaninfo.editors.ColorEditor">
<Color blue="66" green="66" red="66" type="rgb"/>
</Property>
</Properties>
<SyntheticProperties>
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
<SyntheticProperty name="generateCenter" type="boolean" value="false"/>
</SyntheticProperties>
<AuxValues>
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
<AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
</AuxValues>
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="-2" pref="18" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="RoundLabel" alignment="0" min="-2" max="-2" attributes="0"/>
<Group type="102" alignment="0" attributes="0">
<Component id="Player1RollButton" min="-2" pref="119" max="-2" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="Player1Rolled" min="-2" max="-2" attributes="0"/>
</Group>
<Component id="TotalLabel" min="-2" max="-2" attributes="0"/>
<Group type="102" attributes="0">
<Component id="Player2RollButton" min="-2" pref="119" max="-2" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="Player2Rolled" min="-2" max="-2" attributes="0"/>
</Group>
<Component id="WinnerText" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="32767" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<Component id="TitleLabel" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="54" max="-2" attributes="0"/>
<Component id="P1Points" min="-2" pref="120" max="-2" attributes="0"/>
<EmptySpace pref="32" max="32767" attributes="0"/>
<Component id="P2Points" min="-2" pref="120" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="32" max="-2" attributes="0"/>
</Group>
</Group>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="-2" pref="27" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="TitleLabel" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="P1Points" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="P2Points" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="separate" max="-2" attributes="0"/>
<Component id="RoundLabel" min="-2" max="-2" attributes="0"/>
<EmptySpace type="separate" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="Player1RollButton" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="Player1Rolled" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="Player2RollButton" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="Player2Rolled" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="separate" max="-2" attributes="0"/>
<Component id="TotalLabel" min="-2" max="-2" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="WinnerText" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="64" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Component class="javax.swing.JLabel" name="TitleLabel">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Helvetica" size="24" style="1"/>
</Property>
<Property name="foreground" type="java.awt.Color" editor="org.netbeans.beaninfo.editors.ColorEditor">
<Color blue="33" green="0" red="cc" type="rgb"/>
</Property>
<Property name="text" type="java.lang.String" value="Dice game"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="RoundLabel">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Helvetica" size="24" style="1"/>
</Property>
<Property name="text" type="java.lang.String" value="Round:"/>
</Properties>
</Component>
<Component class="javax.swing.JButton" name="Player1RollButton">
<Properties>
<Property name="text" type="java.lang.String" value="Player 1 roll"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="Player1RollButtonActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JButton" name="Player2RollButton">
<Properties>
<Property name="text" type="java.lang.String" value="Player 2 roll"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="Player2RollButtonActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JLabel" name="Player1Rolled">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Helvetica" size="14" style="1"/>
</Property>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="Player2Rolled">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Helvetica" size="14" style="1"/>
</Property>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="TotalLabel">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Helvetica" size="24" style="1"/>
</Property>
<Property name="text" type="java.lang.String" value="Total:"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="WinnerText">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Helvetica" size="18" style="1"/>
</Property>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="P1Points">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Helvetica" size="18" style="1"/>
</Property>
<Property name="text" type="java.lang.String" value="P1 Points:"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="P2Points">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Helvetica" size="18" style="1"/>
</Property>
<Property name="text" type="java.lang.String" value="P2 Points:"/>
<Property name="toolTipText" type="java.lang.String" value=""/>
</Properties>
</Component>
</SubComponents>
</Form>
/*
* 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 Oliw2
*/
import java.util.Random;
public class DiceGameFrame extends javax.swing.JFrame {
int roundNum = 0; // round number starting at 0
static Random rand = new Random(); // new random number using java.util.Random
int playerOnePoints;
int playerTwoPoints; // points variables for player 1 and 2
/**
* Creates new form DiceGameFrame
*/
public DiceGameFrame() {
initComponents();
RoundLabel.setText("Round: " + roundNum);
}
public void roundChange() // what happens when the round changes
{
roundNum = roundNum + 1; // add 1 to the round number
RoundLabel.setText("Round: " + roundNum); // display it on a label
Player1RollButton.setEnabled(true); // enable both buttons so the players can roll
Player2RollButton.setEnabled(true);
if(roundNum >= 3) // if the round gets to 3 the game ends (best of 3)
{
Player1RollButton.setEnabled(false);
Player2RollButton.setEnabled(false);
if(playerOnePoints > playerTwoPoints)
WinnerText.setText("Game! Winner is: Player 1!");
else if(playerTwoPoints > playerOnePoints)
WinnerText.setText("Game! Winner is: Player 2!");
else if(playerTwoPoints == playerOnePoints)
WinnerText.setText("Game! It's a draw!");
}
}
public void Storage()
{
int p1Roll = Integer.parseInt(Player1Rolled.getText());
int p2Roll = Integer.parseInt(Player2Rolled.getText());
int total = p1Roll + p2Roll;
TotalLabel.setText("Total: " + String.valueOf(total));
switch (total)
{
case 7:
case 11:
WinnerText.setText("Player 1 wins this round! ");
playerOnePoints++;
P1Points.setText("P1 Points: " + playerOnePoints);
roundChange();
break;
case 2:
case 3:
case 12:
WinnerText.setText("Draw! Each player gets 1 point.");
playerOnePoints = playerOnePoints + 1;
playerTwoPoints = playerTwoPoints + 1;
P1Points.setText("P1 Points: " + playerOnePoints);
P2Points.setText("P2 Points: " + playerTwoPoints);
roundChange();
break;
case 4:
case 5:
case 6:
WinnerText.setText("Player 2 wins this round!");
playerTwoPoints++;
P2Points.setText("P2 Points: " + playerTwoPoints);
roundChange();
break;
case 8:
case 9:
case 10:
WinnerText.setText("Player 1 wins this round!");
playerOnePoints++;
P1Points.setText("P1 Points: " + playerOnePoints);
roundChange();
break;
}
}
/**
* 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">//GEN-BEGIN:initComponents
private void initComponents() {
TitleLabel = new javax.swing.JLabel();
RoundLabel = new javax.swing.JLabel();
Player1RollButton = new javax.swing.JButton();
Player2RollButton = new javax.swing.JButton();
Player1Rolled = new javax.swing.JLabel();
Player2Rolled = new javax.swing.JLabel();
TotalLabel = new javax.swing.JLabel();
WinnerText = new javax.swing.JLabel();
P1Points = new javax.swing.JLabel();
P2Points = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Dice game");
setBackground(new java.awt.Color(102, 102, 102));
TitleLabel.setFont(new java.awt.Font("Helvetica", 1, 24)); // NOI18N
TitleLabel.setForeground(new java.awt.Color(204, 0, 51));
TitleLabel.setText("Dice game");
RoundLabel.setFont(new java.awt.Font("Helvetica", 1, 24)); // NOI18N
RoundLabel.setText("Round:");
Player1RollButton.setText("Player 1 roll");
Player1RollButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Player1RollButtonActionPerformed(evt);
}
});
Player2RollButton.setText("Player 2 roll");
Player2RollButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Player2RollButtonActionPerformed(evt);
}
});
Player1Rolled.setFont(new java.awt.Font("Helvetica", 1, 14)); // NOI18N
Player2Rolled.setFont(new java.awt.Font("Helvetica", 1, 14)); // NOI18N
TotalLabel.setFont(new java.awt.Font("Helvetica", 1, 24)); // NOI18N
TotalLabel.setText("Total:");
WinnerText.setFont(new java.awt.Font("Helvetica", 1, 18)); // NOI18N
P1Points.setFont(new java.awt.Font("Helvetica", 1, 18)); // NOI18N
P1Points.setText("P1 Points:");
P2Points.setFont(new java.awt.Font("Helvetica", 1, 18)); // NOI18N
P2Points.setText("P2 Points:");
P2Points.setToolTipText("");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(RoundLabel)
.addGroup(layout.createSequentialGroup()
.addComponent(Player1RollButton, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Player1Rolled))
.addComponent(TotalLabel)
.addGroup(layout.createSequentialGroup()
.addComponent(Player2RollButton, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Player2Rolled))
.addComponent(WinnerText))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(TitleLabel)
.addGap(54, 54, 54)
.addComponent(P1Points, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
.addComponent(P2Points, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(32, 32, 32))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(TitleLabel)
.addComponent(P1Points)
.addComponent(P2Points))
.addGap(18, 18, 18)
.addComponent(RoundLabel)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Player1RollButton)
.addComponent(Player1Rolled))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Player2RollButton)
.addComponent(Player2Rolled))
.addGap(18, 18, 18)
.addComponent(TotalLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(WinnerText)
.addContainerGap(64, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void Player1RollButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Player1RollButtonActionPerformed
int diceNum = rand.nextInt(6);
Player1Rolled.setText("" + diceNum);
Player1RollButton.setEnabled(false);
}//GEN-LAST:event_Player1RollButtonActionPerformed
private void Player2RollButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Player2RollButtonActionPerformed
int diceNum = rand.nextInt(6);
Player2Rolled.setText("" + diceNum);
Player2RollButton.setEnabled(false);
Storage();
}//GEN-LAST:event_Player2RollButtonActionPerformed
/**
* @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
*/
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(DiceGameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DiceGameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DiceGameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DiceGameFrame.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 DiceGameFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel P1Points;
private javax.swing.JLabel P2Points;
private javax.swing.JButton Player1RollButton;
private javax.swing.JLabel Player1Rolled;
private javax.swing.JButton Player2RollButton;
private javax.swing.JLabel Player2Rolled;
private javax.swing.JLabel RoundLabel;
private javax.swing.JLabel TitleLabel;
private javax.swing.JLabel TotalLabel;
private javax.swing.JLabel WinnerText;
// End of variables declaration//GEN-END:variables
}
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