2015-11-20 70 views
1

因此,我最近决定使用Java编程重新创建“Dice 10,000”游戏,并且遇到一些问题需要执行某些规则。有4条规则我无法工作。如何计算Java中ArrayList中特定项目的数量

1)任何数量= 10倍的面的3个骰子的值的3

2)一个完整的直的(1 - 6)= 3000点

3)3双= 1000点

4)3 One's = 1,000分

我只是不确定如何让这些工作...我相信我会使用一套,但我对套非常非常缺乏经验,以及如何使用它们和他们包含什么方法。我大概可以算出第二个,但是第一,第三和第四个给我的麻烦最多。

//Andrew M 
import java.util.*; 
import java.io.*; 
class Zilch 
{ 

    public static Random r = new Random(); 
    public static ArrayList<Integer> diceList = new ArrayList<Integer>(); 
    public static int count = 0, humanScore = 0, compScore = 0, humanBank = 0, compBank = 0, lastMove = 0; 
    public static ArrayList<Integer> diceKeep = new ArrayList<Integer>(); 
    public static boolean gameOn = true, x = true; 
    //----------------------------------------------------------------------------------- 
    public static void main(String[] args) 
    { 
     for (int j = 1; j <= 6; j++) 
     { 
      diceList.add(j); 
      diceKeep.add(null); 
     } 
     do 
     { 
      do 
      { 
       x = true; 
       for (int k = 0; k < diceKeep.size(); k++) 
       { 
        if (diceKeep.get(k) != null) 
        { 
         System.out.println("Dice #" + (k + 1) + ": " + diceKeep.get(k) + " -- kept."); 
        } 
       } 
       System.out.println(); 
       System.out.println(); 
       boolean b = true; 
       for (int i = 0; i < diceList.size(); i++) 
       { 
        if(diceList.get(i) != 0) 
        { 
         diceList.set(i, r.nextInt(6) + 1); 
         System.out.println("Dice #" + (i+1) + " is a " + diceList.get(i)); 
        } 
       } 
       System.out.println(); 
       System.out.println("Select which dice to roll again... (Select one at a time, 1 - 6), then type \"go\" to complete your turn."); 
       Scanner input = new Scanner(System.in); 

       /*-----------UNNECCESARY CODE--------------*/ 
        else 
        if (selection.equals("go")) 
        { 
         System.out.println(); 
         System.out.println(); 
         if (diceKeep.get(0) == 1) 
         { 
          humanBank = humanBank + 100; 
          lastMove = 100; 
         } 
         else 
         if (diceKeep.get(0) == 5) 
         { 
          humanBank = humanBank + 500; 
          lastMove = 500; 
         } 

/*--------------I NEED THE RULES TO GO HERE---*/   

         b = false; 
        } 
       } 
       while(b == true); 
       //----------------------------------------------------------------------- 
       if(gameOn == true) 
       { 
        x = true; 
        //Place Score Card Here 
       } 
      } 
      while (x == true); 
     } 
     while (1< 2); //Just here for testing purposes, for now 
    } 
} 

对不起,我知道现在很多。希望你们可以给我一些示例代码让我走?万分感谢。

一切顺利

安德鲁

编辑:截至目前,我将无法到明天早上做出回应。我对缺乏反馈表示歉意,但已经晚了。明天我会回复尝试和回应。

+0

有很多的这样做的方法。如果是我,我会使用一个Map 来配对,其中关键字是骰子的编号,值是出现次数。 – Titus

+0

嗯,我期待把这个放在if语句中,比如说if(/ *有三个fives * /)'还有,你能发表一个描述如何使用Map的答案吗?如果我的问题得到解决,我会很乐意接受。但现在,我正在睡觉。我会明天反馈。 –

+0

使用Map 可以遍历这些值并查看它们中的任何一个是否大于6(三对),或者检查地图是否只有一个条目(完整直线)。 – Titus

回答

1

在下面,我添加一些评论。它在ArrayList中打印数字。

import java.util.ArrayList; 

public class Test { 

    public static void main(String[] args) { 
     int [] i = new int [7]; // array starts with 0, so we can init our array with size 7 (0 to 6). 
     ArrayList<Integer> arr = new ArrayList<Integer>(); 

     // there are 2 times "5" 
     arr.add(5); 
     arr.add(5); 

     //add some null 
     arr.add(null); 

     // there is 1 times "3" 
     arr.add(3); 

     // there is 1 times "6" 
     arr.add(6); 

     //add some null 
     arr.add(null); 

     System.out.println("numbers in arr: " + arr); 

     for (Integer integer : arr) { 
      if (integer!= null) 
       i[integer]++; 
     } 

     for (int c = 0 ;c<i.length ; c++){ 
      System.out.println("there are/is " + c + " ->" + i[c] + " times"); 
     } 
    } 
} 

打印:

numbers in arr: [5, 5, null, 3, 6, null] 
there are/is 0 ->0 times 
there are/is 1 ->0 times 
there are/is 2 ->0 times 
there are/is 3 ->1 times 
there are/is 4 ->0 times 
there are/is 5 ->2 times 
there are/is 6 ->1 times 
相关问题