爱吃羊的猫猫

导航

【计算机数学】DICE GAME

实验项目描述:

In this series of three programming tasks, we will implement together a program that will play optimally in a tricky dice game! Your program will be given a list of dices and will decide who chooses the dice first (you or your opponent).

When the dices are chosen, we will simulate 10000 throws. Each time your number is greater, you get $1 from your opponent. Conversely, each time your number is smaller, you pay $1 to your opponent.

Your ultimate goal is to implement a program that always wins in such a simulation.

1. First Task: Compare Two Dices

Implement a function that takes two dices as input and computes two values: the first value is the number of times the first dice wins (out of all possible 36 choices), the second value is the number of times the second dice wins. We say that a dice wins if the number on it is greater than the number on the other dice.

To debug your implementation, use the following test cases:

Sample 1

Input: dice1 = [1, 2, 3, 4, 5, 6], dice2 = [1, 2, 3, 4, 5, 6]

Output: (15, 15)

Sample 2

Input: dice1 = [1, 1, 6, 6, 8, 8], dice2 = [2, 2, 4, 4, 9, 9]

Output: (16, 20)

2. Second Task: Is there the Best Dice?

Now, your goal is to check whether among the three given dices there is one that is better than the remaining two dices.

Implement a function that takes a list of dices and checks whether there is dice (in this list) that is better than all other dices. We say that a dice is better than another one, if it wins more frequently (that is, out of all 36 possibilities, it wins in a cases, while the second one wins in b cases, and a>b). If there is such a dice, return its (0-based) index. Otherwise, return -1.

Use the following datasets for debugging:

Sample 1

Input: [[1, 1, 6, 6, 8, 8], [2, 2, 4, 4, 9, 9], [3, 3, 5, 5, 7, 7]]

Output: -1

Sample 2

Input: [[1, 1, 2, 4, 5, 7], [1, 2, 2, 3, 4, 7], [1, 2, 3, 4, 5, 6]]

Output: 2

Sample 3

Input: [[3, 3, 3, 3, 3, 3], [6, 6, 2, 2, 2, 2], [4, 4, 4, 4, 0, 0], [5, 5, 5, 1, 1, 1]]

Output: -1

3. Third Task: Implement a Strategy

You are now ready to play!

Implement a function that takes a list of dices (possibly more than three) and returns a strategy. The strategy is a dictionary:

If, after analyzing the given list of dices, you decide to choose a dice first, set strategy["choose_first"] to True and set strategy["first_dice"] to be the (0-based) index of the dice you would like to choose

If you would like to be the second one to choose a dice, set strategy["choose_first"] to False. Then, specify, for each dice that your opponent may take, the dice that you would take in return. Namely, for each i from 0 to len(dices)-1, set strategy[i] to an index j of the dice that you would take if the opponent takes the i-th dice first.

Use the following datasets for debugging:

Sample 1

Input: [[1, 1, 4, 6, 7, 8], [2, 2, 2, 6, 7, 7], [3, 3, 3, 5, 5, 8]]

Output: {'choose_first': False, 0: 1, 1: 2, 2: 0}

Sample 2

Input: [[4, 4, 4, 4, 0, 0], [7, 7, 3, 3, 3, 3], [6, 6, 2, 2, 2, 2], [5, 5, 5, 1, 1, 1]]

Output: {'choose_first': True, 'first_dice': 1}

Note that your answers do not have to coincide with the answers above. First, the order of elements does not matter in the dictionary. Second, the dictionary might contain extra information that is not required in the statement of the problem. For example, {0: 3, 'first_dice': 1, 'choose_first': True} is also a correct output in Sample 2.

Answers:

1. First Task: Compare Two Dices

 1 #include <stdio.h>
 2 int main()
 3 {
 4     int a[6],b[6],c,d;
 5     printf("dice1=");
 6     scanf("%d,%d,%d,%d,%d,%d",&a[0],&a[1],&a[2],&a[3],&a[4],&a[5]);
 7     printf("dice2=");
 8     scanf("%d,%d,%d,%d,%d,%d",&b[0],&b[1],&b[2],&b[3],&b[4],&b[5]);
 9     int i,j,m=0,n=0;
10     for(i=0;i<6;i++)
11     {
12         for(j=0;j<6;j++)
13         {
14             if(a[i]<b[j])
15             {
16                 m++;
17             }
18             else if(a[i]>b[j])
19             {
20                 n++;
21             }
22         }
23     }
24     printf("%d,%d",n,m);
25     return 0;
26 }

2. Second Task: Is there the Best Dice?

 1 #include<stdio.h>
 2 int compare(int a[],int b[])
 3 {
 4     int m=0,n=0,i,j;
 5     for(i=0;i<6;i++)
 6     {
 7         for(j=0;j<6;j++)
 8         {
 9             if(a[i]<b[j])
10             {
11                 m++;
12             }
13             else if(a[i]>b[j])
14             {
15                 n++;
16             }
17         }
18     }
19     if(m<n)
20     {
21         return 0;
22     }
23     if(m>n)
24     {
25         return 1;
26     }
27     else
28     {
29         return 2;
30     }
31 }
32 int main()
33 {
34     int a[6],b[6],c[6],d[6];
35     printf("dice1=");
36     scanf("%d,%d,%d,%d,%d,%d",&a[0],&a[1],&a[2],&a[3],&a[4],&a[5]);
37     printf("dice2=");
38     scanf("%d,%d,%d,%d,%d,%d",&b[0],&b[1],&b[2],&b[3],&b[4],&b[5]);
39     printf("dice3=");
40     scanf("%d,%d,%d,%d,%d,%d",&c[0],&c[1],&c[2],&c[3],&c[4],&c[5]);
41     printf("dice4=");
42     scanf("%d,%d,%d,%d,%d,%d",&d[0],&d[1],&d[2],&d[3],&d[4],&d[5]);
43     int m,n,o,p,q,r;
44     m=compare(a,b);
45     n=compare(a,c);
46     o=compare(a,d);
47     p=compare(b,c);
48     q=compare(b,d);
49     r=compare(c,d);
50     if(m==0&&n==0&&o==0)
51     {
52         printf("0\n");
53     }
54     else if(m==1&&p==0&&q==0)
55     {
56         printf("1\n");
57     }
58     else if(n==1&&p==1&&q==0)
59     {
60         printf("2\n");
61     }
62     else if(o==1&&q==1&&r==1)
63     {
64         printf("3\n");
65     }
66     else 
67     {
68         printf("-1\n");
69     }
70     return 0;
71 }

 

 

 

 

 

 

3. Third Task: Implement a Strategy

  1 #include<stdio.h>
  2 int compare(int a[],int b[])
  3 {
  4     int m=0,n=0,i,j;
  5     for(i=0;i<6;i++)
  6     {
  7         for(j=0;j<6;j++)
  8         {
  9             if(a[i]<b[j])
 10             {
 11                 m++;
 12             }
 13             else if(a[i]>b[j])
 14             {
 15                 n++;
 16             }
 17         }
 18     }
 19     if(m<n)
 20     {
 21         return 0;
 22     }
 23     if(m>n)
 24     {
 25         return 1;
 26     }
 27     else
 28     {
 29         return 2;
 30     }
 31 }
 32 int main()
 33 {
 34     int a[6],b[6],c[6],d[6],e,f;
 35     printf("dice1=");
 36     scanf("%d,%d,%d,%d,%d,%d",&a[0],&a[1],&a[2],&a[3],&a[4],&a[5]);
 37     printf("dice2=");
 38     scanf("%d,%d,%d,%d,%d,%d",&b[0],&b[1],&b[2],&b[3],&b[4],&b[5]);
 39     printf("dice3=");
 40     scanf("%d,%d,%d,%d,%d,%d",&c[0],&c[1],&c[2],&c[3],&c[4],&c[5]);
 41     printf("dice4=");
 42     scanf("%d,%d,%d,%d,%d,%d",&d[0],&d[1],&d[2],&d[3],&d[4],&d[5]);
 43     printf("choose first:");
 44     scanf("%d",&e);
 45     switch(e)
 46     {
 47         case 1:
 48         {
 49             int m,n,o,p,q,r;
 50             m=compare(a,b);
 51             n=compare(a,c);
 52             o=compare(a,d);
 53             p=compare(b,c);
 54             q=compare(b,d);
 55             r=compare(c,d);
 56             if(m==0&&n==0&&o==0)
 57             {
 58                 printf("firstdice:0\n");
 59             }
 60             else if(m==1&&p==0&&q==0)
 61             {
 62                 printf("firstdice:1\n");
 63             }
 64             else if(n==1&&p==1&&q==0)
 65             {
 66                 printf("firstdice:2\n");
 67             }
 68             else if(o==1&&q==1&&r==1)
 69             {
 70                 printf("firstdice:3\n");
 71             }
 72             else 
 73             {
 74                 printf("firstdice:-1\n");
 75             }
 76             break;
 77         }
 78         case 0:
 79         {
 80             printf("0:");
 81             f=compare(a,b);
 82             if(f==1)
 83             {
 84                 printf("1");
 85             }
 86             f=compare(a,c);
 87             if(f==1)
 88             {
 89                 printf("2");
 90             }
 91             printf(",");
 92             printf("1:");
 93             f=compare(b,c);
 94             if(f==1)
 95             {
 96             printf("2");
 97             }
 98             f=compare(b,a);
 99             if(f==1)
100             {
101                 printf("0");
102             }
103             printf(",");
104             printf("2:");
105             f=compare(c,a);
106             if(f==1)
107             {
108                 printf("0");
109             }
110             f=compare(c,b);
111             if(f==1)
112             {
113                 printf("1");
114             }
115             printf("\n");
116             break;
117         }
118         default:break;
119     }
120     return 0;
121 }

 

Summary:

骰子游戏说明寻找最优解的过程是一个把所有解进行循环比较的过程,如果通过比较后有一个解比其他解都要优,就能选出最优解,如果每一个解都能找到一个解比它更优,那就不存在最优解,哪个解更优是相对的。

 

posted on 2019-10-24 12:48  爱吃羊的猫猫  阅读(1261)  评论(0)    收藏  举报