PDA

View Full Version : Making a Matador/monopoly game



Cartmann
09-02-2010, 11:02 AM
Hello, I've been trying to make this monopoly game, as a part of learning the language.

Since my native tounge is danish, and the game is a version of the danish game called "matador" I've tried translating it to English, hopefully making it easier for you guys :P

A picture of the Matador game: http://www.matador-dm.dk/grafik/foto/matador044.jpg

Here we go:


public class Field
{
String name;
public void pass(Player pl)
{
pl.message("You are now passing "+name);
}
public void landed(Player pl)
{
}
}


public class Player
{
String name;
double account;
int fieldnr;

public Player(String name, double account)
{
this.name = name;
this.account = account;
fieldnr = 0;
}
public void message(String message)
{
System.out.println(name+": "+message);
}
public boolean question(String question)
{
String que = name+": Do you want to "+question+"?";
String answer = javax.swing.JOptionPane.showInputDialog(spm, "yes");
System.out.println(spm+" "+svar);
if (svar!=null && svar.equals("yes")) return true;
else return false;
}

public void transaktion(double dollars)
{
account = account + dollars;
System.out.println(name+"s your account now holds "+account+" dollars.");
}

public void pay(Player rechiever, double dollars)
{
System.out.println(name+" pays "+rechiever.name+": "+dollars+" dollars.");
rechiever.transaktion(dollars);
transaktion(-dollars);
}
}



public class Helle extends Field /* helle is like a safespot in which u cannot be harmed */
{
double winnings;

public Helle (int winnings)
{
name = "Helle";
this.winnings = winnings;
}

public void landed(Player pl) /
{
pl.message("You are landing on helle and rechieves "+winnings);
sp.transaktion(winnings);
}
}


public class Start extends Field
{
double winnings;

public Start(double winnings)
{
name = "Start";
this.winnings = winnings;
}

public void pass(Player pl)
{
pl.message("you pass start and rechieves "+winnings);
pl.transaktion(winnings);
}
public void landed(Player pl)
{
pl.message("Du lander på start og modtager "+gevinst);
pl.transaktion(gevinst);
}
}


public class Lot extends Field
{
Player owner;
double price;
double lotRent;

public Lot(String name, double price, double rent)
{
this.name=name;
this.price=price;
this.lotRent=rent;
}

public double calculateRent()
{
return lotRent;
}

public void landed(Player pl)
{
pl.message("You have landed on "+name);
if (pl==owner)
{
pl.message("It's your own lot");
}
else if (owner==null)
{
if (pl.account > price)
{
if (pl.question("buy "+name+" for "+price))
{
pl.transaktion( -price );
owner=pl;
}
}
else pl.message("You can't afford to buy "+name);
}
else
{
double rent = calculateRent();
pl.message("Rent: "+rent);
pl.pay(owner, rent);
}
}
}


public class Refinery extends Lot
{
public Refinery(String name, double price, double rent)
{
super(name, price, rent);
}
}


public class Street extends Lot
{
int numberOfHouses;
double housePrice;

public Street(String name, double price, double rent, double housePrice)
{
super(name, price, rent); r
this.housePrice =housePrice;
numberOfHouses = 0;
}

public double calculateRent()
{
return lotRent + numberOfHouses * housePrice;
}

public void landed(Player pl)
{
super.landed(pl);
if (pl==owner)
{
if (numberOfHouses<5 && pl.konto>housePrice && pl.question("houses are aviable for "+pris))
{
owner.transaktion( -housePrice );
numberOfHouses = numberOfHouses + 1;
pl.message("ou are building a house on "+name+" for "+housePrice);
}
}
}
}



import java.util.*;

public class MonopolyGame
{
ArrayList<Field> fields = new ArrayList<Field>();

ArrayList<Player> Players = new ArrayList<Player>();

int playersTurn = 0;

public MonopolyGame()
{
fields.add(new Start(5000));
fields.add(new Street("Wallstreet", 10000, 400,1000));
fields.add(new Street("Beverly hills",10000, 400,1000));
fields.add(new Refinery("BP", 17000,4200));
fields.add(new Street("Venice beach", 12000, 500,1200));
fields.add(new Street("Hollywood boulevard", 12000, 500,1200));
fields.add(new Street("The strip", 15000, 700,1500));
fields.add(new Safespot(15000));
fields.add(new Street("Time square", 20000,1100,2000));
fields.add(new Street("The presidential Ave", 20000,1100,2000));
}
}


public class UsageOfTheMonopolyGame
{
public static void main(String[] arg)
{
MonopolyGame game = new MonopolyGame();
game.Players.add(new Player("Stallone",50000));
game.Players.add(new Player("Gitte",50000));

// løb gennem 20 runder (40 ture)
for (game.playersTurn=0; game.playersTurn<40; game.playersTurn++)
{
// tag skiftevis Søren og Gitte (% er forklaret i afsnit 2.11.4)
Player pl = game.Players.get(game.playersTurn % game.Players.size());
int toss = (int)(Math.random()*6)+1;
System.out.println("***** "+pl.name+" on field "+pl.fieldnr+" throws a "+toss);

for (int i=1; i<=toss; i=i+1)
{
pl.fieldnr = (pl.fieldnr + 1) % game.fields.size();
Field field = game.fields.get(pl.fieldnr);

if (i<toss) field.pass(pl);
else field.landed(pl);
try { Thread.sleep(300); } catch (Exception e) {}
}
try { Thread.sleep(3000); } catch (Exception e) {} // tur slut, vent 3 sek
}
}
}

Oki i can't make this run right :P i don't know why, I'm going to add the original game (which is in danish, but the programming should be the same just to correct flaws)

If any1 could help me it'll be appreciated and added +rep.

i luffs yeww
09-02-2010, 06:02 PM
Gotta go to the office but I'll check back later.

Offtopic, SRL-Forums > Programming > Programming > Java Programming ? Why is it Programming > Programming?

Cartmann
09-02-2010, 06:33 PM
Offtopic, SRL-Forums > Programming > Programming > Java Programming ? Why is it Programming > Programming?

whut :P?
Do you want me to post it somewhere else? I just presumed since it was from my java programming book that it'll suit in here.

i luffs yeww
09-02-2010, 07:12 PM
No, it's fine, I just don't see why there's a section called Programming under Programming. :)

What's the problem exactly by the way?

Cartmann
09-02-2010, 07:18 PM
no idear, it just wont run, afaik there's no flaws in it o.O

Cartmann
09-06-2010, 08:33 AM
I've fixed all of the flaws now, no red squares in my eclipse, yet im still getting this when running it:

at Street.<init>(Street.java:8)
at MonopolyGame.<init>(MonopolyGame.java:14)
at UsageOfTheMonopolyGame.main(UsageOfTheMonopolyGame .java:5)