简体   繁体   English

如何从if语句动态调用对象的引用名称?

[英]How do I dynamically call a reference name of an object from an if statement?

Please forgive my terminology and lack of understanding of Objects in Java. 请原谅我的术语,并且对Java中的对象缺乏理解。 I'm making a console Monopoly game with some classes, Monopoly, Players, Property etc with Monopoly having my main in it. 我正在制作带有某些类的游戏机大富翁游戏,例如大富翁,玩家,财产等,而大富翁的主要角色是游戏。 I've declared Property and Player objects in the Monopoly class. 我已经在Monopoly类中声明了Property和Player对象。 After a player has landed on a bought property I'm trying to dynamically call the owner of that property to add money into its .money variable of that player from the Property class. 玩家登陆到购买的属性后,我试图动态调用该属性的所有者,以从Property类中向该玩家的.money变量添加资金。

I've tried making a method with if statement to return a Player object as well as just a method that would return a String version of the name of the owner of the property. 我尝试过使用if语句返回一个Player对象的方法,以及只返回该属性所有者名称的String版本的方法。 My problem is I don't know how to use that to actually use as a reference to the player that owns it. 我的问题是我不知道该如何使用它来实际引用拥有它的玩家。

Property Class 物业类别

public class Property {


    boolean available=false;
    Object owner;
    int rent = 0;
    int houses = 0;
    int hotels = 0;



}

Players Class 玩家班

public class Players{
        Players(){
            int money=1500;
    }

        String name="";
        int money=0;
        int pos=0;
        int goCount=0;
        int diceDoubles=0;
        int rollsInJail=0;
        boolean outOfJailFreeCard=false;
        boolean jail=false;
        String player="";

        Scanner in=new Scanner(System.in);
        String getName(){
            name=(String) in.next();
            return name;
        }

    }

Position Class 职位等级

