Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GameOfDice {
- //Function, which calculates the results of the players
- public static int Game(int pl11, int pl12, int pl21, int pl22) {
- //rolled numbers of player 1 are given to the function as variables pl11 and pl12,
- //rolled numbers of player 2 are given to the function as variables pl21 and pl22.
- int result = -1; // result = 0 if player 1 wins, result = 1 if player 2 wins, result = 2 if game is draw
- int[] player1 = new int[2]; //Array to save the rolled numbers of player 1
- int[] player2 = new int[2]; //Array to save the rolled numbers of player 2
- int sum1 = 0; // Sum of Numbers of player 1
- int sum2 = 0; // Sum of Numbers of player 2
- // Saving rolled numbers in arrays player1 and player2
- // You *must* use these given arrays! Other solutions are not allowed!
- player1[0] = pl11;
- player1[1] = pl12;
- player2[0] = pl21;
- player2[1] = pl22;
- // Calculating sum1 and sum2 - use numbers saved in player1 and player2!
- sum1 = player1[0] + player1[1];
- sum2 = player2[0] + player2[1];
- // Output of sum1 and sum2 - nothing to do in this line, only for Information
- System.out.print("player 1: " + sum1 + ", player 2: " + sum2);
- if (sum1 > sum2) { //TODO: add the right condition into round brackets, if player 1 wins
- // set result to 0
- result = 0;
- System.out.println(" - player 1 won");
- } else if (sum2 > sum1) { //TODO: add the right condition into round brackets, if player 2 wins
- // set result to 1
- result = 1;
- System.out.println(" - player 2 won");
- } else {
- // game is draw, set result to 2
- result = 2;
- System.out.println(" - game is draw");
- }
- // returns the result
- return result; //TODO
- }
- /* Important! Do not modify any code below this line! */
- public static void main(String[] args) {
- try {
- 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) {
- throw new IllegalArgumentException("Caution!");
- }
- else {
- Game(Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2]), Integer.parseInt(args[3]));
- }
- } catch (Throwable t) {
- System.out.println("You used wrong arguments!");
- System.out.println("You need exactly 4 numbers between 1 and 6 to play!");
- }
- }
- }
Add Comment
Please, Sign In to add comment