Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dicegame;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Random;
- import java.util.Scanner;
- /**
- *
- * @author Zaki
- */
- public class DiceGame {
- static Scanner L = new Scanner(System.in);
- static Random rand = new Random();
- static int diceroll;
- static int dice1;
- static int dice2;
- static int dice3 = 0;
- static int p1Score;
- static int p2Score;
- static int roundScore;
- static int p1Grand;
- static int p2Grand;
- static int p1suddendeath;
- static int p2suddendeath;
- public static void main(String[] args) {
- int option;
- System.out.println("Welcome to the Dice Game!");
- fileReader();
- do {
- System.out.println("______________________________");
- System.out.println("Main Menu");
- System.out.println("Choose from the following options: \n 1 Register to play \n 2 Log in to play \n 3 Hi-Scores \n 4 End game");
- System.out.println("Enter the preferred number");
- option = L.nextInt();
- L.skip("\n");
- switch (option) {
- case 1:
- Register();
- break;
- case 2:
- LogIn();
- break;
- case 3:
- HiScore();
- break;
- case 4:
- EndGame();
- break;
- default:
- System.out.println("Invalid Option");
- break;
- }
- } while (option > 4 || option <= 3);
- }
- private static void Register() {
- System.out.println("_______________________________");
- System.out.println("Register to play \n Please enter your Username:");
- System.out.println("Please enter your Password");
- }
- private static void LogIn() {
- System.out.println("Player 1 enter your username:");
- System.out.println("Player 2 enter your password:");
- System.out.println("Player 1 enter your username:");
- System.out.println("Player 2 enter your password:");
- round();
- }
- private static void HiScore() {
- System.out.println("INSERT TABLE HERE BRO");
- }
- private static void EndGame() {
- System.out.println("Thank you for playing the Dice Game!");
- }
- private static void game() {
- System.out.println("Lets play! \n Player 1 Press any key to roll the dice");
- char roll = L.nextLine().charAt(0);
- scorecalc();
- System.out.println("Player 2 turn \n Press any key");
- char roll2 = L.nextLine().charAt(0);
- scorecalc2();
- }
- public static int diceRoll() {
- return diceroll = rand.nextInt(6) + 1;
- }
- private static int oddEven(int Score) {
- if (Score % 2 == 0) {
- Score = Score + 10;
- } else {
- Score = Score - 5;
- }
- return Score;
- }
- private static void round() {
- for (int i = 1; i < 6; i++) {
- System.out.println("--------------Round " + i + "---------------");
- game();
- SuddenDeath();
- }
- }
- private static void scorecalc() {
- dice1 = diceRoll();
- dice2 = diceRoll();
- System.out.println("You rolled a " + dice1 + " and a " + dice2);
- if (dice1 == dice2) {
- System.out.println("You rolled a double");
- dice3 = diceRoll();
- System.out.println("The score for the third roll is: " + dice3);
- }
- p1Score = dice1 + dice2 + dice3;
- p1Score = oddEven(p1Score);
- p1Score = BelowZero(p1Score);
- System.out.println("Your Score is: " + p1Score);
- p1Grand = p1Grand + p1Score;
- System.out.println("Player1 Grand Total is: " + p1Grand);
- }
- private static void scorecalc2() {
- dice1 = diceRoll();
- dice2 = diceRoll();
- System.out.println("You rolled a " + dice1 + " and a " + dice2);
- if (dice1 == dice2) {
- System.out.println("You rolled a double");
- dice3 = diceRoll();
- System.out.println("The score for the third roll is: " + dice3);
- }
- p2Score = dice1 + dice2 + dice3;
- p2Score = oddEven(p2Score);
- p2Score = BelowZero(p2Score);
- System.out.println("Your Score is: " + p2Score);
- p2Grand = p2Grand + p2Score;
- System.out.println("Player1 Grand Total is: " + p2Grand);
- }
- private static int BelowZero(int score) {
- if (score < 0) {
- score = 0;
- }
- return score;
- }
- private static void SuddenDeath() {
- if (p1Grand == p2Grand) {
- char trigger = L.nextLine().charAt(0);
- p1suddendeath = diceRoll();
- p2suddendeath = diceRoll();
- do {
- if (p1suddendeath > p2suddendeath) {
- System.out.println("Player 1 Wins!");
- } else if (p2suddendeath > p1suddendeath) {
- System.out.println("Player 2 Wins!");
- } else if (p2suddendeath == p1suddendeath) {
- System.out.println("Sudden Death Again...");
- }
- } while (p1Grand == p2Grand);
- }
- }
- private static void fileReader() {
- try {
- BufferedReader br = new BufferedReader((new FileReader("N:/PlayerData.txt")));
- String s;
- while ((s = br.readLine()) != null) {
- }
- br.close();
- System.out.println("Data read Successfully");
- } catch (IOException e) {
- System.out.println("Error: " + e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement