Thread: Monopoly program help

Page 1 of 2 12 LastLast
Results 1 to 10 of 12
  1. #1 Monopoly program help 
    Irathient Developer

    mr selby's Avatar
    Join Date
    May 2011
    Age
    28
    Posts
    1,183
    Thanks given
    95
    Thanks received
    166
    Rep Power
    97
    hey everyone, so i have a project due on the 27th, im pretty much done it just running into some issues and im looking for outside help on this

    so the overall design of the program has to be a player class that stores the variables to move around the board, ect ... and each property the player lands on they have to pay that propertys rent....

    the issue im having is : when a player lands on a property it does pay it, but it doesnt save the balance of the players bank, so lets say i roll a 3 and have to pay 100 dollars, each player starts with 1500 so the player would then have 1400 but on the next roll i roll a 5 and have to pay 200, it subtracts 200 from the initial 1500 the player starts with ...

    here are my classes :

    Spoiler for main class:
    Code:
    /* Monopoly.java
     * 
     * CSCI 111 Fall 2013 
     * last edited November 2, 2013 by C. Herbert
     * 
     * This package contains code that can be used as the basis of a monopoly game
     * It has a class of BoardSquares for the board squares in a Monopoly game,
     * and a main program that puts the squares into an array.
     * 
     * The main method has code to test the program by printing the data from the array
     * 
     * This is for teaching purposes only. 
     * Monopoly and the names and images used in Monopoly are 
     * registered trademarks of Parker Brothers, Hasbro, and others.
     */
    package monopoly;
    
    import java.util.*;
    import javax.swing.JOptionPane;
    
    /**
     *
     * @author cherbert
     */
    public class Monopoly {
    
        /**
         * @param args the command line arguments
         */
        static int roll1;
        static int roll2;
        static int totalRoll;
    
        public static void main(String[] args) throws Exception {
            String name;
            String token;
            String roll;
            int pBank;
            int pRent;
            boolean reRoll = true;
            name = JOptionPane.showInputDialog("Enter your name");
            token = JOptionPane.showInputDialog("Enter the name of your token");
    
            Player player1;
            player1 = new Player(name, token, 1500, 0);
            BoardSquare[] square = new BoardSquare[40]; // array of 40 monopoly squares
    
            int i; // a loop counter
    
            // call the method to load the array
            loadArray(square);
    
            // test the code by printing the data for each square
            System.out.println("Data from the array of Monopoly board squares. Each line has:\n");
            System.out.println("name of the square, type, rent, price, color\n");
            for (i = 0; i < 40; i++) {
                System.out.println(square[i].toString());
            }
            System.out.println("\n" + player1.toString());
    
            if (Player.location >= 40) {
                System.out.println("You have passed go and have won!");
                System.exit(0);
            }
    
            while (reRoll) {
                rollDice();
                Player.location += totalRoll;
    
                int l = Player.location;
                pRent = square[l].getRent();
                pBank = player1.getBalance();
                pBank -= pRent;
                System.out.println(pBank);
                System.out.println(pRent);
    
                /*Player.location += totalRoll;
                if (Player.location >= 40) {
                    System.out.println("You have passed go and have won!");
                    System.exit(0);
                }*/
                System.out.println("The first roll was a : " + roll1);
                System.out.println("The second roll was a : " + roll2);
                System.out.println("The total roll was : " + totalRoll);
                System.out.println("Your player is now at location :" + player1.getLocation());
                System.out.println("You landed on a property and had to pay : " + pRent);
                System.out.println("You now have :" + pBank + " in the bank");
    
                roll = JOptionPane.showInputDialog("Would you like to roll the dice?");
                if (roll.equalsIgnoreCase("n") || roll.equalsIgnoreCase("no")) {
                    reRoll = false;
                    System.out.println("no");
                }
            }
    
        } // main()
        //***********************************************************************
    
        public static void rollDice() {
            roll1 = (int) (Math.random() * 6 + 1);
            roll2 = (int) (Math.random() * 6 + 1);
            totalRoll = roll1 + roll2;
        }
    
        // method to load the BoardSquare array from a data file
        public static void loadArray(BoardSquare[] square) throws Exception {
            int i; // a loop counter
    
            // declare temporary variables to hold BoardSquare properties read from a file
            String inName;
            String inType;
            int inPrice;
            int inRent;
            String inColor;
    
            // Create a File class object linked to the name of the file to be read
            java.io.File squareFile = new java.io.File("squares.txt");
    
            // Create a Scanner named infile to read the input stream from the file
            Scanner infile = new Scanner(squareFile);
    
            /* This loop reads data into the square array.
             * Each item of data is a separate line in the file.
             * There are 40 sets of data for the 40 squares.
             */
            for (i = 0; i < 40; i++) {
                // read data from the file into temporary variables
                // read Strings directly; parse integers
                inName = infile.nextLine();
                inType = infile.nextLine();
                inPrice = Integer.parseInt(infile.nextLine());
                inRent = Integer.parseInt(infile.nextLine());;
                inColor = infile.nextLine();
    
                // intialze each square with the BoardSquare constructor
                square[i] = new BoardSquare(inName, inType, inPrice, inRent, inColor);
            } // end for
            infile.close();
    
        } // endLoadArray
        //***********************************************************************
    
    } // end class Monopoly
    //***************************************************************************
    
    
    /* code for a class of Monopoly squares
     * 
     * CSCI 111 Fall 2013 
     * last edited November 2, 2013 by C. Herbert
     * Each object in this class is a square for the board game Monopoly.
     * 
     * This is for teaching purposes only. 
     * Monopoly and the names and images in the game are 
     * registered trademarks of Parker Brothers, Hasbro, and others.
     */

    Spoiler for Player class:

    Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package monopoly;
    
    /**
     *
     * @author c312
     */
    public class Player {
        static int location;
        static int balance;
        private final String name;
        private final String token;
        
        public Player()
        {
            name = "";
            token = "";
            location = 0;
            balance = 1500;
        }
        
        public Player(String pName, String pToken, int bal, int loc)
        {
          name = pName;
          token = pToken;
          balance = bal;
          location = loc;
        }
        // accessors for each player
        public String getName()
        {
            return name;
        }// end getName
        
        public String getToken()
        {
            return token;
        }// end getToken()
        
        public int getLocation()
        {
            return location;
        }// end getLocation()
        
        public int getBalance()
        {
            return balance;
        }// end getBalance()
        
        @Override
        // a method to return Player class's data to a string
        public String toString()
        {
            String player;
            player = "Name : " + name + "\n" + "Token : " + token + "\n" + "Location : " + location + "\n" + "Balance: " + balance;
            
            
        return player;
        }// end toString()
        
    }


    Spoiler for outcome from running program a few times:
    Code:
    1320
    180
    The first roll was a : 2
    The second roll was a : 3
    The total roll was : 5
    Your player is now at location :16
    You landed on a property and had to pay : 180
    You now have :1320 in the bank
    1350
    150
    The first roll was a : 6
    The second roll was a : 6
    The total roll was : 12
    Your player is now at location :28
    You landed on a property and had to pay : 150
    You now have :1350 in the bank
    1300
    200
    The first roll was a : 6
    The second roll was a : 1
    The total roll was : 7
    Your player is now at location :35
    You landed on a property and had to pay : 200
    You now have :1300 in the bank
    Reply With Quote  
     

  2. #2  
    Registered Member

    Join Date
    Dec 2012
    Posts
    2,999
    Thanks given
    894
    Thanks received
    921
    Rep Power
    2555
    Because you're not even making an attempt to save it...
    Attached image
    Reply With Quote  
     

  3. #3  
    Irathient Developer

    mr selby's Avatar
    Join Date
    May 2011
    Age
    28
    Posts
    1,183
    Thanks given
    95
    Thanks received
    166
    Rep Power
    97
    Quote Originally Posted by Kaleem View Post
    Because you're not even making an attempt to save it...
    not necessarily "save" but i mean update, the variable for the players "balance" goes back to 1500 after every dice roll?
    Reply With Quote  
     

  4. #4  
    ¯\_(ツ)_/¯


    Join Date
    Jul 2014
    Posts
    1,803
    Thanks given
    928
    Thanks received
    550
    Rep Power
    299
    Aren't you instancing a new player for every roll? so instead of using the previous player its using a new player with a different balance.
    Reply With Quote  
     

  5. #5  
    Irathient Developer

    mr selby's Avatar
    Join Date
    May 2011
    Age
    28
    Posts
    1,183
    Thanks given
    95
    Thanks received
    166
    Rep Power
    97
    Quote Originally Posted by Ugly View Post
    Aren't you instancing a new player for every roll? so instead of using the previous player its using a new player with a different balance.
    im honestly not sure, im just starting to learn this, but i believe player1 counts as that one single instance of the player i created? i could be wrong
    Reply With Quote  
     

  6. #6  
    Respected Member


    Kris's Avatar
    Join Date
    Jun 2016
    Age
    26
    Posts
    3,638
    Thanks given
    820
    Thanks received
    2,642
    Rep Power
    5000
    Quote Originally Posted by mr selby View Post
    im honestly not sure, im just starting to learn this, but i believe player1 counts as that one single instance of the player i created? i could be wrong
    Figure it out. System.out.println(player1);
    add it @ every roll. If the printed Object has a different value on every roll, it means you're instantiating the Player player1 over and over again with every roll.
    Attached image
    Reply With Quote  
     

  7. #7  
    ¯\_(ツ)_/¯


    Join Date
    Jul 2014
    Posts
    1,803
    Thanks given
    928
    Thanks received
    550
    Rep Power
    299
    Quote Originally Posted by mr selby View Post
    im honestly not sure, im just starting to learn this, but i believe player1 counts as that one single instance of the player i created? i could be wrong
    Try this

    Code:
    /* Monopoly.java
     * 
     * CSCI 111 Fall 2013 
     * last edited November 2, 2013 by C. Herbert
     * 
     * This package contains code that can be used as the basis of a monopoly game
     * It has a class of BoardSquares for the board squares in a Monopoly game,
     * and a main program that puts the squares into an array.
     * 
     * The main method has code to test the program by printing the data from the array
     * 
     * This is for teaching purposes only. 
     * Monopoly and the names and images used in Monopoly are 
     * registered trademarks of Parker Brothers, Hasbro, and others.
     */
    package monopoly;
    
    import java.util.*;
    import javaapplication3.monopoly.BoardSquare;
    import javaapplication3.monopoly.Player;
    import javax.swing.JOptionPane;
    
    /**
     *
     * @author cherbert
     */
    public class Monopoly {
    
        /**
         * @param args the command line arguments
         */
        static int roll1;
        static int roll2;
        static int totalRoll;
        
        static Player player1;
    
        public static void main(String[] args) throws Exception {
            String name;
            String token;
            String roll;
            int pBank;
            int pRent;
            boolean reRoll = true;
            name = JOptionPane.showInputDialog("Enter your name");
            token = JOptionPane.showInputDialog("Enter the name of your token");
    
            if (player1 == null)
                player1 = new Player(name, token, 1500, 0);
            BoardSquare[] square = new BoardSquare[40]; // array of 40 monopoly squares
    
            int i; // a loop counter
    
            // call the method to load the array
            loadArray(square);
    
            // test the code by printing the data for each square
            System.out.println("Data from the array of Monopoly board squares. Each line has:\n");
            System.out.println("name of the square, type, rent, price, color\n");
            for (i = 0; i < 40; i++) {
                System.out.println(square[i].toString());
            }
            System.out.println("\n" + player1.toString());
    
            if (player1.getLocation() >= 40) {
                System.out.println("You have passed go and have won!");
                System.exit(0);
            }
    
            while (reRoll) {
                rollDice();
                player1.location += totalRoll;
    
                int l = player1.location;
                pRent = square[l].getRent();
                pBank = player1.getBalance();
                pBank -= pRent;
                System.out.println(pBank);
                System.out.println(pRent);
    
                /*Player.location += totalRoll;
                if (Player.location >= 40) {
                    System.out.println("You have passed go and have won!");
                    System.exit(0);
                }*/
                System.out.println("The first roll was a : " + roll1);
                System.out.println("The second roll was a : " + roll2);
                System.out.println("The total roll was : " + totalRoll);
                System.out.println("Your player is now at location :" + player1.getLocation());
                System.out.println("You landed on a property and had to pay : " + pRent);
                System.out.println("You now have :" + pBank + " in the bank");
    
                roll = JOptionPane.showInputDialog("Would you like to roll the dice?");
                if (roll.equalsIgnoreCase("n") || roll.equalsIgnoreCase("no")) {
                    reRoll = false;
                    System.out.println("no");
                }
            }
    
        } // main()
        //***********************************************************************
    
        public static void rollDice() {
            roll1 = (int) (Math.random() * 6 + 1);
            roll2 = (int) (Math.random() * 6 + 1);
            totalRoll = roll1 + roll2;
        }
    
        // method to load the BoardSquare array from a data file
        public static void loadArray(BoardSquare[] square) throws Exception {
            int i; // a loop counter
    
            // declare temporary variables to hold BoardSquare properties read from a file
            String inName;
            String inType;
            int inPrice;
            int inRent;
            String inColor;
    
            // Create a File class object linked to the name of the file to be read
            java.io.File squareFile = new java.io.File("squares.txt");
    
            // Create a Scanner named infile to read the input stream from the file
            Scanner infile = new Scanner(squareFile);
    
            /* This loop reads data into the square array.
             * Each item of data is a separate line in the file.
             * There are 40 sets of data for the 40 squares.
             */
            for (i = 0; i < 40; i++) {
                // read data from the file into temporary variables
                // read Strings directly; parse integers
                inName = infile.nextLine();
                inType = infile.nextLine();
                inPrice = Integer.parseInt(infile.nextLine());
                inRent = Integer.parseInt(infile.nextLine());;
                inColor = infile.nextLine();
    
                // intialze each square with the BoardSquare constructor
                square[i] = new BoardSquare(inName, inType, inPrice, inRent, inColor);
            } // end for
            infile.close();
    
        } // endLoadArray
        //***********************************************************************
    
    } // end class Monopoly
    //***************************************************************************
    
    
    /* code for a class of Monopoly squares
     * 
     * CSCI 111 Fall 2013 
     * last edited November 2, 2013 by C. Herbert
     * Each object in this class is a square for the board game Monopoly.
     * 
     * This is for teaching purposes only. 
     * Monopoly and the names and images in the game are 
     * registered trademarks of Parker Brothers, Hasbro, and others.
     */
    Reply With Quote  
     

  8. #8  
    Irathient Developer

    mr selby's Avatar
    Join Date
    May 2011
    Age
    28
    Posts
    1,183
    Thanks given
    95
    Thanks received
    166
    Rep Power
    97
    Quote Originally Posted by Ugly View Post
    Try this

    Code:
    /* Monopoly.java
     * 
     * CSCI 111 Fall 2013 
     * last edited November 2, 2013 by C. Herbert
     * 
     * This package contains code that can be used as the basis of a monopoly game
     * It has a class of BoardSquares for the board squares in a Monopoly game,
     * and a main program that puts the squares into an array.
     * 
     * The main method has code to test the program by printing the data from the array
     * 
     * This is for teaching purposes only. 
     * Monopoly and the names and images used in Monopoly are 
     * registered trademarks of Parker Brothers, Hasbro, and others.
     */
    package monopoly;
    
    import java.util.*;
    import javaapplication3.monopoly.BoardSquare;
    import javaapplication3.monopoly.Player;
    import javax.swing.JOptionPane;
    
    /**
     *
     * @author cherbert
     */
    public class Monopoly {
    
        /**
         * @param args the command line arguments
         */
        static int roll1;
        static int roll2;
        static int totalRoll;
        
        static Player player1;
    
        public static void main(String[] args) throws Exception {
            String name;
            String token;
            String roll;
            int pBank;
            int pRent;
            boolean reRoll = true;
            name = JOptionPane.showInputDialog("Enter your name");
            token = JOptionPane.showInputDialog("Enter the name of your token");
    
            if (player1 == null)
                player1 = new Player(name, token, 1500, 0);
            BoardSquare[] square = new BoardSquare[40]; // array of 40 monopoly squares
    
            int i; // a loop counter
    
            // call the method to load the array
            loadArray(square);
    
            // test the code by printing the data for each square
            System.out.println("Data from the array of Monopoly board squares. Each line has:\n");
            System.out.println("name of the square, type, rent, price, color\n");
            for (i = 0; i < 40; i++) {
                System.out.println(square[i].toString());
            }
            System.out.println("\n" + player1.toString());
    
            if (player1.getLocation() >= 40) {
                System.out.println("You have passed go and have won!");
                System.exit(0);
            }
    
            while (reRoll) {
                rollDice();
                player1.location += totalRoll;
    
                int l = player1.location;
                pRent = square[l].getRent();
                pBank = player1.getBalance();
                pBank -= pRent;
                System.out.println(pBank);
                System.out.println(pRent);
    
                /*Player.location += totalRoll;
                if (Player.location >= 40) {
                    System.out.println("You have passed go and have won!");
                    System.exit(0);
                }*/
                System.out.println("The first roll was a : " + roll1);
                System.out.println("The second roll was a : " + roll2);
                System.out.println("The total roll was : " + totalRoll);
                System.out.println("Your player is now at location :" + player1.getLocation());
                System.out.println("You landed on a property and had to pay : " + pRent);
                System.out.println("You now have :" + pBank + " in the bank");
    
                roll = JOptionPane.showInputDialog("Would you like to roll the dice?");
                if (roll.equalsIgnoreCase("n") || roll.equalsIgnoreCase("no")) {
                    reRoll = false;
                    System.out.println("no");
                }
            }
    
        } // main()
        //***********************************************************************
    
        public static void rollDice() {
            roll1 = (int) (Math.random() * 6 + 1);
            roll2 = (int) (Math.random() * 6 + 1);
            totalRoll = roll1 + roll2;
        }
    
        // method to load the BoardSquare array from a data file
        public static void loadArray(BoardSquare[] square) throws Exception {
            int i; // a loop counter
    
            // declare temporary variables to hold BoardSquare properties read from a file
            String inName;
            String inType;
            int inPrice;
            int inRent;
            String inColor;
    
            // Create a File class object linked to the name of the file to be read
            java.io.File squareFile = new java.io.File("squares.txt");
    
            // Create a Scanner named infile to read the input stream from the file
            Scanner infile = new Scanner(squareFile);
    
            /* This loop reads data into the square array.
             * Each item of data is a separate line in the file.
             * There are 40 sets of data for the 40 squares.
             */
            for (i = 0; i < 40; i++) {
                // read data from the file into temporary variables
                // read Strings directly; parse integers
                inName = infile.nextLine();
                inType = infile.nextLine();
                inPrice = Integer.parseInt(infile.nextLine());
                inRent = Integer.parseInt(infile.nextLine());;
                inColor = infile.nextLine();
    
                // intialze each square with the BoardSquare constructor
                square[i] = new BoardSquare(inName, inType, inPrice, inRent, inColor);
            } // end for
            infile.close();
    
        } // endLoadArray
        //***********************************************************************
    
    } // end class Monopoly
    //***************************************************************************
    
    
    /* code for a class of Monopoly squares
     * 
     * CSCI 111 Fall 2013 
     * last edited November 2, 2013 by C. Herbert
     * Each object in this class is a square for the board game Monopoly.
     * 
     * This is for teaching purposes only. 
     * Monopoly and the names and images in the game are 
     * registered trademarks of Parker Brothers, Hasbro, and others.
     */

    did not work
    Reply With Quote  
     

  9. #9  
    ¯\_(ツ)_/¯


    Join Date
    Jul 2014
    Posts
    1,803
    Thanks given
    928
    Thanks received
    550
    Rep Power
    299
    Quote Originally Posted by mr selby View Post
    did not work
    Ima try and get it working and ill let you know what to change.
    Reply With Quote  
     

  10. #10  
    Irathient Developer

    mr selby's Avatar
    Join Date
    May 2011
    Age
    28
    Posts
    1,183
    Thanks given
    95
    Thanks received
    166
    Rep Power
    97
    [SPOIL]
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package monopoly;
    
    /**
     *
     * @author C301
     */
    class BoardSquare 
    {
    
        private String name;    // the name of the square
        private String type;    // property, railroad, utility, plain, tax, or  toJail 
        private int price;      // cost to buy the square; zero means not for sale
        private int rent;       // rent paid by a player who lands on the square 
        private String color;   // many are null; this is not the Java Color class
    
        // constructors
        public BoardSquare() 
        {
            name = "";
            type = "";
            price = 0;
            rent = 0;
            color = "";
        } // end Square()
    
        public BoardSquare(String name, String type, int price, int rent, String color) 
        {
            this.name = name;
            this.type = type;
            this.price = price;
            this.rent = rent;
            this.color = color;
        } // end Square((String name, String type, int price, int rent, String color)
    
        // accesors for each property
        public String getName() 
        {
            return name;
        } //end  getName()
    
        public String getType() 
        {
            return type;
        } //end  getType()
    
        public int getPrice() 
        {
            return price;
        } //end  getPrice()
    
        public int getRent() 
        {
            return rent;
        } //end  getRent()
    
        public String getColor() 
        {
            return color;
        } //end  getColor()
            
        // a method to return the BoardSquare's data as a String
        public String toString() 
        {
        String info;
        info = (name +", "+type+", "+price + ", "+ rent+ ", "+color);
        return info;    
        } //end  toString()
        
    } // end class BoardSquare
    //***************************************************************************
    [/SPOIL]


    https://www.upload.ee/files/6713019/squares.txt.html
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Looking for some Programming Help[PI]
    By Zaves in forum Requests
    Replies: 3
    Last Post: 12-03-2011, 12:19 AM
  2. Free Programing help
    By Brianna in forum Requests
    Replies: 12
    Last Post: 07-29-2011, 09:25 PM
  3. Program help
    By Nathan R in forum Application Development
    Replies: 2
    Last Post: 08-27-2010, 08:13 PM
  4. Paying for Programming help!
    By Diverse Reality in forum Requests
    Replies: 2
    Last Post: 06-15-2010, 07:57 PM
  5. Program help
    By craig903 in forum Graphics
    Replies: 2
    Last Post: 05-29-2008, 09:03 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
  •