mirror of
https://gitea.smigz.com/smiggiddy/odin-codeprojects.git
synced 2025-06-28 12:55:36 -04:00
feat: added game evaluation
This commit is contained in:
parent
75b99514de
commit
8a97cde4fa
1 changed files with 74 additions and 75 deletions
|
@ -89,7 +89,7 @@ const tictactoeBoard = (function() {
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
|
||||||
const playerInfo = (function(){
|
const playerInfo = (function() {
|
||||||
class Player {
|
class Player {
|
||||||
constructor(name, piece) {
|
constructor(name, piece) {
|
||||||
this.piece = piece;
|
this.piece = piece;
|
||||||
|
@ -101,7 +101,7 @@ const playerInfo = (function(){
|
||||||
return new Player(name, piece);
|
return new Player(name, piece);
|
||||||
};
|
};
|
||||||
|
|
||||||
return { player}
|
return { player }
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
|
||||||
|
@ -120,10 +120,6 @@ function block() {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const gameController = (() => {
|
const gameController = (() => {
|
||||||
// Controls the overall flow of the game
|
// Controls the overall flow of the game
|
||||||
let players = [
|
let players = [
|
||||||
|
@ -145,9 +141,12 @@ const gameController = (() => {
|
||||||
if (round < 10 && positionOpen) {
|
if (round < 10 && positionOpen) {
|
||||||
let player = xTurn ? players[0] : players[1];
|
let player = xTurn ? players[0] : players[1];
|
||||||
marker.addToken(player);
|
marker.addToken(player);
|
||||||
tictactoeBoard.placeOnBoard(position, marker ); // position on board
|
tictactoeBoard.placeOnBoard(position, marker); // position on board
|
||||||
tictactoeBoard.printGameBoard();
|
tictactoeBoard.printGameBoard();
|
||||||
switchPlayerTurn();
|
let gameOver = evalGameOutcome(tictactoeBoard);
|
||||||
|
if (!gameOver) {
|
||||||
|
switchPlayerTurn();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -162,20 +161,22 @@ const gameController = (() => {
|
||||||
// create an array of diagnal pieces
|
// create an array of diagnal pieces
|
||||||
let diagArray = [
|
let diagArray = [
|
||||||
[gameBoard[0][0], gameBoard[1][1], gameBoard[2][2]],
|
[gameBoard[0][0], gameBoard[1][1], gameBoard[2][2]],
|
||||||
[gameBoard[0,2], gameBoard[1][1], gameBoard[2][0]]
|
[gameBoard[0, 2], gameBoard[1][1], gameBoard[2][0]]
|
||||||
];
|
];
|
||||||
|
|
||||||
let checkGameBoard = [gameBoard, diagArray, vertArray];
|
let checkGameBoard = [gameBoard, diagArray, vertArray];
|
||||||
for (i=0; i<3; i++) {
|
for (i = 0; i < 3; i++) {
|
||||||
let outcome = _evalGameOutcome(checkGameBoard[i]);
|
let outcome = _evalGameOutcome(checkGameBoard[i]);
|
||||||
if (outcome) return outcome[0];
|
if (outcome) {
|
||||||
|
return true;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
const _evalGameOutcome = arr => {
|
const _evalGameOutcome = arr => {
|
||||||
// Checks if array values are equal to determine winner
|
// Checks if array values are equal to determine winner
|
||||||
let isWinner = checkArr => checkArr.reduce(function(a,b) { return a === b ? a : false; });
|
let isWinner = checkArr => checkArr.reduce(function(a, b) { return a === b ? a : false; });
|
||||||
|
|
||||||
const outcome = arr.find(element => {
|
const outcome = arr.find(element => {
|
||||||
let result = isWinner(element);
|
let result = isWinner(element);
|
||||||
|
@ -196,8 +197,6 @@ gameController.playRound(4);
|
||||||
gameController.playRound(4);
|
gameController.playRound(4);
|
||||||
gameController.playRound(5);
|
gameController.playRound(5);
|
||||||
|
|
||||||
// tictactoeBoard.placeOnBoard(1, block)
|
|
||||||
// tictactoeBoard.printGameBoard();
|
|
||||||
|
|
||||||
|
|
||||||
// const inputController = (function(){
|
// const inputController = (function(){
|
||||||
|
@ -229,58 +228,58 @@ gameController.playRound(5);
|
||||||
|
|
||||||
// tictactoeBoard.playGame();
|
// tictactoeBoard.playGame();
|
||||||
|
|
||||||
// const playGame = async function(){
|
// const playGame = async function(){
|
||||||
// for(let i = 0; i<9; i++) {
|
// for(let i = 0; i<9; i++) {
|
||||||
// const move = await askQuestion();
|
// const move = await askQuestion();
|
||||||
// if (i % 2 === 0) {
|
// if (i % 2 === 0) {
|
||||||
// handleAskQuestion(move, 'x');
|
// handleAskQuestion(move, 'x');
|
||||||
// } else {
|
// } else {
|
||||||
// handleAskQuestion(move, 'o');
|
// handleAskQuestion(move, 'o');
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// if (i > 3) {
|
// if (i > 3) {
|
||||||
// let results = evalGameOutcome(gameBoard);
|
// let results = evalGameOutcome(gameBoard);
|
||||||
// console.log(results + ' game over');
|
// console.log(results + ' game over');
|
||||||
// if (results) return
|
// if (results) return
|
||||||
//
|
//
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// }
|
// }
|
||||||
// rl.close();
|
// rl.close();
|
||||||
// };
|
// };
|
||||||
//
|
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// const evalGameOutcome = gameBoard => {
|
//
|
||||||
// // create a new array of the vertical indexes in the game board
|
// const evalGameOutcome = gameBoard => {
|
||||||
// let vertArray = [];
|
// // create a new array of the vertical indexes in the game board
|
||||||
// gameBoard.forEach((_, index) => vertArray.push(gameBoard.map(e => e[index])));
|
// let vertArray = [];
|
||||||
//
|
// gameBoard.forEach((_, index) => vertArray.push(gameBoard.map(e => e[index])));
|
||||||
// // create an array of diagnal pieces
|
//
|
||||||
// let diagArray = [
|
// // create an array of diagnal pieces
|
||||||
// [gameBoard[0][0], gameBoard[1][1], gameBoard[2][2]],
|
// let diagArray = [
|
||||||
// [gameBoard[0,2], gameBoard[1][1], gameBoard[2][0]]
|
// [gameBoard[0][0], gameBoard[1][1], gameBoard[2][2]],
|
||||||
// ];
|
// [gameBoard[0,2], gameBoard[1][1], gameBoard[2][0]]
|
||||||
//
|
// ];
|
||||||
// let checkGameBoard = [gameBoard, diagArray, vertArray];
|
//
|
||||||
// for (i=0; i<3; i++) {
|
// let checkGameBoard = [gameBoard, diagArray, vertArray];
|
||||||
// let outcome = _evalGameOutcome(checkGameBoard[i]);
|
// for (i=0; i<3; i++) {
|
||||||
// if (outcome) return outcome[0];
|
// let outcome = _evalGameOutcome(checkGameBoard[i]);
|
||||||
// };
|
// if (outcome) return outcome[0];
|
||||||
// return false;
|
// };
|
||||||
// };
|
// return false;
|
||||||
//
|
// };
|
||||||
// const _evalGameOutcome = arr => {
|
//
|
||||||
// // Checks if array values are equal to determine winner
|
// const _evalGameOutcome = arr => {
|
||||||
// let isWinner = checkArr => checkArr.reduce(function(a,b) { return a === b ? a : false; });
|
// // Checks if array values are equal to determine winner
|
||||||
//
|
// let isWinner = checkArr => checkArr.reduce(function(a,b) { return a === b ? a : false; });
|
||||||
// const outcome = arr.find(element => {
|
//
|
||||||
// let result = isWinner(element);
|
// const outcome = arr.find(element => {
|
||||||
// if (result != false) {
|
// let result = isWinner(element);
|
||||||
// return result;
|
// if (result != false) {
|
||||||
// } else {
|
// return result;
|
||||||
// return false;
|
// } else {
|
||||||
// }
|
// return false;
|
||||||
// });
|
// }
|
||||||
// return outcome;
|
// });
|
||||||
// };
|
// return outcome;
|
||||||
|
// };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue