Java Nim游戏、公共无效游戏和调用方法

Java Nim游戏、公共无效游戏和调用方法,java,Java,你好,我是一名学生,正在努力为家庭作业制作一个Nim游戏。我目前对第一个文件中的公共无效播放方法感到困惑,在我的老师发布的评论中 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 我基本上不知道你会怎么称呼电脑游戏

你好,我是一名学生,正在努力为家庭作业制作一个Nim游戏。我目前对第一个文件中的公共无效播放方法感到困惑,在我的老师发布的评论中

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
我基本上不知道你会怎么称呼电脑游戏方法,发送它大理石计数或检查赢家。有人能给我一些提示/帮助吗?我非常累,已经为此工作了很长时间,所以如果我错过了一些简单的事情,对不起。提前谢谢

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;




            }




    }

我认为这只是意味着,在Nim.play的while循环中,需要交替调用人类播放器和计算机播放器。你已经有了一个if来决定该轮到哪个玩家了。更新回合时,只需在if中调用computer.play,在进行另一个分支时调用human.play。

在Nim类中,您可以在回合之间进行切换,在同一位置根据需要调用computer或human的回合数,然后play方法返回由计算机或人执行的“大理石数量”,从大理石计数中减去此返回值

这将一直持续到大理石计数>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;

}
}

希望这有帮助