From: Skullheadx Date: Sun, 17 May 2026 22:10:06 +0000 (-0400) Subject: jail X-Git-Url: http://git.skullheadx.com/about.html?a=commitdiff_plain;h=29fff00532181c0f23552a6254e84f1b25298bf2;p=monopoly-web.git jail --- diff --git a/game/chance.go b/game/chance.go index c1cd556..756fb01 100644 --- a/game/chance.go +++ b/game/chance.go @@ -64,7 +64,7 @@ func ProcessChance() { } case 4: - // TODO: GO TO JAIL + InJailVisitors = append(InJailVisitors, InJailVisitor{visitorID: visitorID, turns: DEFAULT_JAIL_TURNS}) case 5: MoveQueue = append(MoveQueue, GetPlayerMoveDistance(currentPos, ReadingRailroadSpaceID)) case 6: diff --git a/game/chest.go b/game/chest.go index af69211..7f0b348 100644 --- a/game/chest.go +++ b/game/chest.go @@ -74,7 +74,7 @@ func ProcessChest() { case 12: AdjustPlayerMoney(visitorID, 25) case 13: - // TODO: GO TO JAIL + InJailVisitors = append(InJailVisitors, InJailVisitor{visitorID: visitorID, turns: DEFAULT_JAIL_TURNS}) case 14: // TODO: GET OUT OF JAIL FREE case 15: diff --git a/game/game.go b/game/game.go index ac33da8..ef774ef 100644 --- a/game/game.go +++ b/game/game.go @@ -9,6 +9,7 @@ var RandSrc = rand.New(RandSeed) var Users []User var DebtEvents []int32 +var MoveablePlayers []int32 var MoveQueue []int32 var ModifierRailroadRentMultiplier int32 = 1 @@ -18,7 +19,7 @@ var TurnPlayerID int32 = 0 var TurnEndedSignal bool = false var DiceRollsRemaining int32 = 1 var numDiceRolled int32 = 0 -var Doubles bool = false +var RolledDoubles bool = false func ValidateCanRoll(UUID string) bool { if Users[TurnPlayerID].UUID == UUID && DiceRollsRemaining > 0 { @@ -35,6 +36,21 @@ func ValidateCanEndTurn(UUID string) bool { return true } +func ProcessLanding() { + ProcessGo() + ProcessTax() + + ProcessOwnedColors() + ProcessOwnedUtility() + ProcessOwnedRailroad() + + ProcessChance() + ProcessChest() + // ProcessPolice() + + ProcessJail() +} + func RollDice() { // Roll Dice diceRoll1 := RandSrc.Int32N(6) + 1 @@ -43,45 +59,27 @@ func RollDice() { numDiceRolled++ if diceRoll1 == diceRoll2 { - Doubles = true + RolledDoubles = true DiceRollsRemaining++ + RemovePlayerFromJail(TurnPlayerID) } if numDiceRolled >= 3 { - // TODO: GO TO JAIL - } - - // Movement - for { - // condition to stop moving - if len(MoveQueue) == 0 { - break - } - - // does one movement - // TODO: prevent movement while in JAIL - ProcessMovement() - - ProcessGo() - ProcessTax() - - ProcessOwnedColors() - ProcessOwnedUtility() - ProcessOwnedRailroad() - - ProcessChance() - ProcessChest() - - // ProcessPolice() - // ProcessJail() + InJailVisitors = append(InJailVisitors, InJailVisitor{visitorID: TurnPlayerID, turns: DEFAULT_JAIL_TURNS}) } } func EndTurn() { + // next player's turn TurnPlayerID = (TurnPlayerID + 1) % int32(len(Users)) TurnEndedSignal = false + + // reset dice DiceRollsRemaining = 1 numDiceRolled = 0 - Doubles = false + RolledDoubles = false + + // let player move next turn + MoveQueue = MoveQueue[:0] } diff --git a/game/jail.go b/game/jail.go new file mode 100644 index 0000000..a1387d4 --- /dev/null +++ b/game/jail.go @@ -0,0 +1,38 @@ +package game + +const DEFAULT_JAIL_TURNS int32 = 3 + +func RemovePlayerFromJail(playerID int32) { + for i, iJV := range InJailVisitors { + if playerID == iJV.visitorID { + InJailVisitors[i] = InJailVisitors[len(InJailVisitors)-1] + InJailVisitors = InJailVisitors[:len(InJailVisitors)-1] + + } + + } +} + +func RemovePlayerFromMoveable(pID int32) { + for i, playerID := range MoveablePlayers { + if pID == playerID { + MoveablePlayers[i] = MoveablePlayers[len(MoveablePlayers)-1] + MoveablePlayers = MoveablePlayers[:len(MoveablePlayers)-1] + } + } +} + +func ProcessJail() { + for _, iJV := range InJailVisitors { + visitorID := iJV.visitorID + turns := iJV.turns + + if turns <= 0 { + RemovePlayerFromJail(visitorID) + MoveablePlayers = append(MoveablePlayers, visitorID) + } else { + RemovePlayerFromMoveable(visitorID) + } + + } +} diff --git a/game/movement.go b/game/movement.go index 01d2323..284e91b 100644 --- a/game/movement.go +++ b/game/movement.go @@ -273,6 +273,11 @@ type UnownedPropertyVisitor struct { propertyID int32 } +type InJailVisitor struct { + visitorID int32 + turns int32 +} + var ( UnownedPropertyVisitors []UnownedPropertyVisitor OwnedColorVisitors []OwnedColorVisitor @@ -282,16 +287,46 @@ var ( TaxVisitors []int32 ChanceVisitors []int32 ChestVisitors []int32 - JailVisitors []int32 + InJailVisitors []InJailVisitor ParkingVisitors []int32 PoliceVisitors []int32 ) +func AllowedToMove(playerID int32) bool { + for _, pID := range MoveablePlayers { + if playerID == pID { + return true + } + } + return false +} + func ProcessMovement() { - dist := MoveQueue[0] - MoveQueue = MoveQueue[1:] - player := Users[TurnPlayerID] - AdvancePlayer(TurnPlayerID, player.CurrentSpaceID, dist) + for i, playerID := range MoveablePlayers { + if playerID == TurnPlayerID { + + // Movement + for { + // condition to stop moving + if len(MoveQueue) == 0 { + // player can no longer move + MoveablePlayers[i] = MoveablePlayers[len(MoveablePlayers)-1] + MoveablePlayers = MoveablePlayers[:len(MoveablePlayers)-1] + break + } + + dist := MoveQueue[0] + MoveQueue = MoveQueue[1:] + player := Users[playerID] + AdvancePlayer(playerID, player.CurrentSpaceID, dist) + + ProcessLanding() + } + + } + + } + } func CalculateNextPos(currentPosition int32, distance int32) int32 { @@ -306,7 +341,7 @@ func AdvancePlayer(playerID int32, currentPosition int32, diceRoll int32) { numGoPasses := (currentPosition + diceRoll) / (int32(len(BoardSpaces)) - 1) if numGoPasses > 0 { - for _ = range numGoPasses { + for range numGoPasses { GoVisitors = append(GoVisitors, playerID) } } @@ -324,10 +359,10 @@ func AdvancePlayer(playerID int32, currentPosition int32, diceRoll int32) { TaxVisitors = append(TaxVisitors, playerID) // case TypeParking: // nothing ever happens // ParkingVisitors = append(ParkingVisitors, playerID) - case TypePolice: - PoliceVisitors = append(PoliceVisitors, playerID) + case TypePolice: // hardcoding to send straight to jail + InJailVisitors = append(InJailVisitors, InJailVisitor{visitorID: playerID, turns: DEFAULT_JAIL_TURNS}) case TypeJail: - JailVisitors = append(JailVisitors, playerID) + InJailVisitors = append(InJailVisitors, InJailVisitor{visitorID: playerID, turns: DEFAULT_JAIL_TURNS}) case TypeColor: propIndex := SpaceToOwnableProperty[nextPos] if PropertyOwners[propIndex] != -1 { // property owned? diff --git a/main.go b/main.go index e50478b..3303452 100644 --- a/main.go +++ b/main.go @@ -32,6 +32,7 @@ const UUID = "abc" // TODO: UUID in cookie func rollDiceHandler(w http.ResponseWriter, req *http.Request) { if game.ValidateCanRoll(UUID) { game.RollDice() + game.ProcessMovement() } }