扑克牌24点游戏JAVA实现

package auto;

public class Point24Compare
{
 public static int Max = 13;
 public static int Min = 1;
 
 public static void main(String[] args)
 {
  int[] pokers = {0,0,0,0};

  for(int i=0; i<4; i++)
  {
   pokers[i] = (int)Math.round(Math.random()*(Max-Min)+Min);
  }
  
  System.out.println("Select any 4 cards: "+pokers[0]+','+pokers[1]+','+pokers[2]+','+pokers[3]);

  Calculate24Point(pokers);
 }

 public static boolean Calculate24Point(int [] pokers)
 {
  float tmpF = 0;
  
  int[] operator = {0,0,0};
  int[] index = {0,1,2,3};

  int indexMax = 3;
  int indexMin = 0;

  int tmpI = 0;
  
  boolean canSet = false;
  boolean OK = false;

  for(int count=0; count<10000000; count++)
  {
   for(int i=0; i<4; i++)
   {
    if(0 == i)
    {
     index[0] = (int)Math.round(Math.random()*(indexMax-indexMin)+indexMin);
    }
    else
    {
     while(true)
     {
      tmpI = (int)Math.round(Math.random()*(indexMax-indexMin)+indexMin);
      for(int j=0; j<i; j++)
      {
       if(tmpI != index[j])
       {
        canSet = true;
       }
       else
       {
        canSet = false;
        break;
       }
      }
      if(canSet)
      {
       index[i] = tmpI;
       break;
      }
     }
    }
   }

   for(int i=0; i<3; i++)
   {
    operator[i] = (int)Math.round(Math.random()*(indexMax-indexMin)+indexMin);
   }
   
   OK =  Calc24Point(pokers, index, operator);
   
   if(OK)
   {
    break;
   }
  }
  
  if(!OK)
  {
   System.out.println("These cards can not calculate the 24");
  }
  
  return OK;
 }
 
 public static boolean Calc24Point(int[] pokers, int[] index, int[] operator)
 {
  float tmpF = 0;
  
  switch(operator[0])
  {
   case 0:
    tmpF = ((float)pokers[index[0]]) + pokers[index[1]];
    break;
   case 1:
    tmpF = ((float)pokers[index[0]]) - pokers[index[1]];
    break;
   case 2:
    tmpF = ((float)pokers[index[0]]) * pokers[index[1]];
    break;
   case 3:
    tmpF = ((float)pokers[index[0]]) / pokers[index[1]];
    break;
   default:
    return false;
  }
  
  for(int i=1; i<operator.length; i++)
  {
   switch(operator[i])
   {
    case 0:
     tmpF = tmpF + pokers[index[i+1]];
     break;
    case 1:
     tmpF = tmpF - pokers[index[i+1]];
     break;
    case 2:
     tmpF = tmpF * pokers[index[i+1]];
     break;
    case 3:
     tmpF = tmpF / pokers[index[i+1]];
     break;
    default:
     return false;
   }
  }
  
  if(tmpF > 23.9999 && tmpF <24.0001)
  {
   char []oper = new char[operator.length];
   for(int i=0; i<operator.length; i++)
   {
    switch(operator[i])
    {
     case 0:
      oper[i] = '+';
      break;
     case 1:
      oper[i] = '-';
      break;
     case 2:
      oper[i] = '*';
      break;
     case 3:
      oper[i] = '/';
      break;
     default:
      return false;
    }
    
   }
   
   System.out.println("These cards can calculate the 24: (("+String.valueOf(pokers[index[0]])+oper[0]+String.valueOf(pokers[index[1]])+')'+oper[1]+String.valueOf(pokers[index[2]])+')'+oper[2]+String.valueOf(pokers[index[3]]));
   return true;
  }
  
  return false;
 }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值