Advertisement
viligen

ticTacToe_JS

May 24th, 2022
1,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function myFunc(coords) {
  2.     let field = [
  3.         [false, false, false],
  4.         [false, false, false],
  5.         [false, false, false],
  6.     ];
  7.  
  8.     let currentPlayer = "X";
  9.     while (coords.length > 0) {
  10.         let [row, col] = coords.shift().split(" ");
  11.         row = Number(row);
  12.         col = Number(col);
  13.  
  14.         if (
  15.             isNaN(row) ||
  16.             isNaN(col) ||
  17.             row < 0 ||
  18.             row >= field.length ||
  19.             col < 0 ||
  20.             col >= field.length
  21.         ) {
  22.             continue;
  23.         }
  24.  
  25.         if (field[row][col]) {
  26.             console.log("This place is already taken. Please choose another!");
  27.             continue;
  28.         }
  29.         field[row][col] = currentPlayer;
  30.  
  31.         if (
  32.             [
  33.                 checkRow(row, field),
  34.                 checkCol(col, field, currentPlayer),
  35.                 checkDiagonals(field),
  36.             ].some((v) => v === true)
  37.         ) {
  38.             console.log(`Player ${currentPlayer} wins!`);
  39.  
  40.             for (let row = 0; row < field.length; row++) {
  41.                 console.log(field[row].join("\t"));
  42.             }
  43.             return;
  44.         }
  45.         if (!field.flat().includes(false)) {
  46.             console.log("The game ended! Nobody wins :(");
  47.             for (let row = 0; row < field.length; row++) {
  48.                 console.log(field[row].join("\t"));
  49.             }
  50.             return;
  51.         }
  52.         currentPlayer = currentPlayer === "X" ? "O" : "X";
  53.     }
  54.  
  55.     function checkRow(row, field) {
  56.         let currentRow = new Set(field[row]);
  57.         return currentRow.size == 1 && !currentRow.has(false);
  58.     }
  59.  
  60.     function checkCol(col, field, currentPlayer) {
  61.         let currentCol = new Set();
  62.         for (let row = 0; row < field.length; row++) {
  63.             if (
  64.                 field[row][col] === false ||
  65.                 field[row][col] !== currentPlayer
  66.             ) {
  67.                 return false;
  68.             }
  69.         }
  70.         return true;
  71.     }
  72.  
  73.     function checkDiagonals(field) {
  74.         let diagonal1 = new Set();
  75.         let diagonal2 = new Set();
  76.         for (let row = 0; row < field.length; row++) {
  77.             diagonal1.add(field[row][row]);
  78.             diagonal2.add(field[row][field.length - 1 - row]);
  79.         }
  80.        
  81.         if (
  82.             (diagonal1.size === 1 && !diagonal1.has(false)) ||
  83.             (diagonal2.size === 1 && !diagonal2.has(false))
  84.         ) {
  85.             return true;
  86.         }
  87.         return false;
  88.     }
  89. }
  90.  
  91. // myFunc(["0 0", "1 1", "0 1", "1 2", "0 2", "2 2", "1 2", "2 2", "2 1"]);
  92.  
  93. // myFunc(["0 0", "0 1", "0 2", "1 1", "2 0", "2 1", "1 2", "2 2", "2 1"]);
  94.  
  95. myFunc(["0 2", "0 1", "1 1", "0 0", "2 0", "2 1", "1 2", "2 2", "2 1"]);
  96.  
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement