Advertisement
Pripovedac

Vue TicTacToe vol.2

Aug 20th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var board = [
  2.     ['', '', ''],
  3.     ['', '', ''],
  4.     ['', '', '']
  5. ]
  6. var emptyBoard = [
  7.     ['', '', ''],
  8.     ['', '', ''],
  9.     ['', '', '']
  10. ]
  11. var isX = true
  12. var gameOver = false
  13.  
  14. //play() ce biti mejn funkcija:
  15. function play(i, j) {
  16.     if (board[i][j] != '') {
  17.         console.log('Ovo polje je vec zauzeto.')
  18.     }
  19.     else {
  20.        
  21.         if (isX) {
  22.             board[i][j] = 'x'
  23.         }
  24.         else {
  25.             board[i][j] = 'o'
  26.         }
  27.  
  28.         gameOver = checkWinner(board)
  29.         isX = switchTurn(isX)
  30.         if(checkDraw(board)) {
  31.             if(gameOver) {
  32.                 displayWinner(!isX)//ovde ide samo !isX jer
  33.                 //sam gore imao switchTurn(isX)
  34.             }
  35.             else {
  36.             console.log('Neresno. Mozete otpoceti novu igru.')
  37.         }
  38.             resetGame()
  39.         }
  40.         else if(gameOver) {
  41.             displayWinner(!isX)
  42.             resetGame()
  43.         }
  44.         console.log(board)
  45.     }
  46. }
  47.  
  48. function displayWinner(isX) {
  49.     isX ? console.log('Pobednik je takmicar X.') : console.log('Pobednik je takmicar O.')  
  50. }
  51. function switchTurn(isX) {
  52.     reversedX = !isX
  53.     return reversedX
  54. }
  55. function checkWinner(board) {
  56.     const results = [
  57.         checkRow(board, 0),
  58.         checkRow(board, 1),
  59.         checkRow(board, 2),
  60.         checkColumn(board, 0),
  61.         checkColumn(board, 1),
  62.         checkColumn(board, 2),
  63.         checkMainDiagonal(board),
  64.         checkOtherDiagonal(board)
  65.     ]
  66.     return results.some(result => result == true)
  67. }
  68. function checkRow(board, i) {
  69.     gameOver = true
  70.     return board[i][0] != '' && board[i][0] == board[i][1] && board[i][1] == board[i][2]
  71. }
  72. function checkColumn(board, i) {
  73.     return board[0][i] != '' && board[0][i] == board[1][i] && board[1][i] == board[2][i]
  74. }
  75. function checkMainDiagonal(board) {
  76.     gameOver = true
  77.     return board[0][0] != '' && board[0][0] == board[1][1] && board[1][1] == board[2][2]
  78. }
  79. function checkOtherDiagonal(board) {
  80.     gameOver = true
  81.     return board[0][2] != '' && board[0][2] == board[1][1] && board[1][1] == board[2][0]
  82. }
  83. //ova nije cista
  84. function resetGame() {  
  85.   this.board = emptyBoard
  86.   this.gameOver = false
  87.   this.isX = true
  88. }
  89.  
  90. function checkDraw(board) {
  91.       var freeCells = numOfFreeCells(board)
  92.       return freeCells == 0
  93.     }
  94.  
  95. function numOfFreeCells(board) {
  96.     var numOfFreeCells = 0
  97.     board.forEach((row, i) => {
  98.         row.forEach((cell, j) => {
  99.           if (cell == '') {
  100.             numOfFreeCells++
  101.         }
  102.         })
  103.       })
  104.       return numOfFreeCells
  105. }
  106. // function transposeMatrix(matrix) {
  107. //   var result = new Array(matrix[0].length)
  108. //   for(var i = 0; i < result.length; i++) {
  109. //       result[i] = new Array(matrix[0].length)
  110. //       for(var j = 0; j < result[i].length; j++) {
  111. //           result[i][j] = matrix[j][i]
  112. //       }
  113. //   }
  114. //   return result
  115. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement