From 8584c29d1bbbd591f8d147d271cf473960e8bcd1 Mon Sep 17 00:00:00 2001 From: Skullheadx Date: Sat, 16 May 2026 23:25:48 -0400 Subject: [PATCH] helper funcs for has monopoly and adjust money --- game/color.go | 20 +++++++++++ game/game.go | 71 ++++++++++++++++++++---------------- game/go.go | 4 ++- game/movement.go | 94 ++++++++++++++++++++++++++++++++---------------- 4 files changed, 126 insertions(+), 63 deletions(-) diff --git a/game/color.go b/game/color.go index cde26fe..1354782 100644 --- a/game/color.go +++ b/game/color.go @@ -1 +1,21 @@ package game + +func processOwnedColors() { + for _, ocv := range OwnedColorVisitors { + visitorID := ocv.visitorID + ownerID := ocv.ownerID + colorID := ocv.colorID + + prop := ColorProperties[colorID] + prices := ColorPropertyRents[colorID] + + var rent int32 = prices[prop.Houses] + + if prop.Houses == 0 && HasMonopoly(ownerID, prop.GroupID) { + rent *= 2 + } + + AdjustPlayerMoney(visitorID, -rent) + AdjustPlayerMoney(ownerID, rent) + } +} diff --git a/game/game.go b/game/game.go index 0a95690..b137ba9 100644 --- a/game/game.go +++ b/game/game.go @@ -1,33 +1,42 @@ package game -const MaxPlayers int32 = 8 - -var Users [MaxPlayers]User - -// -// const UtilityPrice int32 = 150 -// const UtilityRentMult = [2]int32{4, 10} -// const UtilityMortgageValue int32 = 75 -// -// var Utilities = [2]Utility{ -// {Name: "Electric Company", Mortgaged: false}, -// {Name: "Waterworks", Mortgaged: false}, -// } -// -// const RailroadPrice int32 = 200 -// const RailroadRent = [4]int32{25, 50, 100, 200} -// const RailroadMortgageValue int32 = 100 -// -// var RailRoads = [4]RailRoad{ -// {Name: "Reading Railroad", Mortgaged: false}, -// {Name: "Pennsylvania Railroad", Mortgaged: false}, -// {Name: "B.&O. Railroad", Mortgaged: false}, -// {Name: "Short Line", Mortgaged: false}, -// } -// -// -// var Chest = [3]Chest{ -// {Name: "Community Chest"}, -// {Name: "Community Chest"}, -// {Name: "Community Chest"}, -// } +var Users []User +var DebtEvents []int32 + +func HasMonopoly(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 IsInDebt(playerID int32) (bool, int32) { + for i, pID := range DebtEvents { + if pID == playerID { + return true, int32(i) + } + } + return false, -1 +} + +func AdjustPlayerMoney(playerID int32, amount int32) { + Users[playerID].Money += amount + + inDebt, i := IsInDebt(playerID) + + if Users[playerID].Money < 0 { + if !inDebt { + DebtEvents = append(DebtEvents, playerID) + } + } else { // Money >= 0 + if inDebt { // remove player from DebtEvents table + DebtEvents[i] = DebtEvents[len(DebtEvents)-1] + DebtEvents = DebtEvents[:len(DebtEvents)-1] + } + } + +} diff --git a/game/go.go b/game/go.go index cc1ae84..d88505b 100644 --- a/game/go.go +++ b/game/go.go @@ -1,7 +1,9 @@ package game +const GO_SALARY int32 = 200 + func ProcessGo() { for _, playerID := range GoVisitors { - Users[playerID].Money += 200 + AdjustPlayerMoney(playerID, GO_SALARY) } } diff --git a/game/movement.go b/game/movement.go index 28017be..92356bd 100644 --- a/game/movement.go +++ b/game/movement.go @@ -50,36 +50,60 @@ type PropertyStatic struct { Price int32 } +type ColorGroup int32 + +const ( + GroupBrown ColorGroup = iota + GroupLightBlue + GroupPink + GroupOrange + GroupRed + GroupYellow + GroupGreen + GroupDarkBlue +) + +var ColorGroupSizes = [...]int32{ + GroupBrown: 2, + GroupLightBlue: 3, + GroupPink: 3, + GroupOrange: 3, + GroupRed: 3, + GroupYellow: 3, + GroupGreen: 3, + GroupDarkBlue: 2, +} + type ColorProperty struct { Name string Price int32 - GroupID int32 + GroupID ColorGroup Houses int32 } var ColorProperties = []ColorProperty{ - {GroupID: 0, Houses: 0, Name: "Mediterranean Avenue", Price: 60}, - {GroupID: 0, Houses: 0, Name: "Baltic Avenue", Price: 60}, - {GroupID: 1, Houses: 0, Name: "Oriental Avenue", Price: 100}, - {GroupID: 1, Houses: 0, Name: "Vermont Avenue", Price: 100}, - {GroupID: 1, Houses: 0, Name: "Connecticut Avenue", Price: 120}, - {GroupID: 2, Houses: 0, Name: "St. Charles Place", Price: 140}, - {GroupID: 2, Houses: 0, Name: "States Avenue", Price: 140}, - {GroupID: 2, Houses: 0, Name: "Virginia Avenue", Price: 160}, - {GroupID: 3, Houses: 0, Name: "St. James Place", Price: 180}, - {GroupID: 3, Houses: 0, Name: "Tennessee Avenue", Price: 180}, - {GroupID: 3, Houses: 0, Name: "New York Avenue", Price: 200}, - {GroupID: 4, Houses: 0, Name: "Kentucky Avenue", Price: 220}, - {GroupID: 4, Houses: 0, Name: "Indiana Avenue", Price: 220}, - {GroupID: 4, Houses: 0, Name: "Illinois Avenue", Price: 240}, - {GroupID: 5, Houses: 0, Name: "Atlantic Avenue", Price: 260}, - {GroupID: 5, Houses: 0, Name: "Ventnor Avenue", Price: 260}, - {GroupID: 5, Houses: 0, Name: "Marvin Gardens", Price: 280}, - {GroupID: 6, Houses: 0, Name: "Pacific Avenue", Price: 300}, - {GroupID: 6, Houses: 0, Name: "North Carolina Avenue", Price: 300}, - {GroupID: 6, Houses: 0, Name: "Pennsylvania Avenue", Price: 320}, - {GroupID: 7, Houses: 0, Name: "Park Place", Price: 350}, - {GroupID: 7, Houses: 0, Name: "Boardwalk", Price: 400}, + {GroupID: GroupBrown, Houses: 0, Name: "Mediterranean Avenue", Price: 60}, + {GroupID: GroupBrown, Houses: 0, Name: "Baltic Avenue", Price: 60}, + {GroupID: GroupLightBlue, Houses: 0, Name: "Oriental Avenue", Price: 100}, + {GroupID: GroupLightBlue, Houses: 0, Name: "Vermont Avenue", Price: 100}, + {GroupID: GroupLightBlue, Houses: 0, Name: "Connecticut Avenue", Price: 120}, + {GroupID: GroupPink, Houses: 0, Name: "St. Charles Place", Price: 140}, + {GroupID: GroupPink, Houses: 0, Name: "States Avenue", Price: 140}, + {GroupID: GroupPink, Houses: 0, Name: "Virginia Avenue", Price: 160}, + {GroupID: GroupOrange, Houses: 0, Name: "St. James Place", Price: 180}, + {GroupID: GroupOrange, Houses: 0, Name: "Tennessee Avenue", Price: 180}, + {GroupID: GroupOrange, Houses: 0, Name: "New York Avenue", Price: 200}, + {GroupID: GroupRed, Houses: 0, Name: "Kentucky Avenue", Price: 220}, + {GroupID: GroupRed, Houses: 0, Name: "Indiana Avenue", Price: 220}, + {GroupID: GroupRed, Houses: 0, Name: "Illinois Avenue", Price: 240}, + {GroupID: GroupYellow, Houses: 0, Name: "Atlantic Avenue", Price: 260}, + {GroupID: GroupYellow, Houses: 0, Name: "Ventnor Avenue", Price: 260}, + {GroupID: GroupYellow, Houses: 0, Name: "Marvin Gardens", Price: 280}, + {GroupID: GroupGreen, Houses: 0, Name: "Pacific Avenue", Price: 300}, + {GroupID: GroupGreen, Houses: 0, Name: "North Carolina Avenue", Price: 300}, + {GroupID: GroupGreen, Houses: 0, Name: "Pennsylvania Avenue", Price: 320}, + {GroupID: GroupDarkBlue, Houses: 0, Name: "Park Place", Price: 350}, + {GroupID: GroupDarkBlue, Houses: 0, Name: "Boardwalk", Price: 400}, } var ColorPropertyRents = [][6]int32{ @@ -114,11 +138,19 @@ var RailroadProperties = []PropertyStatic{ {Name: "Short Line", Price: 200}, } +const RailroadPrice int32 = 200 +const RailroadRent = [4]int32{25, 50, 100, 200} +const RailroadMortgageValue int32 = 100 + var UtilityProperties = []PropertyStatic{ {Name: "Electric Company", Price: 150}, {Name: "Waterworks", Price: 150}, } +const UtilityPrice int32 = 150 +const UtilityRentMult = [2]int32{4, 10} +const UtilityMortgageValue int32 = 75 + type TaxSpace struct { Name string Amount int32 @@ -129,7 +161,7 @@ var TaxSpaces = []TaxSpace{ {Name: "Luxury Tax", Amount: 100}, } -var PropertyOwned = []int32{} // playerID +var PropertyOwners = []int32{} // playerID var SpaceToRespProperty = make(map[int32]int32) var SpaceToOwnableProperty = make(map[int32]int32) @@ -149,7 +181,7 @@ func init() { if propertyType == TypeColor || propertyType == TypeRailroad || propertyType == TypeUtility { SpaceToOwnableProperty[spaceID] = ownableIndex - PropertyOwned = append(PropertyOwned, -1) + PropertyOwners = append(PropertyOwners, -1) } switch propertyType { @@ -223,8 +255,8 @@ func AdvancePlayer(playerID int32, currentPosition int32, diceRoll int32) { GoVisitors = append(GoVisitors, playerID) case TypeColor: propIndex := SpaceToOwnableProperty[nextPos] - if PropertyOwned[propIndex] != -1 { // property owned? - ownerID := PropertyOwned[propIndex] + if PropertyOwners[propIndex] != -1 { // property owned? + ownerID := PropertyOwners[propIndex] if ownerID != playerID { // not by you OwnedColorVisitors = append(OwnedColorVisitors, OwnedColorVisitor{visitorID: playerID, ownerID: ownerID, colorID: SpaceToRespProperty[nextPos]}) } @@ -237,8 +269,8 @@ func AdvancePlayer(playerID int32, currentPosition int32, diceRoll int32) { TaxVisitors = append(TaxVisitors, playerID) case TypeRailroad: propIndex := SpaceToOwnableProperty[nextPos] - if PropertyOwned[propIndex] != -1 { // property owned? - ownerID := PropertyOwned[propIndex] + if PropertyOwners[propIndex] != -1 { // property owned? + ownerID := PropertyOwners[propIndex] if ownerID != playerID { // not by you OwnedRailroadVisitors = append(OwnedRailroadVisitors, OwnedRailroadVisitor{visitorID: playerID, ownerID: ownerID, railroadID: SpaceToRespProperty[nextPos]}) } @@ -251,8 +283,8 @@ func AdvancePlayer(playerID int32, currentPosition int32, diceRoll int32) { JailVisitors = append(JailVisitors, playerID) case TypeUtility: propIndex := SpaceToOwnableProperty[nextPos] - if PropertyOwned[propIndex] != -1 { // property owned? - ownerID := PropertyOwned[propIndex] + if PropertyOwners[propIndex] != -1 { // property owned? + ownerID := PropertyOwners[propIndex] if ownerID != playerID { // not by you OwnedUtilityVisitors = append(OwnedUtilityVisitors, OwnedUtilityVisitor{visitorID: playerID, ownerID: ownerID, utilityID: SpaceToRespProperty[nextPos], diceRoll: diceRoll}) } -- 2.54.0