Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * The game starts
- * By the rules - Amir always starts and Tamar chooses the highest value
- * from the edges of the array
- *@while as long as we have not reached the last index in the array
- * Since sumEven < sumOdd - Amir picks the odd indices
- * Since Amir picks the odd indices, Tamar must choose the even indices.
- * In case we reached the last index the value is added to Tamar's score automatically because it is her turn
- * anyway since Amir has started and the value is printed to us.
- */
- while (start < end) {
- if (start % 2 != 1) { //Amir's turn - He can choose from the left side
- amirScore += arr[start];
- System.out.println("Amir took " + arr[start]);
- start++;
- } else if (end % 2 != 1) { //Amir's turn - He can choose from the right side
- amirScore += arr[end];
- System.out.println("Amir took " + arr[end]);
- end--;
- }
- if (start % 2 == 1) { //Tamar's turn - She can choose from the left side
- tamarScore += arr[start];
- System.out.println("Tamar took " + arr[start]);
- start++;
- } else if (end % 2 == 1) { //Tamar's turn - She can choose from the right side
- tamarScore += arr[end];
- System.out.println("Tamar took " + arr[end]);
- end--;
- }
- if (start == end) { //In case we reached the last index
- tamarScore += arr[end];
- System.out.println("Tamar took " + arr[end]);
- end++;
- }
- } //while loop
- } // if #2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement