简体   繁体   English

在2D垄断游戏阵列周围移动玩家

[英]moving a player around a 2d monopoly game array

I am creating the player class for a monopoly board game. 我正在为垄断棋盘游戏创建玩家类。 I am not exactly sure how to make the player move around the board and then store that players position. 我不确定如何使玩家在棋盘上移动然后存储该玩家的位置。 I have created an array with 40 places using 我已经创建了一个包含40个位置的数组

    BoardSquare[] square = new BoardSquare[40];

and i have created two die using 我创建了两个模具

    diceOne=1+(int)(Math.random()*6);
    diceTwo=1+(int)(Math.random()*6);

    roll=diceOne+diceTwo;

this is the code for my player class so far 这是到目前为止我的玩家课程的代码

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

public Player()

{
        name = "";
        token = "";
        location = 0;
        balance = 1500;
        player = (name+token+location+balance);

} 


public Player(String name, String token, int location, int balance)

{
        this.name = name;
        this.token = token;
        this.location = location;
        this.balance = balance;
}

i realize i need to initialize a player to zero and then add the value of the rolled die to give the plahyer a position on the board. 我意识到我需要将一个播放器初始化为零,然后添加滚动骰子的值,以便使弹子板在棋盘上处于一个位置。 However, i am really not sure what happens when space runs out, how to properly develop the for loop, etc. I have looked at some examples but i am not really sure how to properly develop the logic for a board game. 但是,我真的不确定当空间用尽时会发生什么,如何正确开发for循环等。我看过一些示例,但我不确定如何正确开发棋盘游戏的逻辑。 Any suggestions about board game development that are outside of the scope of this question are most welcome. 我们欢迎任何关于棋盘游戏开发的建议,不在此问题范围之内。 thx. 谢谢。

You can use the modulo operator to wrap positions around, eg: 您可以使用模运算符将位置换行,例如:

int newPosition = (oldPosition + diceRoll) % 40;

As for passing Go you have many options. 至于通过Go,您有很多选择。 It's simplest if you define Go as index 0. You can compute the position before modulo 40 and check if its greater than 40 (meaning its about to wrap around). 如果将Go定义为索引0,这是最简单的。您可以在对40取模之前计算位置,并检查其是否大于40(意味着将要折回)。 You can also check to see if the new position is less than the old position (which works presuming you can never have a dice roll >= 40). 您还可以检查新位置是否小于旧位置(假设您永远无法使骰子掷骰大于40,则可以正常工作)。

Just before updating the new position of the player ,do the following check: 在更新播放器的新位置之前,请进行以下检查:

if(position>40)
{
position=position-40
}

update the changed position with the specifics of money 用金钱的细节更新改变的头寸

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM