Crear un juego Tic-Tac-Toe con TypeScript, React y Mocha

Le presentamos una traducci贸n de un art铆culo de Josh Kuttler publicado en blog.bitsrc.io. Aprenda a crear una aplicaci贸n Tic-Tac-Toe con React y TypeScript.



Un simple juego de tres en raya creado de forma modular y cargado en el sitio web de Bit . Puedes cambiar los componentes de mi juego y probarlo en l铆nea en Bit PlayGround usando NPM, Yarn o Bit. Para hacer esto, vaya a mi colecci贸n de componentes .

Cuando creas juegos Tic-Tac-Toe de forma modular, es dif铆cil encontrar una raz贸n por la cual los componentes de la interfaz de usuario puedan volver a usarse. Por lo tanto, me concentr茅 principalmente en las utilidades del juego.

Para la programaci贸n, eleg铆 TypeScript: compil茅 el c贸digo usando TypeScript en el sitio web de Bit . Luego us贸 el marco Mocha para las pruebas.

Para instalar componentes de mi proyecto, primero configure bit.dev como el registro de dominio (copie y pegue en su dispositivo). 隆Esto debe hacerse solo una vez! Si contin煤a utilizando el sitio web de Bit, no necesitar谩 volver a configurarlo.

npm config set '@bit:registry' https://node.bit.dev 

Luego instale el componente utilizando los gestores de paquetes Yarn o NPM:

 npm i @bit/joshk.tic-tac-toe-game.game yarn add @bit/joshk.tic-tac-toe-game.game 

Componente del juego


El componente del juego es el componente principal de mi aplicaci贸n: se cre贸 usando un componente de la placa y dos componentes Prime React .

Utilic茅 el bot贸n y los componentes de entrada de texto para la pantalla de configuraci贸n; puedes probar y ver su c贸digo aqu铆 .



Instale componentes PrimeReact en su proyecto:

 yarn add @bit/primefaces.primereact.inputtext yarn add @bit/primefaces.primereact.button 

Despu茅s de configurar los par谩metros, puede hacer clic en "Reproducir" y ... 隆jugar!

Componente de placa


El componente de tablero crea una tabla din谩mica usando Props, establece la cola para los jugadores y determina el ganador. Puedes probar y ver el c贸digo aqu铆 .



Componente cuadrado


El componente Cuadrado es una celda normal que recibe un valor con un color opcional y env铆a un evento al componente Tablero cuando el valor cambia. Puedes probar y ver el c贸digo aqu铆 .



Funci贸n de celda vac铆a


La funci贸n de celda vac铆a es una funci贸n auxiliar para la funci贸n Winner-calc, que verifica si hay celdas vac铆as en la mesa de juego.

Bit le permite ver documentos componentes y resultados de pruebas:



C贸digo de funci贸n
 /** * @description * check if 2d array have an empty cell * @param {{Array.<string[]>}} matrix 2d array * @param {number} rowsNum number of rows * @param {number} colsNum number of columns * @returns {boolean} return true if empty cell was found, and false if not. * @example * import haveEmptyCell from '@bit/joshk.tic-tac-toe-game.utils.have-empty-cell'; * * const matrix = [ * ['X', 'O', 'X'], * ['O', 'X', 'O'], * ['O', 'X', 'O'] * ]; * const result = haveEmptyCell(matrix, 3, 3); * * export default result * @example * import haveEmptyCell from '@bit/joshk.tic-tac-toe-game.utils.have-empty-cell'; * * const matrix = [ * ['X', 'O', 'X'], * ['O', '', 'O'], * ['O', 'X', 'O'] * ]; * const result = haveEmptyCell(matrix, 3, 3); * * export default result * @example * import haveEmptyCell from '@bit/joshk.tic-tac-toe-game.utils.have-empty-cell'; * * const matrix = [ * ['X', 'O', 'X'], * ['O', , 'O'], * ['O', 'X', 'O'] * ]; * const result = haveEmptyCell(matrix, 3, 3); * * export default result * @example * import haveEmptyCell from '@bit/joshk.tic-tac-toe-game.utils.have-empty-cell'; * * const matrix = [ * ['X', 'O', 'X'], * ['O', null, 'O'], * ['O', 'X', 'O'] * ]; * const result = haveEmptyCell(matrix, 3, 3); * * export default result */ function haveEmptyCell(matrix: Array<Array<string>>, rowsNum: number, colsNum: number): boolean { let empty: boolean = false; for (let x = 0; x < rowsNum; x++) { for (let y = 0; y < colsNum; y++) { const element: any = matrix[x][y]; if (!element) { empty = true; break; } } if (empty) break; } return empty; } export default haveEmptyCell 


