From 3bf91c8953dc73c3a0d417ade4922a65ee252d42 Mon Sep 17 00:00:00 2001 From: AUnicornWithNoLife Date: Sat, 15 May 2021 11:29:14 +0100 Subject: [PATCH] prediction --- src/code.js | 99 ++++++++++++++++++++++++++++++++++++++------------ src/index.html | 0 src/style.css | 12 ++++++ src/theme.js | 0 src/var.js | 2 + 5 files changed, 89 insertions(+), 24 deletions(-) mode change 100644 => 100755 src/code.js mode change 100644 => 100755 src/index.html mode change 100644 => 100755 src/style.css mode change 100644 => 100755 src/theme.js mode change 100644 => 100755 src/var.js diff --git a/src/code.js b/src/code.js old mode 100644 new mode 100755 index d40a1f4..91837e3 --- a/src/code.js +++ b/src/code.js @@ -1,25 +1,14 @@ function move(id) { - if (board[ids[id][0]][ids[id][1]][0] != 0) + if (board[ids[id][0]][ids[id][1]][0] != 0) { return; } - var charater = ""; + var charater = (ptm == 1) ? "X" : "O"; var optm = ptm; - - if (ptm == 1) - { - charater = "X"; - - ptm = 2; - } - else - { - charater = "O"; - - ptm = 1; - } + + ptm = (ptm == 1) ? 2 : 1; board[ids[id][0]][ids[id][1]][1].innerHTML = charater; board[ids[id][0]][ids[id][1]][0] = optm; @@ -52,17 +41,14 @@ function move(id) return; } - - if (ptm == 1) - { - charater = "X"; - } - else - { - charater = "O"; - } + + charater = (ptm == 1) ? "X" : "O"; info['go'].innerHTML = charater + "'s Go"; + + var pre = calculate(); + + board[pre[0]][pre[1]][1].parentElement.classList.add('predict'); } function win() @@ -156,4 +142,69 @@ function reset() tabloid.classList.remove('win'); tabloid.classList.remove('lol'); + + load(); +} + +function calculate() +{ + var like = (ptm == 1) ? 2 : 1; + + for (var x = 0; x <3; x++) + { + if (board[x][0][0] == like && board[x][1][0] == like && board[x][2][0] == 0) + { + return [x, 2]; + } + if (board[x][0][0] == like && board[x][1][0] == 0 && board[x][2][0] == like) + { + return [x, 1]; + } + if (board[x][0][0] == 0 && board[x][1][0] == like && board[x][2][0] == like) + { + return [x, 0]; + } + } + + for (var x = 0; x <3; x++) + { + if (board[0][x][0] == like && board[1][x][0] == like && board[2][x][0] == 0) + { + return [2, x]; + } + if (board[0][x][0] == like && board[1][x][0] == 0 && board[2][x][0] == like) + { + return [1, x]; + } + if (board[0][x][0] == 0 && board[1][x][0] == like && board[2][x][0] == like) + { + return [0, x]; + } + } + + if (board[0][0][0] == like && board[1][1][0] == like && board[2][2][0] == 0) + { + return [2, 2]; + } + if (board[0][0][0] == like && board[1][1][0] == 0 && board[2][2][0] == like) + { + return [1, 1]; + } + if (board[0][0][0] == 0 && board[1][1][0] == like && board[2][2][0] == like) + { + return [0, 0]; + } + + if (board[0][2][0] == like && board[1][1][0] == like && board[2][0][0] == 0) + { + return [2, 0]; + } + if (board[0][2][0] == like && board[1][1][0] == 0 && board[2][0][0] == like) + { + return [1, 1]; + } + if (board[0][2][0] == 0 && board[1][1][0] == like && board[2][0][0] == like) + { + return [0, 2]; + } } \ No newline at end of file diff --git a/src/index.html b/src/index.html old mode 100644 new mode 100755 diff --git a/src/style.css b/src/style.css old mode 100644 new mode 100755 index b0bd99d..f16d340 --- a/src/style.css +++ b/src/style.css @@ -16,6 +16,11 @@ width: 100px; } +#board +{ + margin-right: 16px; +} + .center { margin-left: auto; @@ -72,4 +77,11 @@ button:hover border-radius: 12px; background-color: #b86868; +} + +.predict +{ + border-radius: 12px; + + background-color: #9DB17C; } \ No newline at end of file diff --git a/src/theme.js b/src/theme.js old mode 100644 new mode 100755 diff --git a/src/var.js b/src/var.js old mode 100644 new mode 100755 index 27815d6..109bee5 --- a/src/var.js +++ b/src/var.js @@ -62,6 +62,8 @@ function load() gamemode = 1; root = document.documentElement; + + info['go'].innerHTML = "X's Go"; setup(); }