@rbxts/tic-tac-toe
Version:
Modular tic-tac-toe without dependencies, because why not.
150 lines (149 loc) • 5.18 kB
TypeScript
/// <reference types="@rbxts/compiler-types" />
import { TicTacToeMoveResult } from "./enum/TicTacToeMoveResult";
import { TicTacToeState } from "./enum/TicTacToeState";
import { TicTacToeSymbol } from "./enum/TicTacToeSymbol";
import { TicTacToeOutput } from "./output/TicTacToeOutput";
import { TicTacToePlayer } from "./player/TicTacToePlayer";
import { TicTacToeBoard } from "./TicTacToeBoard";
import { TicTacToeCell } from "./TicTacToeCell";
import { TicTacToeMove } from "./TicTacToeMove";
/**
* The game of TicTacToe.
*
* @template X The type of the player that plays the X symbol.
* @template O The type of the player that plays the O symbol, defaults to X.
*/
export declare class TicTacToeGame<X extends TicTacToePlayer = TicTacToePlayer, O extends TicTacToePlayer = X> {
private playerX;
private playerO;
/** The board used in this game. */
private board;
/** The outputs that the game announces to. */
private outputs;
/** The player that is responsible for making the next move. */
private currentPlayer;
/** The current state of the game. */
private state;
/** The current winner of the game, if there is one. */
private winner?;
/** The current winning cells if there are any. */
private winningCells?;
/** The last move that was made, if there is one. */
private lastMove?;
/**
* Constructs a new TicTacToeGame.
*
* @param playerX The player that plays the X symbol.
* @param playerO The player that plays the O symbol.
* @param firstPlayer The symbol of the player that will start, or omit to choose randomly.
* @param outputs The outputs that the game announces to.
*/
constructor(playerX: X, playerO: O, firstPlayer?: TicTacToeSymbol, ...outputs: TicTacToeOutput<TicTacToeGame<X, O>>[]);
/**
* Requests a move from the current player and handles the result.
* If the game is in any state except in progress, this method
* does nothing.
*
* If the player does not return a move, the game will be cancelled. Otherwise,
* the move is verified and registered, any state changes are recorded, and
* the current player is changed.
*
* @returns The result of the move.
*/
nextMove(): TicTacToeMoveResult;
/**
* Returns the symbol representing the current player.
*/
getCurrentPlayerSymbol(): Exclude<TicTacToeSymbol, TicTacToeSymbol.EMPTY>;
/**
* Handles any effects of the specified move.
*
* This checks the current state of the board,
* and sets the games state and variables accordingly.
* This will also announce to the outputs.
*
* @param move The move that was made.
*/
private handleMoveMade;
/**
* Cancels the game, if the game is in progress.
*/
cancel(): void;
/**
* Returns the player that is responsible for making the next move.
*/
getNextPlayer(): X | O;
/**
* Returns a random player of the two players.
*/
private getRandomPlayer;
/**
* Returns the player specified with the specified symbol,
* or a random player if the symbol is {@link TicTacToeSymbol.EMPTY}
*/
private getPlayerFromSymbol;
/**
* Resets the game. This will clear the board,
* reset the current player and state.
*
* This will also announce to the outputs that a new game has started.
* This will NOT announce that a game has ended, as this is not meant
* to be used to end a game, for that use {@link cancel}.
*/
reset(firstPlayer?: TicTacToeSymbol): void;
/**
* Returns the current player.
*/
getCurrentPlayer(): X | O;
/**
* Returns whether the game is in any state except in progress.
*/
isOver(): boolean;
/**
* Returns the current state of the game.
*/
getState(): TicTacToeState;
/**
* Returns the board of the game.
*/
getBoard(): TicTacToeBoard;
/**
* Returns a copy of the game's board.
*/
getBoardCopy(): TicTacToeBoard;
/**
* Returns the last move that was made.
*/
getLastMove(): TicTacToeMove | undefined;
/**
* Returns the current winner, if any.
*/
getWinner(): X | O | undefined;
/**
* Returns the current winning cells, if any.
*/
getWinningCells(): TicTacToeCell[] | undefined;
/**
* Returns the player representing the X symbol.
*/
getPlayerX(): X;
/**
* Returns the player representing the O symbol.
*/
getPlayerO(): O;
/**
* Add outputs to the game.
* These will be addressed whenever a significant event occurs.
*
* @param output The output to add.
*/
addOutputs(...outputs: TicTacToeOutput<TicTacToeGame<X, O>>[]): void;
/**
* Removes an output from the game.
*/
removeOutput(output: TicTacToeOutput): void;
/**
* Returns the outputs of the game.
*/
getOutputs(): TicTacToeOutput<TicTacToeGame<X, O>>[];
}