Java 小游戏 新手上路

自己写了一个像大富翁一样的小游戏,以动物为原型的,java新手上路。各路大神请多指教。大家给我看看有没有什么可以改进的地方,,,?
在这里插入图片描述

?import java.util.Scanner;

public class Game
{
private int trailLength;
private Trail natureTrail;
private Player playerOne = new Player();
private Player playerTwo = new Player(“Computer”);
private Player currentTurn = null; // indicates current player turn. Player one: human, Player two: computer
private Dice dice = new Dice(1, 4);

// Display a welcome message on the screen
public void displayWelcomeMessage()
{
    System.out.println("Welcome to play Nature Trail, Have fun !!!");
}

// Request the length of the trail.
public void getInputForTrailLength()
{
    System.out.println("Please enter the length of the trail(A number between 10-20):");
    Scanner scanner = new Scanner(System.in);
    int length = scanner.nextInt();
    while (length>20 || length < 10)
    {
        System.out.println("Invalid input, please enter again:");
        length = scanner.nextInt();
    }
    this.trailLength = length;
}

// Request the human player to enter their name.
public void getInputForPlayerName()
{
    System.out.println("Please enter the your name:");
    System.out.println("The name must be between 1 and 6 characters in length and not begin or end with a space character");
    Scanner scanner = new Scanner(System.in);
    String name = scanner.nextLine();
    while (name.length() > 6 || name.length() < 1 || name.charAt(0)=='\0' || name.charAt(name.length()-1) == '\0')
    {
        System.out.println("Invalid input, please enter again:");
        name = scanner.nextLine();
    }
    playerOne.setName(name);
}

// Roll the dice.
public int rollDice(Player player)
{
    // if player is computer
    if(player == playerTwo)
    {
        System.out.println("Player Computer is rolling the dice...");
        int diceValue = dice.generateRandomNumber();
        System.out.println("The dice value is " + diceValue);
        return diceValue;
    }
    System.out.println("Please roll the dice by press Enter:");
    Scanner scanner = new Scanner(System.in);
    String enter = scanner.nextLine();
    while (!enter.equals(""))
    {
        System.out.println("Invalid input, enter again:");
        enter = scanner.nextLine();
    }
    int diceValue = dice.generateRandomNumber();
    System.out.println("Dice value is " + diceValue);
    return diceValue;
}

// Move the player a number of places.
public void move(Player player, int step)
{
    System.out.println("Moving " + player.getName() + " to the right place");
    int newPos = player.getPosition() + step;
    if(newPos < 0)
    {
        player.setPosition(0);
    } 
      else 
      {
        player.setPosition(newPos);
      }
}

// Request the player object to increment their score.
public void increasePlayerScore(Player player, int score)
{
    player.increaseScore(score);
    System.out.println("Player "  + player.getName() + "'s score increased " + score + ". Latest score is " + player.getScore());
}

// Randomly generate the positions of the features on the nature trail.
public void generateNatureTrailRandomPosition()
{
    // initialise nature features
    natureTrail = new Trail(trailLength);
}

// Display the nature trail with the position of each player.
public void displayNatureTrail()
{
    System.out.print("S");
    for(int i=0; i<trailLength; i++)
    {
        boolean isEmpty = true;
        if(playerOne.getPosition() == i)
        {
            isEmpty = false;
            System.out.print("H");
        }
        if(playerTwo.getPosition() == i)
        {
            isEmpty = false;
            System.out.print("C");
        }
        if(isEmpty)
        {
            System.out.print("_");
        }
    }
    System.out.println("F");
}

// Player takes a turn.
public Player takeTurn()
{
    if(currentTurn == null)
    {
        System.out.println("Please press enter to roll dice to determine the first turn");
        Scanner scanner = new Scanner(System.in);
        scanner.nextLine();
        int humanDice = dice.generateRandomNumber();
        int computerDice = dice.generateRandomNumber();
        while(humanDice == computerDice)
        {
            humanDice = dice.generateRandomNumber();
            computerDice = dice.generateRandomNumber();
        }
        System.out.println("Human dice value: " + humanDice);
        System.out.println("Computer dice value: " + computerDice);
        if(humanDice > computerDice)
        {
            System.out.println("Human has the first turn!\n");
            currentTurn = playerOne;
            return playerOne;
        } 
          else 
          {
            System.out.println("Computer has the first turn!\n");
            currentTurn = playerTwo;
            return playerTwo;
          }
    }
    currentTurn = (currentTurn == playerOne) ? playerTwo : playerOne;
    System.out.println();
    System.out.println("Now it is Player " + currentTurn.getName() + "'s turn");
    return  currentTurn;
}

// Display the game result, return true if game is over
public boolean displayResult(Player player)
{
    System.out.println("Player " + player.getName() + "'s turn ended!");
    System.out.println("The score of Player " + player.getName() + " is " + player.getScore());
    if(player.getPosition()>=(trailLength-1))
    {
        System.out.println("Player " + player.getName() +  " reached the finish position!");
        increasePlayerScore(player, 10);
        System.out.println("Player " + playerOne.getName() + "'s final score is " + playerOne.getScore());
        System.out.println("Player " + playerTwo.getName() + " 's final score is " + playerTwo.getScore());
        if(playerOne.getScore() > playerTwo.getScore())
        {
            System.out.println("Player " + playerOne.getName() + " wins the game with higher score! Congratulations!");
        } 
          else
          {
            System.out.println("Player " + playerOne.getName() + " wins the game with higher score! Game over!");
          }
        return true;
    }
    return false;
}

// to determined whether player sight an animal or not, and also the animal type.
public void sightingAnimal(Player player)
{
    int x = 1 + (int)(Math.random() * 1);   //  1 indicates sighting an animal
    if(x == 1)
    {
        int animalType = 1 + (int)(Math.random() * 5);
        // case 1:Koala(10), case 2: Emu(7), case 3: Wombat(5), case 4: Kangaroo(2), case 5: Redback spider(-5)
        switch (animalType)
        {
            case 1:
                System.out.println("Player" + player.getName() + " is sighting a Koala");
                increasePlayerScore(player, 10);
                break;
            case 2:
                System.out.println("Player" + player.getName() + " is sighting an Emu");
                increasePlayerScore(player, 7);
                break;
            case 3:
                System.out.println("Player" + player.getName() + " is sighting a Wombat");
                increasePlayerScore(player, 5);
                break;
            case 4:
                System.out.println("Player" + player.getName() + " is sighting a Kangaroo");
                increasePlayerScore(player, 2);
                break;
            case 5:
                System.out.println("Player" + player.getName() + " is sighting a Redback spide");
                increasePlayerScore(player, -5);
                break;
            default:
        }
    } 
      else 
      {
        System.out.println("Player " + player.getName() +  " does not sight any animal in this place.");
      }
}

public Trail getNatureTrail() 
{
    return natureTrail;
}

public Player getPlayerOne() 
{
    return playerOne;
}

public Player getPlayerTwo() 
{
    return playerTwo;
}

public int getTrailLength() 
{
    return trailLength;
}

public void setTrailLength(int trailLength) 
{
    this.trailLength = trailLength;
}

public void setNatureTrail(Trail natureTrail) 
{
    this.natureTrail = natureTrail;
}

public void setPlayerOne(Player playerOne)
{
    this.playerOne = playerOne;
}

public void setPlayerTwo(Player playerTwo) 
{
    this.playerTwo = playerTwo;
}

public Player getCurrentTurn() 
{
    return currentTurn;
}

public void setCurrentTurn(Player currentTurn) 
{
    this.currentTurn = currentTurn;
}

public static void main(String[] args)
{
    Game game = new Game();
    game.displayWelcomeMessage();
    game.getInputForTrailLength();
    game.generateNatureTrailRandomPosition();
    game.getInputForPlayerName();
    Player currentPlayer = game.takeTurn();
    while (game.getPlayerOne().getPosition() < game.getTrailLength()
            || game.getPlayerTwo().getPosition() < game.getTrailLength())
    {
        int diceNum = game.rollDice(currentPlayer);
        game.move(currentPlayer, diceNum);
        // to determine whether player sight an animal here
        game.sightingAnimal(currentPlayer);
        // checking if the new place has nature feature
        for(NatureFeature feature : game.getNatureTrail().getFeatures())
        {
            if(feature.getFeaturePosition() == currentPlayer.getPosition())
            {
                System.out.println("Player" + currentPlayer.getName() + "encounter nature feature " + "\"" + feature.getFeatureType() + "\"");
                game.move(currentPlayer, feature.getSpacePenalty());
            }
        }
        game.displayNatureTrail();
        boolean gameOver = game.displayResult(currentPlayer);
        if(gameOver)
        {
            break;
        }
        currentPlayer = game.takeTurn();
    }
}

}

