R/3-init.R

###########################################################
### Define methods to handle the codenames game

#' Codenames Game Interface
#'
#' This function executes the entire game, including computer AI if either
#'  the \code{sm1} or \code{sm2} argument is set to \code{"cpu"}.
#'
#' @param sm1 Spy master 1. Select from "human" or "cpu".
#' @param sm2 Spy master 2. Select from "human" or "cpu".
#' @return The endgame \code{Board} object.
#'
#' @export
codenames <- function(sm1 = "human", sm2 = "cpu"){

  cat("Making a new board...\n")
  board <- makeNewBoard(defaultTeam = "R")

  if(sm1 == "human" | sm2 == "human"){

    cat("You decided on using at least one human spy master.\n")
    cat("Other plays should now let the spy master(s) see the spy layout.\n")
    n <- readline(prompt = "Press any key to view the spy layout.")
    print(matrix(paste0("[--", board@spies, "--]"), nrow = 5, ncol = 5))
    n <- readline(prompt = "Press any key to hide the spy layout.")
    cat(rep("\n", 1000))
  }

  if(table(board@spies)["B"] > table(board@spies)["R"]){

    cat("Player 2 (Blue) goes first!\n")
    first <- "B"

  }else{

    cat("Player 1 (Red) goes first!\n")
    first <- "R"
  }

  # Cycle through spy master roles until game end
  turn <- first
  while(!board@endgame){

    if(turn == "B"){

      cat("Begin turn for Player 2 (Blue):\n")
      board@team <- "B"
      if(sm1 == "cpu"){ board <- spyMaster.CPU(board)
      }else{ board <- spyMaster.Human(board)
      }
      turn <- "R"

    }else if(turn == "R"){

      cat("Begin turn for Player 1 (Red):\n")
      board@team <- "R"
      if(sm2 == "cpu"){ board <- spyMaster.CPU(board)
      }else{ board <- spyMaster.Human(board)
      }
      turn <- "B"
    }
  }

  # Handle the end of the game
  if(board@winner == "B"){

    cat("Player 2 (Blue) wins!\n")

  }else if(board@winner == "R"){

    cat("Player 1 (Red) wins!\n")

  }else{

    stop("DEBUG ERROR: No winner selected.")
  }

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