Results 1 to 15 of 15
  1. #1
    cyimking is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default Monopoly program help!

    Ok im having a bit trouble in my monopoly program. Its a basic non finish program ( alot of things are not added) but it still runs. So far my program allows player 1 to go around the board and it will show his location and dice roll. But when it ask the player to purchase the location he lands on, the program get funky.

    Java Code: (when ask if you are playing with player 2 just type 0.)
    Java Code:
    import java.util.*;
    public class Monopoly {
    
    
    	public static void main(String[] args) 
    	{
    		Scanner reader = new Scanner (System.in);
    		ArrayList<String> ownership = new ArrayList<String>();
    		
    		Locations Move = new Locations();
    		Locations space = new Locations();
    		Locations pricepad = new Locations();
    		Locations location = new Locations();
    		//Locations die1 = new Locations();
    		
    		Location1 Move1 = new Location1();
    		Location1 space1 = new Location1();
    		//Location1 die2 = new Location1();
    		
    		Location2 Move2 = new Location2();
    		Location2 space2 = new Location2();
    		//Location2 die3 = new Location2();
    		
    		Menus menu = new Menus();
    		Menus prices = new Menus();
    		Menus afterroll = new Menus();
    		Menus rules = new Menus ();
    
    		int roll,roll1,roll2,roll3,roll4;                   //Player's dice roll
    		int human, human1, human2, human3 = 0;              //Players
    		int price = 0,price1 = 0,price2 = 0,price3;         //Player's money
    		//int dice1, dice2;                                 //Dices for player1
    		//int dice3,dice4;                                  //Dice for player2
    		//int dice5,dice6;                                  //Dice for player3
    		//String menu1;                                     //Beginning turn menu
    		int menuOption;                                     //Beginning turn options
    		boolean game = false;
    
    		/*give the dice name to the dice in each class
    		 *Helps see if the user had roll a double
    		dice1 = die1.getMove();
    		dice2 = die1.getMove();
    		dice3 = die2.getMove();
    		dice4 = die2.getMove();
    		dice5 = die3.getMove();
    		dice6 = die3.getMove();*/
    	      
    		System.out.println(rules.getRules());
    		//See how many players are playing
    
    		System.out.println("\nIs player 1 playing? : ");
    		human = reader.nextInt();
    		System.out.println("\nIs player 2 playing? : ");
    		human1 = reader.nextInt();
    		System.out.println("\nIs player 3 playing? : ");
    		human2 = reader.nextInt();
    		 if ( human2 == 1){
    		   System.out.println("\nIs player 4 playing? : ");
    		human3 = reader.nextInt();
    		}
            
    		 //ASK USERS FOR A BEGINNING PRICE
    		 System.out.println("How much money would you like to start off with? ");
    		 menuOption = 5;
    		 while ( menuOption != 4){
    			 System.out.print(prices.getPrices());
    			 menuOption = reader.nextInt();
    			 System.out.println("");
    			 
    			 if( menuOption == 1){
    				 price = 1500;
    				 price1 = 1500;
    				 price2 = 1500;
    				 price3 = 1500;
    				 break;
    			 }
    			 else if ( menuOption == 2){
    				 price = 2000;
    				 price1 = 2000;
    				 price2 = 2000;
    				 price3 = 2000;
    				 break;
    			 }
    			 else if (menuOption == 3){
    				 price = 2500;
    				 price1 = 2500;
    				 price2 = 2500;
    				 price2 = 2500;
    				 break;
    			 }
    		 }
    		 
    		
    		while ( game != true){
    		//Begin turn for player one
    		if(human == 1){
    		
    		System.out.println("\nIts player 1 turn");	
    		
    	     //Set menu up
    	      menuOption = 5;
    	      while (menuOption != 4)
    	      {
    	    	  System.out.print(menu.getMenu());
    	    	  menuOption = reader.nextInt();
    	    	  System.out.println("");
    	    	  
    	    	  if ( menuOption == 1)
    	    	  {
    	    		  roll1 = Move.getMove();
    	    		  space.getSpace();  
    	    	      System.out.print("\nYour rolled a " + roll1 +  " and landed on " + space.getLocations()+ " and you have $" + price + " left.");
    	    	     //Give a menu option to see if the player want to buy the location!
    	    	      menuOption = 3;
    	    	      //Do a if statement so the user wont have a chance in "buying" chance and community chest.
    	    	      if( space.getSpace() != 2 ||space.getSpace() != 4 ||space.getSpace() != 7 || space.getSpace() != 10 ||
    	    	      space.getSpace() != 17 || space.getSpace() != 20 || space.getSpace() != 22 || space.getSpace() != 30 ||
    	    	      space.getSpace() != 33){
    	    	      while ( menuOption != 2){
    	    	    	  System.out.print(afterroll.getAfterroll());
    	    	    	  menuOption = reader.nextInt();
    	    	    	  System.out.println("");
    	    	    	  
    	    	    	  if ( menuOption == 1)
    	    	    	  {
    	    	    		  if(pricepad.getSpace() > price)
    	    	    			  System.out.println("You doesnt have enough money!");
    	    	    		  else{
    	    	 	    		  price -= pricepad.getSpace();
    	    	    			  ownership.add(space.getLocations());
    	    	    			  System.out.println("Congrats you bought " + space.getLocations() + ", and have $" + price + " left.");//}
    	    	    		  break;
    	    	    	  }}
    	    	    	  else if ( menuOption == 2){
    	    	    		  System.out.println("Your turn will end!");
    	    	    	  }
    	    	      }}
    	    	      else if ( space.getSpace() == 4){
    	    	    	  System.out.println("You landed on Income Tax and you will pay $200 to the bank. ");
    	    	    	  price -= 200;
    	    	    	  break;
    	    	      }
    	    	      else if (space.getSpace() == 38 ){
    	    	    	  System.out.println("You landed on Luxury Tax and you will pay $100 to the bank. ");
    	    	    	  price -= 100;
    	    	    	  break;
    	    	      }
    	    	      break;
    	    	  }
    	    	  else if (menuOption == 2){
    	    		  System.out.println("You currently have $" +price+ ".");
    	    	  }
    	    	  else if (menuOption == 3){
    	    		  System.out.println("You currently own " + ownership);
    	    	  }
    	    	  else if (menuOption == 4){
    	    		  System.out.println("Please switch to player 2");
    	    	  }
    
    	    	  else
    	    		  //invalid option
    	    		  System.out.println("Invalid Option!");
    	      }
    	      System.out.println("\nPlease switch to player 2!");}
    		
    		//BEGIN PLAYER 2 TURN
    		if(human1 == 1){
    			System.out.println("\nIts player 2 turn");	
    			//Set menu up
    		      menuOption = 5;
    		      while (menuOption != 4)
    		      {
    		    	  System.out.print(menu.getMenu());
    		    	  menuOption = reader.nextInt();
    		    	  System.out.println("");
    		    	  
    		    	  if ( menuOption == 1)
    		    	  {
    		  			roll2 = Move1.getMove();
    				    space1.getSpace();
    					System.out.print("\nYour rolled a " + roll2 +  " and landed on " + space1.getLocations() + " and you have $" + price1 + " left.");
    		    	      break;
    		    	  }
    		    	  else if (menuOption == 2){
    		    		  System.out.println("You currently have $" +price1);
    		    	  }
    		    	  else if (menuOption == 3){
    		    		  System.out.println("Doesnt work as of now, sorry.");
    		    	  }
    		    	  else if (menuOption == 4){
    		    		  System.out.println("Please switch to player 3");
    		    	  }
    
    		    	  else
    		    		  //invalid option
    		    		  System.out.println("Invalid Option!");
    		      }
    		      System.out.println("\nPlease switch to player 3!");}
    								
    		//BEGIN PLAYER 3 TURN!
    		if(human2 == 1){
    			System.out.println("\nIts player 3 turn" );	
    			//Set menu up
    		      menuOption = 5;
    		      while (menuOption != 4)
    		      {
    		    	  System.out.print(menu.getMenu());
    		    	  menuOption = reader.nextInt();
    		    	  System.out.println("");
    		    	  
    		    	  if ( menuOption == 1)
    		    	  {
    		  			roll3 = Move2.getMove();
    					space2.getSpace();
    					System.out.print("\nYour rolled a " + roll3 +  " and landed on " + space2.getLocations()+ " and you have $" + price2 + " left.");
    		    	      break;
    		    	  }
    		    	  else if (menuOption == 2){
    		    		  System.out.println("You currently have $" +price1);
    		    	  }
    		    	  else if (menuOption == 3){
    		    		  System.out.println("Doesnt work as of now, sorry.");
    		    	  }
    		    	  else if (menuOption == 4){
    		    		  System.out.println("Please switch to player 3");
    		    	  }
    
    		    	  else
    		    		  //invalid option
    		    		  System.out.println("Invalid Option!");
    		      }
    		      System.out.println("\nPlease switch to player 1!");}
    Class:Menu
    Java Code:
    /* 
     * The point of this class is to less cluther the monopoly program.
     * This class will store all menus for the game as well in the future the rules of monopoly.
      **************************Written by Lamar Nelson **********************************
     */
    public class Menus 
    {
          private String menu,prices,afterroll,rules;
          
          public String getMenu()
          {
        	  menu = "\n1) Would you like roll?(ONLY ONE PER TURN!, If you select this option after you rolled the first time then your turn will end!)"
    	    	   + "\n2) Would you like to view the amount of money you have?"
    	     	   + "\n3) Would you like to view properties bought? (DONT WORK RIGHT NOW!)"
    	    	   + "\n4) End Turn"
    	    	   + "\nEnter your option: ";
        	  
        	  return menu;
          }
          
          public String getPrices()
          {
        	  prices = "\n1) $1,500"
        		     + "\n2) $2,000"
        		     + "\n3) $2,500"
        		     + "\nEnter your option: ";
        	  
        	  return prices;
          }
          
          public String getAfterroll(){
        	  
        	  afterroll = "\n1) Would you like to purchase this?"
        		        + "\n2) Exit"
        		        + "\nEnter your option: ";
        	  return afterroll;
          }
          
          public String getRules(){
        	  rules = "Welcome to My Monopoly game! The rules in the game are simple: " 
    	           + "\n1.If you would like to say yes, then push the number (1) "
    	           + "\n2.If you would like to say no then push any other number!, Also EVERY NUMBER WILL NOT NEED A DECIMAL! DO NOT ENTER A DECIMAL NUMBERS SUCH AS (40.32)"
    	           + "\n3.ALOT OF THE OPTIONS DOESNT WORK YET! DO NOT SELECT OPTION 3, FOR THE 2ND MENU AND OPTION 1 FOR THE 3RD MENU!";
        	  return rules;
          }
    }
    Locations
    Java Code:
     /* 
      *1. Point of this class is to less clutter the monopoly java program
      *2. Its gains dice1 and dice 2 then add them together to form move
      *3. getSpace is to store move, and add each roll together, so the player can go around the board
      *  a. If the space becomes greater than 40 (Last box in monopoly (GO)) then the equation below will allow the player "space" to reset
      *  ... which would allow them to bypass GO and land on the correct square 
      *4. getLocation is to store each location in the game of monopoly
      *  a. Its goes by the number of "space", if space equal 13 then the location will match the number 13 in the game of monopoly.
      *  b. pricepad is needed to store how much each property is worth, which would allow the player to purchase the property
      *    ... which will be subtracted from their income
      *  c. plocation is needed to store each property name, so if the player buy the property then they will be able to view it
      *               *********************** Future Task *********************************************
      *               1. Make it so, that when player pass go, then it will add $200 to the income
      *               
      *               *********************** Errors Found and Need to be Fix *************************
      *               1. In the Monopoly class when player buy a property a bug occurs
      *               - If player roll a 8 and land on the respected property, instead of the player..
      *               - buying the property he/she landed on, the player buys the property that's..
      *               - double the property number. (if player lands on 3 and want to buy, the program
      *               - make the player buy the 6th property instead)
      *               2. Player rolls, then player get "n" and lands on the "n" space, but when ask to 
      *               -buy, it do ( n * 2 = a, then a * 2) then but it on that space.
      *               -Rolled a 9, lands on Con. then program but me on 36 "chance"
      *                 
                      *********************** Written by Lamar Nelson ********************************* 
      */
    public class Locations 
    {
    	private static String location,plocation;
    	private static int dice1,dice2,move,space,pricepad;
    	Dice die1 = new Dice ();
    
    	public int getMove(){
    	dice1 = die1.getRoll();               //Get 1st dice
    	dice2 = die1.getRoll();
    	move = dice1 + dice2;
    	
    	return move;
    	
    	}
         public int getSpace()
         {
        	 space += move;
        	 if (space > 40){
        		 space = ((space - move) - 40) * -1;
        		 System.out.println("\nYou went pass go, you are awarded $200 ");
        		 pricepad = 200;}
        	 return space;
         }
    		public String getLocations ()
          {
        
    			if (space == 0)
    				location = "Go";
    			if ( space == 1){
    				location = ("Mediterranean Ave.");
    				pricepad = 60;}
    			if ( space == 2){
    				location = ("Community Chest");}
    			if ( space == 3){
    				location = ("Baltic Ave.");
    				pricepad = 60;}
    			if (space == 4){
    				location = ("Income Tax");
    				pricepad = 200;}
    			if ( space == 5){
    				location = ("Reading Railroad");
    				pricepad = 200;}
    			if (space == 6){
    				location = ("Oriental Ave.");
    				pricepad = 100;}
    			if ( space == 7)
    				location = ("Chance");
    			if ( space == 8){
    				location = ("Vermont Ave.");
    				pricepad = 100;}
    			if (space == 9){
    				location = ("Connecticut Ave.");
    				pricepad = 120;}
    			if ( space == 10)
    				location = ("Jail - Just Visiting");
    			if (space == 11){
    				location = ("St. Charles Place");
    				pricepad = 140;}
    			if ( space == 12){
    				location = ("Electric Company");
    				pricepad = 150;}
    			if ( space == 13){
    				location = ("States Ave.");
    				pricepad = 140;}
    			if ( space == 14){
    				location = ("Virginia Ave.");
    				pricepad = 160;}
    			if ( space == 15){
    				location = ("Pennsylvania Railroad ");
    				pricepad = 200;}
    			if ( space == 16){
    				location = ("St. James Place");
    				pricepad = 180;}
    			if ( space == 17){
    				location = ("Community Chest");}
    			if ( space == 18){
    				location = ("Tennessee Ave.");
    				pricepad = 180;}
    			if ( space == 19){
    				location = ("New York Ave.");
    				pricepad = 200;}
    			if (space == 20)
    				location = ("Free Parking");
    			if ( space == 21){
    				location = ("Kentucky Ave.");
    				pricepad = 220;}
    			if ( space == 22)
    				location = ("Chance");
    			if (space == 23){
    				location = ("Indiana Ave.");
    				pricepad = 220;}
    			if ( space == 24){
    				location = ("Illinois Ave.");
    				pricepad = 240;}
    			if ( space == 25){
    				location = ("B. & O. Railroad");
    				pricepad = 200;}
    			if ( space == 26){
    				location = ("Atlantic Ave.");
    				pricepad = 260;}
    			if ( space == 27){
    				location = ("Ventnor Ave.");
    				pricepad = 260;}
    			if ( space == 28){
    				location = ("Water Works");
    				pricepad = 150;}
    			if (space == 29){
    				location = ("Marvin Gardens");
    				pricepad = 280;}
    			if ( space == 30)
    				location = ("In Jail - out next turn");
    			if ( space == 31){
    				location = ("Pacific Ave.");
    				pricepad = 300;}
    			if (space == 32){
    				location = ("North Carolina Ave.");
    				pricepad = 300;}
    			if ( space == 33)
    				location = ("Community Chest");
    			if ( space == 34){
    				location = ("Pennsylvania Ave.");
    				pricepad = 320;}
    			if ( space == 35){
    				location = ("Short Line Railroad");
    				pricepad = 200;}
    			if ( space == 36)
    				location = ("Chance");
    			if ( space == 37){
    				location = ("Park Place");
    				pricepad = 350;}
    			if ( space == 38){
    				location = ("Luxury Tax");
    				pricepad = 100;}
    			if ( space == 39){
    				location = ("Boardwalk");
    				pricepad = 400;}
    			if ( space == 40){
    				location = ("Go");
    				pricepad = 200;}
    		//if (space > 40)
    		//{
    			//space = 40 - space ;
    		//}
    		
    		//if ( space == 41)
    			//location = ("You are in jail (2nd turn)");
    		//if ( space == 42)
    			//location = ("You are in jail (Last turn)");
    		return location;  
          }
    }
    If there is a simpler way to do this, then please tell thanks

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    A simpler way would be to have an array of objects. One element for each square.
    The object would hold the location and price with special code for Go and jail.
    The array would be indexed by the space variable.

    A comment on the code style:
    When you have a mutually exclusive choice to make where only one if test would be true, make all the other if statements else if so that when one is true the rest will NOT be tested.

    Instead of using if/else if as you have here, use a switch/case statement.
    Last edited by Norm; 05-15-2011 at 01:25 AM.

  3. #3
    cyimking is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Can u give me an example (kinda new to it)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You will want to read up on how to create classes and how to use arrays, and you can find a great resource with excellent tutorials and example code here: The Really Big Index

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    An example of what?
    How to define a class?
    How to code if/else if
    How to code switch/case?



    Cross posted at:
    Monopoly program help!

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,419
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    Cross posted at:
    Monopoly program help!
    Thanks forthe heads-up!

    db

  7. #7
    cyimking is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Sorry for the cross post, But what do u recommend i do? Re-do the program?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Cross posts are ok IF you let everyone know about it and put in links to the other sites so everyone is up to date on what is going on.

    Yes, I recommend rewriting, at least the long list of if statements.
    One way would be to create a class to hold the data (and perhaps some logic for Go and jail), create an array with one element for each square and then index that with the square the player lands on.
    Another one would be to use a switch statement with the case for each square.

    Using classes would give you more flexibility to add logic for each square such as you might need for the exception cases: Jail and Go or when there are hotels etc

  9. #9
    cyimking is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Oh ok, what is a switch statement

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    I suggest you find a reference book or textbook for the Java language.

    Go back to the post by Fubarable, go to the link and search for switch.

  11. #11
    cyimking is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Ok thanks for the help (my computer science teacher did not teach me this information)

  12. #12
    cyimking is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    How would i do a switch statement with the locations? I looked it up and i cant figure it out.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    What are the locations? are they ints or what?
    What does a case statement use to select the code to be executed?

  14. #14
    cyimking is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    The locations are Strings (Park Place..etc..)

    and im not sure what a case statement does. I googled it and nothing came up on what execute a case statement,

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by cyimking View Post
    The locations are Strings (Park Place..etc..)

    and im not sure what a case statement does. I googled it and nothing came up on what execute a case statement,
    You'd probably have better luck Googling for the Switch statement rather than case. You can find out about how to use the switch/case block here: The Switch Statement

Similar Threads

  1. how to create a monopoly board using java?
    By dizzymj17 in forum AWT / Swing
    Replies: 4
    Last Post: 04-18-2011, 02:38 PM
  2. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  3. Simple Monopoly Game
    By AtoN in forum Java Gaming
    Replies: 2
    Last Post: 01-24-2011, 02:31 PM
  4. Monopoly
    By Tompakorv in forum New To Java
    Replies: 0
    Last Post: 02-03-2010, 05:32 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •