ttt_game: Tic-Tac-Toe Game

Description Usage Value Examples

View source: R/ttt_game.R

Description

Object that encapsulates a tic-tac-toe game.

Usage

1

Value

ttt_game object

Fields

state

3 x 3 matrix of current state

nextmover, prevmover

Next and previous mover (1 or 2)

history

N x 2 matrix of game history, each row represents a move by (player, position)

Methods

play(position, ...)

Play a move. At default, play is made by the next mover, but can be changed by setting the 'nextmover' argument.

Input:

  • position: position to play

  • ...: Variables to overload

Output: TRUE iff a move is legal and game has not been over.

undo()

Undo the previous play

Input: None

Output: NULL

is_legal(position)

Check if the position is a legal move

Input:

  • position: position to check

Output: TRUE if the given position is a legal move

legal_moves()

Returns all legal moves

Input: None

Output: Integer vector of legal moves

check_win(player)

Check if the given player has won.

Input:

  • player: player (1 or 2)

  • ...: Variables to be overloaded

Output: TRUE iff the given player has won

check_result()

Check the result from the board state

Input: None

Output:

  • -1: undetermined yet

  • 0: draw

  • 1: won by player 1

  • 2: won by player 2

next_state(position, ...)

Returns the hypothetical next state without changing the state field.

Input:

  • position: position to play

Output: state matrix

show_board()

print the boad on consle

Input: None

Output: NULL

to_index(position)

Convert a position to the index

Input:

  • position: a position

Output: an integer 1 to 9, or 0 for a invalid position

index_to_str(position)

Convert a position to a location representation in the form of "A1"

Input:

  • position: a position

Output: a character

Examples

1
2
3
4
5
6
7
x <- ttt_game()
x$play(3)
x$play(5)
x$show_board()

x$undo()
x$show_board()

kota7/tictactoe documentation built on May 20, 2019, 1:11 p.m.