【问题标题】:Syntax and logic for moving player around game board array using do while loop java programming使用 do while 循环 java 编程在游戏板阵列中移动玩家的语法和逻辑
【发布时间】:2013-11-17 07:20:59
【问题描述】:

我试图让玩家在 java 中的二维板数组中移动,同时利用访问器和修改器方法来更新我的对象的属性,所以我将尝试尽可能深入地提出这个问题。我不理解正确移动播放器并返回所需信息所必须使用的逻辑或语法。我正在使用开发的代码。我的工作是在棋盘上移动玩家并从垄断文本文件中返回信息,包括玩家位置和新的银行余额。此外,如果银行余额为零,我必须询问玩家是否想继续并停止游戏。我真的需要有关 main 方法中的 do while 循环的帮助。我无法使语法正确,并且对逻辑的工作原理没有很好的理解。到目前为止,我正在发布我的所有代码。

package monopoly;

import java.util.*;

public class Monopoly {

/**
 * @param args the command line arguments
 */

public static void main(String[] args) throws Exception {

    BoardSquare[] square = new BoardSquare[40]; // array of 40 monopoly squares


    Player thePlayer = new Player();//new object thePlayer from Player class
    thePlayer.getLocation();//call method getLocation which should be instantiated from player class to zero
    int i;
    loadArray(square);

    Dice diceOne = new Dice();//new dice object
    Dice diceTwo = new Dice();//new dice object
    int rollOne;    //variable to hold rollOne
    int rollTwo;    //variable to hold rollTwo
    int rollTotal;  //variable to hold rollTotal

        do {

            rollOne = diceOne.roll();
            rollTwo = diceTwo.roll();
            rollTotal = rollOne+rollTwo;

            BoardSquare newPosition = square[thePlayer.getLocation() + rollTotal];
        } 

        while (thePlayer.getBalance() > 0);

        // test the code by printing the data for each square

        System.out.println("Data from the array of Monopoly board squares. Each line has:\n");

        System.out.println("name of the square, type, rent, price, color\n");

        for(i = 0; i < 40; i++)

        System.out.println( square[i].toString() );     
}

//**************************************************************

// method to load the BoardSquare array from a data file
public static void loadArray(BoardSquare[] square) throws Exception {

    int i; // a loop counter

    // declare temporary variables to hold BoardSquare properties read from a file
    String inName;    
    String inType;
    int inPrice;
    int inRent;
    String inColor;

    // Create a File class object linked to the name of the file to be read
    java.io.File squareFile = new java.io.File("squares.txt");

    // Create a Scanner named infile to read the input stream from the file
    Scanner infile = new Scanner(squareFile);


    /* 
     * This loop reads data into the square array.
     * Each item of data is a separate line in the file.
     * There are 40 sets of data for the 40 squares.
     */

    for(i = 0; i < 40; i++) {

        // read data from the file into temporary variables
        // read Strings directly; parse integers
        inName  = infile.nextLine(); 
        inType  = infile.nextLine(); 
        inPrice = Integer.parseInt( infile.nextLine() );
        inRent  = Integer.parseInt( infile.nextLine() );;
        inColor = infile.nextLine(); 

        // intialze each square with the BoardSquare constructor
        square[i] = new BoardSquare(inName, inType, inPrice, inRent, inColor);
    } // end for

    infile.close();

}    // endLoadArray

}    // end class Monopoly

//**************************************************************

class BoardSquare {

private String name;    // the name of the square
private String type;    // property, railroad, utility, plain, tax, or  toJail 
private int price;      // cost to buy the square; zero means not for sale
private int rent;       // rent paid by a player who lands on the square 
private String color;   // many are null; this is not the Java Color class

// constructors
public BoardSquare() {
    name = "";
    type = "";
    price = 0;
    rent = 0;
    color = "";
} // end Square()

public BoardSquare(String name, String type, int price, int rent, String color) {
    this.name = name;
    this.type = type;
    this.price = price;
    this.rent = rent;
    this.color = color;
} // end Square((String name, String type, int price, int rent, String color)

// accesors for each property
public String getName() {
    return name;
}   //end getName()

public String getType() {
    return type;
}   //end  getType()

public int getPrice() {
    return price;
}   //end  getPrice()

public int getRent() {
    return rent;
}   //end  getRent()

public String getColor() {
    return color;
}   //end  getColor()

// a method to return the BoardSquare's data as a String
public String toString() {
    String info;
    info = (name +", "+type+", "+price + ", "+ rent+ ", "+color);
    return info;    
}   //end  toString()

} // end class BoardSquare

//**************************************************************

class Player {
    private String name;
    private String token;
    private int location;
    private int balance;
    private String player;

    public Player() {
        name = "";
        token = "";
        location = 0;
        balance = 1500;
} // end Square()

    public Player(String name, String token, int location, int balance) {
        this.name = name;
        this.token = token;
        this.location = location;
        this.balance = balance;       
    }    

/*
 * @return the name
 */

public String getName() {
    return name;
}

/**
 * @param name the name to set
 */

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

/**
 * @return the token
 */

public String getToken() {
    return token;
}

/**
 * @param token the token to set
 */

public void setToken(String token) {
    this.token = token;
}

/**
 * @return the location
 */

public int getLocation() {
    return location;
}

/**
 * @param location the location to set
 */

public void setLocation(int location) {
    this.location = location;
}

/**
 * @return the balance
 */

public int getBalance() {
    return balance;
}

/**
 * @param balance the balance to set
 */

public void setBalance(int balance) {
    this.balance = balance;
}

/**
 * @return the player
 */

public String getPlayer() {
    return player;
}

/**
 * @param player the player to set
 */

public void setPlayer(String player) {
    this.player = player;
}

void setLocation() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

} //end class player

//**************************************************************

class Dice {

    public static int roll() {
        int total;
        total = 1 + (int)(Math.random() * 6);

        return total;
    }
}

【问题讨论】:

  • 编辑并包含输出。并指向错误的输出。

标签: java arrays for-loop logic do-while


【解决方案1】:

首先写下循环中应该发生的事情。我建议以下几点:

  • 板子的打印状态
  • 掷骰子移动玩家
  • 在新的方格上执行操作(即询问玩家是否想购买它或如果该方格属于其他人,则收钱)
  • 太上头了,但现在是下一位玩家

您最好为所有这些步骤编写函数。如前所述,您可能需要多个玩家实例。

编辑:

从句法上讲,您几乎已经拥有了所有内容。

这会打印板子的当前状态,只需将其复制到您需要的地方即可:

for( i=0; i<40; i++)
System.out.println( square[i].toString() );

循环中的代码移动播放器:

rollOne=diceOne.roll();
rollTwo=diceTwo.roll();
rollTotal=rollOne+rollTwo;

BoardSquare newPosition=square[thePlayer.getLocation()+rollTotal];

那么剩下的唯一步骤实际上就是对目标方块执行操作。这可能最好是播放器的一个功能,所以应该是:

player.ActionOnSquare(newPosition);

这需要播放器类中的方法:

public void OperationOnSquare(BoardSquare newSquare)
{
     // Perform some operation, like reduce balance
}

最后,你可以询问玩家是否想继续。提到的步骤 5 和 6(您的意思是写 6 和 7)是多余的,因为这已经在下一个循环的开始。

【讨论】:

  • 感谢您的建议。这项任务实际上现在只需要一名球员。我开发的逻辑如下: 1.根据播放器类中的get方法调用应该初始化为零的播放器对象。 2. 掷骰子 3. 在数组中分配新位置并返回数据 4. 从余额中减去租金 5. 询问玩家是否想再次移动 5. 再次掷骰子 6. 将新位置添加到数组中的先前位置并返回数据 I am不知道如何在我的 do while 循环中的 main 方法中开发语法来执行此操作
猜你喜欢
  • 2015-12-21
  • 1970-01-01
  • 1970-01-01
  • 2012-03-01
  • 1970-01-01
  • 2011-12-13
  • 2015-10-22
  • 2014-08-10
  • 1970-01-01
相关资源
最近更新 更多