Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (async function (){
- const checkFuncs = [
- checkByX,
- checkByY,
- checkByDiagonal,
- checkByDiagonal2
- ];
- const board = {
- 11: 1, 12: 1, 13: 0,
- 21: 0, 22: 1, 23: 1,
- 31: 1, 32: 0, 33: 1
- }
- let step = 11;
- let maxRows = 3;
- let maxCols = 3;
- let possibleIterations = maxCols;
- // let maxNum = parseInt(`${maxRows}${maxCols}`); maybe if I check the input this variable will be used.
- console.log('tic tac toe');
- console.log('your move:', process.argv[2]);
- let input = parseInt(process.argv[2]);
- let line = [];
- let hasWinner = false;
- for(let i = 0; i < checkFuncs.length; i++) {
- let hasLine = checkFuncs[i](input);
- if (hasLine) {
- printLine(hasLine);
- hasWinner = true;
- break;
- }
- }
- if(!hasWinner) {
- console.log('There is no line!');
- }
- function printLine(hasLine) {
- console.log(line);
- }
- function checkByX(input) {
- let hasLine = true;
- let row = parseInt(input / 10);
- for(let col = 1; col < 4; col++) {
- let position = `${row}${col}`;
- let cell = parseInt(position);
- if (board[cell] === 1) {
- line.push(position);
- } else {
- line = [];
- hasLine = false;
- break;
- }
- }
- return hasLine;
- }
- function checkByY(input) {
- let hasLine = true;
- let col = parseInt(input % 10);
- for(let row = 1; row < 4; row++) {
- let position = `${row}${col}`;
- let cell = parseInt(position);
- if (board[cell] === 1) {
- line.push(position);
- } else {
- line = [];
- hasLine = false;
- break;
- }
- }
- return hasLine;
- }
- // up left to down right
- function checkByDiagonal(input) {
- let hasLine = true;
- input = parseInt(input);
- let position = `${input} `;
- // up left to down right
- let row = parseInt(input / 10);
- while(row > 1) {
- input -= step;
- row = parseInt(input / 10);
- }
- for(let i = 0; i < possibleIterations; i++){
- position = `${input} `;
- if (!board[input] || board[input] === 0) {
- line = [];
- hasLine = false;
- break;
- } else {
- line.push(position);
- }
- input += step;
- }
- return hasLine;
- }
- // down right to up left
- function checkByDiagonal2(input) {
- let hasLine = true;
- input = parseInt(input);
- let position = `${input} `;
- let row = parseInt(input / 10);
- while(row != 3) {
- input += step;
- row = parseInt(input / 10);
- }
- for(let i = 0; i < possibleIterations; i++){
- position = `${input} `;
- if (!board[input] || board[input] === 0) {
- line = [];
- hasLine = false;
- break;
- } else {
- line.push(position);
- }
- input -= step;
- }
- return hasLine;
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement