2011-11-21 12 views
0

こんにちは私は宿題のためにNimのゲームを作成するのに苦労しています。私はあなたのコンピュータのplayメソッドを呼び出す方法私は基本的に得ることはありませんNim Game、Public Void PlayおよびCallingメソッド

call computer;s play method, send it marbleCount, and 
// reduce marblecount by the amount the computer takes 
// Then you need to check for a winner 
// switch the turn 

コメントの中で、私の先生が掲示最初のファイルでます。public void play()メソッド、上で、それを送っ現在混乱していますmarbleCountまたは勝者を確認してください。誰かが私にいくつかのヒント/助けてくれますか?私は非常に疲れており、これを長い間働いていますので、もし私が何かを見逃してしまったら、すみません。前もって感謝します。

import java.util.Random; 
import javax.swing.JOptionPane; 

/** 
* Represents a game of Nim, where players take turns removing marbles from a pile. 
* The player that takes the last marble wins the game. 
*/ 
public class Nim 
{ 
    public static int COMPUTER = 0; 
    public static int HUMAN = 1; 

    private Player computer; 
    private Player human; 
    private int marbleCount; 
    private int turn;   // whose turn it is 
    private int winner; 

    /** 
    * Constructs a Nim game. There are two players, one is the computer and the other is human. 
    * The player who gets to go first is chosen at random. The number of marbles in the pile is 
    * generated randomly between 10 and 100. A message is displayed showing how many marbles the 
    * starts with. The contructor calls the play() method to start the game. 
    */ 
    public Nim() 
    { 
     Random rand = new Random(); 
     // assign values for all instance fields before play() is called 
     computer = new Player(COMPUTER); 
     human = new Player (HUMAN); 
     marbleCount = rand.nextInt(91) + 10; 
     turn = rand.nextInt(2); 
     winner = HUMAN; 
     // JOptionPane - Show number of marbles 
     JOptionPane.showInputDialog(marbleCount); 

     play(); 
    } 

    /** 
    * The play method continues game play until the pile is reduced to 0. Whoever takes the last marble 
    * loses the game. At the beginning of each play, the number of marbles left is displayed. The player 
    * whose turn it is calls the Player play method, sending it the number of marbles left. When the player 
    * takes marbles, the marbleCount is decreased. When the marbles are gone, the winner of the game is 
    * displayed. 
    */ 
    public void play() 
    // while there are marbles left 
    // if else for turn 
    // if its the computers turn you are going to call the computer's play method, 
    // last method in the player file 
     // call computer;s play method, send it marbleCount, and 
     // reduce marblecount by the amount the computer takes 
     // Then you need to check for a winner 
     // switch the turn 
    // else human's turn 
     //call the human's play method - send it marbleCount 
      // reduce marbleCount by the human's take 
      // check for a winner 
      // switch the turn 
    { 
     while(marbleCount >= 0) 
      JOptionPane.showMessageDialog(null,"There are " + marbleCount + "marbles left"); 
      if(turn = 0) 

       turn = 1; 
      else 
       turn = 0; 













     // At the end of play (when the loop exits), the winner is displayed 
     if (winner == COMPUTER) 
      JOptionPane.showMessageDialog(null,"Computer wins!"); 
     else 
      JOptionPane.showMessageDialog(null,"Human wins!"); 
    } 
} 

最初のファイルの最後

import java.util.Random; 
import javax.swing.JOptionPane; 

/** 
* Represents a player in the game of Nim. The player is either human or computer. 
* Humans are prompted for their plays. 
*/ 
public class Player 
{ 
    public static int COMPUTER = 0; 
    public static int HUMAN = 1; 

    private int type; // computer or human 

    /** 
    * Constructs a player of type t. 0 is COMPUTER, 1 is HUMAN. 
    * If t is not 0 or 1, type will be set to HUMAN. 
    * @param t type of player 
    */ 
    public Player(int t) 
    { 
     type = t; 
     if(!(type == 0 || type == 1)) 
      type = 1; 
    } 

    /** 
    * Determines how many marbles are taken, and returns that number. 
    * Human player will be asked how many marbles they want to take. 
    * If the move is illegal, they will be asked again until they select a legal move. 
    * The computer will take a random number of marbles between 1 and pileSize/2. 
    * @param pileSize the number of marbles in the pile 
    * @return the number of marbles taken by the player 
    */ 
    public int play(int pileSize) 
    { 
     Random r = new Random(); 
     int numTaken; 
     if(type == COMPUTER) 
     { 
      if(pileSize == 1) 
      { 
       numTaken = 1; 
       JOptionPane.showMessageDialog(null," Computer took 1 marble "); 
      } 
      else 
      { 

      numTaken = r.nextInt(pileSize/2) + 1; // POSSIBLE ERROR 
      pileSize = pileSize - numTaken; 
      JOptionPane.showMessageDialog(null," Computer took " + numTaken + "marbles "); 
      } 
     } 
     else 
      { 
       boolean correct; 
       do 
       { 
       String numTakenbyhuman = JOptionPane.showInputDialog("There are " + pileSize + "marbles" + "How many marbles will you take ?"); 
       numTaken = Integer.parseInt(numTakenbyhuman); 
       pileSize = pileSize - numTaken; 
       JOptionPane.showMessageDialog(null," Computer took " + pileSize + " marbles "); 
       if(1 <= numTaken <= pileSize/2) 
        correct = false; 
       else 
        correct = true; 
       } 
       while(!correct); 
        JOptionPane.showMessageDialog(null, "You must take between " + pilesize/2 + " 1 marbles"); 


       } 
       return numTaken; 




      } 




    } 

答えて

0

私はそれだけでNim.play()のためのあなたのwhileループ内で、あなたは人間のプレイヤーとコンピュータプレイヤーへの呼び出しを交互に必要、ということだと思います。どのプレイヤーの順番を判断するには、すでにifがあります。ターンを更新するときにはcomputer.play()に電話し、他のブランチに行くときにはhuman.play()に電話するだけです。

0

あなたのNimクラスでは、コンピュータまたは人間のいずれかに電話をかけなければならないターンに応じて、同じ場所でターンを切り替えるかどうかをチェックします。その後、playメソッドは、人間は、この返された値をmarbleCountから減算します。

これはmarbleCount> 0まで続きます。その後、勝者を決定する順番によって決まります。

私はあなたのコードビットを変更し、あなたは

import java.util.Random; 
import javax.swing.JOptionPane; 

/** 
    * Represents a game of Nim, where players take turns removing marbles from a 
    * pile. The player that takes the last marble wins the game. 
    */ 
    public class Nim { 
public static int COMPUTER = 0; 

public static int HUMAN  = 1; 

private Player  computer; 

private Player  human; 

private int   marbleCount; 

private int   turn;    // whose turn it is 

private int   winner; 

/** 
* Constructs a Nim game. There are two players, one is the computer and the 
* other is human. The player who gets to go first is chosen at random. The 
* number of marbles in the pile is generated randomly between 10 and 100. A 
* message is displayed showing how many marbles the starts with. The 
* contructor calls the play() method to start the game. 
*/ 
public Nim() { 
    Random rand = new Random(); 
    // assign values for all instance fields before play() is called 
    computer = new Player(COMPUTER); 
    human = new Player(HUMAN); 
    marbleCount = rand.nextInt(91) + 10; 
    turn = rand.nextInt(2); 
    winner = HUMAN; 
    // JOptionPane - Show number of marbles 
    //JOptionPane.showInputDialog(marbleCount); 

    play(); 
} 

/** 
* The play method continues game play until the pile is reduced to 0. 
* Whoever takes the last marble loses the game. At the beginning of each 
* play, the number of marbles left is displayed. The player whose turn it 
* is calls the Player play method, sending it the number of marbles left. 
* When the player takes marbles, the marbleCount is decreased. When the 
* marbles are gone, the winner of the game is displayed. 
*/ 
public void play() 
// while there are marbles left 
// if else for turn 
// if its the computers turn you are going to call the computer's play 
// method, 
// last method in the player file 
// call computer;s play method, send it marbleCount, and 
// reduce marblecount by the amount the computer takes 
// Then you need to check for a winner 
// switch the turn 
// else human's turn 
// call the human's play method - send it marbleCount 
// reduce marbleCount by the human's take 
// check for a winner 
// switch the turn 
{ 
    while (marbleCount > 0) { 
     JOptionPane.showMessageDialog(null, "There are " + marbleCount + "marbles left"); 
     int marblesTaken = 0; 
     if (turn == 0) { 
      marblesTaken = computer.play(marbleCount); 
     } else { 
      marblesTaken = human.play(marbleCount); 
     } 
     marbleCount = marbleCount - marblesTaken; 
     turn = (marbleCount > 0 ? turn == 1 ? 0 : 1 : turn); 
    } 

    // At the end of play (when the loop exits), the winner is displayed 
    if (turn == 0) 
     JOptionPane.showMessageDialog(null, "Computer wins!"); 
    else 
     JOptionPane.showMessageDialog(null, "Human wins!"); 
} 

public static void main(String[] args) { 
    new Nim(); 
} 

}

そして、これが

import java.util.Random; 
import javax.swing.JOptionPane; 

/** 
    * Represents a player in the game of Nim. The player is either human or 
    * computer. Humans are prompted for their plays. 
    */ 
public class Player { 
public static int COMPUTER = 0; 

public static int HUMAN  = 1; 

private int   type;    // computer or human 

/** 
* Constructs a player of type t. 0 is COMPUTER, 1 is HUMAN. If t is not 0 
* or 1, type will be set to HUMAN. 
* 
* @param t 
*   type of player 
*/ 
public Player(int t) { 
    type = t; 
    if (!(type == 0 || type == 1)) 
     type = 1; 
} 

/** 
* Determines how many marbles are taken, and returns that number. Human 
* player will be asked how many marbles they want to take. If the move is 
* illegal, they will be asked again until they select a legal move. The 
* computer will take a random number of marbles between 1 and pileSize/2. 
* 
* @param pileSize 
*   the number of marbles in the pile 
* @return the number of marbles taken by the player 
*/ 
public int play(int pileSize) { 
    Random r = new Random(); 
    int numTaken; 
    if (type == COMPUTER) { 
     if (pileSize == 1) { 
      numTaken = 1; 
      JOptionPane.showMessageDialog(null, " Computer took 1 marble "); 
     } else { 
      numTaken = r.nextInt(pileSize/2) + 1; // POSSIBLE ERROR 
      pileSize = pileSize - numTaken; 
      JOptionPane.showMessageDialog(null, " Computer took " + numTaken + "marbles "); 
     } 
    } else { 
     boolean correct; 
     do { 
      JOptionPane.showMessageDialog(null, "You must take between " + pileSize/2 + " marbles"); 
      String numTakenbyhuman = JOptionPane.showInputDialog("There are " + pileSize + "marbles " 
        + "How many marbles will you take ?"); 
      numTaken = Integer.parseInt(numTakenbyhuman); 
      // pileSize = pileSize - numTaken; 
      JOptionPane.showMessageDialog(null, " Human took " + numTaken + " marbles "); 
      if (1 <= numTaken && numTaken < pileSize/2) 
       correct = false; 
      else 
       correct = true; 

     } while (!correct); 
     // JOptionPane.showMessageDialog(null, "You must take between " + 
     // pileSize + " 1 marbles"); 

    } 
    return numTaken; 

} 

}

この情報がお役に立てば幸いプレーヤーで違いを確認するために比較することができます

関連する問題