I am using a player object (playerObject) that has properties and is changed constantly. I initialize with 4 playerObjects with their own properties and name.
This is a clone of the board game monopoly.
When a player lands on a property and checks function canIBuy() and they buy it, it purchases that property for every player. Why is this happening?
Posting all the JS code in case there is something i am missing, although this is the broken part.
- Player lands on space
- checks isProperty()
- if property, runs canIBuy()
- buys it if its available and they have the cash
const logs = document.getElementById("logDiv") const stats = document.getElementById("stats") var player1,player2,player3,player4 var players = []
const availableNames = [
"dog", "battleship", "race car", "top hat", "cat", "penguin", "t-rex", "rubber ducky"
]
const spots = [
'Go','Mediterranean Avenue','Community Chest','Baltic Avenue','Income Tax','Reading Railroad','Oriental Avenue','Chance','Vermont Avenue','Connecticut Avenue','Jail / Just Visiting','St. Charles Place','Electric Company','States Avenue','Virginia Avenue','Pennsylvania Railroad','St. James Place','Community Chest','Tennessee Avenue','New York Avenue','Free Parking','Kentucky Avenue','Chance','Indiana Avenue','Illinois Avenue','B. & O. Railroad','Atlantic Avenue','Ventnor Avenue','Water Works','Marvin Gardens','Go To Jail','Pacific Avenue','North Carolina Avenue','Community Chest','Pennsylvania Avenue','Short Line','Chance','Park Place','Luxury Tax','Boardwalk'
]
// spots that can not be bought
const nonproperties = [
0,2,4,7,10,17,20,22,30,33,36,38
]
const spotPrices = [-1,60,-1,60,-1,200,100,-1,100,120,-1,140,150,140,160,200,180,-1,180,200,-1,220,-1,220,240,200,260,260,150,280,-1,300,300,-1,320,200,-1,350,-1,400]
const spotRents = [-1,2,-1,4,-1,-1,6,-1,6,8,-1,10,-1,10,12,-1,14,-1,14,16,-1,18,-1,18,20,-1,22,22,-1,22,-1,26,26,-1,-1,28,-1,35,-1,50];
function log(x) {
logs.innerHTML += "</br>" + x
}
const playerObject = {
name:undefined,
cash:1500,
owned:[],
currentSpot:0,
inJail:false,
getOutOfJailRolls:0,
init() {
log(bold(this.name) + " has joined the game!")
statRefresh()
},
move(r1,r2) {
let newSpot = this.currentSpot+(r1+r2)
if (newSpot >= spots.length) {
newSpot = newSpot - spots.length
log(bold(this.name)+ " passed Go and collected $200")
this.cash += 200
}
log (bold(this.name) + " rolled: "+r1+" & "+r2)
log(bold(this.name) + " moved "+(r1+r2)+" spaces from "+spots[this.currentSpot]+" to "+spots[newSpot])
this.currentSpot = newSpot
if (isProperty(this.currentSpot)) {
canIBuy(this)
console.log("checking if "+this.name+" can buy "+spots[this.currentSpot])
}
statRefresh()
},
jail() {
this.currentSpot = 10 // JAIL SPOT
this.inJail = true
log(bold(this.name)+" has rolled their third set of doubles and went straight to jail! ")
log("</br>")
statRefresh()
},
purchase() {
this.owned.push(this.currentSpot)
this.cash -= spotPrices[this.currentSpot]
}
}
function initalizeGame() {
player1 = Object.create(playerObject)
player1.name = availableNames[Math.floor(Math.random() * availableNames.length)]
availableNames.splice(availableNames.indexOf(player1.name), 1)
player1.init()
player2 = Object.create(playerObject)
player2.name = availableNames[Math.floor(Math.random() * availableNames.length)]
availableNames.splice(availableNames.indexOf(player2.name), 1)
player2.init()
player3 = Object.create(playerObject)
player3.name = availableNames[Math.floor(Math.random() * availableNames.length)]
availableNames.splice(availableNames.indexOf(player3.name), 1)
player3.init()
player4 = Object.create(playerObject)
player4.name = availableNames[Math.floor(Math.random() * availableNames.length)]
availableNames.splice(availableNames.indexOf(player4.name), 1)
player4.init()
log("<br/>")
players.push(player1,player2,player3,player4)
statRefresh()
}
document.getElementById("nextTurn").onclick = function() {
roll(player1)
roll(player2)
roll(player3)
roll(player4)
}
function roll(player) {
if (!player.inJail) {
let r1,r2
let consec = 0
let consecTimes = ["first", "second", "third"]
while (r1 == r2) {
r1 = Math.ceil(Math.random() * (6-1 + 1))
r2 = Math.ceil(Math.random() * (6-1 + 1))
if (r1 == r2) {
log("<strong>"+player.name + "</strong> rolled doubles for the " +consecTimes[consec]+" time.")
consec++
if (consec >= 3) {
return player.jail()
}
}
if (consec < 3) {
player.move(r1,r2)
}
}
log("<br/>")
} else {
if (player.cash > 1000000) {
log(bold(player.name)+ " paid to get out of jail. Rolling to move...")
player.inJail = false
roll(player)
} else {
r1 = Math.ceil(Math.random() * (6-1 + 1))
r2 = Math.ceil(Math.random() * (6-1 + 1))
log(bold(player.name) + " rolled a "+r1+" & "+r2+" in jail")
player.getOutOfJailRolls++
if (r1 == r2 || player.getOutOfJailRolls > 3) {
log(bold(player.name) + " has busted out of jail. Rolling to move...")
player.inJail = false
player.getOutOfJailRolls = 0
roll(player)
} else {
log(bold(player.name) + " is stuck in jail after rolling "+ r1+" & "+r2 +" ("+player.getOutOfJailRolls+"/3 tries)")
}
}
}
}
function bold(x) {
return "<strong>"+x+"</strong>"
}
function statRefresh() {
var t = document.createElement("table")
stats.innerHTML = ""
for (let i=0;i<players.length;i++) {
var r = t.insertRow(0)
var name = r.insertCell(0)
name.innerText = players[i].name
var cash = r.insertCell(1)
cash.innerText = "$"+players[i].cash
var spot = r.insertCell(2)
spot.innerText = spots[players[i].currentSpot]
var jail = r.insertCell(3)
jail.innerText = "Jailed:"+players[i].inJail
var prop = r.insertCell(4)
prop.innerText = players[i].owned
stats.append(t)
}
}
function isProperty(x) {
if (nonproperties.indexOf(x) == -1) {
return true
}
}
function canIBuy(x) {
//console.log("canibuy:"+x.name)
let alreadyPurchased = false
let purchasedBy
for (let i=0;i<players.length;i++) {
if (players[i].owned.indexOf(x.currentSpot) != -1) {
alreadyPurchased = true
purchasedBy = players[i]
log(spots[x.currentSpot]+" is already owned by "+purchasedBy.name)
}
}
if (alreadyPurchased) {
payRent(x, purchasedBy, x.currentSpot)
} else if (x.cash > spotPrices[x.currentSpot]) {
if (Math.floor(Math.random() * 9) + 1 <= 8) {
// buy it 80% of the time if you have the cash
log(bold(x.name) + " is buying "+spots[x.currentSpot] +" for $"+spotPrices[x.currentSpot])
console.log(x)
x.purchase()
console.log(players)
} else {
log(bold(x.name) + " decided not to buy "+spots[x.currentSpot])
}
}
}
function payRent(payer, payee, spot) {
let rent = spotPrices[spot]
if (payer.cash > rent) {
payer.cash -= rent
payee.cash += rent
log(bold(payer.name)+" paid " +payee.name+" $"+rent+" for rent at "+spots[spot])
}
}
initalizeGame()