Commit 01cd33ce authored by Tom's avatar Tom

Dice Game Resit

parent 17fd45f1
......@@ -27,7 +27,7 @@ public class DiceGameClass extends javax.swing.JFrame {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
//Here I set the text that is displayed inside of the various labels ant buttons
round.setText("Round 1");
round.setText("Round 0");
roll.setText("Roll");
roll.addActionListener(new java.awt.event.ActionListener() {
......@@ -36,6 +36,7 @@ public class DiceGameClass extends javax.swing.JFrame {
}
});
dice1.setText("Dice 1 =");
dice2.setText("Dice 2 =");
......@@ -105,31 +106,48 @@ public class DiceGameClass extends javax.swing.JFrame {
pack();
}
int clicked;
int p1p = 0;
int p2p = 0;
int r = 0;
//when the player clicks the roll button
private void rollActionPerformed(java.awt.event.ActionEvent evt) {
//Here i set up the variables I will be using
int d1 = 0;
int d2 = 0;
private static void rollActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
//Here I tried to replace the text of the dice1 label
int d1;
public void dice() {
int d3 = 0;
int d4 = 0;
int w = 5;
dice1.setText(String.valueOf(d1));
clicked++;
}
public static void main(String args[]) {
//Here i set up the variables I will be using
int d1 = 0;
int d2 = 0;
int r = 0;
int proceed;
//Here is a while statement, it finds the current round, if that round is under 3 the loop will run
while(r < 3) {
r = r + 1;
r++;
roll.setText("Roll");
//Makes sure that the user has clicked the roll button the same amount of times as the round counter, this makes it so that all three rounds are not played out simultaneously without any user input
if (clicked == r || clicked > r) {
System.out.print(r + " ");
//The following if statements use the round counter to figure out who's turn it is and change the turn text to that player. They also figure out if the game has ended or not
if(r == 1 || r == 3 || r == 5) {
turn.setText("Player 1's Turn");
}
if(r == 2 || r == 4 || r == 6) {
turn.setText("Player 2's Turn");
}
if(r == 7) {
turn.setText("Game Over");
textArea.setText("Game Over\nPlayer 1 Scored " + p1p + "\nPlayer 2 scored " + p2p);
return;
}
Random rand = new Random();
......@@ -149,14 +167,125 @@ public class DiceGameClass extends javax.swing.JFrame {
//Here I added 1 to the result to reach the minimum value of 1, this was required because d2's default value is 0
d2 += 1;
//Here I added a variable called overall, this add both dice together so that I can calculate that players overall score.
int overall = d1 + d2;
round.setText("Round: " + r);
//These lines allow me to display the score of both dice and the score of both added together
dice1.setText("Dice 1 = " + d1);
dice2.setText("Dice 1 = " + d2);
//Here is an if statement to find out if the overall throw is worth 7 or 11, if i is the first player wins and gains 5 points.
if(overall == 7 || overall == 11) {
//Displays the overall score of that throw
textArea.setText("Overall Dice Score: " + overall + "\n\nCurrent Player wins and earns 5 points!");
//Add 5 points to player's total score after working out who is the current player
if(r == 1 || r == 3 || r == 5) {
p1p = p1p + w;
}
if(r == 2 || r == 4 || r == 6) {
p2p = p2p + w;
}
p1ScoreTable.setText("Player 1 = " + p1p);
p2ScoreTable.setText("Player 2 = " + p2p);
}
else if(overall == 2 || overall == 3 || overall == 12) {
//Displays the overall score of that throw
textArea.setText("Overall Dice Score: " + overall + "\n\nIt's a draw! Both Players earn 5 points!");
//Adds 5 points to both players total score
p1p = p1p + w;
p2p = p2p + w;
p1ScoreTable.setText("Player 1 = " + p1p);
p2ScoreTable.setText("Player 2 = " + p2p);
}
else if(overall == 4 || overall == 5 || overall == 6 || overall == 8 || overall == 9 || overall == 10) {
//Here I changed the rolls button text to "Roll_2", I did this so I could find out if the player rolls for a second time when a challenge...
//number is activated
d3 = rand.nextInt(6);
//Here I added 1 to the result to reach the minimum value of 1, this was required because d1's default value is 0
d3 += 1;
System.out.print("Player 1 rolled a" + d1 + " and " + d2 + "!\nOverall thats " + (d1 + d2) + "!");
// This gets a random number between 0 and 6
d4 = rand.nextInt(6);
//Here I added 1 to the result to reach the minimum value of 1, this was required because d2's default value is 0
d4 += 1;
//Here I added a variable called overall, this add both dice together so that I can calculate that players overall score.
int overall2 = d3 + d4;
//if the score of the first player is greater or equal to the second player, the first player earns 5 points
if(overall == overall2 || overall > overall2) {
//Figures out who the player is and gives the deserved points
if(r == 1 || r == 3 || r == 5) {
p1p = p1p + w;
}
else if(r == 2 || r == 4 || r == 6) {
p2p = p2p + w;
}
textArea.setText("CHALLENGE NUMBER\n\nOther Player Rolled: " + overall2 + "\nThis is less than the current players " + overall +
"\n\nCurrent Player earns 5 points!");
}
else if(overall < overall2) {
if(r == 1 || r == 3 || r == 5) {
p2p = p2p + w;
}
else if(r == 2 || r == 4 || r == 6) {
p1p = p1p + w;
}
textArea.setText("CHALLENGE NUMBER\n\nOther Player Rolled: " + overall2 + "\nThis is more than the current players " + overall +
"\n\nOther Player earns 5 points!");
}
p1ScoreTable.setText("Player 1 = " + p1p);
p2ScoreTable.setText("Player 2 = " + p2p);
}
}
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
......
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