2013-10-14 111 views
2

我正在制作一个java卡片游戏,现在我尝试为电脑播放器创建“一个大脑”。条件:通过条件从数组列表中选择3个元素

  • 在桌子上有3张牌(只有值,不适合);

  • 电脑播放器(CP)有6张卡;

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

  • 当然,卡片组必须是最佳的。

示例: 在桌子上是具有值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); 
        } 
+0

“不工作真或根本不工作”不给任何线索让我们去努力。提供更多细节。 – djna

+0

我们无法帮助您编程逻辑,只是试图告诉我们您失去的位置,我们会尽力指出您正确的方向 – Reinherd

回答

0

会有这样的帮助吗?

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,以简化计算

0

我想你,如果其他条件需要是正确的..

不知道,但检查这个代码为。

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

我认为这应该工作...

相关问题