<commit_before>'use strict';

var fb = new Firebase('https://tic-tac-toes-app.firebaseio.com'),
  gameArr = [['','',''],['','',''],['','','']],
  currPlayer = '',
  turnCounter = 0,
  player1 = '', // '/images/tack.jpg',
  player2 = '/images/tick.jpg', // '/images/tick.jpg';
  //TODO: add function that toggles isPlayer1 to true or false depending on whether the player is the first or second to join the game
  cellImg;
//login register logout features

$('#registerButton').click(function() {
  var user     = $('#userEmail').val(),
      password = $('#<PASSWORD>').val();

  fb.createUser({
    email: user,
    password: password
  }, function(error, userData) {
    if (error) {
    switch (error.code) {
      case "EMAIL_TAKEN":
        alert('This email is already in use.');
        break;
      case "INVALID_EMAIL":
        alert('The specified email is not valid.');
        break;
      default:
        alert('Error creating user:', error);
      }
    } else {
      fb.authWithPassword({
        'email': user,
        'password': password
      }, function(error, authData) {
          if (error) {
            alert("Login Failed!", error);
          } else {
            $('#loginForm').hide("slow");
            console.log(authData);
            sendToFb(authData);
          }
      });
      $('#loginForm').hide("slow");
      //$('#boardWrapper').toggle();
      clearGame();
      renderBoard(gameArr);
    }
  });
});


$('#loginButton').click(function() {
  var user     = $('#userEmail').val(),
      password = $('#<PASSWORD>').val();
  fb.authWithPassword({
    'email': user,
    'password': password
  }, function(error, authData) {
    if (error) {
      alert("Login Failed!", error);
    } else {
      $('#loginForm').hide("slow");
      clearGame();
      renderBoard(gameArr);
      setPlayerImg();
    }
  });
});


$('#logoutButton').click(function() {
  var playerInfo = fb.getAuth(),
    playerId = playerInfo.uid,
    fbPlayer = new Firebase('https://tic-tac-toes-app.firebaseio.com/players/' + playerId);
    fbPlayer.child('wonCurrGame').set(false);
  fb.unauth();
  $('#boardWrapper').empty();
  alert('Logout successful! Come back soon!');
  $('#loginForm').show("slow");
});


function sendToFb(data) {
  console.log(data)
  fb.child('players').child(data.uid).set({
    userName: data.uid,
    wins: 0,
    losses: 0,
    gamesPlayed: [''],
    opponentsPlayed: [''],
    isPlayer1: true,
    accountExpiration: data.expires,
    wonCurrGame: false,
    currGame: '',
    email: data.password.email
  });
}


function clearGame () {
  gameArr = [['','',''],['','',''],['','','']];
  turnCounter =  0;
  currPlayer = player1;
}

//Create board

function renderBoard(x) {
  $('table').empty();
	var $tbody = $('<tbody></tbody>');

	x.forEach(function(row) {
		var $tr = $('<tr></tr>');
	 row.forEach(function(cell) {
		$tr.append($('<td><img src="' + cell + '"></img></td>'));
  });
    $tbody.append($tr);
  });
  $('table').append($tbody);
}
var row,
    col;
//Click to select move.
$('#boardWrapper').on('click', 'tbody tr td', function(){
  row = this.parentElement.sectionRowIndex;
  col  = this.cellIndex;
  if (gameArr[row][col] === '') {
    gameArr[row][col] = currPlayer;
    cellImg = gameArr[row][col];
    checkForWin(gameArr);
    playerTurn();
    renderBoard(gameArr);
    gameOverCheck();
  } else {
    alert('That space is taken please choose another:)');
  }
});


