简体   繁体   中英

Two corners coin game in java

The 2 corners coin game receive an array. The goal of the game is accumulate the number of points (values of the elements in the array) the most. you can take points only from the 2 corners of the array.

There are 2 conditions for the game:

1) The first player (amir) would never lose (He will win or finish with a tie) and he will not necessarily choose the biggest edge.

2) The second player (Tamara) will always take the points at the highest corner.

My Output:

amir took 16
tamara took 23
amira took 30
tamara took 15
amir took 19
tamara took 21
amir took 14
tamara took 13
Final Score:
amir total 79
tamara total 72

Expected Output:

Amir took 16
Tamara took 23
Amir took 30
Tamara took 15
Amir took 19
Tamara took 21
Amir took 13
Tamara took 14
Final Score:
Amir total 78
Tamara total 73

The problem:

-amir will select 16 so tamara will must select the 23 so in the next turn he will select 30 (becuase tamara will always select the biggest corner).

-amir in the last turn will select 13 and not 14 because he is already won so he do not care about the point/coin value.

The reasons why amir choose 13 and not 14: 1. because tamara will always select the biggest "end" 2. because he is not loosing this game (65 vs 59) so he will choose 13 and not 14 this is strategy - just not loosing (can finish with tie or win the game) he can plan all his move because he can see the array from the begging and he want to not loose this game with less move

-amir knows from the first turn what is next move because he can not lose in this game (he can finish the game as a winner or finish with the same score of tamara)

-Amir can calculate in advance the full moves tree of the game and how Tamar can respond to each move and then how he will respond to each move. The problem with such a solution could be a huge tree (the number of different games that Amir and Tamara can play is 2 ^ K - and if K is bigger then the powerful computer will take trillions of years). Therefore, an effective solution such as this is required in this game. and requires Amir to carry out a few actions for work out a strategy for himself

The array:

int[] array1 = {16,23,30,14,13,21,19,15};
TEST.coingame(array1);
System.out.println();

My code:

public static void coingame(int[] arr)
{ 
   int n = arr.length;  
   int i = 0, j = n-1,p1=0,p2=0,totalP2=0,totalP1=0; 

   while(j > i){

      if(arr[j]+arr[j-1]>arr[i]+arr[i+1]){
      p1 = arr[j];--j;
         if(arr[j]>arr[i]){
            p2=arr[j];--j;
         }else{  
            p2=arr[i];++i;
         } 
      }else{ 
         p1 = arr[i];++i;
         if(arr[j]>arr[i]){
            p2=arr[j];--j;
         }else{  
            p2=arr[i];++i;
         } 
      }

      System.out.println ("amir took "+p1);totalP1+=p1;
      System.out.println ("tamara took "+p2);totalP2+=p2;
   }

   System.out.println ("Final Score:");
   System.out.println ("amir total "+totalP1);
   System.out.println ("tamara total "+totalP2);
} 

Edit: (the answer of Akshay Batra)

public static int[] pickByAmir(int[] coins, int amirTook, int tamaraTook, int start, int end) {
    if(start>end) {
        int[] res = new int[2];
        res[0] = amirTook;
        res[1] = tamaraTook;
        return res;
    }
    int[] a = new int[2];
    a[0] = amirTook;
    a[1] = tamaraTook;
    if(coins.length==0)
        return a;
    amirTook = coins[start];
    coins = pickByTamara(coins, ++start , end);
    tamaraTook = coins[start];
    a = pickByAmir(coins, amirTook+a[0], tamaraTook+a[1], ++start, end);
    int[] b = new int[2];
    b[0] = amirTook;
    b[1] = tamaraTook;
    if(a[0]<a[1]){
        amirTook = coins[end];
        coins = pickByTamara(coins, start, --end);
        b = pickByAmir(coins, amirTook+b[0], tamaraTook+b[1], ++start, end);

        if(a[0]<b[0])
            return b;
    }
        System.out.println ("Amir took "+amirTook);
        System.out.println ("Tamara took "+tamaraTook);
    return a;
}
public static int[] pickByTamara(int[] coins, int start, int end){
    return coins[start] > coins[end] ? coins : swapArray(coins, start, end);
}

public static int[] swapArray(int[] coins, int start, int end) {
    int temp = coins[start];
    coins[start] = coins[end];
    coins[end] = temp;
    return coins;
}

public static void coingame(int[] arr) {
    int[] a = pickByAmir(arr, 0, 0, 0, arr.length-1);
    System.out.println ("Final Score: ");
    System.out.println ("Amir total: "+a[0]);
    System.out.println ("Tamara total: "+a[1]);
    }

this code works with your inputs, try it with different inputs and see if it breaks, if it does, optimise your solution

call this way from caller method int[] a = pickByAmir(array1, 0, 0, 0, array1.length-1);

a[0] will have amir's total and a[1] will have tamara's

int[] pickByAmir(int[] coins, int amirTook, int tamaraTook, int start, int end) {
    if(start>end) {
        int[] res = new int[2];
        res[0] = amirTook;
        res[1] = tamaraTook;
        return res;
    }
    int[] a = new int[2];
    a[0] = amirTook;
    a[1] = tamaraTook;
    if(coins.length==0)
        return a;
    amirTook = coins[start];
    coins = pickByTamara(coins, ++start , end);
    tamaraTook = coins[start];
    a = pickByAmir(coins, amirTook+a[0], tamaraTook+a[1], ++start, end);
    int[] b = new int[2];
    b[0] = amirTook;
    b[1] = tamaraTook;
    if(a[0]<a[1]){
        amirTook = coins[end];
        coins = pickByTamara(coins, start, --end);
        b = pickByAmir(coins, amirTook+b[0], tamaraTook+b[1], ++start, end);
        if(a[0]<b[0])
            return b;
    }
    return a;
}
int[] pickByTamara(int[] coins, int start, int end){
    return coins[start] > coins[end] ? coins : swapArray(coins, start, end);
}

int[] swapArray(int[] coins, int start, int end) {
    int temp = coins[start];
    coins[start] = coins[end];
    coins[end] = temp;
    return coins;
}

GOOD LUCK

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM