Skip to content

Commit

Permalink
Merge pull request #727 from FuelLabs/tictactoe-docs
Browse files Browse the repository at this point in the history
tictactoe docs
  • Loading branch information
SwayStar123 authored Aug 17, 2023
2 parents 63667b7 + 96160e8 commit f890763
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ library;

use core::ops::Eq;

/// Represents the state of a game.
pub enum State {
/// The game is currently being played.
Playing: (),
/// The game has ended.
Ended: (),
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
library;

/// Errors related to the state of the game.
pub enum GameStateError {
/// The game has ended.
GameHasEnded: (),
/// The game has not ended.
GameHasNotEnded: (),
}

/// Errors made by players.
pub enum PlayerError {
/// It is not the player's turn.
IncorrectPlayerTurn: (),
}

/// Errors related to the position of a cell.
pub enum PositionError {
/// The cell is occupied.
CellIsNotEmpty: (),
/// The cell is out of bounds.
InvalidPosition: (),
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
library;

/// Event for when a game results in a draw.
pub struct GameDrawnEvent {
/// The first player.
player_one: Identity,
/// The second player.
player_two: Identity,
}

/// Event for when a game is won.
pub struct GameWonEvent {
/// The winning player.
player: Identity,
}

/// Event for when a new game is started.
pub struct NewGameEvent {
/// The first player.
player_one: Identity,
/// The second player.
player_two: Identity,
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ abi Game {
///
/// # Arguments
///
/// * `player_one`: the first player to make a move.
/// * `player_two`: the second player to make a move.
/// * `player_one`: [Identity] - The first player to make a move.
/// * `player_two`: [Identity] - The second player to make a move.
///
/// # Reverts
///
Expand All @@ -16,11 +16,13 @@ abi Game {

/// Allows a player to make a move at a `position`.
///
/// # Additional Information
///
/// It also determines if the game has been won or drawn.
///
/// # Arguments
///
/// * `position`: the position where the player wants to move.
/// * `position`: [u64] - The position where the player wants to move.
///
/// # Reverts
///
Expand Down
20 changes: 20 additions & 0 deletions games/TicTacToe/project/contracts/tictactoe-contract/src/utils.sw
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ const MATCHES = [
];

/// Checks if a player has won.
///
/// # Arguments
///
/// * `board`: [Vec<Option<Identity>>] - A vector of all the markers on the board.
/// * `player`: [Identity] - The player to check for.
///
/// # Returns
///
/// * [bool] - True if the player has won, false otherwise.
pub fn win_check(board: Vec<Option<Identity>>, player: Identity) -> bool {
let mut i = 0;
while (i < 8) {
Expand All @@ -35,6 +44,17 @@ pub fn win_check(board: Vec<Option<Identity>>, player: Identity) -> bool {
}

/// Checks if the game ends up in a draw.
///
/// # Arguments
///
/// * `board`: [Vec<Option<Identity>>] - A vector of all the markers on the board.
/// * `player_one`: [Identity] - The first player.
/// * `player_two`: [Identity] - The second player.
/// * `move_counter`: [u64] - The number of moves made.
///
/// # Returns
///
/// * [bool] - True if the game has ended in a draw, false otherwise.
pub fn draw(
board: Vec<Option<Identity>>,
player_one: Identity,
Expand Down

0 comments on commit f890763

Please sign in to comment.