java - 按条件从数组列表中选择 3 个元素

标签 java android arraylist

<分区>

我正在制作一个 java 纸牌游戏,现在我尝试为计算机玩家创建一个“大脑”。条件:

  • table 上有3张牌(只有点数,没有花色);

  • 电脑玩家(CP)有6张牌;

  • 要赢得一轮 CP 必须至少击败 table 上的 2 张牌,否则
    必须击败 1 张牌并与另外 2 张牌平局;

  • 当然,卡组一定要最优。

例子: table 上有1、2、3的牌,CP有1、1、3、2、5、6的牌,必须选择2的牌打败Card1,而不是选择3的牌打败Card2 而不是选择值为 1 的卡,因为它已经击败了 2 张卡。 现在我写了下一个代码,但它不工作或根本不工作。

for (i = 0, j = i++, k = j++; i < oppHand.size() - 2 && j < oppHand.size() - 1 && k < oppHand.size(); i++) {

a = oppHand.get(i).getPower(); //value of card1 from CP hand
b = oppHand.get(j).getPower(); //value of card2 from CP hand
c = oppHand.get(k).getPower(); //value of card3 from CP hand

x = oppHand.indexOf(i);        //position of card1 in a CP hand
y = oppHand.indexOf(j);        //position of card2 in a CP hand
z = oppHand.indexOf(k);        //position of card3 in a CP hand

if (a > Score1 && b > Score2 ||  a> Score1 && c > Score3) {    //Score - value of the cards on the table
choice1 = x;
choice2 = y;
choice3 = z;}

else if (a > Score1 && b > Score3 || a > Score1 && c > Score2) {
choice1 = x;
choice2 = z;
choice3 = y;} ........

// moving cards from the CP hand to the table with assignment values to the piles
    validPower5 = oppHand.get(choice1).getPower();  
    discardPile5.add(0, oppHand.get(choice1));
    oppHand.remove(choice1);

    validPower6 = oppHand.get(choice2).getPower();  
    discardPile6.add(0, oppHand.get(choice2));
    oppHand.remove(choice2);

    validPower7 = oppHand.get(choice3).getPower();  
    discardPile7.add(0, oppHand.get(choice3));
    oppHand.remove(choice3);
                    }

最佳答案

这样的事情会有帮助吗?

sortCardsOnTable();//now Score1 <= Score2 <= Score2
sortOpponentCards();
boolean beatScore1 = false;
boolean beatScore2 = false;
int indexFirst = 0;
int indexSecond = 0;
int indexThird = 0;

for(int i = 0;i < oppHand.size();i++){
   Card c = oppHand.get(i);
   if(!beatScore1 && c.getPower() > Score1){
      indexFirst = i;
      beatScore1 = true;
   }
   else if(beatScore1 && c.getPower() > Score2){
      indexSecond = i;
      beatScore2 = true;
      indexThird = 1;//here compare with indexFirst so you dont pull the first card twice, just pull the first available card
   }
}

你应该对你的 Cardlist 进行排序,以简化计算

关于java - 按条件从数组列表中选择 3 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19355584/

相关文章:

java - 如何创建 session 以及如何使用 session 在jsp中从mysql数据库获取数据?

android - 自定义抽屉布局

java - 更改局部变量中的字典对象

java - bundle.getParcelableArrayList() 返回 null

java - 如何为 Jetty SSL 设置密码套件的顺序?

android EditText imeOptions OnClick

powershell - 为什么仅添加一个PSCustomObject时PowerShell会将ArrayList转换为PSCustomObject?

java - 将对象添加到 ArrayList<object>

java - 使用 jdbc Access (.mdb) 数据库操作

java - Android Java GUI freeze/w ProgressBar