Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var box = document.getElementsByTagName('td');
  2. var startBoard = document.getElementById('newGame');
  3. const playerX = 'X';
  4. const playerO = 'O';
  5. var player = playerX;
  6. var winner;
  7. var game = 'newGame';
  8. var counter = 0;
  9.  
  10.    console.log(startBoard);
  11.  
  12. startBoard.addEventListener('click', function() {
  13.     game = 'newGame';
  14.     emptyBoard();
  15. }, false);
  16.  
  17. for(var i = 0; i < box.length; i++) {
  18.     //box[i].addEventListener('click', turnClick(), false);
  19.     box[i].addEventListener('click', function() {
  20.              //  console.log(box[i].innerText);
  21.   //  if (box[i].innerText == '\xa0') {
  22.                   //box[i].addEventListener('click', function() {  
  23.        if(box[i] != "\xa0") {
  24.            this.innerHTML = player;
  25.             counter ++;
  26.             changePlayer();
  27.             console.log(counter);
  28.         }
  29.     }, false);
  30. }
  31.  
  32. function turnClick(){
  33.    if(game == 'newGame') {
  34.         if (this.innerText == '\xa0') {  
  35.             this.innerHTML = player;
  36.             counter ++;
  37.             changePlayer();
  38.             checkWinner();
  39.             console.log(counter);
  40.        }
  41.    }
  42. }
  43.  
  44. function emptyBoard() {
  45.     player = playerX;
  46.     counter = 0;
  47.     winner = '';
  48.     for(var i = 0; i < box.length; i++) {
  49.         box[i] = '\xa0';
  50.     }
  51. }
  52.  
  53. function changePlayer (){
  54.     //console.log(box.target.id);
  55. if (counter % 2 == 0 || counter == 0) {
  56.     player = playerX;
  57. }
  58. else {
  59.     player = playerO;
  60. }}
  61. /*startBoard[boxID] = player;
  62. document.getElementById(boxID).innerText = player;
  63. let gameWin = checkWinner(startBoard, player)
  64. if (gameWin) gameOver(gameWon)
  65. }
  66. */
  67. function checkWinner(startBoard, player){
  68.     if((topLeft === 'X' && topCenter === 'X' && topRight === 'X') ||
  69.     (centerLeft === 'X' && centerCenter === 'X' && centerRight === 'X') ||
  70.     (bottomLeft === 'X' && bottomCenter === 'X' && bottomRight === 'X') ||
  71.     (topLeft === 'X' && centerLeft === 'X' && bottomLeft === 'X') ||
  72.     (topCenter === 'X' && centerCenter === 'X' && bottomCenter === 'X') ||
  73.     (topRight === 'X' && centerRight === 'X' && bottomRight === 'X') ||
  74.     (topLeft === 'X' && centerCenter === 'X' && bottomRight === 'X') ||
  75.     (topRight === 'X' && centerCenter === 'X' && bottomLeft === 'X')) {
  76.         winner = playerX;
  77.         "X has won";
  78. }
  79. else {
  80. if((topLeft === 'O' && topCenter === 'O' && topRight === 'O') ||
  81. (centerLeft === 'O' && centerCenter === 'O' && centerRight === 'O') ||
  82. (bottomLeft === 'O' && bottomCenter === 'O' && bottomRight === 'O') ||
  83. (topLeft === 'O' && centerLeft === 'O' && bottomLeft === 'O') ||
  84. (topCenter === 'O' && centerCenter === 'O' && bottomCenter === 'O') ||
  85. (topRight === 'O' && centerRight === 'O' && bottomRight === 'O') ||
  86. (topLeft === 'O' && centerCenter === 'O' && bottomRight === 'O') ||
  87. (topRight === 'O' && centerCenter === 'O' && bottomLeft === 'O')) {
  88.     winner = playerO;
  89. }}
  90. return winner;
  91. }
  92.  
  93. function tieGame() {
  94.         for (var i = 0; i < 9; i++){
  95.             if(box[i] !='\xa0'){
  96.             box[i].removeEventListener('click', turnClick, false);
  97.         }
  98.     }
  99.  
  100. }
  101.  
  102. function newGame() {
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement