From: Skullheadx Date: Sun, 17 May 2026 04:10:17 +0000 (-0400) Subject: separate helpers, add railroad X-Git-Url: http://git.skullheadx.com/nixos/blog/static/gitweb.css?a=commitdiff_plain;h=a84223f0924735871c7ee17138a573ff577ec11b;p=monopoly-web.git separate helpers, add railroad --- diff --git a/game/color.go b/game/color.go index 55c3ed4..937cfb2 100644 --- a/game/color.go +++ b/game/color.go @@ -1,5 +1,16 @@ package game +func HasColorMonopoly(playerID int32, targetGroup ColorGroup) bool { + var ownedCount int32 + for colorID, ownerID := range PropertyOwners { + if ownerID == playerID && OwnablePropertyType[RespPropertyToOwnable[int32(colorID)]] == TypeColor && ColorProperties[colorID].GroupID == targetGroup { + ownedCount++ + } + } + + return ownedCount == ColorGroupSizes[targetGroup] +} + func processOwnedColors() { for _, oCV := range OwnedColorVisitors { visitorID := oCV.visitorID @@ -11,7 +22,7 @@ func processOwnedColors() { var rent int32 = prices[prop.Houses] - if prop.Houses == 0 && HasMonopoly(ownerID, prop.GroupID) { + if prop.Houses == 0 && HasColorMonopoly(ownerID, prop.GroupID) { rent *= 2 } diff --git a/game/game.go b/game/game.go index 7c6cfc8..29467db 100644 --- a/game/game.go +++ b/game/game.go @@ -3,28 +3,6 @@ package game var Users []User var DebtEvents []int32 -func HasColorMonopoly(playerID int32, targetGroup ColorGroup) bool { - var ownedCount int32 - for colorID, ownerID := range PropertyOwners { - if ownerID == playerID && ColorProperties[colorID].GroupID == targetGroup { - ownedCount++ - } - } - - return ownedCount == ColorGroupSizes[targetGroup] -} - -func HasUtiltyMonopoly(playerID int32) bool { - var ownedCount int32 - for _, ownerID := range PropertyOwners { - if ownerID == playerID { - ownedCount++ - } - } - - return ownedCount == int32(len(UtilityProperties)) -} - func IsInDebt(playerID int32) (bool, int32) { for i, pID := range DebtEvents { if pID == playerID { diff --git a/game/movement.go b/game/movement.go index 0eba618..e74bb6f 100644 --- a/game/movement.go +++ b/game/movement.go @@ -1,8 +1,6 @@ package game -const BoardSpaces = 40 - -var BoardSpaceTypes = [BoardSpaces]PropertyType{ +var BoardSpaces = [...]PropertyType{ TypeGo, TypeColor, TypeChest, @@ -106,7 +104,7 @@ var ColorProperties = []ColorProperty{ {GroupID: GroupDarkBlue, Houses: 0, Name: "Boardwalk", Price: 400}, } -var ColorPropertyRents = [][6]int32{ +var ColorPropertyRents = [][]int32{ {2, 10, 30, 90, 160, 250}, {4, 20, 60, 180, 320, 450}, {6, 30, 90, 270, 400, 550}, @@ -139,7 +137,9 @@ var RailroadProperties = []PropertyStatic{ } const RailroadPrice int32 = 200 -const RailroadRent = [4]int32{25, 50, 100, 200} + +var RailroadRent = [...]int32{25, 50, 100, 200} + const RailroadMortgageValue int32 = 100 var UtilityProperties = []PropertyStatic{ @@ -148,7 +148,9 @@ var UtilityProperties = []PropertyStatic{ } const UtilityPrice int32 = 150 -const UtilityRentMult = [2]int32{4, 10} + +var UtilityRentMult = [...]int32{4, 10} + const UtilityMortgageValue int32 = 75 type TaxSpace struct { @@ -161,8 +163,9 @@ var TaxSpaces = []TaxSpace{ {Name: "Luxury Tax", Amount: 100}, } -var PropertyOwners = []int32{} // playerID -var PropertyMortgages = []int32{} // mortgaged ownablePropertyIDs +var PropertyOwners = []int32{} // playerID +var OwnablePropertyType = []PropertyType{} // uses ownablePropertyID +var PropertyMortgages = []int32{} // mortgaged ownablePropertyIDs func IsMortgaged(ownablePropertyID int32) bool { for _, oPID := range PropertyMortgages { @@ -177,6 +180,9 @@ var SpaceToRespProperty = make(map[int32]int32) var SpaceToOwnableProperty = make(map[int32]int32) var SpaceToTaxSpace = make(map[int32]int32) +var OwnableToRespProperty = make(map[int32]int32) +var RespPropertyToOwnable = make(map[int32]int32) + func init() { var ( colorIndex int32 = 0 @@ -186,29 +192,38 @@ func init() { ownableIndex int32 = 0 ) - for i, propertyType := range BoardSpaceTypes { + for i, propertyType := range BoardSpaces { spaceID := int32(i) if propertyType == TypeColor || propertyType == TypeRailroad || propertyType == TypeUtility { SpaceToOwnableProperty[spaceID] = ownableIndex PropertyOwners = append(PropertyOwners, -1) + OwnablePropertyType = append(OwnablePropertyType, propertyType) } switch propertyType { case TypeColor: SpaceToRespProperty[spaceID] = colorIndex + OwnableToRespProperty[ownableIndex] = colorIndex + RespPropertyToOwnable[colorIndex] = ownableIndex colorIndex++ case TypeRailroad: SpaceToRespProperty[spaceID] = railroadIndex + OwnableToRespProperty[ownableIndex] = railroadIndex + RespPropertyToOwnable[railroadIndex] = ownableIndex railroadIndex++ case TypeUtility: SpaceToRespProperty[spaceID] = utilityIndex + OwnableToRespProperty[ownableIndex] = utilityIndex + RespPropertyToOwnable[utilityIndex] = ownableIndex utilityIndex++ case TypeTax: SpaceToTaxSpace[spaceID] = taxIndex taxIndex++ } + ownableIndex++ + } } @@ -253,12 +268,12 @@ var ( func AdvancePlayer(playerID int32, currentPosition int32, diceRoll int32) { nextPos := (currentPosition + diceRoll) - if nextPos > BoardSpaces-1 { // Passed Go, but did not land on Go + if nextPos > int32(len(BoardSpaces))-1 { // Passed Go, but did not land on Go GoVisitors = append(GoVisitors, playerID) } - nextPos %= BoardSpaces + nextPos %= int32(len(BoardSpaces)) - propType := BoardSpaceTypes[nextPos] + propType := BoardSpaces[nextPos] switch propType { case TypeGo: diff --git a/game/railroad.go b/game/railroad.go new file mode 100644 index 0000000..44e6bfa --- /dev/null +++ b/game/railroad.go @@ -0,0 +1,25 @@ +package game + +func numRailroadOwned(playerID int32) int32 { + var ownedCount int32 = 0 + for railroadID, ownerID := range PropertyOwners { + if ownerID == playerID && OwnablePropertyType[RespPropertyToOwnable[int32(railroadID)]] == TypeRailroad { + ownedCount++ + } + } + + return ownedCount +} + +func processOwnedRailroad() { + for _, oRV := range OwnedRailroadVisitors { + visitorID := oRV.visitorID + ownerID := oRV.ownerID + // railroadID := oRV.railroadID + + var rent int32 = RailroadRent[numRailroadOwned(ownerID)] + + AdjustPlayerMoney(visitorID, -rent) + AdjustPlayerMoney(ownerID, rent) + } +} diff --git a/game/utility.go b/game/utility.go index 292a7d2..897e3c9 100644 --- a/game/utility.go +++ b/game/utility.go @@ -1,5 +1,16 @@ package game +func numUtilities(playerID int32) int32 { + var ownedCount int32 = 0 + for utilityID, ownerID := range PropertyOwners { + if ownerID == playerID && OwnablePropertyType[RespPropertyToOwnable[int32(utilityID)]] == TypeUtility { + ownedCount++ + } + } + + return ownedCount +} + func processOwnedUtility() { for _, oUV := range OwnedUtilityVisitors { visitorID := oUV.visitorID @@ -7,7 +18,7 @@ func processOwnedUtility() { // utilityID := oUV.utilityID diceRoll := oUV.diceRoll - var rent int32 = UtilityRentMult[HasUtiltyMonopoly(ownerID)] * diceRoll + var rent int32 = UtilityRentMult[numUtilities(ownerID)] * diceRoll AdjustPlayerMoney(visitorID, -rent) AdjustPlayerMoney(ownerID, rent)