Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 140 lines (126 sloc) 2.907 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Existential Crisis: Tic-Tac-Toe</title>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

  <style type="text/css">
    table td {
      width: 200px;
      height: 200px;
      border: 1px solid #000;
      text-align: center;
      font-size: 108px;
      vertical-align: middle;
    }
    table {
      border-collapse: collapse;
    }
    .play-X {
      color: #000;
    }
    .play-O {
      color: #f00;
    }
  </style>

</head>
<body>

<h1>Tic-Tac-Toe</h1>

<table>
  <tr>
    <td data-row="0" data-column="0">&nbsp;</td>
    <td data-row="0" data-column="1">&nbsp;</td>
    <td data-row="0" data-column="2">&nbsp;</td>
  </tr>
  <tr>
    <td data-row="1" data-column="0">&nbsp;</td>
    <td data-row="1" data-column="1">&nbsp;</td>
    <td data-row="1" data-column="2">&nbsp;</td>
  </tr>
  <tr>
    <td data-row="2" data-column="0">&nbsp;</td>
    <td data-row="2" data-column="1">&nbsp;</td>
    <td data-row="2" data-column="2">&nbsp;</td>
  </tr>
</table>

<script type="text/javascript">
var WINNER = false;
var BOARD = [
  ['', '', ''], // [column 0, column 1, column 2]
  ['', '', ''],
  ['', '', '']
];
var COLOR = 'X';

$('td').click(function(e) {
  if (WINNER) {
    return;
  }

  // Mark the move, if legal.
  var target = $(e.target);
  var column = parseInt(target.data('column'));
  var row = parseInt(target.data('row'));
  if (!BOARD[row][column]) {
    BOARD[row][column] = COLOR;
  } else {
    alert('Illegal move!');
    return;
  }

  // Toggle who's playing.
  if (COLOR == 'X') {
    COLOR = 'O';
  } else {
    COLOR = 'X';
  }

  // Update board state.
  $('td').each(function() {
    var cell = $(e.target);
    var column = parseInt(cell.data('column'));
    var row = parseInt(cell.data('row'));
    if (BOARD[row][column]) {
      cell.text(BOARD[row][column]);
      cell.addClass('play-' + BOARD[row][column]);
    } else {
      cell.html('&nbsp;');
      cell.attr('class', '');
    }
  });

  // Check for a winner.
  // Horizontal
  for (var i = 0; i < 3; i++) {
    if (BOARD[i][0] &&
        BOARD[i][0] == BOARD[i][1] &&
        BOARD[i][1] == BOARD[i][2]) {
      alert(BOARD[i][0] + ' has won!');
      WINNER = true;
      return;
    }
  }

  // Vertical
  for (var i = 0; i < 3; i++) {
    if (BOARD[0][i] &&
        BOARD[0][i] == BOARD[1][i] &&
        BOARD[1][i] == BOARD[2][i]) {
      alert(BOARD[0][i] + ' has won!');
      WINNER = true;
      return;
    }
  }

  // Diagnol
  if (BOARD[0][0] &&
      BOARD[0][0] == BOARD[1][1] &&
      BOARD[1][1] == BOARD[2][2]) {
    alert(BOARD[0][0] + ' has won!');
    WINNER = true;
    return;
  }
  if (BOARD[0][2] &&
      BOARD[0][2] == BOARD[1][1] &&
      BOARD[1][1] == BOARD[2][0]) {
    alert(BOARD[0][2] + ' has won!');
    WINNER = true;
    return;
  }
});
</script>

</body>
</html>
Something went wrong with that request. Please try again.