无限while循环和java中的循环错误计数

无限while循环和java中的循环错误计数

问题描述:

我试图创建游戏nim。我做到了,所以电脑可以随机抽取1-3张卡片。没有电脑会这样做,但是当只有一张或三张卡片时,它将无限循环。无限while循环和java中的循环错误计数

主文件。不需要触及

package nimgame; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
/** 
* 
* @author Angela 
*/ 
public class NimApp extends JFrame implements ActionListener { 

    private static final int ROWS = 3; 
    private JTextField[] gameFields; // Where sticks for each row shown 
    private JTextField rowField;  // Where player enters row to select 
    private JTextField sticksField; // Where player enters sticks to take 
    private JButton playButton;  // Pressed to take sticks 
    private JButton AIButton;  // Pressed to make AI's move 

    private NimGame nim; 

    public NimApp() { 

     // Build the fields for the game play 
     rowField = new JTextField(5); 
     sticksField = new JTextField(5); 
     playButton = new JButton("PLAYER"); 
     AIButton = new JButton("COMPUTER"); 
     playButton.addActionListener(this); 
     AIButton.addActionListener(this); 
     AIButton.setEnabled(false); 

     // Create the layout 
     JPanel mainPanel = new JPanel(new BorderLayout()); 
     getContentPane().add(mainPanel); 

     JPanel sticksPanel = new JPanel(new GridLayout(3, 1)); 
     mainPanel.add(sticksPanel, BorderLayout.EAST); 

     JPanel playPanel = new JPanel(new GridLayout(3, 2)); 
     mainPanel.add(playPanel, BorderLayout.CENTER); 

     // Add the fields to the play panel 
     playPanel.add(new JLabel("Row: ", JLabel.RIGHT)); 
     playPanel.add(rowField); 
     playPanel.add(new JLabel("Sticks: ", JLabel.RIGHT)); 
     playPanel.add(sticksField); 
     playPanel.add(playButton); 
     playPanel.add(AIButton); 

     // Build the array of textfields to display the sticks 
     gameFields = new JTextField[ROWS]; 
     for (int i = 0; i < ROWS; i++) { 
      gameFields[i] = new JTextField(10); 
      gameFields[i].setEditable(false); 
      sticksPanel.add(gameFields[i]); 
     } 
     setSize(350, 150); 
     setVisible(true); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     nim = new NimGame(new int[]{3, 5, 7}); 
     draw(); 
    } 

    // Utility function to redraw game 
    private void draw() { 
     for (int row = 0; row < ROWS; row++) { 
      String sticks = ""; 
      for (int j = 0; j < nim.getRow(row); j++) { 
       sticks += "| "; 
      } 
      gameFields[row].setText(sticks); 
     } 
     rowField.setText(""); 
     sticksField.setText(""); 
    } 

    public void actionPerformed(ActionEvent e) { 

     // Player move 
     if (e.getSource() == playButton) { 

      // Get the row and number of sticks to take 
      int row = Integer.parseInt(rowField.getText())-1; 
      int sticks = Integer.parseInt(sticksField.getText()); 

      // Play that move 
      nim.play(row, sticks); 

      // Redisplay the board and enable the AI button 
      draw(); 
      playButton.setEnabled(false); 
      AIButton.setEnabled(true); 

      // Determine whether the game is over 
      if (nim.isOver()) { 
       JOptionPane.showMessageDialog(null, "You win!"); 
       playButton.setEnabled(false); 
      } 
     } 

     // Computer move 
     if (e.getSource() == AIButton) { 

      // Determine computer move 
      nim.AIMove(); 

      // Redraw board 
      draw(); 
      AIButton.setEnabled(false); 
      playButton.setEnabled(true); 

      // Is the game over? 
      if (nim.isOver()) { 
       JOptionPane.showMessageDialog(null, "You win!"); 
       playButton.setEnabled(false); 
      } 
     } 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     NimApp a = new NimApp(); 
    } 
} 

这是这应该计数留在电路板上的所有枝主文件

package nimgame; 
import java.util.Random; 
/** 
* 
* @author Angela 
*/ 
public class NimGame { 
    int[] Sticks; //creating an array of sticks 
    int totalSticks=0; 

    public NimGame(int[] initialSticks){ 
    Sticks = initialSticks;} 

