iPoetDev / MiniTicTacToe

MiniTicTacToe - NativeJS: UI - Client Controller - OOP Model/'Server'
https://ipoetdev.github.io/MiniTicTacToe/
BSD 2-Clause "Simplified" License
0 stars 1 forks source link

[CODE] App Class API: GameLogic() #9

Open iPoetDev opened 8 months ago

iPoetDev commented 8 months ago

CODE:

Outline | Architecture | Modality: OOO JavaScript: Class Version: 2.2 Components:

  1. HTML: Script tag
  2. JS: Internal Script : verses
  3. JS: External User Custom Code File Name
    2: game.html or game.js

Related


Previous

See #8 for a prior Class definition

Design: GameLogic() class.

The class GameLogic here is responsible for maintaining the state and rules of a game.

Decisions

Changelog

Class API

1. Constructor

The constructor function gets called whenever a new instance of the class GameLogic is created. It initializes the game's state, such as the number of turns played, the winning status, the win sequences, the game grid, etc.

2. Class | Instance Members

Class Member Access/Use Get/Set Type Default Class Constant Value Use Case
this.DEBUG Private, Internal ✅/✅ {boolean} false n/a {false, true} Enabled/disables class debug mode
this.LEVEL Private, Internal ✅/✅ {number} 0 n/a {0,1,2,3,4,5,6} Toggles level/type of console logging to DevTools
this._EMPTYCELL Private, Internal ✅/✅ {null} null GameLogic.CELL_RESET null Default, named placeholder for null, an empty cell
this._turns Private/As-Is Data ✅/❌ {number} 0 GameLogic.TURN_INIT n<=8 The sequences of grid indexes that are winning combinations of moves for a 3x3 UI grid
this._currentcell Private/As-Is Data ✅/✅ {string} , empty string GameLogic.CELL_RESET `,X,O` Value of the current grid's cell, by reference
this._draw Private/End State ✅/✅ {boolean} false GameLogic.NO_DRAW {false, true} Draw is a valid end state/game result
this._won Private/End State ✅/✅ {boolean, string} false GameLogic.IN_PLAY {false,X,O} Win is valid end state/game result
this._winSeq Private/Check Conditional ✅/❌ {array} Array(8)<string> GameLogic.WIN_COMBINATIONS GameLogic.WIN_COMBINATIONS Set of Winning Combinations of 3 length string sequences, i.e. combination of winning moves/sequences
this._grid Private/UI Control ✅/✅ {array} Array(9)<null|string> GameLogic.NEW_GRID null,X,O Data structure that generates the game board/grid and contains the game moves as string tokens
this._xChars Private/UI Tokens - {array} Array(2)<string> n/a [x,X] Character by lower\upper-case, variability in UI design for X Token
this._oChars Private/UI Tokens - {array} Array(2)<string> n/a [o,O] Character by lower\upper-case, variability in UI design for O Token
this._xTurns Private/Internal ✅/❌ {string} ._xTurn GameLogic.X_TURN_PROP .xTurn Property to select each X turn and update the grid by
this._oTurns Private/Internal ✅/❌ {string} ._oTurn GameLogic.O_TURN_PROP .oTurn Property to select each O turn and update the grid by

3. Class Properties (Getters/Setters)

Extensive Use Cases

