/* Rock-Scissors-Paper Game (Janken) Replicator Equation */ #include#include #define h 0.01 double FUNC1(); double FUNC2(); main() { int j; /*j; step number*/ double ny1=0.0,y1=0.2; double ny2=0.0,y2=0.5; double nt=0.0,t=0.0; double k11=0.0,k12=0.0,k21=0.0,k22=0.0, k31=0.0,k32=0.0,k41=0.0,k42=0.0; for(j=0; j<=2000;j++) { k11=h*FUNC1(t,y1,y2); k12=h*FUNC2(t,y1,y2); k21=h*FUNC1(t+0.5*h,y1+0.5*k11,y2+0.5*k12); k22=h*FUNC2(t+0.5*h,y1+0.5*k11,y2+0.5*k12); k31=h*FUNC1(t+0.5*h,y1+0.5*k21,y2+0.5*k22); k32=h*FUNC2(t+0.5*h,y1+0.5*k21,y2+0.5*k22); k41=h*FUNC1(t+h,y1+k31,y2+k32); k42=h*FUNC2(t+h,y1+k31,y2+k32); ny1=y1+0.166666667*(k11+2.0*k21+2.0*k31+k41); ny2=y2+0.166666667*(k12+2.0*k22+2.0*k32+k42); nt=t+h; /*printf("%f %f \n",y1,y2);*/ printf("%d %f \n",j,y2); /*printf("%f %f %f\n",y1,y2,1.0-y1-y2);*/ y1=ny1; y2=ny2; t=nt; } return 0; } double FUNC1(double t, double y1, double y2) { return ( (y1+(2.0+a)*y2-(1.0+0.5*a*(1.0-y1*y1-y2*y2-(1.0-y1-y2)*(1.0-y1-y2))))*y1 ); } double FUNC2(double t, double y1, double y2) { return ( (y2+(2.0+a)*(1-y1-y2)-(1.0+0.5*a*(1.0-y1*y1-y2*y2-(1.0-y1-y2)*(1.0-y1-y2))))*y2 ); }
出力結果
3すくみ
/*
戦略を持つじゃんけん
Computerは「相手の過去の手を記憶し、確率的に計算し、手を考える」
*/
#include <stdio.h>
main(void)
{
int man,computer,M,judge;
static int table[3][3]={{0,0,0}, /* 戦略テーブル */
{0,0,0},
{0,0,0}},
hist[3]={0,0,0}; /* 勝敗の度数 */
static char *hand[3]={"グー","チョキ","パー"};
M=0;
while (1) {
if (table[M][0]>table[M][1] && table[M][0]>table[M][2])
computer=2;
else if (table[M][1]>table[M][2])
computer=0;
else
computer=1;
printf("0:グー 1:チョキ 2:パー\n");
printf("あなたの手 ");scanf("%d",&man);
printf("コンピュータの手 %s\n",hand[computer]);
judge=(computer-man+3)%3;
switch (judge){
case 0: printf("ひきわけ\n");break;
case 1: printf("あなたの勝ち\n");break;
case 2: printf("コンピュータの勝ち\n");break;
}
hist[judge]++;
table[M][man]++; /* 学習 */
M=man;
printf("--- %d勝%d敗%d分 ---\n\n",hist[1],hist[2],hist[0]);
}
}
/*
戦略を持つじゃんけん
Computerは「相手の過去の手を記憶し、確率的に計算し、手を考える」
*/
#include <stdio.h>
main(void)
{
int man,computer,C,M,judge;
static int table[3][3][3]={{{0,0,0},{0,0,0},{0,0,0}}, /* 戦略テーブル */
{{0,0,0},{0,0,0},{0,0,0}},
{{0,0,0},{0,0,0},{0,0,0}}},
hist[3]={0,0,0}; /* 勝敗の度数 */
static char *hand[3]={"グー","チョキ","パー"};
C=M=0;
while (1) {
if (table[C][M][0]>table[C][M][1] && table[C][M][0]>table[C][M][2])
computer=0;
else if (table[C][M][1]>table[C][M][2])
computer=1;
else
computer=2;
printf("0:グー 1:チョキ 2:パー\n");
printf("あなたの手 ");scanf("%d",&man);
printf("コンピュータの手 %s\n",hand[computer]);
judge=(computer-man+3)%3; /* 判定 */
switch (judge){
case 0: printf("ひきわけ\n");
table[C][M][(computer+2)%3]++;break;
case 1: printf("あなたの勝ち\n");
table[C][M][computer]--;break;
case 2: printf("コンピュータの勝ち\n");
table[C][M][computer]++;break;
}
M=man; /* 1つ前の状態を保存 */
C=computer;
hist[judge]++;
printf("--- %d勝%d敗%d分 ---\n\n",hist[1],hist[2],hist[0]);
}
}
Back to C Language

