]> Skullheadx's Git Forge - monopoly-web.git/commitdiff
separate helpers, add railroad
authorSkullheadx <admonty1@protonmail.com>
Sun, 17 May 2026 04:10:17 +0000 (00:10 -0400)
committerSkullheadx <admonty1@protonmail.com>
Sun, 17 May 2026 04:10:17 +0000 (00:10 -0400)
game/color.go
game/game.go
game/movement.go
game/railroad.go [new file with mode: 0644]
game/utility.go

index 55c3ed41b731c69fd1dff62da31d14c54d6d895e..937cfb2a8cf790261a0c3a705f50d4701c698833 100644 (file)
@@ -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
                }
 
index 7c6cfc869538743cb134c94838af16412350d21f..29467db44820865e93369f9ffbc79c78cd4c4b94 100644 (file)
@@ -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 {
index 0eba618322fb5328bb745644aa1715bbff02742c..e74bb6f3f310db02ab95694fa5a66b7da40355cd 100644 (file)
@@ -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 (file)
index 0000000..44e6bfa
--- /dev/null
@@ -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)
+       }
+}
index 292a7d266a7ef8d061ea24fcb614ae47b6c5c2e6..897e3c921b156e8f776e604df630a26335a97c30 100644 (file)
@@ -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)