public class Position {


public static Players playerFind(Object str, Players obj){
    if(str.equals(Monopoly.player1)){
        return Monopoly.player1;
    }
    else if(str.equals(Monopoly.player2)){
        return Monopoly.player2;
    }
    else if(str.equals(Monopoly.player3)){
        return Monopoly.player3;
    }
    else if(str.equals(Monopoly.player4)){
        return Monopoly.player4;
    }
    else if(str.equals(Monopoly.player5)){
        return Monopoly.player5;
    }
    else if(str.equals(Monopoly.player6)){
        return Monopoly.player6;
    }
    return obj;
}    

public static void place(int num, Players obj, Property prop)
    {
        Scanner in=new Scanner(System.in);



        if(num==1) {

            System.out.println(obj.name+" landed on Mediterranean Avenue.");
            if(Monopoly.mediterranean.available==true){
                System.out.println("Would you like to buy this property?(Yes or No)");
                String str = in.next();
                if(str.startsWith("y") || str.startsWith("Y")){
                    Monopoly.mediterranean.available=false;
                    Monopoly.mediterranean.owner=obj;
                    obj.money-=60;
                    System.out.println("You now own Meditarreanean Avenue!");
                    System.out.println("Your balance is "+obj.money);
                }
            }
            else{
                Players obj2 = playerFind(Monopoly.mediterranean.owner, obj);    
                System.out.println("Rent is due! "+Monopoly.mediterranean.owner+" owns this property. M2 has been subtracted from your account"
                       + " and added into "+Monopoly.mediterranean.owner+"'s bank roll.");
                Players.obj2.money+=2;
                System.out.println("Your balance is "+obj.money);

                }
        }
        else if(num==2) {
            System.out.println(obj.name+" landed on Community Chest.");


        }//etc...

Monopoly Class 垄断类

public class Monopoly {
        static Players player1 = new Players();
        static Players player2 = new Players();
        static Players player3 = new Players();
        static Players player4 = new Players();
        static Players player5 = new Players();
        static Players player6 = new Players();
        //static Position position= new Position();
        static CommunityChest chest=new CommunityChest();
        static Property property=new Property();
        static Property mediterranean=new Property();
        static Property baltic=new Property();
        static Property reading=new Property();
        static Property oriental=new Property();
        static Property vermont=new Property();
        static Property connecticut=new Property();
        static Property stCharles=new Property();
        static Property states=new Property();
        static Property virginia=new Property();
        static Property pennRail=new Property();
        static Property stJames=new Property();
        static Property tennessee=new Property();
        static Property newYork=new Property();
        static Property kentucky=new Property();
        static Property indiana=new Property();
        static Property illinois=new Property();
        static Property bAndC=new Property();
        static Property atlantic=new Property();
        static Property ventor=new Property();
        static Property marvin=new Property();
        static Property pacific=new Property();
        static Property northCar=new Property();
        static Property pennAve=new Property();
        static Property shortLine=new Property();
        static Property parkPlace=new Property();
        static Property board=new Property();
        static Property electric=new Property();
        static Property water=new Property();

        static int playerNum=intro();

public static void play(int num, int rollCount){
        int roll1=0, roll2, i=0;


        while(i<rollCount-1){
            int i1=i+1;
            System.out.println("Beginning of turn "+i1);

            if(num>0){
                turn(player1);
                System.out.println(player1.name+" money count:M"+player1.money);
            }
            if(num>1){
                turn(player2);
                System.out.println(player2.name+" money count:M"+player2.money);
            }
            if(num>2){
                turn(player3);
            }
            if(num>3){
                turn(player4);
            }
            if(num>4){
                turn(player5);
            }
            if(num==6){
                turn(player6);
            }
            System.out.println("End of turn "+i1+"\n");
            i++;

        }
    }

    public static void turn(Players obj){
        Scanner in1=new Scanner(System.in);

        int roll1=roll();
        int roll2=roll();
        obj.pos=obj.pos + roll1 + roll2;
        System.out.println(obj.name+" rolled a "+roll1+" and a "+roll2);
        if(obj.jail==false){
            System.out.println(obj.name + " has landed on "+place(obj.pos));
        }

        if(roll1!=roll2){
            obj.diceDoubles=0;
        }

        Position.place(obj.pos, obj, property);

        if(obj.jail==true){//roll while in jail, check first in case they roll doubles and continue their turn. Turn must end after going to jail
            if(obj.outOfJailFreeCard==true){//if get out of jail card, prompt to use it rightaways
                    System.out.println("You've rolled thrice doubles in a row and have ended in up jail. Luckily you have a 'Get out of jail free' card!");
                    System.out.print("Would you like to use your 'Get out of jail free' card right now?: (Y/N)");
                    String check=in1.next();
                    if(check.startsWith("y") || check.startsWith("Y")){
                        obj.pos=10;
                        obj.outOfJailFreeCard=false;
                    }
                }
            if(roll1!=roll2){
                obj.rollsInJail+=1;
                if(obj.rollsInJail==3){
                    obj.pos=10+roll1+roll2;
                    obj.money-=50;
                    obj.rollsInJail=0;
                }return;//does this break out of method to end turn??
            }
            if(roll1==roll2){
                obj.pos=10+roll1+roll2;
                obj.rollsInJail=0;
            }return;//does this break out of method to end turn??
        }

        while(roll1==roll2 && obj.diceDoubles<3){//check for doubles
            obj.diceDoubles+=1;
            if(obj.diceDoubles==3){
                obj.jail=true;
                obj.diceDoubles=0;
                System.out.println("You've rolled doubles 3 times in a row. You are now in jail!");
                break;
            }
            else{turn(obj);
            break;}
        }

        if(obj.pos>39){//pass go, collect M200
            obj.pos=obj.pos - 39;
            obj.goCount=obj.goCount+1;
            obj.money+=200;
        }

        in1.close();// close scanner
    }

    public static void main(String[] args) {

        play(playerNum, 60);
    }

I'm trying to dynamically find the owner and make a reference to the actual object instance to add to its .money variable. 我试图动态查找所有者,并引用要添加到其.money变量的实际对象实例。

The problem is that you have owner as an Object reference, which doesn't have the money field. 问题在于您将所有者作为Object引用,而该引用没有money字段。 You need to use the Player class instead: 您需要改用Player类:

public class Property
{
 boolean available=false;
 Player owner;
 int rent = 0;
 int houses = 0;
 int hotels = 0;
}

And then you can directly move the rent between the two players, eg: 然后,您可以直接在两个玩家之间移动租金,例如:

Property property = Monopoly.mediterranean;
activePlayer.money-= property.rent;
property.owner.money+= property.rent;

In a real game you would then add more checks, eg does the player have enough money to cover the rent or are there problems with parallelism that need to be covered, but that is the basic solution to your problem. 在真实游戏中,您需要添加更多支票,例如玩家是否有足够的钱来支付租金,或者是否存在需要解决的并行性问题,但这是解决问题的基本方法。

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

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