Monopoly Check If Player Owns A Set
Solution 1:
It would be more convenient if you had your positions as an array rather that a plain object. We'll covert it internally using map()
, so array methods such as filter()
and every()
can be used:
functiondoesOwnSet(player, type) {
// we'll use "chaining" here, so every next method will be called upon// what previous method have returned// `return` statement will return the result of the very last method// first, lets take an array of `position` object keys// which are "position1", "position2" and so onreturnObject.keys(positions)
// then, create an array of positions object// this will return Array
.map(function (key) {
return positions[key];
})
// then, pick up only positions with specified type (aka set)// this will return Array
.filter(function (pos) {
return pos.type === type;
})
// finally, check if specified player owns every position of the set// this will return Boolean
.every(function (pos) {
return pos.owner === player;
});
}
You could use this function in if
statement like so:
if (doesOwnSet(players.player1, 'brown')) {
// give some reward to player1
}
Solution 2:
The most efficient way would be to store references to the positions inside the user object, and have separate colour_group objects like so:
color_groups = {
blue:{
positions:[
{..position1..},
{..position2..}
...
}
},
green:{
positions:[
{..position3..},
{..position4..}
...
]
},
....
}
If you build color_groups from the positions hash using something like this,
color_groups = {};
for (var pos_name in positions) {
var pos = positions[pos_name];
if (!color_groups[pos.type]) color_groups[pos.type] = {positions:{}};
color_groups[pos.type].positions[pos_name] = pos; // Assignment works by reference, i.e. it does not copy 'pos'
}
Then each position object in the positions hash will also appear in the color_groups hash. When you change the owner of a position, its owner will also appear to change in the color_groups hash because they both refer to the same object.
You should use arrays for storing some of your data rather than objects, since arrays can be iterated over more conveniently. You should also look into creating classes using a constructor and a prototype.
It would be helpful for you to do something similar to the users object, so you can conveniently get a list of property each user owns. You would then know when a player owns a color_group if:
player1.positions[color_group_type].length == color_groups[color_group_type].length
Without changing your data structure, Pavlo's answer will work fine.
Solution 3:
If a player buys a place, the method buy is called with the player. The places are defined in an Array. The first for checks for all streets, whether the player has it or not. If Yes, the second for checks whether another place is owned, which is not the first. If the type of both match, there is a set owned by the player. This script only checks two places, it is easy to check more.
functionplayer_has_set (player){
var streets = newArray();
streets[0]="brown";
streets[1]="brown";
//...for(var i=0;i<streets.length;i++){
if(player.has(i)){
var first_street_type=streets[i];
for(var j=0;j<streets.length;j++){
if(player.has(j)&&j!==i){
var second_street_type=streets[j];
if(first_street_type===second_street_type){
//SET
}
}
}
}
}
}
Post a Comment for "Monopoly Check If Player Owns A Set"