    public int getRow(int r){ 
     return Sticks[r];} 

    public void play(int r, int s){ 
    Sticks[r]=Sticks[r]-s;} 

支持类。它应该返回true,如果留下一个棒,并且如果板上有多个棒,则返回false。我在for循环中遇到了问题,并且正确地计算了这些枝条。

public boolean isOver(){ 
    int theTotal = 0; 
    for (int i =1; i<Sticks.length; i++){ 
     theTotal = totalSticks + Sticks[i]; 
      System.out.println(totalSticks);} 
     totalSticks = theTotal; 
     System.out.println(totalSticks); 
     if (totalSticks<=1){ 
      return true; 
     } 
     return false; 

这种方法应该随机生成一行和一定数量的棍棒以放弃计算机。这是我得到我无限循环的地方。

public void AIMove(){ 
    Random randomInt = new Random(); 
    int RandomRow = randomInt.nextInt(3); 
    int RandomDiscard = randomInt.nextInt(3-0)+1; 
    int randomize=0; 



    while (RandomDiscard >Sticks[RandomRow] && totalSticks>1){ 
     RandomRow = randomInt.nextInt(3); 
     RandomDiscard = randomInt.nextInt(3-0)+1; 
     Sticks[RandomRow]=Sticks[RandomRow]-RandomDiscard; 
     randomize = 1;} 

    if (randomize==0) 
    Sticks[RandomRow]=Sticks[RandomRow]-RandomDiscard; 

    if (totalSticks <= 1){ 
    Sticks[RandomRow]=Sticks[RandomRow]-1;} 
    isOver(); 
    } 

      } 
+0

你想用'randomInt.nextInt(3-0)+ 1'做什么?另外,你似乎正在比较'RandomDiscard'(**和**也是*修改*它和你在循环中比较的东西)。我想你想评论while循环的前两行。但我不确定,因为我不知道你在做什么。 –

+0

对于randomint(3-0)+1我得到1-3之间的随机数字。而在循环中,我需要检查一下我要取出的数字(随机丢弃物)是否大于我在该行中的数字。如果它大于我需要选择一个新的随机新丢弃和一个新的随机行 – kkkkkkkkkkkkkkkkkkkkkkk

条件终止while环是当你在循环体上有RandomDiscard > Sticks[RandomRow]totalSticks>1的值时Sticks[RandomRow]至少减1,保持条件。

为了打破循环无论是在身体,你需要增加Sticks[RandomRow]或减少totalSticks或更改条件RandomDiscard < Sticks[RandomRow] || totalSticks>1

编辑(下午10时18): 望着问题和解决方案,我得到了更好的办法来解决这里的问题是我做了什么:

public void AIMove(){ 
    Random randomInt = new Random(); 

    boolean tryRemove = true; 

    while(tryRemove && totalSticks > 1){ 
     int RandomRow = randomInt.nextInt(3); 
     if(Sticks[RandomRow] <= 0)//the computer can't remove from this row 
      continue; 

     //the max number to remove from row 
     int size = 3; 
     if(Sticks[RandomRow] < 3)//this row have lest that 3 cards 
      size = Sticks[RandomROw];//make the max number to remove from the row be the number of cards on the row 
     int RandomDiscard = randomInt.nextInt(size) + 1; 
     Sticks[RandomRow] = Sticks[RandomRow] - RandomDiscard; 
     //I don't know if this is needed, but since we remove a RandomDiscard amount lest decrease the totalSticks 
     totalSticks = totalSticks - RandomDiscard; 
     //exit loop 
     tryRemove = false; 
    } 

    if(totalSticks <= 1){ 
     Sticks[RandomRow]=Sticks[RandomRow]-1; 
     isOver(); 
    } 


} 

我所做的是把随机循环和if语句在一个循环。

+0

非常感谢你! – kkkkkkkkkkkkkkkkkkkkkkk

theTotal = totalSticks + Sticks[i]; 

应该是

theTotal = theTotal + Sticks[i]; 

此外,用于循环或许应该与索引0,而不是1开始:

for (int i =1; i<Sticks.length; i++){ 
+0

谢谢!你能看到我为什么在无限循环? – kkkkkkkkkkkkkkkkkkkkkkk