Skip to content

Instantly share code, notes, and snippets.

@htkcodes
Created November 17, 2017 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save htkcodes/5e174542be4e245f6a4f5fc35d4f3d52 to your computer and use it in GitHub Desktop.
Save htkcodes/5e174542be4e245f6a4f5fc35d4f3d52 to your computer and use it in GitHub Desktop.
kek
function f(a, b) {
this.s = a;
this.o = b;
this.value = this.v = null;
}
f.prototype = {constructor:f};
function h(a, b) {
a.value = b;
a.v && a.v.text(a.value);
}
function k(a) {
this.size = a;
this.cells = [];
for (a = 0; a < this.size; a++) {
this.cells[a] = [];
for (var b = 0; b < this.size; b++) {
this.cells[a][b] = new f(a, b);
}
}
}
k.prototype = {constructor:k, clone:function() {
for (var a = new k(this.size), b = 0; b < a.cells.length; b++) {
for (var c = 0; c < a.cells[0].length; c++) {
h(a.cells[b][c], this.cells[b][c].value);
}
}
return a;
}};
function l(a) {
function b(a) {
var b = a[0];
return a.every(function(a) {
return null !== b.value && a.value === b.value;
});
}
for (var c = 0; c < a.size; c++) {
if (b(a.cells[c])) {
return a.cells[c];
}
if (b(m(a, c))) {
return m(a, c);
}
if ((0 === c || 1 === c) && b(n(a, c))) {
return n(a, c);
}
}
return null;
}
function p(a) {
for (var b = [], c = 0; c < a.cells.length; c++) {
for (var d = 0; d < a.cells[c].length; d++) {
null !== a.cells[c][d].value || b.push(a.cells[c][d]);
}
}
return b;
}
function n(a, b) {
return a.cells.map(function(a, d) {
return a[0 === b ? d : a.length - d - 1];
});
}
function m(a, b) {
return a.cells.map(function(a) {
return a[b];
});
}
function q(a, b, c) {
this.a = a;
this.type = b;
this.b = c;
}
q.prototype = {constructor:q, m:function() {
}};
function t(a, b) {
q.call(this, a, "person", b);
}
t.prototype.m = function() {
this.a.display.C.G(this.b.name + "'s turn!");
return !0;
};
t.prototype = Object.create(q.prototype);
t.prototype.constructor = t;
function u(a, b) {
q.call(this, a, "easy", b);
}
u.prototype.m = function() {
var a = p(this.a.h);
if (0 < a.length) {
return this.a.move(a[Math.floor(Math.random() * a.length)]), !0;
}
};
u.prototype = Object.create(q.prototype);
u.prototype.constructor = u;
function w(a, b) {
q.call(this, a, "regular", b);
w.prototype = Object.create(q.prototype);
w.prototype.constructor = regularControllerController;
}
w.prototype.m = function() {
for (var a = x(this.a, this.b.id), b = p(this.a.h), c = 0; c < b.length; c++) {
var d = this.a.h.clone();
h(d.cells[b[c].s][b[c].o], a.b.g);
if (y(d, a.b)) {
return this.a.move(b[c]), !0;
}
}
for (c = 0; c < b.length; c++) {
if (d = this.a.h.clone(), h(d.cells[b[c].s][b[c].o], a.i.g), y(d, a.i)) {
return this.a.move(b[c]), !0;
}
}
if (0 < b.length) {
return this.a.move(b[Math.floor(Math.random() * b.length)]), !0;
}
};
function z(a, b) {
q.call(this, a, "difficult", b);
}
z.prototype.m = function() {
var a = x(this.a, this.b.id);
this.move = null;
A(this, this.a.h.clone(), a, 0);
this.move && this.a.move(this.move);
};
function A(a, b, c, d) {
var e = p(b);
if (y(b, a.b)) {
e = 1;
} else {
var g = a.b;
e = y(b, x(g.a, g.id).i) ? -1 : 0 === e.length ? 0 : null;
}
if (null !== e) {
return {f:e, depth:d};
}
e = p(b);
g = [];
for (var r = {b:c.i, i:c.b}, v = 0; v < e.length; v++) {
var D = b.clone();
h(D.cells[e[v].s][e[v].o], c.b.g);
g.push(A(a, D, r, d + 1));
}
b = B(a, e, g, c);
0 === d && (a.move = a.a.h.cells[b.s][b.o]);
return {f:b.f, depth:b.depth};
}
function B(a, b, c, d) {
var e = d.b === a.b ? c.reduce(function(a, b) {
return b.f > a.f || b.f === a.f && b.depth > a.depth ? b : a;
}) : c.reduce(function(a, b) {
return b.f < a.f || b.f === a.f && b.depth > a.depth ? b : a;
});
a = b.filter(function(a, b) {
return c[b].f === e.f && c[b].depth === e.depth;
});
a = a[Math.floor(Math.random() * a.length)];
return {s:a.s, o:a.o, f:e.f, depth:e.depth};
}
z.prototype = Object.create(q.prototype);
z.prototype.constructor = z;
function C(a, b, c, d, e) {
this.a = a;
this.id = b;
this.name = c;
this.g = d;
this.controller = null;
E(this, e);
this.K = 0;
}
C.prototype = {constructor:C};
function E(a, b) {
a.controller = "person" === b ? new t(a.a, a) : "easy" === b ? new u(a.a, a) : "regular" === b ? new w(a.a, a) : "difficult" === b ? new z(a.a, a) : null;
}
function F(a, b) {
return a.g !== b ? (a.g = b, !0) : !1;
}
function G(a, b) {
this.A = a;
this.B = b;
this.display = new H(this, b);
this.j = new C(this, "p1", "Player 1", "X", "person");
this.l = new C(this, "p2", "Player 2", "O", "difficult");
I(this);
}
G.prototype = {constructor:G, move:function(a) {
if (null !== a.value) {
return !1;
}
h(a, this.c.g);
y(this.h, this.c) ? (this.c.K++, a = this.c.name + " wins!", this.c = null, this.display.C.G(a), J(this.A)) : 0 === p(this.h).length ? (this.c = null, this.display.C.G("It's a draw."), J(this.A)) : (this.c = this.c === this.j ? this.l : this.j, this.c.controller.m());
}};
function x(a, b) {
return "p1" === b ? {b:a.j, i:a.l} : "p2" === b ? {b:a.l, i:a.j} : null;
}
function y(a, b) {
var c = l(a);
return Array.isArray(c) && c[0].value === b.g;
}
function I(a) {
$(".newGameDialog").O();
a.h = new k(3);
var b = a.h;
b.B = a.A;
b.B.empty();
for (var c = 0; c < b.cells.length; c++) {
for (var d = $("<div />", {H:"row", "data-row":c}), e = 0; e < b.cells[0].length; e++) {
var g = $("<div />", {H:"cell", "data-row":c, "data-col":e});
d.append(g);
var r = b.cells[c][e];
r.v = g;
r.v.text(r.value);
}
b.B.append(d);
}
a.display.update();
a.c = ("X" === a.j.g ? {b:a.j, i:a.l} : "X" === a.l.g ? {b:a.l, i:a.j} : null).b;
a.c.controller.m();
}
function H(a, b) {
this.a = a;
this.C = b.find("#message");
this.I = K(b.find("#p1"));
this.J = K(b.find("#p2"));
}
H.prototype = {constructor:H, update:function() {
var a = this.a.j, b = this.I;
b.F.u(a.name);
b.D.u(a.g);
a = this.a.l;
b = this.J;
b.F.u(a.name);
b.D.u(a.g);
}};
function J(a) {
boardWidth = a.width();
boardHeight = a.height();
boardPos = a.position();
$(".newGameDialog").M({left:boardPos.left + boardWidth / 2 - 150, top:boardPos.top + boardHeight / 2 - 100});
$(".newGameDialog").N();
}
function K(a) {
return {F:a.find(".player-input-name"), D:a.find(".player-select-marker"), L:a.find(".player-select-controller")};
}
$(document).ready(function() {
var a = new G($(".board-container"), $(".display-container"), $("#newGameModal"));
$("#reset-button").w("click", function(b) {
b.preventDefault();
I(a);
});
$(".board-container").w("click", ".cell", function() {
var b = $(this).data("row"), c = $(this).data("col");
a.c && "person" === a.c.controller.type && a.move(a.h.cells[b][c]);
});
$(".player-container").w("change", ".player-input-name", function() {
var b = $(this).data("player"), c = $(this).u();
x(a, b).b.name = c;
});
$(".player-container").w("change", ".player-select-marker", function() {
var b = $(this).data("player"), c = $(this).u();
b = x(a, b);
F(b.b, c) && (F(b.i, "X" === c ? "O" : "X"), a.display.update());
});
$(".player-container").w("change", ".player-select-controller", function() {
var b = $(this).data("player"), c = $(this).u();
b = x(a, b);
E(b.b, c);
b.b === a.c && a.c.controller.m();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment