Guest User

Untitled

a guest
Feb 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. public class GameOfDice {
  2.  
  3.     //Function, which calculates the results of the players
  4.     public static int Game(int pl11, int pl12, int pl21, int pl22) {   
  5.         //rolled numbers of player 1 are given to the function as variables pl11 and pl12,
  6.         //rolled numbers of player 2 are given to the function as variables pl21 and pl22.
  7.        
  8.         int result = -1; // result = 0 if player 1 wins, result = 1 if player 2 wins, result = 2 if game is draw
  9.         int[] player1 = new int[2]; //Array to save the rolled numbers of player 1
  10.         int[] player2 = new int[2]; //Array to save the rolled numbers of player 2
  11.         int sum1 = 0; // Sum of Numbers of player 1  
  12.         int sum2 = 0; // Sum of Numbers of player 2
  13.        
  14.         // Saving rolled numbers in arrays player1 and player2  
  15.         // You *must* use these given arrays! Other solutions are not allowed!
  16.  
  17.         player1[0] = pl11;
  18.         player1[1] = pl12;
  19.        
  20.         player2[0] = pl21;
  21.         player2[1] = pl22;
  22.  
  23.         // Calculating sum1 and sum2 - use numbers saved in player1 and player2!
  24.        
  25.         sum1 = player1[0] + player1[1];
  26.         sum2 = player2[0] + player2[1];
  27.        
  28.         // Output of sum1 and sum2 - nothing to do in this line, only for Information
  29.         System.out.print("player 1: " + sum1 + ", player 2: " + sum2);
  30.        
  31.         if (sum1 > sum2) { //TODO: add the right condition into round brackets, if player 1 wins
  32.             // set result to 0
  33.            
  34.             result = 0;
  35.            
  36.             System.out.println(" - player 1 won");
  37.    
  38.         } else if (sum2 > sum1) { //TODO: add the right condition into round brackets, if player 2 wins
  39.             // set result to 1
  40.            
  41.             result = 1;
  42.            
  43.             System.out.println(" - player 2 won");
  44.            
  45.         } else {
  46.             // game is draw, set result to 2
  47.            
  48.             result = 2;
  49.            
  50.             System.out.println(" - game is draw");
  51.            
  52.         }
  53.         // returns the result
  54.         return result; //TODO
  55.     }
  56.  
  57.     /* Important! Do not modify any code below this line! */
  58.     public static void main(String[] args) {
  59.  
  60.         try {
  61.             if (args.length != 4 || Integer.parseInt(args[0]) > 6 || Integer.parseInt(args[1]) > 6 || Integer.parseInt(args[2]) > 6 || Integer.parseInt(args[3]) > 6 || Integer.parseInt(args[0]) < 1 || Integer.parseInt(args[1]) < 1 || Integer.parseInt(args[2]) < 1 || Integer.parseInt(args[3]) < 1) {
  62.                 throw new IllegalArgumentException("Caution!");
  63.             }
  64.             else {
  65.                 Game(Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2]), Integer.parseInt(args[3]));
  66.  
  67.             }
  68.         } catch (Throwable t) {
  69.             System.out.println("You used wrong arguments!");
  70.             System.out.println("You need exactly 4 numbers between 1 and 6 to play!");
  71.         }
  72.     }
  73. }
Add Comment
Please, Sign In to add comment