0.0.1-alpha2Component that renders the value of a Cell.
(object)
| Name | Description |
|---|---|
props.onClick function
|
Function to be triggered when component is clicked. |
props.value number
|
Value of Cell . |
props.isSelectable boolean
|
Whether the cell is selectable. |
props.turn string
|
Current turn, one of TURNS . |
props.taken boolean
|
Whether the cell is taken. |
props.undefined any
(default 'cell-comp')
|
Functional Component that renders the Token in a Game.
(object)
| Name | Description |
|---|---|
props.rowIndex number
|
Row index of the Token . |
props.colIndex number
|
Column index of the Token . |
props.turn number
|
Current turn, one of TURNS . |
props.soundEnabled boolean
|
Whether sound is enabled. |
props.soundLocked boolean
|
Whether sound is locked. |
props.undefined any
(default 'cell-effects-comp')
|
Functional Component that renders the game controls.
Functional Component that renders a Game.
(object)
| Name | Description |
|---|---|
props.board array
|
2 dimensional array of numbers representing the values of Cells in a Board . |
props.player1Direction string
|
Direction of player 1, one of PLAYER_DIRECTIONS . |
props.player2Direction string
|
Direction of player 2, one of PLAYER_DIRECTIONS . |
props.undefined any
(default 'game-comp')
|
Functional Component that renders an overlay when a Game ends.
(object)
| Name | Description |
|---|---|
props.isOver boolean
|
Whether the game is over. |
props.result number
|
The result of the game. |
props.soundEnabled boolean
|
Whether sound is enabled. |
props.soundLocked boolean
|
Whether sound is locked. |
props.reset function
|
Function to reset the Game . |
props.undefined any
(default 'game-end-comp')
|
Functional Component that renders the score of a Player.
Functional Component that renders the Token in a Game.
Container to connect a CellComp.
Container to connect a CellEffectsComp.
(object)
Container to connect a ControlsComp.
(object)
Container to connect a GameComp.
Container to connect a GameEndComp.
Container to connect a ScoreComp.
(object)
| Name | Description |
|---|---|
props.playerName number
|
Flag to identify a Player . See flags.TURNS . |
props.direction number
|
Flag to identify a Player 's direction. See flags.PLAYER_DIRECTIONS . |
Container to connect a WildCardComp.
(object)
Class to save and retrieve the state of the application in local storage. It is based on this code.
Class representing an agent. An agent is a Player that plays the game following a certain criteria. It is considered a type of AI.
Extends Player
Get the position of the highest value in a matrix. The position is either in the column or row of the Token, depending on the agent's direction.
In the following matrix, the token is located at position [3, 3]
| 2 3 4 7 8 |
| 1 -1 3 5 6 |
| 2 7 6 4 1 |
| 5 2 9 0 2 |
| 8 7 9 2 1 |
If the direction of the agent is HORIZONTAL, the algorithm determines the
highest value in the row where the Token is located. Only the values greater
than 0 are considered. The highest value is 9; therefore, the output of the
will be the position of that value: [3, 2].
If the direction of the agent is VERTICAL, the algorithm determines the
highest value in the column where the Token is located. Only the values greater
than 0 are considered. The highest value is 7; therefore, the output of the
will be the position of that value: [0, 3].
If all the values of the row or column are lower than 0, the output will be
[-1, -1].
array:
Array with 2 numbers defining the row and column of the highest value.
Get the position to end the game with victory when possible. The position is either in the column or row of the Token, depending on the agent's direction.
The algorithm relies on the utils.getRowsReadinessToEndGame function.
Get the position of the value with the best gain in a matrix. The position is either in the column or row of the Token, depending on the agent's direction.
The algorithm relies on the utils.getRowsIndicesByOrderedGain function.
(Token)
(array)
(object
= {agent:0,player:0})
Object with the scores of the agent and the player.
array:
Array with 2 numbers defining the row and column of the value
with best gain.
Get the position of the value with the best average difference in a matrix. The position is either in the column or row of the Token, depending on the agent's direction.
In the following matrix, the token is located at position [3, 3]
| 1 2 3 4 5 6 7|
|-1 6 4 7 3 -1 5|
| 1 2 3 -1 5 6 7|
| 1 2 3 0 5 6 7|
|-1 -1 -1 8 -1 -1 -1|
| 4 -1 5 9 9 1 2|
|-1 1 2 3 -1 9 3|
If the direction of the agent is HORIZONTAL, the algorithm evaluates each
row, to find the average value. The evaluation is not performed in the row
of the Token nor in rows which values in the same column of the Token
is less than 0.
In the above matrix, the evaluation is not done in rows 2
and 3.
The evaluation do not consider the values less than 0.
The average of a row is the sum of valid values divided by the number of
valid values. In the first row the average is (1 + 2 + 3 + 5 + 6 + 7) / 6,
in the second row, the average is (6 + 4 + 3 + 5) / 4. In the third
column the average is 0 since there are no valid values.
Once each row is evaluated, the average values are subtracted from the main value (the value in the same column of the token). For the above matrix, there are the following results:
row main average difference
0 4 4 0
1 7 4.5 2.5
2 -1 Not considered, invalid value
3 0 Not considered, token's position
4 8 0 8
5 9 4.2 4.8
6 3 3.75 -0.75
The row with the highest difference is selected. The output is the selected row,
and the column of the Token, in this example [4, 3].
The process is similar if the direction of the agent is VERTICAL.
array:
Array with 2 numbers defining the row and column of
the best average value.
Class representing a board. A board has a matrix of Cells.
(number)
(array?)
2 dimensional array of numbers.
If this parameter is given,
boardSize
is ignored.
Update the value of the Cells based on the current
and old positions of the Token. The value of the Cell in
the current position of the Token is set to 0.
The value of the Cell in the old position of the Token
is set to -1.
If the current and old position of the Token is the same,
the value of the corresponding Cell is set to 0.
(Token)
Class representing a cell. A cell is a positioned element in the Board that contains a value.
Flags used in the logic's game.
Define the status of the game.
RESTING: The token is static.
MOVING_TOKEN: The token is moving.
Identify active player.
PLAYER1: Human player.
PLAYER2: AI player.
Identify players' directions.
NONE: Set at the begining of every match.
VERTICAL
HORIZONTAL
Define continuity of game.
CONTINUE: The game can go on.
OVER: The game has finished. When the board has no more numbers, or the current player is not able to select a number.
Define the result of the game.
WON: The player has won.
LOST: The player has lost.
DRAW: The game ended in a draw.
Class representing a game. A game is made of a Board a Token and 2 Players, one of them is an Agent.
Move the Token to the given position. If the Players' directions have not been set yet, the function defines them.
In the following board, the token is located at position [3, 3]
| 2 3 4 7 8 |
| 1 -1 3 5 6 |
| 2 7 6 4 1 |
| 5 2 9 0 2 |
| 8 7 9 2 1 |
When the directions are not set yet, the Player that makes the first move can move the Token along its row or column.
If the move is along the row, the direction of the Player is set to HORIZONTAL; therefore, the direction of the other Player is set to VERTICAL.
If the move is along the column, the direction of the Player is set to VERTICAL; therefore, the direction of the other Player is set to HORIZONTAL.
Update the last value based on the Cell located in the token's position. The corresponding field in the game is updated.
Update the status of the game. For possible values see flags.GAME_STATUSES.
(number)
Use the Token to update the values of the Cells in the Board. See Board#update.
Update the score of the current Player.
Pass the token to the other Player.
Update the continuity of the game. For possible values see flags.GAME_CONTINUITY.
Get the current Player, the one that has to move the Token.
Get the next Player, the one that does not have to move the Token.
Serialize game. See Token#serialize Board#serialize Player#serialize
object:
Serialized game.
Update game from literal. See Token#updateFromObject Board#updateFromVector Player#updateFromObject
(object)
Class representing a player. A player has a direction of playing and a score.
Class representing a token. A token is the element that moves in the Board to select a Cell.
Utility functions for the logic's game.
Rotate a square matrix clockwisely.
The following matrix:
| 1 2 3 |
| 4 5 6 |
| 7 8 9 |
It is converted to the next one:
| 7 4 1 |
| 8 5 2 |
| 9 6 3 |
(array)
2 dimensional array of number representing a square matrix.
array:
2 dimensional array of number representing a rotated square matrix.
Rotate a square matrix counter-clockwisely.
The following matrix:
| 1 2 3 |
| 4 5 6 |
| 7 8 9 |
It is converted to the next one:
| 3 6 9 |
| 2 5 8 |
| 1 4 7 |
(array)
2 dimensional array of number representing a square matrix.
array:
2 dimensional array of number representing a rotated square matrix.
Rotate row and column indices of an element in a square matrix clockwisely.
In the following 5x5 matrix, take the element located at position [3, 3]
| 2 3 4 7 8 |
| 1 -1 3 5 6 |
| 2 7 6 4 1 |
| 5 2 9 0 2 |
| 8 7 9 2 1 |
The matrix rotated clockwisely will be the next one:
| 8 5 2 1 2 |
| 7 2 7 -1 3 |
| 9 9 6 3 4 |
| 2 0 4 5 7 |
| 1 2 1 6 8 |
Then, the output will be [3, 1]
array:
Array with two numbers defining the rotated row and column indices.
Rotate row and column indices of an element in a square matrix counter-clockwisely.
In the following 5x5 matrix, take the element located at position [3, 3]
| 2 3 4 7 8 |
| 1 -1 3 5 6 |
| 2 7 6 4 1 |
| 5 2 9 0 2 |
| 8 7 9 2 1 |
The matrix rotated clockwisely will be the next one:
| 8 6 1 2 1 |
| 7 5 4 0 2 |
| 4 3 6 9 9 |
| 3 -1 7 2 7 |
| 2 1 2 5 8 |
Then, the output will be [1, 3]
array:
Array with two numbers defining the rotated row and column indices.
Get the readiness to end game of each row in a matrix given a
column. A row is ready to end game when all its elements (except
the one in the column passed as argument) are less than 0.
Also, a row is available when the element in the column passed as
argument is greater than 0.
In the following matrix, the token is located in the position [3, 3]
| -1 -1 -1 7 -1 |
| -1 -1 -1 -1 -1 |
| 2 7 6 -1 1 |
| 5 2 9 0 2 |
| 8 7 9 5 1 |
The output will be the following array:
[
[true, true],
[true, false],
[false, false],
[false, false],
[false, true]
]
Where the first element of each sub-array indicates if the row is ready to end game, and the second element indicates if the row is available.
(array)
2 dimensional array of numbers
representing a square matrix.
(number)
Index of the column where the
token is located.
array:
Array of arrays, each containing two boolean
values indicating readiness and availability of each row.
Get the horizontal gains of a matrix given a column. The gains are calculated by subtracting the values of each column in the matrix from the column which index is passed as argument.
In the following matrix, the token is located in the position [3, 3]
| 2 3 4 7 8 |
| 1 -1 3 5 6 |
| 2 7 6 4 1 |
| 5 2 9 0 2 |
| 8 7 9 2 1 |
The output will be the following matrix:
| 5 4 3 0 -1 |
| 4 0 2 0 -1 |
| 2 -3 -2 0 3 |
| 0 0 0 0 0 |
|-6 -5 -7 0 1 |
Notice how the elements in the row and column of the token are set to 0.
Also, the positions where the value is less than 0 are set to 0 as well
e.g. position [1, 1].
(number)
Index of the column.
(array)
2 dimensional array of numbers representing a square matrix.
array:
2 dimensional array of numbers representing the matrix of gains.
Get the indices of the element in the game matrix which gain is the highest. VERTICAL direction assumed.
Given the following game and gain matrices:
| 2 3 4 7 8 | | 5 4 3 0 -1 |
| 1 -1 3 5 5 | | 4 0 2 0 0 |
| 2 7 6 4 1 | | 2 -3 -2 0 3 |
| 5 2 9 0 2 | | 0 0 0 0 0 |
| 8 7 9 2 1 | |-6 -5 -7 0 1 |
Where the token is at position [3, 3]. The objective is to
select the element in the same column of the token, which value
gives the overall best gain. It assumes that the best move for
the opponent is to select the highest value in the row that may
be selected by the current player:
Possible rows to select:
row 0: possible opponent's selections: 2, 3, 4, x, 8 => best: 8
row 1: possible opponent's selections: 1, x, 3, x, 5 => best: 5
row 2: possible opponent's selections: 2, 7, 6, x, 1 => best: 7
row 3: not possible, the token is here.
row 4: possible opponent's selections: 8, 7, 9, x, 1 => best: 9
For each row, the best gain is the element which difference is the lowest when subtracting the best opponent's selection:
lowest value = (player's selection) - (opponent's best selection)
row: 0, lowest value: -1, column: 4 (7 - 8 = -1)
row: 1, lowest value: 0, column: 4 (5 - 5 = 0)
row: 2, lowest value: -3, column: 1 (4 - 7 = -3)
row: 3, this is the column of the token, it is not considered.
row: 4, lowest value: -7, column: 2 (2 - 9 = -7)
Notice in row 1, the lowest values are at columns 1
and 4.
Generally, the lowest column is selected. But in this case,
the value at [1, 1] in the game matrix is less than 0. Thus,
that element is not considered.
The resulting lowest values are the following:
row 0: -1
row 1: 0
row 2: -3
row 3: Not considered, the token is here.
row 4: -7
The chosen value is the highest one: 0. Therefore, the row of that
value is selected: row 1.
The output will be the selected row and the column of the token:
row 1, column 3.
(number)
(number)
(array)
2 dimensional array of numbers representing a square board matrix.
(array)
2 dimensional array of numbers representing a square gains matrix.
(array)
Array of arrays, each containing two boolean
values indicating readiness and availability of each row to end game.
array:
Array objects containing gain, rowIndex, isRowReadyToEndGame,
isRowAvailable, and availableColumns, sorted by gain descending.
Get the available cells per column in a board matrix, avoiding the row and column of the token.
In the following matrix, the token is located in the position [3, 3]
| 2 3 4 -1 -1 |
| 1 -1 3 5 6 |
| -1 7 -1 4 1 |
| 5 -1 9 0 2 |
| 8 7 9 2 1 |
The output will be the following array:
[
[0, 1, 4], // column 0
[0, 2, 4], // column 1
[0, 1, 4], // column 2
[], // column 3 (token column)
[1, 2, 4] // column 4
]
(number)
Index of the row where the token is located.
(number)
Index of the column where the token is located.
(array)
2 dimensional array of numbers representing a square matrix.
array:
Array of arrays, each containing the row indices of available cells per column.
Game actions to be dispatched.
Dispatch gameActionTypes.SCORES_UPDATED.
Type of game actions to be dispatched.
Update the state of the game based on the received gameActions
(object
= initialState)
Game state of the application.
(object)
Action with relevant data to update the state
of the application.
Object:
New game state after applying the action.
Update settings state. on the received settingsActions
(any
= initialState)
Settings state of the application.
(any)
Action to update the state.
Object:
New settings state after applying the action.
Settings actions to be dispatched.
Dispatch settingsActionTypes.SOUND_TOGGLE.
Dispatch settingsActionTypes.LOCK_SOUND.
Dispatch settingsActionTypes.UNLOCK_SOUND.
Dispatch settingsActionTypes.LOCK_CONTROLS.
Dispatch settingsActionTypes.UNLOCK_CONTROLS.
Type of settings actions to be dispatched.
The sound setting has been toggled. This action is dispatched when the user enables or disables sound.
The sound has been locked. This action is dispatched when the sequence of moves has been completed.
The sound has been unlocked. This action is dispatched when the user makes a move.
The controls have been locked. This action is dispatched when the user start moves. This prevents changing settings during moves.
The controls have been unlocked. This action is dispatched when moves have ended. This allows changing settings while no moves are in progress.