Skip to content

Instantly share code, notes, and snippets.

@howmanysmall
Created November 25, 2019 20:49
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 howmanysmall/c90613272a69d9d44895c64b45f52503 to your computer and use it in GitHub Desktop.
Save howmanysmall/c90613272a69d9d44895c64b45f52503 to your computer and use it in GitHub Desktop.
var MARKERS = ["X", "O"];
var PLAYER_1_NAME = document.getElementById("player1name");
var PLAYER_2_NAME = document.getElementById("player2name");
var GAME_MESSAGE = document.getElementById("game-message");
var WIN_SOUND = document.getElementById("win-sound");
var ERROR_SOUND = document.getElementById("error-sound");
var WIN_CODES = [7, 56, 73, 84, 146, 273, 292, 448];
var VALUES = [1, 2, 4, 8, 16, 32, 64, 128, 256];
GAME_MESSAGE.innerText = "Please enter names below.";
var PlayerTurn = 0;
var Players = [];
var IsGameRunning = false;
var IsGameOver = false;
var Totals = [0, 0];
var CurrentTurn = 0;
var Winner;
var Noop = function () { };
var GrammarFix = function (PlayerName) { return PlayerName.substring(PlayerName.length - 1) === "s" ? PlayerName + "'" : PlayerName + "'s"; };
var IsWin = function () {
for (var _i = 0, WIN_CODES_1 = WIN_CODES; _i < WIN_CODES_1.length; _i++) {
var WinCode = WIN_CODES_1[_i];
if ((Totals[PlayerTurn] & WinCode) === WinCode)
return true;
}
return false;
};
var PlayGame = function (Div, ButtonValue) {
if (IsGameRunning && !IsGameOver && CurrentTurn < 9) {
CurrentTurn++;
Div.innerText = MARKERS[PlayerTurn];
if (CurrentTurn === 9 && !Winner) {
GAME_MESSAGE.innerText = "Stalemate!";
ERROR_SOUND.play();
}
else {
Totals[PlayerTurn] += ButtonValue;
if (IsWin()) {
Winner = Players[PlayerTurn];
GAME_MESSAGE.innerText = "The winner is " + Winner + "!";
IsGameOver = true;
WIN_SOUND.play();
}
else {
PlayerTurn = PlayerTurn ? 0 : 1;
Div.onclick = Noop; // The reason for this is because it's simply faster.
GAME_MESSAGE.innerText = "It is " + GrammarFix(Players[PlayerTurn]) + " turn!";
}
}
}
};
var SubmitFunction = function () {
if (!IsGameRunning) {
if (PLAYER_1_NAME.value != "" && PLAYER_2_NAME.value != "") {
IsGameRunning = true;
Players[0] = PLAYER_1_NAME.value;
Players[1] = PLAYER_2_NAME.value;
GAME_MESSAGE.innerText = "It is " + GrammarFix(Players[PlayerTurn]) + " turn!";
}
else
GAME_MESSAGE.innerText = "Names cannot be empty!";
}
else {
var PREVIOUS_MESSAGE_1 = GAME_MESSAGE.innerText;
GAME_MESSAGE.innerText = "Game is already running!";
setTimeout(function () { return GAME_MESSAGE.innerText = PREVIOUS_MESSAGE_1; }, 2000);
}
};
const MARKERS: string[] = ["X", "O"];
const PLAYER_1_NAME = document.getElementById("player1name") as HTMLInputElement;
const PLAYER_2_NAME = document.getElementById("player2name") as HTMLInputElement;
const GAME_MESSAGE = document.getElementById("game-message") as HTMLHeadElement;
const WIN_SOUND = document.getElementById("win-sound") as HTMLAudioElement;
const ERROR_SOUND = document.getElementById("error-sound") as HTMLAudioElement;
const WIN_CODES: number[] = [7, 56, 73, 84, 146, 273, 292, 448];
const VALUES: number[] = [1, 2, 4, 8, 16, 32, 64, 128, 256];
GAME_MESSAGE.innerText = "Please enter names below.";
let PlayerTurn: number = 0;
let Players: string[] = [];
let IsGameRunning: boolean = false;
let IsGameOver: boolean = false;
let Totals: number[] = [0, 0];
let CurrentTurn: number = 0;
let Winner: string;
let Noop = (): void => {}
let GrammarFix = (PlayerName: string): string => PlayerName.substring(PlayerName.length - 1) === "s" ? PlayerName + "'" : PlayerName + "'s";
let IsWin = (): boolean =>
{
for (const WinCode of WIN_CODES)
if ((Totals[PlayerTurn] & WinCode) === WinCode)
return true;
return false;
}
let PlayGame = (Div: HTMLDivElement, ButtonValue: number): void =>
{
if (IsGameRunning && !IsGameOver && CurrentTurn < 9)
{
CurrentTurn++;
Div.innerText = MARKERS[PlayerTurn];
if (CurrentTurn === 9 && !Winner)
{
GAME_MESSAGE.innerText = `Stalemate!`;
ERROR_SOUND.play();
} else
{
Totals[PlayerTurn] += ButtonValue;
if (IsWin())
{
Winner = Players[PlayerTurn];
GAME_MESSAGE.innerText = `The winner is ${Winner}!`;
IsGameOver = true;
WIN_SOUND.play();
} else
{
PlayerTurn = PlayerTurn ? 0 : 1;
Div.onclick = Noop; // The reason for this is because it's simply faster.
GAME_MESSAGE.innerText = `It is ${GrammarFix(Players[PlayerTurn])} turn!`;
}
}
}
}
let SubmitFunction = (): void =>
{
if (!IsGameRunning)
{
if (PLAYER_1_NAME.value != "" && PLAYER_2_NAME.value != "")
{
IsGameRunning = true;
Players[0] = PLAYER_1_NAME.value;
Players[1] = PLAYER_2_NAME.value;
GAME_MESSAGE.innerText = `It is ${GrammarFix(Players[PlayerTurn])} turn!`;
} else
GAME_MESSAGE.innerText = "Names cannot be empty!";
} else
{
const PREVIOUS_MESSAGE = GAME_MESSAGE.innerText;
GAME_MESSAGE.innerText = "Game is already running!";
setTimeout(() => GAME_MESSAGE.innerText = PREVIOUS_MESSAGE, 2000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment