Commit e5cab3b8 authored by jordan.dalby's avatar jordan.dalby

Initial commit

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_Jordan_Dalby</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</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"/>
</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 max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<Component id="roundWinnerLabel" max="32767" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
</Group>
<Group type="102" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="roundLabel" alignment="0" min="-2" pref="183" max="-2" attributes="0"/>
<Group type="103" alignment="0" groupAlignment="1" max="-2" attributes="0">
<Group type="102" alignment="0" attributes="0">
<Component id="outcomeOneLabel" min="-2" pref="53" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="outcomeTwoLabel" max="32767" attributes="0"/>
</Group>
<Component id="turnLabel" alignment="0" max="32767" attributes="0"/>
<Component id="roll" alignment="0" pref="119" max="32767" attributes="0"/>
</Group>
</Group>
<EmptySpace type="separate" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="jLabel6" min="-2" max="-2" attributes="0"/>
<Component id="jLabel7" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" max="-2" attributes="0">
<Component id="jLabel5" pref="88" max="32767" attributes="0"/>
<Component id="playerTwoPointsLabel" alignment="0" max="32767" attributes="0"/>
<Component id="playerOnePointsLabel" alignment="0" max="32767" attributes="0"/>
</Group>
<EmptySpace min="0" pref="46" max="32767" 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 max="-2" attributes="0"/>
<Group type="103" groupAlignment="1" attributes="0">
<Component id="roundLabel" min="-2" pref="32" max="-2" attributes="0"/>
<Component id="jLabel5" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<Component id="turnLabel" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="9" max="-2" attributes="0"/>
<Component id="roll" min="-2" max="-2" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<Component id="jLabel6" min="-2" pref="17" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="jLabel7" min="-2" max="-2" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<Component id="playerOnePointsLabel" min="-2" pref="17" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="playerTwoPointsLabel" min="-2" max="-2" attributes="0"/>
</Group>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" max="-2" attributes="0">
<Component id="outcomeOneLabel" pref="14" max="32767" attributes="0"/>
<Component id="outcomeTwoLabel" max="32767" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Component id="roundWinnerLabel" pref="38" max="32767" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Component class="javax.swing.JLabel" name="roundLabel">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="18" style="0"/>
</Property>
<Property name="text" type="java.lang.String" value="Current Round: 1"/>
<Property name="toolTipText" type="java.lang.String" value=""/>
</Properties>
</Component>
<Component class="javax.swing.JButton" name="roll">
<Properties>
<Property name="text" type="java.lang.String" value="Roll the dice!"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="rollActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JLabel" name="turnLabel">
<Properties>
<Property name="horizontalAlignment" type="int" value="0"/>
<Property name="text" type="java.lang.String" value="It&apos;s player 1&apos;s turn!"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="outcomeOneLabel">
<Properties>
<Property name="horizontalAlignment" type="int" value="0"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="outcomeTwoLabel">
<Properties>
<Property name="horizontalAlignment" type="int" value="0"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel5">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="18" style="0"/>
</Property>
<Property name="horizontalAlignment" type="int" value="0"/>
<Property name="text" type="java.lang.String" value="Points"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel6">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="14" style="0"/>
</Property>
<Property name="text" type="java.lang.String" value="Player 1"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel7">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="14" style="0"/>
</Property>
<Property name="text" type="java.lang.String" value="Player 2"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="roundWinnerLabel">
</Component>
<Component class="javax.swing.JLabel" name="playerOnePointsLabel">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="14" style="0"/>
</Property>
<Property name="horizontalAlignment" type="int" value="0"/>
<Property name="text" type="java.lang.String" value="0"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="playerTwoPointsLabel">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="14" style="0"/>
</Property>
<Property name="horizontalAlignment" type="int" value="0"/>
<Property name="text" type="java.lang.String" value="0"/>
</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.
*/
package com.jordan.dicegame;
import javax.swing.JButton;
/**
*
* @author Jordan
*/
public class DiceGame extends javax.swing.JFrame {
private Game g; // create a Game instance
/**
* Creates new form DiceGame
*/
public DiceGame() {
initComponents();
g = new Game(this); // initialize the Game variable
}
/**
* 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() {
roundLabel = new javax.swing.JLabel();
roll = new javax.swing.JButton();
turnLabel = new javax.swing.JLabel();
outcomeOneLabel = new javax.swing.JLabel();
outcomeTwoLabel = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
roundWinnerLabel = new javax.swing.JLabel();
playerOnePointsLabel = new javax.swing.JLabel();
playerTwoPointsLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
roundLabel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
roundLabel.setText("Current Round: 1");
roundLabel.setToolTipText("");
roll.setText("Roll the dice!");
roll.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
rollActionPerformed(evt);
}
});
turnLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
turnLabel.setText("It's player 1's turn!");
outcomeOneLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
outcomeTwoLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel5.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel5.setText("Points");
jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel6.setText("Player 1");
jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel7.setText("Player 2");
playerOnePointsLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
playerOnePointsLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
playerOnePointsLabel.setText("0");
playerTwoPointsLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
playerTwoPointsLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
playerTwoPointsLabel.setText("0");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(roundWinnerLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(roundLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(outcomeOneLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(outcomeTwoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(turnLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(roll, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel6)
.addComponent(jLabel7))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)
.addComponent(playerTwoPointsLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(playerOnePointsLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(0, 46, Short.MAX_VALUE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(roundLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(turnLabel)
.addGap(9, 9, 9)
.addComponent(roll))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel7))
.addGroup(layout.createSequentialGroup()
.addComponent(playerOnePointsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(playerTwoPointsLabel)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(outcomeOneLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 14, Short.MAX_VALUE)
.addComponent(outcomeTwoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(roundWinnerLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void rollActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rollActionPerformed
// TODO add your handling code here:
g.rollDice(); // roll the dice
}//GEN-LAST:event_rollActionPerformed
public void clearInterface()
{
outcomeOneLabel.setText(""); // set the outcomeOneLabel text to nothing
outcomeTwoLabel.setText(""); // set the outcomeTwoLabel text to nothing
roundWinnerLabel.setText(""); // set the roundWinnerLabel text to nothing
}
public void updateInterface(Game game)
{
playerOnePointsLabel.setText(String.valueOf(game.getPlayers().get(0).getScore())); // update the user interface
playerTwoPointsLabel.setText(String.valueOf(game.getPlayers().get(1).getScore())); // update the user interface
turnLabel.setText("It's " + game.getTurn().getName() + "'s turn"); // update the turn label
roundLabel.setText("Round: " + game.getRoundNumber()); // update the round number
}
public void writeToOutcomeLabels(int firstRoll, int secondRoll, String roundWinner)
{
outcomeOneLabel.setText(String.valueOf(firstRoll)); // set the first label to the first roll
outcomeTwoLabel.setText(String.valueOf(secondRoll)); // set the second label to the second roll
roundWinnerLabel.setText(roundWinner); // set the round winner text
}
public JButton getRollButton()
{
return this.roll; // return the roll button variable
}
/**
* @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(DiceGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DiceGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DiceGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DiceGame.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 DiceGame().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel outcomeOneLabel;
private javax.swing.JLabel outcomeTwoLabel;
private javax.swing.JLabel playerOnePointsLabel;
private javax.swing.JLabel playerTwoPointsLabel;
private javax.swing.JButton roll;
private javax.swing.JLabel roundLabel;
private javax.swing.JLabel roundWinnerLabel;
private javax.swing.JLabel turnLabel;
// End of variables declaration//GEN-END:variables
}
/*
* 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.
*/
package com.jordan.dicegame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Timer;
/**
*
* @author Jordan
*/
public class Game
{
private List<Player> players = new ArrayList<Player>(); // arraylist to store all the players in the game
private Turn turn; // variable to store who's turn it is
private boolean challenging = false; // variable to store wether a number is a challenging number
private int previousSum = 0; // create a previous sum variable and set it to 0
private int round = 1; // create a round variable and set it to 1
private Random random; // create a variable to store the Random instance
private DiceGame diceGame; // store a DiceGame instance
public Game(DiceGame game) // constructor for passing the DiceGame instance
{
this.diceGame = game; // set the global diceGame to game
this.random = new Random(); // initialize the random variable
initializeGame(); // call the initializeGame method
diceGame.updateInterface(this); // update the interface
}
private void initializeGame()
{
turn = Turn.PLAYER_1; // set the turn to player 1's turn
players.add(new Player(Turn.PLAYER_1, 0)); // add a new player to the players list
players.add(new Player(Turn.PLAYER_2, 0)); // add a new player to the players list
}
public void rollDice()
{
diceGame.getRollButton().setEnabled(false); // set the roll button to be disabled
diceGame.clearInterface(); // clear the interface
if (turn.equals(Turn.PLAYER_1)) // if it's the first players turn
turn = Turn.PLAYER_2; // set it to the second players turn
else
turn = Turn.PLAYER_1; // set it to the first players turn
try // try...
{
Thread.sleep(100); // sleep for a small amount of time
}
catch (InterruptedException ex) // catch an exception
{
System.out.println("Problem with the timer"); // print to console
}
int sum = 0; // keep track of the sum
int numOne = getRandomInt(); // +1 to make sure the value is not 1
int numTwo = getRandomInt(); // +1 to make sure the value is not 1
sum = numOne+numTwo; // add the two numbers together
diceGame.getRollButton().setEnabled(true); // set the roll button to be enabled
if (challenging) // if this is a challenging round
{
if (sum > previousSum) // if the current sum is greater than the previous sum
{
diceGame.writeToOutcomeLabels(numOne, numTwo, "Player 2 wins that round!"); // update the UI
for (Player p : players) // loop over all players in the players list
if (p.getTurn().equals(Turn.PLAYER_2)) // if the player is player 2
p.increaseScore(); // increase their score by 1
}
else // otherwise...
{
diceGame.writeToOutcomeLabels(numOne, numTwo, "Player 1 wins that round!"); // update the UI
for (Player p : players) // loop over all players in the players list
if (p.getTurn().equals(Turn.PLAYER_1)) // if it's player 1
p.increaseScore(); // increase their score by 1
}
diceGame.updateInterface(this); // update the UI
challenging = false; // set challenging to false
}
else // otherwise
{
round+=1; // increase the round counter
determineOutcome(numOne, numTwo, sum); // call the determineOutcome method
}
}
private int getRandomInt()
{
return this.random.nextInt(6)+1; // +1 to make sure the value is not 0
}
private void determineOutcome(int numOne, int numTwo, int sum)
{
previousSum = sum; // set the previous sum to the sum
String text = ""; // create a text variable
switch(sum) // switch over the value
{
case 7: // if 7 or 11 do this:
case 11:
// player 1 wins
text = "Player 1 wins that round!"; // set the text variable
for (Player p : players) // loop over all players in the players list
if (p.getTurn().equals(Turn.PLAYER_1)) // if the player is player 1
p.increaseScore(); // increase their score by 1
break;
case 2: // if 2, 3, or 12 do this:
case 3:
case 12:
// draw, each player gets 1 point
for (Player p : players) // loop over all players in the players list
p.increaseScore(); // increase their score by 1
text = "It's a draw!"; // say it's a draw
break;
default: // if any other number do this:
challenging = true; // set challenging to true
text = "<html>Challenging number! Player 2's turn, <br>they must score higher than " + previousSum + "</html>"; // set the text variable
// the number is a challenging number
break;
}
if (challenging) // if challenging is true
{
turn = Turn.PLAYER_2; // set it to player 2's turn
}
diceGame.updateInterface(this); // update the interface
diceGame.writeToOutcomeLabels(numOne, numTwo, text); // update the outcome labels
}
public List<Player> getPlayers()
{
return this.players; // return the players list
}
public Turn getTurn()
{
return this.turn; // return the turn variable
}
public int getRoundNumber()
{
return this.round; // return the round variable
}
}
/*
* 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.
*/
package com.jordan.dicegame;
/**
*
* @author Jordan
*/
public class Player
{
private Turn turn; // create a variable to store which turn this player goes on
private int score; // create a variable to store the score of this player
public Player(Turn turn, int score)
{
this.turn = turn; // set the global turn to turn
this.score = score; // set the global score to score
}
public Turn getTurn()
{
return this.turn; // return the turn variable
}
public int getScore()
{
return this.score; // return the score variable
}
public void increaseScore()
{
this.score += 1; // increase the score variable by 1
}
}
/*
* 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.
*/
package com.jordan.dicegame;
/**
*
* @author Jordan
*/
public enum Turn
{
PLAYER_1("It's player 1's turn!", "Player 1"), // create an Enum value with a string
PLAYER_2("It's player 2's turn!", "Player 2"); // create an Enum value with a string
private String s; // create a string variable to initialize each enum value
private String name; // create a st ring variable to initialize each enum value
Turn(String s, String name) // constructor to determine the structure of the enum values
{
this.s = s; // set the global s to s
this.name = name; // set the global name to name
}
public String getString()
{
return s; // return the string accociated with the enum value
}
public String getName()
{
return this.name; // return the string name
}
}
com\jordan\dicegame\Player.class
com\jordan\dicegame\Game.class
com\jordan\dicegame\Turn.class
com\jordan\dicegame\DiceGame$1.class
com\jordan\dicegame\DiceGame$2.class
com\jordan\dicegame\DiceGame.class
E:\Documents\NetBeansProjects\DiceGame_Jordan_Dalby\src\main\java\com\jordan\dicegame\DiceGame.java
E:\Documents\NetBeansProjects\DiceGame_Jordan_Dalby\src\main\java\com\jordan\dicegame\Player.java
E:\Documents\NetBeansProjects\DiceGame_Jordan_Dalby\src\main\java\com\jordan\dicegame\Game.java
E:\Documents\NetBeansProjects\DiceGame_Jordan_Dalby\src\main\java\com\jordan\dicegame\Turn.java
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