Property Use Case Class Member Value/Type Getter Setter Access Mutable Intent
this.DEVMODE Instance Property,
Default Value
this.DEBUG - Public Gets/Sets the class debug state, affects the default value for per function optional param: debug
this.LOGLEVEL Instance Property,
Default Value
this.LEVEL - Public Gets/Sets the class VERBOSITY level for console, affects the default value for per function optional param: level
:--- :--- :--- :--- :--- :--- :--- :--- :---
GameLogic.P1 Encapsulated Class Constant,
Default Value
-- {string} Public 🛑 Gets the immutable Player 1 token: X
GameLogic.P2 Encapsulated Class Constant,
Default Value
-- {string} Public 🛑 Gets the immutable Player 2 token: O
GameLogic.P1_TOKENS Encapsulated Class Constant this._xChars {array} Private 🛑 Gets array of Player 1 token characters: X or x: A design choice for variability in token font/display based on font choice
GameLogic.P2_TOKENS Encapsulated Class Constant this._oChars {array} Private 🛑 Gets array of Player 2 token: O or o
GameLogic.REG_SEARCH_LENGTH Encapsulated Class Constant,
Default Value
-- {number} Private 🛑 Gets regex length of win sequence to search : 3
GameLogic.REG_SEARCH_ALL Encapsulated Class Constant,
Default Value
-- {string} Private 🛑 Gets global regex flag to search for all instances, not just first found: g
GameLogic.REG_SEARCH_FILTER Encapsulated Class Constant,
Default Value
-- {string} Private 🛑 Gets string to replace & filter out of the regex search on test: ``
GameLogic.WIN_COMBINATIONS Encapsulated Class Constant,
Default Value
-- {array(8)<strings(length:3)>} Private Gets the possible (8) winning combinations/permutations as array of (8) strings
GameLogic.AS_DRAW Encapsulated Class Constant,
Default Value
-- {string} Public Gets game's (end) state for a draw (has a result/end state), which is a truthy logic: true
GameLogic.IN_PLAY Encapsulated Class Constant,
Default Value
-- {string} Public Gets game's state for in play (no result/end state), which is a falsey logic: false
GameLoic.CELL_RESET Encapsulated Class Constant,
Default Value
-- {null} Private Gets default cell value for grid array initialisation/reset: null
GameLogic.MAX_LENGTH Encapsulated Class Constant,
Default Value
-- {null} Private Gets the default grid array's length/size: 9
GameLogic.TURN_RESET Encapsulated Class Constant,
Default Value
-- {string} Private Gets default turn value for initialisation/reset: ' '
GameLogic.TURN_INIT Encapsulated Class Constant,
Default Value
-- {string} Private Gets default turn init value for the number of turns per game, as this increments: ' '
GameLogic.X_TURNS_PROP Encapsulated Class Constant,
Default Value
-- {string} Private Gets default turn property for X 's turn tracking: '._xTurns'
GameLogic.O_TURNS_PROP Encapsulated Class Constant,
Default Value
-- {string} Private Gets default turn property for O's turn tracking : '._oTurns'
:--- :--- :--- :--- :--- :--- :--- :--- :---
this.WIN_SEQUENCE Instance Property,
As-Is Value
this._winSeq {string} Private Gets the current, static, Win Sequence array 's value
this.GRID Instance Property,
As-Is Value
this._grid {Array} Public Gets/sets the current grid's array
this.NEW_GRID Instance Property,
As-Is Value
this._grid new {Array} Private Gets/sets (initialises) the new grid's array of 9 length and null values: see MAX_LENGTH, CELL_RESET
this.IFWON Instance Property,
As-Is Value
this._won {boolean|string} Public Gets/sets the game's current/end win state, and winner. Initialsed as: false, per turn is false for next round, or ends as: X v O
this.IFDRAWN Instance Property,
As-Is Value
this._draw {boolean} Public Gets/sets the game's current/end drawn state. Initialised as: false, end state: true
this.TURNS Instance Property,
As-Is Value
this._turns {string} Public Gets the current, static, Win Sequence array's value
this.MAX_TURNS Instance Property,
As-Is Value
--- {number} Private Computes the max number of turns from grid length: MAX_LENGTH - 1
this.CELL Instance Property,
As-Is Value
--- {string|null} Private Gets/sets the current cell reference to grid value: X or O
:--- :--- :--- :--- :--- :--- :--- :--- :---
this.currentCell(index) Accessor/helper API function,
Computed Value
--- {string} Public Sets/returns the class member for current cell by retrieves the current cell value from the grid at index: *
:--- :--- :--- :--- :--- :--- :--- :--- :---

4. Class General Helpers/Debug

Function Use Case Parameters Defaults Return Access Intent
SELECT() API Helpers --- --- --- --- --- ---
this._console Logging & debugging message, debug, level, ...args debug, level {void} Public/Protected Log function calls for class level debugging/inspection

5. select() and Helper Functions

Function Callable by Key Parameters Defaults Inner Closures Return Access Intent
SELECT() API Helpers --- --- --- --- --- --- ---
_getRandomCharacter _updateTurnsAndGrid characterArray debug, level _getRandomIndex array(index Private Returns a random character from the given character array.
_updateTurnsAndGrid _isEvenTurn index, characterArray, turnProperty debug, level n/a {void} Private Update the turns and Cell (grid at the specified index)
_isInvalidMove select index debug, level n/a {boolean} Private Checks if the given move is invalid
_isEvenTurn select index debug, level _isTurnEven
_chooseChar
_selectProp
{void} Private Determines if the turn is even or odd and calls to update the turns and grid accordingly
_incrementTurn select n/a n/a n/a {void} Private Increment Turn property (a counter) by 1
select UI index debug, level _whoWins,
_whoDraws,
_hasValidMove,
Object <anon><br> cell, next \|\| endstate, message, state, valid, outcome Public Selects a cell, by index, on the game board, and updates the game state, checks the end game state, returns a game state object to pass to other APIs/UI calls.

6.1 checkWinner and Helper Functions

Function Callable by Key Parameters Defaults Inner Closures Return Access Intent
checkWinner() API Helpers --- --- --- --- --- --- ---
_checkSequenceWin checkWinner turns, sequence debug, level n/a {boolean} Private Checks whether the sequence of turns is a winner.
checkWinner n/a n/a debug, level n/a {boolean|string} Public Checks if the sequence of turns, assigns a winner, AND end state flag: False, Draw (True), P1, P2.

6.2 checkWinningPlay and Helper Functions

Function Callable by Key Parameters Defaults Inner Closures Return Access Intent
checkWinninPlay() API Helpers --- --- --- --- --- --- ---
_checkSequenceWin checkWinner turns, sequence debug, level n/a {boolean} Private Checks whether the sequence of turns is a winner.
checkWinninPlay n/a n/a debug, level _assignWinner
_defaultRound
...
{boolean|string} Public Checks if the sequence of turns, assigns a winner or a draw, AND end state flag: False, Draw (True), P1, P2.

7. reset

Function Callable by Key Parameters Defaults Inner Closures Return Access Intent
reset UI n/a default, index n/a {void} Public Reset the game state to its initial values using Class Constants (properties).
iPoetDev commented 8 months ago

GameLogics: ToDo:

Sprint: 03: Ends 23-01-07 Bump: Previous: 23-01-06 v00.00.03.003 Changelog: 23-01-07 v00.00.03.004

iPoetDev commented 8 months ago

GameLogics: ToDo:

Sprint: 03: Ends 23-01-07 Bump: Previous: 23-01-06 v00.00.03.003 Changelog: 23-01-07 v00.00.03.004

feat/fix (App): Update/Refactor GameLogic from App class.

Intent: Update the UI dynamically when underlying array updates. Issue: Fixed: ToDo:

iPoetDev commented 8 months ago

GameLogics: ToDo:

Sprint: 03: Ends 23-01-07 Bump: Previous: 23-01-06 v00.00.03.003 Changelog: 23-01-07 v00.00.03.004

feat/fix (App): Update/Refactor GameLogic from App class. Intent: Alpha class design, JSDoc, code layout and Webstorm inspections Issue: Fixed: Files: game.html, game.js (same class def)

ToDo:

Changelog: 23-01-08 v00.00.04.001

Branch: fix/game-app-001

iPoetDev commented 8 months ago