Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a GUl-based program that displays a Monopoly game. Add labels for the four

ID: 3739801 • Letter: W

Question

Write a GUl-based program that displays a Monopoly game. Add labels for the four train stations, the free space and each of the property spaces. Add buttons for all the Chance cells and set the text of these to a question mark. When the user clicks on one of the buttons, set its text to a message of your choice, chosen randomly from four messages. On the jail spot, create a drop-down list with the choices, "Just Visiting" or "In Jail. If just visiting, display a message, Good to have a visitor." If in jail, display a message, "Crime doesn't pay!'. If you add images to the labels, you can receive .5 points per image MONOPOLY

Explanation / Answer

import monopoli.*;

public class Main {

public static void main(String[] args){

try{

if(args.length <=0)

System.out.println("Usage: java Monopoly [c|g] [t]");

else {

Game g = new Game(args[0]);

if(args.length > 1)

g.newCheatGame();

else

g.newGame();

}

}

catch(Exception e){

System.out.println(e);

}

}

}

package monopoli;

import user_interface.*;

import java.util.*;

public class Game {

private static Interactive ui;

private static Dice dice = new Dice();

public static Board board = null;

private static Bank bank = new Bank();

private static ArrayList<Player> inGame = new ArrayList<Player>();

private static ArrayList<Player> gameOvers = new ArrayList<Player>();

private static Deck community = null;

private static Deck chance = null;

private boolean endTurn = false;

private boolean endGame = false;

private Player current = null;

private int doubles = 0;

private int turns = 0;

public Game(String arg) throws Exception{

if (arg.equals("c"))

ui = new CommandLine();

else

throw new Exception("Feature not implemented yet.");

}

public void gameSetup(){

try{

board = new Board();

community = new Deck("properties/community.properties");

chance = new Deck("properties/chance.properties");

}

catch (Exception e){

ui.printException("Eccezione in game setup: ");

ui.printException(e.getMessage());

}

playerSetup();

}

public void newGame(){

ui.welcomeScreen();

gameSetup();
begin();

}

public void newCheatGame(){

System.out.println("Cheater!");

ui.welcomeScreen();

playerSetup();

}

public void playerSetup(){

int n=0;

while (n<2 || n>6){

n = ui.askNumPlayers();

if (n<2 || n>6)

System.out.println("2 to 6 players allowed.");

}

int tbs = n;

while(tbs>0){

String tmp = ui.askPlayersNames();

inGame.add(new Player(tmp));

tbs--;

}

doSetup(n);
int i = turnOrder();

current = inGame.get(i);

}

public void doSetup(int n){

switch(n){

case 2:

propertiesSetup(7);

addMoney(350000);

break;

case 3:

propertiesSetup(6);

addMoney(300000);

break;

case 4:

propertiesSetup(5);

addMoney(250000);

break;

case 5:

propertiesSetup(4);

addMoney(200000);

break;

case 6:

propertiesSetup(3);

addMoney(150000);

break;

}

}

public void propertiesSetup(int n){

Random r = new Random();

int i = r.nextInt(39);

while(n>0){

for(Player p:inGame){

boolean done = false;

while(!done){

if(board.getSquare(i).sellable() && board.getSquare(i).getOwner().equals(null)){

System.out.println("Assegno "+board.getSquare(i).getName()+"("+i+") a "+p.getName());

p.addProperty(board.getSquare(i));

try {

board.getSquare(i).newOwner(p);

}

catch(Exception e){

System.out.println("Unknown error");

}

done = true;

}

else{

i = r.nextInt(39);

}

}

i = r.nextInt(39);

}

n--;

}

}

public void addMoney(int n){

for(Player p:inGame)

p.deposit(n);

}

public void addDeadPlayer(Player p){

if(inGame.remove(p))

gameOvers.add(0,p);

}

public int turnOrder(){

int max=0;

int index=0;

for (int i=0; i<inGame.size();i++){

dice.roll();

System.out.println(inGame.get(i).getName() + " rolled " + dice.getRollTot());

if (dice.getRollTot()>max){

max = dice.getRollTot();

index = i;

}

}

return index;

}

public boolean isGameEnd(){

if(inGame.size() == 1)

return true;

return false;

}

public void setEndGame(boolean e){

endGame = e;

}

public boolean endTurn(){

return endTurn;

}

public void setEndTurn(boolean e){

endTurn = e;

}

public Player getCurrentPlayer(){

return current;

}

public Player getNextPlayer(Player p){

int i = inGame.indexOf(p);

if (i == inGame.size()-1)

i = 0;

else

i = i+1;

return inGame.get(i);

}

public void changeTurn(){

getNextPlayer(current);

doubles = 0;

turns += 1;

}

public void prisonHandling(Player p, int action){

switch(action){

case 1:

current.transfer(5000, null);

current.free(dice.getRollTot());

break;

case 2:

current.setCanExit(false);

current.free(dice.getRollTot());

break;

case 3:

int temp_throws = 0;

dice.roll();

while(temp_throws < 3){

if(dice.isDouble()){

current.free(dice.getRollTot());

ui.notifyOutOfPrison();

temp_throws = 3;

}

else{

dice.roll();

ui.notifyRoll(dice.getRollTot(),dice.isDouble());

}

}

}

}

public int getTurns(){

return turns;

}

public int getDoubles(){

return doubles;

}

public void begin(){

while(!endGame){

ui.beginTurn(current.toString());

while(!endTurn()){

if(current.isJailed()){

int action = ui.askHandlePrison();

prisonHandling(current, action);

setEndTurn(true);

}

else{

if(ui.askDices()){

dice.roll();

ui.notifyRoll(dice.getRollTot(), dice.isDouble());

current.walkTo(dice.getRollTot());

}

ui.notifyNewPosition(board.getSquare(current.getPos()).toString());

if(board.getSquare(current.getPos()).getOwner() == current){

}

else if (board.getSquare(current.getPos()).getOwner() == null){

if(ui.askBuySquare())

current.buySquare(board.getSquare(current.getPos()));

setEndTurn(true);

}

else {

/* gestione della casella nel caso in cui il proprietario non sia il giocatore corrente */

board.getSquare(current.getPos()).handleSquare(current);

if(current.isJailed())

ui.notifyInPrison();

setEndTurn(true);

}

}

}

if(current.isBankrupt()){

current.setBankruptcy();

addDeadPlayer(current);

}

if(isGameEnd())

setEndGame(true);

if(endTurn())

changeTurn();

}

if(ui.endGame(isGameEnd())){

ui.notifyWinner(inGame.get(0).toString());

String tmp = "";

for(Player p:gameOvers)

tmp += p.getName()+",";

ui.notifyOthers(tmp);

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote