Created
May 23, 2017 15:22
-
-
Save llSourcell/9cf319d7d3ddd1e462206b66fc4d40ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Define 10x20 grid as the board | |
| var grid = [ | |
| [0,0,0,0,0,0,0,0,0,0], | |
| [0,0,0,0,0,0,0,0,0,0], | |
| [0,0,0,0,0,0,0,0,0,0], | |
| [0,0,0,0,0,0,0,0,0,0], | |
| [0,0,0,0,0,0,0,0,0,0], | |
| [0,0,0,0,0,0,0,0,0,0], | |
| [0,0,0,0,0,0,0,0,0,0], | |
| [0,0,0,0,0,0,0,0,0,0], | |
| [0,0,0,0,0,0,0,0,0,0], | |
| [0,0,0,0,0,0,0,0,0,0], | |
| [0,0,0,0,0,0,0,0,0,0], | |
| [0,0,0,0,0,0,0,0,0,0], | |
| [0,0,0,0,0,0,0,0,0,0], | |
| [0,0,0,0,0,0,0,0,0,0], | |
| [0,0,0,0,0,0,0,0,0,0], | |
| [0,0,0,0,0,0,0,0,0,0], | |
| [0,0,0,0,0,0,0,0,0,0], | |
| [0,0,0,0,0,0,0,0,0,0], | |
| [0,0,0,0,0,0,0,0,0,0], | |
| [0,0,0,0,0,0,0,0,0,0], | |
| ]; | |
| //Block shapes | |
| var shapes = { | |
| I: [[0,0,0,0], [1,1,1,1], [0,0,0,0], [0,0,0,0]], | |
| J: [[2,0,0], [2,2,2], [0,0,0]], | |
| L: [[0,0,3], [3,3,3], [0,0,0]], | |
| O: [[4,4], [4,4]], | |
| S: [[0,5,5], [5,5,0], [0,0,0]], | |
| T: [[0,6,0], [6,6,6], [0,0,0]], | |
| Z: [[7,7,0], [0,7,7], [0,0,0]] | |
| }; | |
| //Block colors | |
| var colors = ["F92338", "C973FF", "1C76BC", "FEE356", "53D504", "36E0FF", "F8931D"]; | |
| //Used to help create a seeded generated random number for choosing shapes. makes results deterministic (reproducible) for debugging | |
| var rndSeed = 1; | |
| //BLOCK SHAPES | |
| //coordinates and shape parameter of current block we can update | |
| var currentShape = {x: 0, y: 0, shape: undefined}; | |
| //store shape of upcoming block | |
| var upcomingShape; | |
| //stores shapes | |
| var bag = []; | |
| //index for shapes in the bag | |
| var bagIndex = 0; | |
| //GAME VALUES | |
| //Game score | |
| var score = 0; | |
| // game speed | |
| var speed = 500; | |
| // boolean for changing game speed | |
| var changeSpeed = false; | |
| //for storing current state, we can load later | |
| var saveState; | |
| //stores current game state | |
| var roundState; | |
| //list of available game speeds | |
| var speeds = [500,100,1,0]; | |
| //inded in game speed array | |
| var speedIndex = 0; | |
| //turn ai on or off | |
| var ai = true; | |
| //drawing game vs updating algorithms | |
| var draw = true; | |
| //how many so far? | |
| var movesTaken = 0; | |
| //max number of moves allowed in a generation | |
| var moveLimit = 500; | |
| //consists of move the 7 move parameters | |
| var moveAlgorithm = {}; | |
| //set to highest rate move | |
| var inspectMoveSelection = false; | |
| //GENETIC ALGORITHM VALUES | |
| //stores number of genomes, init at 50 | |
| var populationSize = 50; | |
| //stores genomes | |
| var genomes = []; | |
| //index of current genome in genomes array | |
| var currentGenome = -1; | |
| //generation number | |
| var generation = 0; | |
| //stores values for a generation | |
| var archive = { | |
| populationSize: 0, | |
| currentGeneration: 0, | |
| elites: [], | |
| genomes: [] | |
| }; | |
| //rate of mutation | |
| var mutationRate = 0.05; | |
| //helps calculate mutation | |
| var mutationStep = 0.2; | |
| //main function, called on load | |
| function initialize() { | |
| //init pop size | |
| archive.populationSize = populationSize; | |
| //get the next available shape from the bag | |
| nextShape(); | |
| //applies the shape to the grid | |
| applyShape(); | |
| //set both save state and current state from the game | |
| saveState = getState(); | |
| roundState = getState(); | |
| //create an initial population of genomes | |
| createInitialPopulation(); | |
| //the game loop | |
| var loop = function(){ | |
| //boolean for changing game speed | |
| if (changeSpeed) { | |
| //restart the clock | |
| //stop time | |
| clearInterval(interval); | |
| //set time, like a digital watch | |
| interval = setInterval(loop, speed); | |
| //and don't change it | |
| changeInterval = false; | |
| } | |
| if (speed === 0) { | |
| //no need to draw on screen elements | |
| draw = false; | |
| //updates the game (update fitness, make a move, evaluate next move) | |
| update(); | |
| update(); | |
| update(); | |
| } else { | |
| //draw the elements | |
| draw = true; | |
| } | |
| //update regardless | |
| update(); | |
| if (speed === 0) { | |
| //now draw elements | |
| draw = true; | |
| //now update the score | |
| updateScore(); | |
| } | |
| }; | |
| //timer interval | |
| var interval = setInterval(loop, speed); | |
| } | |
| document.onLoad = initialize(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment