如何在Java中进行交替转弯 [英] How to make alternating turns in Java

查看:354
本文介绍了如何在Java中进行交替转弯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我在AP计算机科学的9年级,我们得到了一个制作游戏的项目。我选了一个我老师建议的简单的Game of Chi游戏。但我无法弄清楚如何在计算机和播放器之间进行交替转弯。

Okay so I'm in 9th grade in AP computer science and we were given a project to make a game. I picked a simple Game of Chi thing that my teacher had suggested. But what i cannot figure out is how to make alternating turns between the computer and the player.

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();

}
}


推荐答案

在你的main方法中创建一些while循环来检查游戏是否结束。

in your main method create some form of while loop that checks if the game is over.

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

除非你问如何使电脑首先播放然后转动播放器先发挥。在这种情况下,
添加一个计数器来追踪最后一个人。

unless you are asking how to make it the computer plays first then next turn player plays first. in that case add a counter to track who went last.

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

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

To randomly select the player that goes first on turn one you can initialize the counter with an odd or even number.

int counter = Math.random()*2;

这篇关于如何在Java中进行交替转弯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