R/2-spyMaster.R

###########################################################
### Define methods to handle the spy master role (CPU)

#' Execute Single Spy Master Role (CPU)
#'
#' This method handles all actions for the entire CPU turn.
#'
#' @param board The most recent \code{Board} object.
#' @return An updated \code{Board} object.
#'
#' @export
setGeneric("spyMaster.CPU",
           function(board) standardGeneric("spyMaster.CPU")
)

#' @describeIn spyMaster.CPU Method for \code{Board} objects.
#' @export
setMethod("spyMaster.CPU", "Board",
          function(board){

            cat("Take a look at the board: \n")
            print(board)
            cat("Let me think of a clue...\n")

            # Find the best clue for the target words still on the board
            clues <- sort(considerInstance(board), decreasing = TRUE)

            # Calculate the number of words that match this clue (min. score: 0)
            stillintended <- board@words[board@spies == board@team & board@onboard]
            clues.number <- sum(codenames:::reference[names(clues)[1], stillintended] != 0)

            # Add clue to the clue history (i.e., remove clue from play)
            board@history <- append(board@history, names(clues)[1])

            # - Give the clue and check the player's guess
            # - If guess is correct, offer another guess
            #   - max guesses: clues.number + 1
            cat("Okay, I got it!\n")
            for(i in 1:clues.number){

              cat("Your clue is ", names(clues)[1], ": ", clues.number, sep = "")
              guess <- askPlayer("\nWhat will you choose?")
              board <- checkGuess(board, guess)

              # If the guess finishes the game, make an announcement
              if(board@endgame){

                cat("The game is now over.\n")
                return(board)
              }

              # If the guess is correct, allow another guess
              if(board@lastguess & i < clues.number){

                cat("Good guessing! Make another guess or type 'pass'.\n")
                print(board)
              }

              # If the guess is wrong, do not allow another guess
              if(!board@lastguess){

                cat("Incorrect guess. Moving on...\n")
                return(board)
              }
            }

            if(board@lastguess){

              cat("Good guessing! Make a bonus guess or type 'pass'.\n")
              print(board)
              guess <- askPlayer("What will you choose?")
              board <- checkGuess(board, guess)
            }

            # If the guess finishes the game, make an announcement
            if(board@endgame){

              cat("The game is now over.\n")
              return(board)
            }

            return(board)
          }
)

###########################################################
### Define methods to handle the spy master role (Human)

#' Execute Single Spy Master Role (Human)
#'
#' This method handles all actions for the entire human turn.
#'
#' @param board The most recent \code{Board} object.
#' @return An updated \code{Board} object.
#'
#' @export
setGeneric("spyMaster.Human",
           function(board) standardGeneric("spyMaster.Human")
)

#' @describeIn spyMaster.Human Method for \code{Board} objects.
#' @export
setMethod("spyMaster.Human", "Board",
          function(board){

            cat("Take a look at the board: \n")
            print(board)
            cat("The spy master should provide a clue.\n")
            clues.number <- as.numeric(askPlayer("How many clues did the spy master give?"))

            # - Give the clue and check the player's guess
            # - If guess is correct, offer another guess
            #   - max guesses: clues.number + 1
            for(i in 1:clues.number){

              cat("The spy master has offered", clues.number, "clue(s).")
              guess <- askPlayer("\nWhat will you choose?")
              board <- checkGuess(board, guess)

              # If the guess finishes the game, make an announcement
              if(board@endgame){

                cat("The game is now over.\n")
                return(board)
              }

              # If the guess is correct, allow another guess
              if(board@lastguess & i < clues.number){

                cat("Good guessing! Make another guess or type 'pass'.\n")
                print(board)
              }

              # If the guess is wrong, do not allow another guess
              if(!board@lastguess){

                cat("Incorrect guess. Moving on...\n")
                return(board)
              }
            }

            if(board@lastguess){

              cat("Good guessing! Make a bonus guess or type 'pass'.\n")
              print(board)
              guess <- askPlayer("What will you choose?")
              board <- checkGuess(board, guess)
            }

            # If the guess finishes the game, make an announcement
            if(board@endgame){

              cat("The game is now over.\n")
              return(board)
            }

            return(board)
          }
)
tpq/codenames documentation built on May 31, 2019, 6:50 p.m.