Funci贸n de c谩lculo ganador


El c谩lculo del ganador es una funci贸n que calcula al ganador en planos horizontales, verticales y diagonales.

Bit le permite ver documentos componentes y resultados de pruebas:



C贸digo de funci贸n
 /** * @description * check winner horizontal, vertical and diagonal * @param {Array.<string[]>} matrix 2d array with X and O * @param {number} rowsNum number of rows * @param {number} colsNum number of columns * @param {number} numToWin the number of matching to win * @param {number} lastRow the row number of the square player click * @param {number} lastCol the column number of the square player click * @returns {string} return the winner, X or O or '' if no one win. * @example * import winnerCalc from '@bit/joshk.tic-tac-toe-game.utils.winner-calc'; * * const matrix = [ * ['O', 'O', 'X'], * ['O', 'X', ''], * ['X', '', ''] * ]; * const result = winnerCalc(matrix, 3, 3, 3, 0, 2); * * export default result */ import haveEmptyCell from '../HaveEmptyCell' function winnerCalc(matrix: Array<Array<string>>, rowsNum: number, colsNum: number, numToWin: number, lastRow: number, lastCol: number): string { let winner: string = ''; let match: number = 0; const lastValue: string = matrix[lastRow][lastCol]; //check Horizontal for (let c = 0; c < colsNum; c++) { let currentValue = matrix[lastRow][c]; if (currentValue === lastValue) match++; else match = 0; if (match === numToWin) { winner = lastValue; break; } } if (winner !== '') return winner; match = 0; //check Vertical for (let r = 0; r < rowsNum; r++) { let currentValue = matrix[r][lastCol]; if (currentValue === lastValue) match++; else match = 0; if (match === numToWin) { winner = lastValue; break; } } if (winner !== '') return winner; //check diagonal top-left to bottom-right - include middle match = 0; for (let r = 0; r <= rowsNum - numToWin; r++) { let rowPosition = r; for (let column = 0; column < colsNum && rowPosition < rowsNum; column++) { const currentValue = matrix[rowPosition][column]; if (currentValue === lastValue) match++; else match = 0; if (match === numToWin) { winner = lastValue; break; } rowPosition++; } if (winner !== '') break; } if (winner !== '') return winner; //check diagonal top-left to bottom-right - after middle match = 0; for (let c = 1; c <= colsNum - numToWin; c++) { let columnPosition = c; for (let row = 0; row < rowsNum && columnPosition < colsNum; row++) { let currentValue = matrix[row][columnPosition]; if (currentValue === lastValue) match++; else match = 0; if (match === numToWin) { winner = lastValue; break; } columnPosition++; } if (winner !== '') break; } if (winner !== '') return winner; //check diagonal bottom-left to top-right - include middle match = 0; for (let r = rowsNum - 1; r >= rowsNum - numToWin - 1; r--) { let rowPosition = r; for (let column = 0; column < colsNum && rowPosition < rowsNum && rowPosition >= 0; column++) { let currentValue = matrix[rowPosition][column]; if (currentValue === lastValue) match++; else match = 0; if (match === numToWin) { winner = lastValue; break; } rowPosition--; } if (winner !== '') break; } if (winner !== '') return winner; //check diagonal bottom-left to top-right - after middle match = 0; for (let c = 1; c < colsNum; c++) { let columnPosition = c; for (let row = rowsNum - 1; row < rowsNum && row >= 0 && columnPosition < colsNum && columnPosition >= 1; row--) { console.log(`[${row}][${columnPosition}]`); let currentValue = matrix[row][columnPosition]; if (currentValue === lastValue) match++; else match = 0; if (match === numToWin) { winner = lastValue; break; } columnPosition++; } if (winner !== '') break; } if (winner !== '') return winner; if(haveEmptyCell(matrix, rowsNum, colsNum) === false) { winner = '-1'; } return winner; } export default winnerCalc 


El proyecto est谩 disponible en mi colecci贸n en Bit y en mi repositorio de GitHub .

Si茅ntase libre de comentar este art铆culo y suscribirse a mi Twitter .

Source: https://habr.com/ru/post/456000/


All Articles