Originally Posted by
mr selby
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.
*/