Skip to content

Commit

Permalink
do notcheck extra jumps if just became king
Browse files Browse the repository at this point in the history
  • Loading branch information
Longwater1234 committed Sep 13, 2024
1 parent 02af1f1 commit dc71795
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
3 changes: 1 addition & 2 deletions room/capture_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func processCapturePiece(basePayload *game.BasePayload, gameMap map[int32]*game.
return true
}

// validateCapture when player `p` attacks by opponent's piece. returns TRUE if valid, else FALSE
// validateCapture when player `p` attacks by opponent's piece, AND then updates gameMap. returns TRUE if success
func validateCapture(captureReq *game.CapturePayload, gameMap map[int32]*game.Piece) bool {
if captureReq.GetDetails() == nil || captureReq.GetDestination() == nil {
return false
Expand Down Expand Up @@ -73,7 +73,6 @@ func validateCapture(captureReq *game.CapturePayload, gameMap map[int32]*game.Pi
if !success {
return false
}

delete(gameMap, hunterSrc) // set hunter's old location empty!
delete(gameMap, preyCell) // set Prey's old location empty!
gameMap[destCell.GetCellIndex()] = hunterPiecePtr // move hunter to new location
Expand Down
37 changes: 34 additions & 3 deletions room/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func RunMatch(p1 *player.Player, p2 *player.Player, gameOver chan bool) {
var gameMap = generateGameMap(p1, p2) // map of cell index --> pieces.

//START GAME MAIN LOOP
// --- top ---
for {
if isPlayerRedTurn {
// ============= IT'S PLAYER 1 (RED's) TURN =============//
Expand Down Expand Up @@ -79,6 +78,7 @@ func RunMatch(p1 *player.Player, p2 *player.Player, gameOver chan bool) {
} else if payload.GetCapturePayload() != nil {
//if MESSAGE TYPE == "capture"
log.Println("capture", payload.GetCapturePayload().String())
isKingBefore := getKingStatusBefore(payload.GetCapturePayload(), gameMap)
valid := processCapturePiece(&payload, gameMap, p1, p2)
if !valid {
gameOver <- true
Expand All @@ -90,8 +90,10 @@ func RunMatch(p1 *player.Player, p2 *player.Player, gameOver chan bool) {
return
}
//check for extra opportunities for P1. if NONE, toggle turns
isKingNow := getKingStatusAfter(payload.GetCapturePayload(), gameMap)
currentCell := payload.GetCapturePayload().Destination.CellIndex
if hasExtraTargets(p1, currentCell, gameMap) {
var needCheck bool = isKingBefore == isKingNow
if needCheck && hasExtraTargets(p1, currentCell, gameMap) {
log.Println(p1.Name, " have extra targets!")
continue
}
Expand Down Expand Up @@ -133,6 +135,7 @@ func RunMatch(p1 *player.Player, p2 *player.Player, gameOver chan bool) {
} else if payload.GetCapturePayload() != nil {
//if MESSAGE TYPE == "capture"
log.Println("capture", payload.GetCapturePayload().String())
isKingBefore := getKingStatusBefore(payload.GetCapturePayload(), gameMap)
valid := processCapturePiece(&payload, gameMap, p2, p1)
if !valid {
gameOver <- true
Expand All @@ -144,8 +147,10 @@ func RunMatch(p1 *player.Player, p2 *player.Player, gameOver chan bool) {
return
}
//check for extra opportunities for P2. if NONE, toggle turns
isKingNow := getKingStatusAfter(payload.GetCapturePayload(), gameMap)
hunterCurrCell := payload.GetCapturePayload().Destination.CellIndex
if hasExtraTargets(p2, hunterCurrCell, gameMap) {
var needCheck bool = isKingBefore == isKingNow
if needCheck && hasExtraTargets(p2, hunterCurrCell, gameMap) {
log.Println(p2.Name, " have extra targets!")
continue
}
Expand All @@ -155,3 +160,29 @@ func RunMatch(p1 *player.Player, p2 *player.Player, gameOver chan bool) {
}
}
}

// getKingStatusBefore capturing the opponent. Returns TRUE if piece is King at given current Cell
func getKingStatusBefore(capturePayload *game.CapturePayload, gameMap map[int32]*game.Piece) bool {
if capturePayload == nil || capturePayload.GetDetails() == nil {
return false
}
srcCell := capturePayload.GetDetails().GetHunterSrcCell()
if piecePtr, exists := gameMap[srcCell]; !exists {
return false
} else {
return piecePtr.IsKing
}
}

// getKingStatusAfter capturing opponent. Returns TRUE if piece is King at destination Cell
func getKingStatusAfter(capturePayload *game.CapturePayload, gameMap map[int32]*game.Piece) bool {
if capturePayload == nil || capturePayload.GetDestination() == nil {
return false
}
destCell := capturePayload.GetDestination().GetCellIndex()
if piecePtr, exists := gameMap[destCell]; !exists {
return false
} else {
return piecePtr.IsKing
}
}

0 comments on commit dc71795

Please sign in to comment.