?public class Player {
private String name;
private int position = 0;
private int score = 0;

public Player(){

}

public Player(String name){
    this.name = name;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getPosition() {
    return position;
}

public void setPosition(int position) {
    this.position = position;
}

public int getScore() {
    return score;
}

public void increaseScore(int score) {
    this.score += score;
}

}

?public class Dice
{
private int maximumValue;
private int minimumValue;

public Dice(int min, int max)
{
    this.maximumValue = max;
    this.minimumValue = min;
}

public int generateRandomNumber()
{
    return minimumValue+ (int)(Math.random()* maximumValue);
}

public int getMaximumValue() 
{
    return maximumValue;
}

public void setMaximumValue(int maximumValue) 
{
    this.maximumValue = maximumValue;
}

public int getMinimumValue() 
{
    return minimumValue;
}


public void setMinimumValue(int minimumValue) 
{
    this.minimumValue = minimumValue;
}

}

?public class Trail
{
private NatureFeature[] features;

public Trail (int trailLength)
{
    NatureFeature[] natureFeatures = new NatureFeature[4];
    natureFeatures[0] = new NatureFeature(((int) (Math.random() * trailLength)), "Creek", -2);
    natureFeatures[1] = new NatureFeature(((int) (Math.random() * trailLength)), "Bridge", 4);
    natureFeatures[2] = new NatureFeature(((int) (Math.random() * trailLength)), "Fallen tree", -3);
    natureFeatures[3] = new NatureFeature(((int) (Math.random() * trailLength)), "Landslide", -5);
    this.features = natureFeatures;
}

public NatureFeature[] getFeatures() 
{
    return features;
}

public void setFeatures(NatureFeature[] features) 
{
    this.features = features;
}

}

?public class NatureFeature {
private int featurePosition;
private String featureType;
private int spacePenalty;

public NatureFeature(int featurePosition, String featureType, int spacePenalty){
    this.featurePosition = featurePosition;
    this.featureType = featureType;
    this.spacePenalty = spacePenalty;
}

public int getFeaturePosition() {
    return featurePosition;
}

public String getFeatureType() {
    return featureType;
}

public int getSpacePenalty() {
    return spacePenalty;
}

public void setFeaturePosition(int featurePosition) {
    this.featurePosition = featurePosition;
}

public void setFeatureType(String featureType) {
    this.featureType = featureType;
}

public void setSpacePenalty(int spacePenalty) {
    this.spacePenalty = spacePenalty;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值