闲来无事之剪刀石头布

####前言:
最近闲来无聊,想写写大一的工程实践剪刀石头布,但与之不同的是这次机器人出剪刀石头布并不是随机,而是经过数据统计针对了某特定习惯的出法,理论上提高了机器人的胜率。
####数据统计
1.hotencode 0 代表布 1 代表剪刀 2 代表石头 存在胜负关系
0 < 1, 1<2, 2<0

2.剪刀石头布有三种结果使用3x3的矩阵
(1) 111 111 111 \begin{matrix} 1 1 1 \\ 1 1 1 \\ 1 11 \end{matrix} \tag{1} 111111111(1)
纵向代表0,1,2,横向表示上一次出0,1,2后出0,1,2的次数,例如如果上次出得0,然后出的1,那么结果如图2,在(0,1)+1.
(1) 121 111 111 \begin{matrix} 1 2 1 \\ 1 1 1 \\ 1 1 1 \end{matrix} \tag{1} 121111111(1)
3.有两种结果 0.输 1.赢 使用两个3x3的矩阵
为什么使用两个矩阵呢?
因为人是会反思的动物,在失败后会改变策略我们也应该使计算机有新的策略来反复应对
####逻辑设计
流程图
程序第一次运行时,没有任何数据,所以通过随机数随机出0,1,2,然后与玩家比胜负,胜利在1矩阵记录数据否者在0矩阵记录数据,比如 第一次程序(因为都为1机会相等)随机出0,玩家出1,玩家胜,程序通过矩阵出0,玩家出2,玩家负,则在1【胜利0】矩阵的(1,2)处+1。

###最后神秘的代码
(切换模式还有点麻烦。。。。)

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ROCK 2
#define SCI 1
#define PAPER 0
#define WIN 3
#define LOSE 4
#define PASS 5
#define ERR 100
int jude(int seq,int res);
int evolve();
double * get_ele_pro(int matrix[3][3],int row);
int lp_output();
int model[2][3][3]={
{
{1,1,1},
{1,1,1},
{1,1,1}
},
{
{1,1,1},
{1,1,1},
{1,1,1}
}
};

int a;

int main(int argc ,char * argv[])
{
  int pre = 0 ;
  // Assume that the first time is a stone
  int counter= 0;
  int human;
  int computer;
  double * probility;
  double rnd;
  srand((unsigned int )time(NULL));
  while(1){
    // printf("wait for input \n");
    // scanf("%d",&human);
    human = lp_output();
    if(counter < 10 ){
      computer = rand()%3;

    }
    else{
       probility=get_ele_pro(model[0],pre);
       rnd = (double)(rand()%100)/100;
       if(rnd<probility[0]){
         computer = 1;
       }
       else if(rnd<probility[1])
        computer = 2;
      else
        computer =0 ;
    }
    jude(human,computer);
    model[0][pre][human]++;
    pre = human;
    counter ++;
    if (counter == 1000)
    {
      break;
    }
  }

  // printf("......");
  // evolve();
  return 0;
}

int lp_output()//test fixed mode
{

  a++;
  return a%3;
}

int evolve()
{
  // int temp;
  // scanf("%d\n",&temp);
  // if(temp>2||temp <0)
  // {
  //   exit(0);
  // }
  // srand((unsigned int )time(NULL));
  // int count=0;
  // int req = count;
  // int res = rand()%3;
  // if(jude(req,res)==WIN)
  //   {
  //
  //     printf("%d %d\n",req,res );
  //   }
  //   else
  //   {
  //
  //     printf("%d %d\n",req,res );
  //   }
  // while()
  // {
  //   int result = count % 3;
  //   if()
  //
  //   count ++;
  // }
}
int jude(int seq,int res)
{
  switch (seq) {
    case PAPER:
    if(res==SCI)
    {
      printf("conputer WIN\n" );
      return WIN;
    }
    else if(res == ROCK)
    {
    printf("human WIN\n" );
    return  LOSE;
    }
    else
    {
      printf("pass\n");
      return PASS;
    }

    break;
    case SCI:
    if(res == ROCK)
    {
      printf("conputer WIN\n" );
      return WIN;
    }
    else if(res == SCI)
    {
      printf("PASS\n");
      return PASS;
    }
    else
    printf("human WIN\n" );
    return LOSE;
    case ROCK:
    if (res == PAPER)
    {
      printf("computer WIN\n");
      return WIN;
    }
    else if(res == ROCK )
    {
      printf("pass\n");
      return PASS;
    }
    else
    printf("human WIN\n");
    return LOSE;
    default:
    printf("worry value");
    break;
  }

}

double * get_ele_pro(int matrix[3][3],int row)
{
  double * pro;
  pro = (double * )malloc(sizeof(double)*3);
  for(int i =0;i<3;i++)
  {
    pro[i]=matrix[row][i]/((double)matrix[row][0]+matrix[row][1]+matrix[row][2]);
  }
  return pro;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值