0

我在 9 年级的 AP 计算机科学专业,我们得到了一个制作游戏的项目。我选择了我的老师建议的一个简单的智游戏。但我想不通的是如何在计算机和播放器之间进行交替转动。

import java.util.Scanner;

/**
 * Game of Chi .. see main for gameplay
 * 
 * @author .....
 */
public class GameOfChi {

private static int numStonesLeft = 0;

public static void main(String[] args) {
    numStonesLeft = (int) (Math.random() * 16) + 15;// btw 15 & 30 stones
    System.out.println("This is the Game of Chi.");
    System.out.println("There is a pile of " + numStonesLeft
            + " stones between us.");
    System.out.println("We alternate taking either 1,2 or 3 stones.");
    System.out.println("The person who takes the last stone loses");

    // Write a loop to alternate computerTurn() and playerTurn()
    // checking after each turn see if there is a winner to print
    // and to break the loop ... then output the winner

    computerTurn(); // invoke the computerTurn() method
    playerTurn(); // invoke the playerTurn() method
while (numStonesLeft> 0);


}

/**
 * The computerTurn method chooses a random number from 1 to 3 if
 * numStonesLeft is greater than or equal to 3, otherwise chooses a random
 * number from 1 to numStonesLeft.
 * 
 * Then decrements numStonesLeft appropriately and prints the turn.
 */
public static void computerTurn() {
    int stonesChosen = 1 + (int) (Math.random() * Math
            .min(3, numStonesLeft));
    numStonesLeft -= stonesChosen;
    System.out.println("\nI took " + stonesChosen + " stones.");
    System.out.println("There are " + numStonesLeft + " stones left.");
}

/**
 * The playerTurn method prompts the user for a valid number of stones to
 * choose and reads an int value from the user and will repeat this action
 * while the user input is invalid. (i.e. user must choose 1, 2 or 3 AND
 * their choice must be less than or equal to numStonesLeft.)
 * 
 * Also decrements numStonesLeft appropriately and prints the turn.
 */
public static void playerTurn() {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Number of stones you take this turn:");
    int stonesChosen = 0;
    stonesChosen = keyboard.nextInt();

    while (stonesChosen > 3 || stonesChosen < 1) {
        ;
        System.out.println("That is an invalid number of stones.");
        stonesChosen = keyboard.nextInt();
    }

    if (stonesChosen <= 3 || stonesChosen >= 1)
        ;
    {
        System.out.println("\nYou took " + stonesChosen + " stones.");
        System.out.println("There are " + (numStonesLeft - stonesChosen)
                + " stones left.");
    }
    stonesChosen = keyboard.nextInt();

}
}
4

2 回答 2

1

在您的 main 方法中创建某种形式的 while 循环来检查游戏是否结束。

while(!gameEnded)
{
    playerTurn();
    computerTurn();
}

除非你问如何让电脑先播放,然后下一个回合玩家先播放。在这种情况下,添加一个计数器来跟踪最后一个去的人。

while(!gameEnded)
{
    if(counter%2 = 0)
    {
        playerTurn();
        computerTurn();
    }
    if(counter%2 = 1)
    {            
        computerTurn();
        playerTurn();
    }
    couter++;
}

要随机选择第一个回合的玩家,您可以使用奇数或偶数初始化计数器。

int counter = Math.random()*2;
于 2013-11-07T01:28:43.303 回答
0
do {
    //
    // Other code from above as required.
    // ...
    //

    computerTurn(); // invoke the computerTurn() method
    //
    // Can numStonesLeft be 0 after the computer turn?
    // If so, perhaps you need an extra check...
    //
    playerTurn(); // invoke the playerTurn() method  
} while (numStonesLeft> 0);
于 2013-11-07T01:27:50.080 回答