2

Good Afternoon,

can someone please help me figure out why my Tic Tac Toe javascript game wont play intelligently...meaning that it shouldn't let me win. I've spent hours on this and still cant figure it out. Here is the link to my project: http://codepen.io/tbraden30/pen/WrorjE Thank you so much!

"use-strict"
$(document).ready(function() {
  var gameBoard = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
    humanPlayer = 'X',
    EMPTY = ' ',
    computerPlayer = 'O',
    currentPlayer = 'computer',
    choice;

  function buildBoard(board) {
    $('td').each(function(index) {
      $(this).text(board[index]);
    });
  }

  function makeMove(board, symbol, move) {
    board[move] = symbol;
  }

  function checkWin(board, player) {
    return ((board[0] == player && board[1] == player && board[2] == player) || (board[3] == player && board[4] == player && board[5] == player) || (board[6] == player && board[7] == player && board[8] == player) || (board[0] == player && board[3] == player && board[6] == player) || (board[1] == player && board[4] == player && board[7] == player) || (board[0] == player && board[4] == player && board[8] == player) || (board[2] == player && board[4] == player && board[6] == player) || (board[2] == player && board[5] == player && board[8] == player));
  }

  function boardSpaceOpen(board, move) {
    return board[move] == EMPTY;
  }

  function boardIsFull(board) {
    for (var i = 0; i < board.length; i++) {
      if (boardSpaceOpen(board, i)) {
        return false;
      }
    }
    return true;
  }

  function resetBoard() {
    gameBoard = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '];
    buildBoard(gameBoard);
    currentPlayer = 'computer';
    computerPlay();
  }



  function computerPlay() {
    var moves = getOpenSpaces(gameBoard);
    var move = moves[0];
    var score = -10000;
    for (var i = 0; i < moves.length; i++) {
      var newBoard = gameBoard.slice();
      newBoard[moves[i]] = computerPlayer;

      if (checkWin(computerPlayer, newBoard)) {
        move = moves[i];
        break;
      }
      var newScore = minimax(humanPlayer, newBoard);
      if (newScore > score) {
        score = newScore;
        choice = moves[i];
      }
    }
    makeMove(gameBoard, computerPlayer, move);
    buildBoard(gameBoard);

    if (checkWin(gameBoard, computerPlayer)) {
      alert('computer Wins!!');
      resetBoard();
    } else if (boardIsFull(gameBoard)) {
      alert('DRAW!!');
      resetBoard();
    } else {
      currentPlayer = 'human';
    }
  }

  function getOpenSpaces(board) {
    var emptySpaces = [];
    for (var i = 0; i < board.length; i++) {
      if (boardSpaceOpen(board, i)) {
        emptySpaces.push(i);
      }
    }
    return emptySpaces;
  }

  function getOpponentOf(player) {
    return currentPlayer == 'computer' ? 'human' : 'computer';
  }

  function minimax(board, player) {
    if (checkWin(board, humanPlayer)) {
      return -100
    }
    if (checkWin(board, computerPlayer)) {
      return 100
    }
    if (boardIsFull(board)) {
      return 0;
    }

    var availableMoves = getOpenSpaces(board);
    var scores = availableMoves.map(function(move){
      var newBoard = board.slice();
      newBoard[move] = player; 
      return minimax(getOpponentOf(currentPlayer), newBoard);
    });

    if(currentPlayer == 'computer'){
      return Math.max.apply(null,scores);
    }
    else{
      return Math.min.apply(null,scores)
    }
  }



  $('#reset').on('click', function() {
    resetBoard();
  });

  $('.piece').on('click', function() {
   humanPlayer = this.id;
   computerPlayer = humanPlayer == 'X' ? 'O' : 'X';
   computerPlay();
  });

  $('td').on('click', function() {
    if (currentPlayer == 'human') {
      var move = this.id;
      if (boardSpaceOpen(gameBoard, move)) {
        makeMove(gameBoard, humanPlayer, move);
        buildBoard(gameBoard);

        if (checkWin(gameBoard, humanPlayer)) {
          alert('You Won!!');
          resetBoard();
        } else if (boardIsFull(gameBoard)) {
          alert('DRAW!!');
          resetBoard();
        } else {
          currentPlayer = 'computer';
          computerPlay();
        }
      }
    }
  });

});
  • This question is sooooo off-topic, but I can't help: Are you telling us that your javascript tic-tac-toe is not enough clever and it does let you win? Really? I don't think an algorithm like this gets usually more clever than whoever coded it... Also maybe read a bit here: http://stackoverflow.com/help/how-to-ask – Gavriel Jan 31 '16 at 21:16
  • 1
    first start with let the computer check if it should block 2 in a row – giorgio Jan 31 '16 at 21:19
  • Is this "please do my homework"? – crackmigg Jan 31 '16 at 21:52
  • This is I'm working my ass off but I'm still stuck lol – theodore braden Feb 01 '16 at 22:18
  • I'm saying that im not supposed to be able to beat the computer. I'm supposed to ether tie or lose. – theodore braden Feb 01 '16 at 22:20

0 Answers0