Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(document).ready(function () {
- var arrWinners = [[7, 8, 9], [4, 5, 6], [1, 2, 3], [3, 6, 9], [2, 5, 8], [1, 4, 7], [1, 5, 9], [3, 5, 7]];
- var arrMoves = [[7, 8, 9], [4, 5, 6], [1, 2, 3], [3, 6, 9], [2, 5, 8], [1, 4, 7], [1, 5, 9], [3, 5, 7]
- , [2, 4, 1], [2, 6, 3], [4, 8, 7], [6, 8, 9], [1, 9, 6], [4, 9, 7], [2, 7, 1]];
- var xMoves = [];
- var oMoves = []
- var human;
- var dirty = [];
- var xTurn = false;
- $(".pick").hide();
- $(".pick").fadeIn(1000);
- $("table").hide();
- $(".x").click(function () {
- $(".pick").hide()
- $("#picked").text("You Are playing with X")
- $(".game").fadeIn(3000);
- xTurn = true;
- human = "X";
- })
- $(".o").click(function () {
- $(".pick").hide()
- $("#picked").text("You Are playing with O")
- $(".game").fadeIn(3000);
- human = "O";
- })
- $("td").click(function (event) {
- console.log(dirty);
- if (xTurn) {
- xTurn = false;
- moveHuman(xMoves);
- var value = "#" + event.target.id;
- $(value).text("X").fadeIn("1000");
- pcPlay("O");
- }
- else {
- xTurn = true;
- if (human === "O") {
- moveHuman(oMoves);
- var value = "#" + event.target.id;
- $(value).text("O").fadeIn("1000");
- pcPlay("X");
- }
- }
- });
- function moveHuman(arr) {
- arr.push(parseInt(event.target.id));
- dirty.push(parseInt(event.target.id));
- for (var i = 0; i < arrWinners.length; i++) {
- var count = 0;
- for (var j = 0; j < 3; j++) {
- if ((arr.indexOf(arrWinners[i][j])) !== -1) count++;
- if (count == 3) alert("win");
- }
- }
- }
- function movePc(arr, random) {
- arr.push(parseInt(event.target.id));
- dirty.push(parseInt(random));
- for (var i = 0; i < arrWinners.length; i++) {
- var count = 0;
- for (var j = 0; j < 3; j++) {
- if ((arr.indexOf(arrWinners[i][j])) !== -1) count++;
- if (count == 3) alert("win");
- }
- }
- }
- function pcPlay(type) {
- var random = Math.floor(Math.random() * 10)+1;
- while (dirty.indexOf(random) !== -1) {
- console.log(random);
- random = Math.floor(Math.random * 10);
- }
- movePc(xMoves,random);
- var value = "#" + random;
- $(value).text(type).fadeIn("1000");
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement