Skip to content

Instantly share code, notes, and snippets.

@1oonie
Created October 9, 2021 10:52
Show Gist options
  • Save 1oonie/f7c066cea34fb86814b3a83d585e7646 to your computer and use it in GitHub Desktop.
Save 1oonie/f7c066cea34fb86814b3a83d585e7646 to your computer and use it in GitHub Desktop.
tictactoe
!function(){function r(r,e){for(let t=0;t<3;t++){if(r[0][t]===e&&r[1][t]===e&&r[2][t]===e)return!0;if(r[t][0]===e&&r[t][1]===e&&r[t][2]===e)return!0}return r[0][0]===e&&r[1][1]===e&&r[2][2]===e||r[2][0]===e&&r[1][1]===e&&r[0][2]===e}function e(e){for(let r=0;r<3;r++)for(let t=0;t<3;t++)if("-"===e[r][t])return!1;return!0!==r(e,"x")&&!0!==r(e,"o")}function t(r,e,t){if(parseInt(t)<4){if("-"!==r[0][parseInt(t)-1])return alert("Invalid Move!"),!1;r[0][parseInt(t)-1]=e}else if(parseInt(t)<7){if("-"!==r[1][parseInt(t)-4])return alert("Invalid Move!"),!1;r[1][parseInt(t)-4]=e}else{if(!(parseInt(t)<10))return alert("Invalid Move!"),!1;if("-"!==r[2][parseInt(t)-7])return alert("Invalid Move!"),!1;r[2][parseInt(t)-7]=e}return r}function n(r,e){var t=prompt(`${function(r){var e="";for(let n in r){var t="";for(let e in r[n])t+=`${r[n][e]} `;e+=`${t}\n`}return e}(r)}${e} what is your move? (Type q to quit)`),n=["1","2","3","4","5","6","7","8","9"];for(let r in n)if(n[r]===t)return t;if("q"===t)throw new Error("Stop script");return alert("Invalid Move!"),!1}for(var a=[["-","-","-"],["-","-","-"],["-","-","-"]];;){for(var i=!1,o=!1;!1===i&&!1===o;)!1!==(i=n(a,"Player 1"))&&(!1!==(o=t(a,"x",i))||(i=!1));if(r(a=o,"x"))return alert("Player 1 won!");if(e(a))return alert("It was a draw!");for(i=!1,o=!1;!1===i&&!1===o;)!1!==(i=n(a,"Player 2"))&&(!1!==(o=t(a,"o",i))||(i=!1));if(r(a=o,"o"))return alert("Player 2 won!");if(e(a))return alert("It was a draw!")}}();
(function () {
var player1 = "x";
var player2 = "o";
function createNewBoard() {
return [["-", "-", "-"], ["-", "-", "-"], ["-", "-", "-"]]
};
function checkWin(board, player) {
for (let i = 0; i < 3; i++) {
if(board[0][i] === player && board[1][i] === player && board[2][i] === player){
return true;
};
if(board[i][0] === player && board[i][1] === player && board[i][2] === player){
return true;
};
};
if(board[0][0] === player && board[1][1] === player && board[2][2] === player){
return true;
};
if(board[2][0] === player && board[1][1] === player && board[0][2] === player){
return true;
} else{
return false
};
};
function checkDraw(board) {
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[i][j] === "-"){
return false;
};
};
};
if (checkWin(board, "x") === true || checkWin(board, "o") === true){
return false;
};
return true;
};
function playerMove(board, player, moveInt) {
if (parseInt(moveInt) < 4) {
if (board[0][parseInt(moveInt)-1] === "-") {
board[0][parseInt(moveInt)-1] = player;
} else {
alert("Invalid Move!");
return false;
};
} else if (parseInt(moveInt) < 7) {
if (board[1][parseInt(moveInt)-4] === "-"){
board[1][parseInt(moveInt)-4] = player;
} else {
alert("Invalid Move!");
return false;
};
} else if (parseInt(moveInt) < 10) {
if (board[2][parseInt(moveInt)-7] === "-") {
board[2][parseInt(moveInt)-7] = player;
} else {
alert("Invalid Move!");
return false;
};
} else {
alert("Invalid Move!");
return false;
};
return board;
};
function printableBoard(board) {
var res = "";
for (let row in board) {
var rowString = "";
for (let item in board[row]) {
rowString += `${board[row][item]} `;
};
res += `${rowString}\n`;
};
return res;
};
function getUserInput(board, player) {
var input = prompt(`${printableBoard(board)}${player} what is your move? (Type q to quit)`);
var numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9"];
for (let value in numbers) {
if (numbers[value] === input) {
return input;
} else {
continue;
};
};
if (input === "q") {
throw new Error("Stop script");
}
alert("Invalid Move!");
return false;
};
var board = createNewBoard();
while (true) {
var input = false;
var move = false;
while (input === false && move === false) {
input = getUserInput(board, "Player 1")
if (input === false) {
continue;
} else {
move = playerMove(board, player1, input);
if (move === false) {
input = false;
continue;
};
};
};
board = move;
if (checkWin(board, player1)) {
return alert("Player 1 won!");
} else if (checkDraw(board)) {
return alert("It was a draw!");
};
var input = false;
var move = false;
while (input === false && move === false) {
input = getUserInput(board, "Player 2")
if (input === false) {
continue;
} else {
move = playerMove(board, player2, input);
if (move === false) {
input = false;
continue;
};
};
};
board = move;
if (checkWin(board, player2)) {
return alert("Player 2 won!");
} else if (checkDraw(board)) {
return alert("It was a draw!");
};
};
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment