Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. (async function (){
  2.  
  3. const checkFuncs = [
  4. checkByX,
  5. checkByY,
  6. checkByDiagonal,
  7. checkByDiagonal2
  8. ];
  9.  
  10. const board = {
  11. 11: 1, 12: 1, 13: 0,
  12. 21: 0, 22: 1, 23: 1,
  13. 31: 1, 32: 0, 33: 1
  14. }
  15.  
  16. let step = 11;
  17. let maxRows = 3;
  18. let maxCols = 3;
  19. let possibleIterations = maxCols;
  20. // let maxNum = parseInt(`${maxRows}${maxCols}`); maybe if I check the input this variable will be used.
  21.  
  22. console.log('tic tac toe');
  23. console.log('your move:', process.argv[2]);
  24.  
  25. let input = parseInt(process.argv[2]);
  26.  
  27. let line = [];
  28. let hasWinner = false;
  29.  
  30. for(let i = 0; i < checkFuncs.length; i++) {
  31. let hasLine = checkFuncs[i](input);
  32. if (hasLine) {
  33. printLine(hasLine);
  34. hasWinner = true;
  35. break;
  36. }
  37. }
  38.  
  39. if(!hasWinner) {
  40. console.log('There is no line!');
  41. }
  42.  
  43.  
  44. function printLine(hasLine) {
  45. console.log(line);
  46. }
  47.  
  48. function checkByX(input) {
  49.  
  50. let hasLine = true;
  51. let row = parseInt(input / 10);
  52.  
  53. for(let col = 1; col < 4; col++) {
  54.  
  55. let position = `${row}${col}`;
  56. let cell = parseInt(position);
  57. if (board[cell] === 1) {
  58. line.push(position);
  59. } else {
  60. line = [];
  61. hasLine = false;
  62. break;
  63. }
  64. }
  65.  
  66. return hasLine;
  67. }
  68.  
  69. function checkByY(input) {
  70. let hasLine = true;
  71. let col = parseInt(input % 10);
  72.  
  73. for(let row = 1; row < 4; row++) {
  74.  
  75. let position = `${row}${col}`;
  76. let cell = parseInt(position);
  77. if (board[cell] === 1) {
  78. line.push(position);
  79. } else {
  80. line = [];
  81. hasLine = false;
  82. break;
  83. }
  84. }
  85.  
  86. return hasLine;
  87. }
  88.  
  89. // up left to down right
  90. function checkByDiagonal(input) {
  91. let hasLine = true;
  92. input = parseInt(input);
  93.  
  94. let position = `${input} `;
  95.  
  96. // up left to down right
  97.  
  98. let row = parseInt(input / 10);
  99.  
  100. while(row > 1) {
  101. input -= step;
  102. row = parseInt(input / 10);
  103. }
  104.  
  105. for(let i = 0; i < possibleIterations; i++){
  106.  
  107. position = `${input} `;
  108.  
  109. if (!board[input] || board[input] === 0) {
  110. line = [];
  111. hasLine = false;
  112. break;
  113. } else {
  114. line.push(position);
  115. }
  116.  
  117. input += step;
  118. }
  119.  
  120. return hasLine;
  121. }
  122.  
  123. // down right to up left
  124. function checkByDiagonal2(input) {
  125. let hasLine = true;
  126. input = parseInt(input);
  127.  
  128. let position = `${input} `;
  129.  
  130. let row = parseInt(input / 10);
  131.  
  132. while(row != 3) {
  133. input += step;
  134. row = parseInt(input / 10);
  135. }
  136.  
  137. for(let i = 0; i < possibleIterations; i++){
  138.  
  139. position = `${input} `;
  140.  
  141. if (!board[input] || board[input] === 0) {
  142. line = [];
  143. hasLine = false;
  144. break;
  145. } else {
  146. line.push(position);
  147. }
  148.  
  149. input -= step;
  150. }
  151.  
  152. return hasLine;
  153. }
  154.  
  155. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement