Title: | Tic-Tac-Toe Package for R |
---|---|
Description: | Play the classic game of tic-tac-toe (naughts and crosses). |
Authors: | Johan Jordaan |
Maintainer: | Johan Jordaan <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.2 |
Built: | 2024-11-05 05:27:45 UTC |
Source: | https://github.com/johanjordaan/rt3 |
It's value is the character "_".
EMPTY
EMPTY
An object of class character
of length 1.
Internally this player calls getMoves and then picks the first entry in the list of moves. A player is a function that takes a game state as input and returns a valid move index.
firstAvailableMovePlayer(gameState)
firstAvailableMovePlayer(gameState)
gameState |
The gameState that the player should act on. |
moveIndex Index to a valid move as returned by the getMoves function.
gameState <- startGame() move <- firstAvailableMovePlayer(gameState)
gameState <- startGame() move <- firstAvailableMovePlayer(gameState)
The boards state represented by a list. It contains a list of X's, O's and EMPTY's. It's initially filled by EMPTY's.
The player who needs to make the next move. This either X or O.
the player who was the first player to move in this game state. This either X or O.
The list of moves made by players to get to this game state. This initially filled with 0's.
The player turn list. It contains a list of alternating X's and O's
Number of moves made to get to this game state.
This indicates wheter this is a final game state. It is final if either X or O has won if there is no winner: NONE.
If there is a winner in this games state the value is either X or O. If the game state is a draw or the game is not finished the value is NONE.
gameState
gameState
An object of class list
of length 8.
Get the list of valid move from the game state.
getMoves(gameState)
getMoves(gameState)
gameState |
The gameState for which moves must be calculated. |
validMoves An array (["integer"]) of valid moves based on the provided game state.
gameState <- startGame() validMoves <- getMoves(gameState)
gameState <- startGame() validMoves <- getMoves(gameState)
Apply the move to the current game state an produce a new game state.
makeMove(gameState, move)
makeMove(gameState, move)
gameState |
The gameState to apply the move to. |
move |
The move to be applied to the game state. |
gameState The game state after applying the move to the game state.
gameState <- startGame() gameState <- makeMove(gameState,1)
gameState <- startGame() gameState <- makeMove(gameState,1)
It's value is the character "_".
NONE
NONE
An object of class character
of length 1.
It's value is the character "O".
O
O
An object of class character
of length 1.
Play a game of Tic-Tac-Toe using the two provided stragies.
playGame(px, po)
playGame(px, po)
px |
The X player strategy. |
po |
The O player strategy. |
gameState The final gameState after playing a full game.
px <- firstAvailableMovePlayer py <- randomMovePlayer finalGameState <- playGame(px,py)
px <- firstAvailableMovePlayer py <- randomMovePlayer finalGameState <- playGame(px,py)
Internally this player calls getMoves and then picks an entry in the list of moves at random.
A player is a function that takes a game state as input and returns a valid move index.
randomMovePlayer(gameState)
randomMovePlayer(gameState)
gameState |
The gameState that the player should act on. |
moveIndex Index to a valid move as returned by the getMoves function.
gameState <- startGame() move <- randomMovePlayer(gameState)
gameState <- startGame() move <- randomMovePlayer(gameState)
The rt3 package provides functions to allow a user to simulate tic-tac-toe games. It provides a convenient gameState object as well as simple interface for developing new types of players.
playGame Play a game of tic-tac-toe.
gameState A tic-tac-toe game state.
X The X player.
O The O player.
EMPTY The EMPTY constant. Used to indicate an empty board position.
NONE The NONE constant. Used to indicate a draw.
These functions are used by the playGame function.The will also be usefull in building game decsion trees for more complex players.
startGame Create a new tic-tac-toe game state.
getMoves Get the current set of valid moves for a given game state
makeMove Apply a move to the given game state and return the resulting game state
randomMovePlayer A player that plays random valid moves
firstAvailableMovePlayer A player that always plays the first move available
https://en.wikipedia.org/wiki/Tic-tac-toe
This function starts a new game. It randomly assigns a starting player and returns a new game state object.
startGame()
startGame()
gameState A new gameState.
gameState <- startGame()
gameState <- startGame()
It's value is the character "O".
X
X
An object of class character
of length 1.