//switch between players and increment turn counter.
function gameOverCheck () {
  if (turnCounter < 9) {
    turnCounter++
  }
  else {
    alert('GAME OVER!');<commit_msg>fixed issue with img src for current player not rendering properly in the renderboard function in the debugger<commit_after>'use strict';

var fb = new Firebase('https://tic-tac-toes-app.firebaseio.com'),
  gameArr = [['','',''],['','',''],['','','']],
  currPlayer = '',
  turnCounter = 0,
  player1 = '', // '/images/tack.jpg',
  player2 = '/images/tick.jpg', // '/images/tick.jpg';
  //TODO: add function that toggles isPlayer1 to true or false depending on whether the player is the first or second to join the game
  cellImg;
//login register logout features

$('#registerButton').click(function() {
  var user     = $('#userEmail').val(),
      password = $('#userPassword').val();

  fb.createUser({
    email: user,
    password: password
  }, function(error, userData) {
    if (error) {
    switch (error.code) {
      case "EMAIL_TAKEN":
        alert('This email is already in use.');
        break;
      case "INVALID_EMAIL":
        alert('The specified email is not valid.');
        break;
      default:
        alert('Error creating user:', error);
      }
    } else {
      fb.authWithPassword({
        'email': user,
        'password': password
      }, function(error, authData) {
          if (error) {
            alert("Login Failed!", error);
          } else {
            $('#loginForm').hide("slow");
            console.log(authData);
            sendToFb(authData);
          }
      });
      $('#loginForm').hide("slow");
      //$('#boardWrapper').toggle();
      clearGame();
      renderBoard(gameArr);
    }
  });
});


$('#loginButton').click(function() {
  var user     = $('#userEmail').val(),
      password = $('#<PASSWORD>').val();
  fb.authWithPassword({
    'email': user,
    'password': password
  }, function(error, authData) {
    if (error) {
      alert("Login Failed!", error);
    } else {
      $('#loginForm').hide("slow");
      clearGame();
      setPlayerImg();
      renderBoard(gameArr);
    }
  });
});


$('#logoutButton').click(function() {
  var playerInfo = fb.getAuth(),
    playerId = playerInfo.uid,
    fbPlayer = new Firebase('https://tic-tac-toes-app.firebaseio.com/players/' + playerId);
    fbPlayer.child('wonCurrGame').set(false);
  fb.unauth();
  $('#boardWrapper').empty();
  alert('Logout successful! Come back soon!');
  $('#loginForm').show("slow");
});


function sendToFb(data) {
  console.log(data)
  fb.child('players').child(data.uid).set({
    userName: data.uid,
    wins: 0,
    losses: 0,
    gamesPlayed: [''],
    opponentsPlayed: [''],
    isPlayer1: true,
    accountExpiration: data.expires,
    wonCurrGame: false,
    currGame: '',
    email: data.password.email
  });
}


function clearGame () {
  gameArr = [['','',''],['','',''],['','','']];
  turnCounter =  0;
  currPlayer = player1;
}

//Create board

function renderBoard(x) {
  $('table').empty();
	var $tbody = $('<tbody></tbody>');

	x.forEach(function(row) {
		var $tr = $('<tr></tr>');
	 row.forEach(function(cell) {
		$tr.append($('<td><img src="' + cell + '"></img></td>'));
  });
    $tbody.append($tr);
  });
  $('table').append($tbody);
}
var row,
    col;

//Click to select move

$('#boardWrapper').on('click', 'tbody tr td', function(){
  row = this.parentElement.sectionRowIndex;
  col  = this.cellIndex;
  if (gameArr[row][col] === '') {
    gameArr[row][col] = currPlayer;
    cellImg = gameArr[row][col];
    checkForWin(gameArr);
    playerTurn();
    renderBoard(gameArr);
    gameOverCheck();
  } else {
    alert('That space is taken please choose another:)');
  }
});


//switch between players and increment turn counter.
function gameOverCheck () {
  if (turnCounter < 9) {
    turnCounter++
  }
  else {
    alert('GAME OVER!');
{'fraction_non_alphanumeric': 0.20101120524733535, 'fraction_numerical': 0.002732987154960372, 'mean_word_length': 2.9626421223605846, 'pattern_counts': {'":': 4, '<': 24, '<?xml version=': 0, '>': 22, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '192822', 'n_tokens_mistral': 2411, 'n_tokens_neox': 2376, 'n_words': 504}