2017-01-02 77 views
1

我有一个Java程序,计算机将决定哪个玩家先行,并从数组中选择随机数字。当玩家删除数字时,数组会减少。所有这些都将使用随机完成。但是当我运行我的代码时,它不是我期望的那样。我可以知道我哪里错了吗?使用随机数字的Java游戏

这是我的代码:

[的Class]

public class StickBag { 

    private int numOfSticks; 

    public StickBag(int numOfSticks) 
    { 
     this.numOfSticks = numOfSticks; 
    } 

    public int getNumOfSticks() { 
     return numOfSticks; 
    } 

    public void setNumOfSticks(int numOfSticks) { 
     this.numOfSticks = numOfSticks; 
    } 

    public int remove(int n) 
    { 
     numOfSticks = numOfSticks - n; 
     return numOfSticks; 
    } 
} 

[主要]

public class StickGameApp { 

    public static void main(String[] args) { 

     StickBag s1 = new StickBag(25); 

     System.out.println("Welcome to the game of sticks!"); 
     System.out.println("There are initially " + s1.getNumOfSticks() + " sticks on the board."); 

     int minP = 1; 
     int maxP = 2; 
     int randP; 

     int minN = 1; 
     int maxN = 10; 
     int randNum; 

     randP = minP + (int)(Math.random()*(maxP)); 
     randNum = minN + (int)(Math.random()*(maxN)); 

     for (int i=0; i<10; i++) 
     { 
      if(s1.getNumOfSticks() > randNum) 
      { 
      System.out.println("Computer player " + randP + " choose " + randNum + " sticks "); 
      s1.remove(randNum); 
      } 
      else 
      { 
       System.out.println("Computer player " + randP + " wants to choose " + randNum + " sticks but is unable to."); 
       System.out.println("Computer player " + randP + " loses "); 
      } 
     } 

    } 
} 

当运行的代码,它显示如下:


欢迎来到st游戏icks!

板上最初有25根支杆。

电脑玩家2选择6支

电脑玩家2选择6支

电脑玩家2选择6支

电脑玩家2选择6支

电脑玩家2希望选择6支,但无法。 电脑播放器2丢失

电脑播放器2想要选择6支,但无法。 电脑播放器2丢失

电脑播放器2想要选择6支,但无法。 电脑播放器2丢失

电脑播放器2想要选择6支,但无法。 电脑播放器2丢失

电脑播放器2想要选择6支,但无法。 电脑播放器2丢失

电脑播放器2想要选择6支,但无法。 电脑玩家2输


但我希望它显示是这样的:


欢迎棒的游戏!

板上最初有25根支杆。

电脑播放器1选择5支。

电脑播放器2选择7支。

电脑播放器1选择7支。

电脑玩家2想要选择7支,但无法。

电脑玩家2,你输了。


我可以知道我哪里错了吗?

+0

1.您的播放器在for循环之外被选中。因此,曾经选择过的玩家永远不会改变。 2.无论游戏结束与否,你都可以定义一个10的迭代。你需要根据我猜的棍子的数量来确定循环的结束。 3.如果您使用随机,则无法确定结果(您希望显示的结果)。 – kism3t

+0

谢谢你指出我的错误。我相当新的Java,但现在我明白了为什么。欣赏它 –

回答

1

你的错误仅仅是你在运行游戏的for循环之前放置了播放器和计算机的随机数发生器randPrandNum,因此随机数选择将只执行一次。

你的代码应该是:

for (int i=0; i<10; i++) 
    { 
    randP = minP + (int)(Math.random()*(maxP)); 
    randNum = minN + (int)(Math.random()*(maxN)); 
     if(s1.getNumOfSticks() > randNum) 
     { 
     System.out.println("Computer player " + randP + " choose " + randNum + " sticks "); 
     s1.remove(randNum); 
     } 
     else 
     { 
      System.out.println("Computer player " + randP + " wants to choose " + randNum + " sticks but is unable to."); 
      System.out.println("Computer player " + randP + " loses "); 
     } 
    } 

} 
+0

不用客气。 – Oghli

0

一件事,你选择the.player.outside the.loop,所以无论选择将始终运行所有回合

+0

非常感谢您指出我的错误。实际上,我对Java很陌生。 –

0

您将需要移动的这两行代码:

randP = minP + (int)(Math.random()*(maxP)); 
randNum = minN + (int)(Math.random()*(maxN)); 

for循环的开头。这将确保在每次迭代时,您都可以选择新玩家和新阵列位置。

+0

谢谢你的解释。欣赏它。 –

0

我不能肯定这是否是正确的做法,以你的游戏,但我希望你觉得它有用。

import java.util.*; 

public class Main { 

public static void main(String[] args) { 

    Random rand = new Random(); 
    StickBag s1 = new StickBag(25); 
    int total= s1.getNumOfSticks(); 

    System.out.println("Welcome to the game of sticks!"); 
    System.out.println("There are initially " + s1.getNumOfSticks() + " sticks on the board."); 

    int randP = 0; 
    int randNum; 
    int P1=0,P2=0; 


    for (int i=0; i<10; i++) 
    { 

      randP = rand.nextInt(2)+1; 
     randNum = rand.nextInt(10)+1; 


     if (s1.getNumOfSticks() - randNum > 0) 
     { 
     System.out.println("\n Computer player #" + randP + " chooses " + randNum + " sticks and takes them. "); 
     s1.remove(randNum); 

     System.out.println(" Computer player #" + (!(randP == 2) ? 2 : 1) + " wants to choose " + randNum + " sticks but is unable to."); 

     // Evaluate the score 
     if (randP == 1) 
      randP = (P1 = P1 + randNum); 
     else 
      randP = (P2 = P2 + randNum); 
     } 
    } 

    System.out.println("\n\n---------------------------------------------------------------------------"); 
    System.out.println("\t Player #1 has " + P1 + " sticks and Player #2 has " + P2 + " sticks. \n" + ((P1 > P2) ? "Player 1 wins" : "Player 2 wins")); 
    System.out.println("The sticks remaining in the bag are: " + s1.getNumOfSticks());}} 
+0

哇!这对我确实很有用。谢谢! –