Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Array.prototype.randomize = function () {
- let i = this.length;
- if (i === 0) return false;
- while (--i) {
- let j = Math.floor(Math.random() * (i + 1));
- let tempi = this[i];
- let tempj = this[j];
- this[i] = tempj;
- this[j] = tempi;
- }
- };
- Array.prototype.toObject = function () {
- let o = {};
- for (let i = 0; i < this.length; i++) {
- o[this[i]] = '';
- }
- return o;
- };
- function bindEvent(el, eventName, eventHandler) {
- if (el.addEventListener) {
- el.addEventListener(eventName, eventHandler, false);
- } else if (el.attachEvent) {
- el.attachEvent('on' + eventName, eventHandler);
- }
- }
- function div(parent, className) {
- let r = document.createElement('div');
- r.className = className;
- parent.appendChild(r);
- return r;
- }
- let WordDisplay = (function () {
- let display = document.getElementById('display'),
- characters;
- class WordDisplay {
- constructor(puzzle) {
- characters = [];
- while (display.hasChildNodes()) {
- display.removeChild(display.firstChild);
- }
- let word = div(display, 'word');
- for (let i = 0; i < puzzle.length; ++i) {
- if (puzzle[i] == ' ') {
- characters.push(div(display, 'space'));
- word = div(display, 'word');
- }
- else {
- characters.push(div(word, 'letter'));
- }
- //console.log(i, puzzle[i], word.children, characters.length);
- }
- div(display, 'clear');
- }
- showLetter(i, letter) {
- characters[i].innerHTML = letter;
- }
- }
- return WordDisplay;
- })();
- let Wheel = (function () {
- let wheel = document.getElementById('wheel'),
- wheelValues =
- [5000, 500, 900,
- 700, 300, 800,
- 550, 400, 500,
- 600, 350, 500,
- 900, 0, 650,
- 300, 700, 400,
- 800, 500, 450,
- 500, 300, 0],
- spinTimeout = false,
- spinModifier = function () {
- return Math.random() * 10 + 20;
- },
- modifier = spinModifier(),
- slowdownSpeed = 0.5,
- prefix = (function () {
- if (document.body.style.MozTransform !== undefined) {
- return "MozTransform";
- } else if (document.body.style.WebkitTransform !== undefined) {
- return "WebkitTransform";
- } else if (document.body.style.OTransform !== undefined) {
- return "OTransform";
- } else {
- return "";
- }
- }()),
- degreeToRadian = function (deg) {
- return deg / (Math.PI * 180);
- };
- function Wheel() {}
- Wheel.prototype.rotate = function (degrees) {
- let val = "rotate(-" + degrees + "deg)";
- if (wheel.style[prefix] !== undefined) wheel.style[prefix] = val;
- let rad = degreeToRadian(degrees % 360),
- filter = "progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand', M11=" + rad + ", M12=-" + rad + ", M21=" + rad + ", M22=" + rad + ")";
- if (wheel.style.filter !== undefined) wheel.style.filter = filter;
- wheel.setAttribute("data-rotation", degrees);
- };
- Wheel.prototype.addEventListener = function (eventName, eventHandler) {
- wheel.addEventListener(eventName, eventHandler, false);
- }
- Wheel.prototype.spin = function (callback, amount) {
- let _this = this;
- clearTimeout(spinTimeout);
- modifier -= slowdownSpeed;
- if (amount === undefined) {
- amount = parseInt(wheel.getAttribute('data-rotation'), 10);
- }
- this.rotate(amount);
- if (modifier > 0) {
- spinTimeout = setTimeout(function () {
- _this.spin(callback, amount + modifier);
- }, 1000 / 5);
- } else {
- let dataRotation = parseInt(wheel.getAttribute('data-rotation'), 10);
- modifier = spinModifier();
- let divider = 360 / wheelValues.length;
- let offset = divider / 2; //half division
- let wheelValue = wheelValues[Math.floor(Math.ceil((dataRotation + offset) % 360) / divider)];
- switch (wheelValue) {
- case 0:
- return callback(0);
- case -1:
- return callback("Dodatkowy obrót kołem ");
- case -2:
- return callback("Tracisz kolejkę");
- default:
- return callback(wheelValue);
- }
- }
- };
- return Wheel;
- })();
- let store;
- function show(text, inputType) {
- let showPrompt = document.getElementById('show');
- let div = document.getElementById('content');
- let textInput = document.querySelector("#box-input");
- let button = document.querySelector("#box-input-btn");
- button.addEventListener("click", () =>{
- store = textInput.value;
- disable();
- });
- showPrompt.style.display = "block";
- div.innerHTML = text;
- textInput.style.display = inputType;
- }
- function disable() {
- let showPrompt = document.getElementById('show');
- showPrompt.style.display = "none";
- }
- let wheelFortune = (function () {
- let wheel = new Wheel(),
- vowels = ['A', 'E', 'I', 'O', 'U' , 'Y'],
- wordDisplay,
- spinWheel = document.getElementById('spin'),
- buyVowel = document.getElementById('vowel'),
- newButton = document.getElementById('newpuzzle'),
- cash = document.getElementById('cash'),
- solve = document.getElementById('solve');
- function wheelFortune(puzzles) {
- let _this = this;
- this.puzzles = puzzles;
- this.puzzles.randomize();
- this.currentCash = 0;
- this.puzzleSolved = false;
- bindEvent(buyVowel, "click", function () {
- if (_this.currentCash > 200) {
- if (_this.createGuessPrompt("wpisz samogłoskę", true) !== false) {
- _this.currentCash -= 200;
- _this.updateCash();
- }
- } else {
- show('Potrzebujesz 150 zł aby kupić samogłoskę', 'none');
- }
- });
- bindEvent(newButton, "click", function () {
- _this.newRound();
- });
- let spinTheWheel = function () {
- wheel.spin(function (valueSpun) {
- if (isNaN(valueSpun)) {
- alert(valueSpun);
- } else {
- //is a valid number
- if (valueSpun === 0) {
- show('Bankrut', 'none');
- _this.currentCash = 0;
- } else {
- //spun greater than 0
- let amountFound = _this.createGuessPrompt(valueSpun);
- _this.currentCash += (valueSpun * amountFound);
- }
- _this.updateCash();
- }
- });
- };
- bindEvent(spinWheel, "click", spinTheWheel);
- bindEvent(wheel, "click", spinTheWheel);
- function arrays_equal(a, b) {
- return !(a < b || b < a);
- }
- bindEvent(solve, "click", function () {
- if (!_this.puzzleSolved) {
- show("Podaj rozwiązanie", 'block');
- //let solution = prompt('bla','');
- let solution = store;
- //console.log(solve)
- // console.log(solve2)
- if (solution) {
- guess = solution.toUpperCase().split("");
- if (arrays_equal(guess, _this.currentPuzzleArray)) {
- for (let i = 0; i < guess.length; ++i) {
- _this.guessLetter(guess[i], false, true);
- }
- }
- if (!_this.puzzleSolved) {
- show('Złe hasło, spróbuj ponownie. Pamietaj o znakach diakrytycznych(ą, ę, ć itd.)', 'none');
- }
- }
- }
- });
- this.startRound(0); //start the 1st round
- }
- wheelFortune.prototype.updateCash = function () {
- cash.innerHTML = this.currentCash;
- };
- wheelFortune.prototype.guessLetter = function (guess, isVowel, solvingPuzzle) {
- let timesFound = 0;
- solvingPuzzle = solvingPuzzle === undefined ? false : true;
- //find it:
- if (guess.length && !this.puzzleSolved) {
- if (!solvingPuzzle && !isVowel && (guess in vowels.toObject())) {
- show('Nie możesz zgadywać teraz samogłosek', 'none');
- return false;
- }
- if (!solvingPuzzle && isVowel && !(guess in vowels.toObject())) {
- show('Nie możesz zgadywać teraz spółgłosek', 'none');
- return false;
- }
- if (guess in this.guessedArray) {
- return 0;
- }
- for (let i = 0; i < this.currentPuzzleArray.length; ++i) {
- if (guess == this.currentPuzzleArray[i]) {
- wordDisplay.showLetter(i, guess);
- ++timesFound;
- }
- }
- if (timesFound > 0) {
- this.guessedArray.push(guess);
- if (this.guessedArray.length == this.lettersInPuzzle.length) {
- show('BRAWOOOO odgadłeś hasło 🎉🎉🎉', 'none');
- this.puzzleSolved = true;
- }
- }
- return timesFound;
- }
- return false;
- };
- let guessTimes = 0;
- wheelFortune.prototype.createGuessPrompt = function (valueSpun, isVowel) {
- isVowel = isVowel === undefined ? false : true;
- if (!this.puzzleSolved) {
- let letter;
- if (isVowel) {
- letter = prompt("Wpisz samogłoskę", "");
- } else {
- letter = prompt("Wylosowałeś " + valueSpun + " wpisz spółgłoskę", "");
- }
- if (letter) {
- let guess = letter.toUpperCase().charAt(0);
- let timesFound = this.guessLetter(guess, isVowel);
- if (timesFound === false) {
- ++guessTimes;
- if (guessTimes < 5) {
- return this.createGuessPrompt(valueSpun, isVowel);
- }
- }
- guessTimes = 0;
- return timesFound;
- } else {
- ++guessTimes;
- if (guessTimes < 5) {
- return this.createGuessPrompt(valueSpun, isVowel);
- } else {
- // reset guessTimes
- guessTimes = 0;
- }
- }
- }
- return false;
- };
- wheelFortune.prototype.newRound = function () {
- let round = ++this.round;
- if (round < this.puzzles.length) {
- this.startRound(round);
- } else {
- show('brak!', 'none');
- }
- };
- wheelFortune.prototype.startRound = function (round) {
- this.round = round;
- this.lettersInPuzzle = [];
- this.guessedArray = [];
- this.puzzleSolved = false;
- this.currentPuzzle = this.puzzles[this.round].toUpperCase();
- this.currentPuzzleArray = this.currentPuzzle.split("");
- for (let i = 0; i < this.currentPuzzleArray.length; i++) {
- if (!(this.currentPuzzleArray[i] in this.lettersInPuzzle)) {
- this.lettersInPuzzle.push(this.currentPuzzleArray[i]);
- }
- }
- wordDisplay = new WordDisplay(this.currentPuzzleArray);
- };
- return wheelFortune;
- })();
- let Game = new wheelFortune([
- "reforma walutowa","reforma monetarna","bank polski","katarzyna lewandowska","Borów","reforma systemu podatkowego","grabski",
- "halina brzezinska","książka idea polski","reforma rolna","Powązki Warszawskie","grabski","grabski"]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement