'im trying to have the player move to one square to another square and take turns and have the properties have those affects and im really stuck?
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Monopoly {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Player p1 = new Player();
p1.Player("","",0,1500);
System.out.println("please enter your name:");
String newName = input.nextLine();
p1.setName(newName);
System.out.println("the name is " + p1.getName());
System.out.println("please enter your token: ");
String newToken= input.nextLine();
p1.setToken(newToken);
System.out.println("the token is " + p1.getToken());
System.out.println("your location is "+p1.getLocation());
System.out.println("your balance is "+p1.getBalance());
System.out.println(p1);
Player p2 = new Player();
p2.Player("","",0 ,1500);
System.out.println("please enter your name:");
String NewName = input.nextLine();
p2.setName(NewName);
System.out.println("please enter your token: ");
String coolToken= input.nextLine();
p1.setToken(coolToken);
System.out.println("the token is " + p2.getToken());
System.out.println("your location is "+p2.getLocation());
System.out.println("your balance is "+p2.getBalance());
System.out.println(p2);
int die1;
int die2;
die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
int roll = die1 + die2;
int roll2 = die1 + die2;
System.out.println("here are your rolls: "+ roll);
System.out.println("Here is the rolls for player 2: " + roll2);
// Array of 40 monopoly squares
BoardSquare[] square = new BoardSquare[40];
for (int i = 0; i < square.length; i++) {
loadArray(square);
if( roll == 2 && roll2 == 2){
p1.setLocation(i++ - 38);
p2.setLocation(i++ - 38);
System.out.println("Here is the location for the first player" );
}
else if (roll == 4 && roll2 == 4){
p1.setLocation(i++ - 36);
p2.setLocation(i++ - 36);
System.out.println("Here is the location for the first player" );
}
else if (roll == 5 && roll2 == 5){
p1.setLocation(i++ - 35);
p2.setLocation(i++ - 35);
System.out.println("Here is the location for the first player" );
}
else if (roll == 6 && roll2 == 6){
p1.setLocation(i++ - 34);
p2.setLocation(i++ - 34);
System.out.println("Here is the location for the first player" );
}
else if (roll == 7 && roll2 == 7){
p1.setLocation(i++ - 33);
p2.setLocation(i++ - 33);
System.out.println("Here is the location for the first player" );
}
else if (roll == 8 && roll2 == 8){
p1.setLocation(i++ - 32);
p2.setLocation(i++ - 32);
System.out.println("Here is the location for the first player" );
}
else if (roll == 9 && roll2 == 9){
p1.setLocation(i++ - 31);
p2.setLocation(i++ - 31);
System.out.println("Here is the location for the first player" );
}
else if (roll == 10 && roll2 == 10){
p1.setLocation(i++ - 30);
p2.setLocation(i++ - 30);
System.out.println("Here is the location for the first player" );
}
else if (roll == 11 && roll2 == 11){
p1.setLocation(i++ - 29);
p2.setLocation(i++ - 29);
System.out.println("Here is the location for the first player" );
}
else if (roll == 12 && roll2 == 12){
p1.setLocation(i++ - 28);
p2.setLocation(i++ - 28);
System.out.println("Here is the location for the first player" );
}
}
}
// Method to load the BoardSquare array from a data file
public static void loadArray(BoardSquare[] square) {
// declare temporary variables to hold BoardSquare properties read from a file
String inName;
String inType;
int inPrice;
int inRent;
String inColor;
try {
// Create a File class object linked to the name of the file to be read
File squareFile = new File("squares.txt");
// Create a Scanner named infile to read the input stream from the file
Scanner infile = new Scanner(squareFile);
for (int i = 0; i < 40; i++) {
// Read data from the file into temporary variables.
// Read Strings directly; parse integers.
inName = infile.nextLine();
inType = infile.nextLine();
inRent = Integer.parseInt(infile.nextLine());
inPrice = Integer.parseInt(infile.nextLine());
inColor = infile.nextLine();
// initialize each square with the BoardSquare constructor
square[i] = new BoardSquare(inName, inType, inPrice, inRent, inColor);
}
infile.close();
} catch (FileNotFoundException e) {
System.out.println("Error: " + e.getMessage());
System.exit(1);
}
}
}
heres the player class
package edu.ccp.csci111;
public class Player {
private String name;
private String token;
private int location = 0;
private int balance = 1500;
public void player(){
}
public void Player(String name,String token,int location,int balance){
this.name = name;
this.token =token;
this.location = location;
this.balance = balance;
}
public String getName() {
return name;
}
public String getToken() {
return token;
}
public int getLocation() {
return location;
}
public int getBalance() {
return balance;
}
public void setName(String name) {
this.name = name;
}
public void setToken(String token) {
this.token = token;
}
public void setLocation(int newlocation) {
this.location = newlocation;
}
public void setBalance(int newbalance) {
this.balance = newbalance;
}
public String toString() {
return "Player" + " There name is = " + name + " " + ", token= ' " + token + " " + ", location= " + location + " that's the location, balance= " + balance + " that's the balance";
}
}
here is the bordersquare class
package edu.ccp.csci111;
/**
* Class to create a monopoly board square.
*/
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 = "";
}
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;
}
// Accesors for each property
public String getName() {
return this.name;
}
public String getType() {
return this.type;
}
public int getPrice() {
return this.price;
}
public int getRent() {
return this.rent;
}
public String getColor() {
return this.color;
}
// A method to return the BoardSquare's data as a String
public String toString() {
return ("Property: " + this.name + ", Type: " + this.type +
", Price: " + this.price + ", Rent: " + this.rent +
", Color: " + this.color);
}
}
so basically what im trying to do is make two players move from one square to another and im really stuck and i try using while loops within the for loops i have my dice and players and board i just need to find away to make the turns and just away for each of the players go one at time and play the game properly can anyone help me ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
