Tuesday, 15 June 2010

java - Monopoly game problems -



java - Monopoly game problems -

i have problem in class, shipping, in monopoly game. want find out how many shippings player owns , thereby determine rent. (it danish version of game, think rules differ country country). utilize collections.frequency reason goes else statement , gives me 8 times rent. here code:

package fields; import java.util.collections; import java.util.scanner; public class shipping extends ownable { public int rent; private int occurence; private string yesorno; private shipping shipping; scanner scan = new scanner(system.in); public shipping(int fieldnumber, string fieldname, int price) { super(fieldnumber, fieldname, price); this.rent = 500; } public int getrent() { homecoming rent; } @override public int getprice() { homecoming price; } @override public string getname() { homecoming fieldname; } @override public int getnumber() { homecoming fieldnumber; } @override public matador.player getowner() { homecoming owner; } @override public void getconsequence() { if(getowner() == null) { system.out.println("do want purchase it? (y/n)"); yesorno = scan.next(); if(yesorno.equalsignorecase("y")) { matador.main.currentplayer.money -= getprice(); owner = matador.main.currentplayer; matador.main.currentplayer.ownedgrounds.add((shipping) data.board[matador.main.currentplayer.getposition()]); system.out.println(matador.main.currentplayer.getname() + " bought " + getname()); } else { system.out.println(matador.main.currentplayer.getname() + " did not purchase " + data.board[matador.main.currentplayer.getposition()].getname()); } } else if(getowner() != matador.main.currentplayer) { occurence = collections.frequency(owner.ownedgrounds, shipping); if(occurence == 1) { system.out.println(matador.main.currentplayer.getname() + " needs pay " + owner.getname() + " " + getrent()); matador.main.currentplayer.money -= getrent(); owner.money += getrent(); } else if(occurence == 2) { system.out.println(matador.main.currentplayer.getname() + " needs pay " + owner.getname() + " " + (2 * getrent())); matador.main.currentplayer.money -= (2 * getrent()); owner.money += (2 * getrent()); } else if(occurence == 3) { system.out.println(matador.main.currentplayer.getname() + " needs pay " + owner.getname() + " " + (4 * getrent())); matador.main.currentplayer.money -= (4 * getrent()); owner.money += (4 * getrent()); } else { system.out.println(matador.main.currentplayer.getname() + " needs pay " + owner.getname() + " " + (8 * getrent())); matador.main.currentplayer.money -= (8 * getrent()); owner.money += (8 * getrent()); } } } }

i think problem object seek frequency of, have no idea.

thanks in advance!

regards!

occurence = collections.frequency(owner.ownedgrounds, shipping);

collections.frequency relies on object's equals method compare objects in collection object occurrences wish find. since shipping class doesn't seem override equals, uses object's default implementation, compares references.

therefore frequency of 0 (unless pass shipping reference in collection, in case frequency of 1, still wrong result).

java collections frequency

No comments:

Post a Comment