markovChain
This commit is contained in:
122
sketches/GameOfLive.js
Normal file
122
sketches/GameOfLive.js
Normal file
@@ -0,0 +1,122 @@
|
||||
let entities = [];
|
||||
let entityWidth = 10;
|
||||
|
||||
function setup() {
|
||||
createCanvas(windowWidth, windowHeight);
|
||||
|
||||
fill(20,150,200)
|
||||
|
||||
// Glider
|
||||
entities.push({ x: 0, y: 0 });
|
||||
entities.push({ x: entityWidth, y: 0 });
|
||||
entities.push({ x: entityWidth * 2, y: 0 });
|
||||
entities.push({ x: 0, y: entityWidth });
|
||||
entities.push({ x: entityWidth, y: entityWidth * 2 });
|
||||
|
||||
entities.push({ x: entityWidth * 3, y: entityWidth * 4 });
|
||||
entities.push({ x: entityWidth * 4, y: entityWidth * 4 });
|
||||
entities.push({ x: entityWidth * 5, y: entityWidth * 4 });
|
||||
}
|
||||
|
||||
function draw() {
|
||||
background(255);
|
||||
drawEntities();
|
||||
runLife();
|
||||
summonRandomCubes(50, width);
|
||||
}
|
||||
|
||||
function drawEntities() {
|
||||
for (let cell of entities) {
|
||||
if (cell.x < -width/2 || cell.x > width/2 || cell.y < -height/2 || cell.y > height/2){
|
||||
continue;
|
||||
}
|
||||
square(width / 2 + cell.x, height / 2 + cell.y, entityWidth);
|
||||
}
|
||||
}
|
||||
|
||||
function runLife() {
|
||||
let next = [];
|
||||
|
||||
let candidates = [];
|
||||
for (let cell of entities) {
|
||||
if (!isInArray(candidates, cell.x, cell.y)) {
|
||||
candidates.push({ x: cell.x, y: cell.y });
|
||||
}
|
||||
for (let dx = -1; dx <= 1; dx++) {
|
||||
for (let dy = -1; dy <= 1; dy++) {
|
||||
if (dx === 0 && dy === 0) continue;
|
||||
let nx = cell.x + dx * entityWidth;
|
||||
let ny = cell.y + dy * entityWidth;
|
||||
if (!isInArray(candidates, nx, ny)) {
|
||||
candidates.push({ x: nx, y: ny });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Apply rules
|
||||
for (let cand of candidates) {
|
||||
let neighbors = 0;
|
||||
for (let cell of entities) {
|
||||
let dx = Math.abs(cand.x - cell.x);
|
||||
let dy = Math.abs(cand.y - cell.y);
|
||||
if ((dx === entityWidth && dy === 0) ||
|
||||
(dx === 0 && dy === entityWidth) ||
|
||||
(dx === entityWidth && dy === entityWidth)) {
|
||||
neighbors++;
|
||||
}
|
||||
}
|
||||
let isAlive = isInArray(entities, cand.x, cand.y);
|
||||
if (isAlive && (neighbors === 2 || neighbors === 3)) {
|
||||
next.push({ x: cand.x, y: cand.y });
|
||||
}
|
||||
if (!isAlive && neighbors === 3) {
|
||||
next.push({ x: cand.x, y: cand.y });
|
||||
}
|
||||
}
|
||||
entities = next;
|
||||
}
|
||||
|
||||
function isInArray(arr, x, y) {
|
||||
for (let item of arr) {
|
||||
if (item.x === x && item.y === y) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function mousePressed() {
|
||||
let newX = Math.round((mouseX - width / 2) / entityWidth) * entityWidth;
|
||||
let newY = Math.round((mouseY - height / 2) / entityWidth) * entityWidth;
|
||||
|
||||
let alreadyExists = false;
|
||||
for (let cell of entities) {
|
||||
if (cell.x === newX && cell.y === newY) {
|
||||
alreadyExists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!alreadyExists) {
|
||||
entities.push({ x: newX, y: newY });
|
||||
}
|
||||
}
|
||||
|
||||
function summonRandomCubes(amount, area){
|
||||
for (let i = 0; i < amount; i++){
|
||||
let newX = Math.round((random(-area, area) - width / 2) / entityWidth) * entityWidth;
|
||||
let newY = Math.round((random(-area, area) - height / 2) / entityWidth) * entityWidth;
|
||||
let alreadyExists = false;
|
||||
for (let cell of entities) {
|
||||
if (cell.x === newX && cell.y === newY) {
|
||||
alreadyExists = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!alreadyExists) {
|
||||
entities.push({ x: newX, y: newY });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function windowResized() {
|
||||
resizeCanvas(windowWidth, windowHeight);
|
||||
}
|
||||
@@ -4,28 +4,38 @@ let wordListPlain = [];
|
||||
let startWord = "";
|
||||
let finalText = "";
|
||||
let cleanedText = "";
|
||||
let wordToIndex = {};
|
||||
|
||||
async function setup() {
|
||||
trainingText = await loadStrings('assets/text/cpdv_text_only.txt');
|
||||
createCanvas(windowWidth, windowHeight)
|
||||
background(255, 255, 230)
|
||||
trainingText = await loadStrings('assets/text/TheSubspaceEmissaryWorldConquest.txt');
|
||||
createCanvas(windowWidth, windowHeight);
|
||||
background(255, 255, 230);
|
||||
|
||||
console.log(trainingText);
|
||||
trainingText = splitText(trainingText);
|
||||
console.log(trainingText);
|
||||
for (i = 0; i < trainingText.length; i++) {
|
||||
if (wordList.indexOf(trainingText[i]) == -1) {
|
||||
if (i % 1000 == 0) {
|
||||
console.log('i',i);
|
||||
}
|
||||
wordList.push([trainingText[i], []]);
|
||||
wordListPlain.push(trainingText[i]);
|
||||
for (j = i; j < trainingText.length; j++) {
|
||||
if (trainingText[j] == wordList[wordList.length - 1][0]) {
|
||||
if (trainingText[j + 1] != undefined){
|
||||
wordList[wordList.length - 1][1].push(trainingText[j + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
wordToIndex = {};
|
||||
wordList = [];
|
||||
wordListPlain = [];
|
||||
|
||||
for (let i = 0; i < trainingText.length; i++) {
|
||||
let word = trainingText[i];
|
||||
let nextWord = trainingText[i + 1];
|
||||
|
||||
if (wordToIndex[word] === undefined) {
|
||||
wordToIndex[word] = wordList.length;
|
||||
wordList.push([word, []]);
|
||||
wordListPlain.push(word);
|
||||
}
|
||||
|
||||
if (nextWord !== undefined) {
|
||||
let idx = wordToIndex[word];
|
||||
wordList[idx][1].push(nextWord);
|
||||
}
|
||||
|
||||
if (i % 1000 === 0) {
|
||||
console.log('i', i);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,53 +43,56 @@ async function setup() {
|
||||
startWord = wordList[round(random(0, wordList.length - 1))][0];
|
||||
finalText = startWord;
|
||||
|
||||
for (i = 0; i < 100; i++) {
|
||||
for (let i = 0; i < 100; i++) {
|
||||
startWord = generateNextWord(startWord);
|
||||
console.log(startWord)
|
||||
finalText += " " + startWord;
|
||||
console.log(startWord);
|
||||
if (startWord === '.' || startWord === ',' || startWord === ';' || startWord === ':' || startWord === '!' || startWord === '?') {
|
||||
finalText += startWord;
|
||||
} else {
|
||||
finalText += " " + startWord;
|
||||
}
|
||||
}
|
||||
|
||||
textSize(19)
|
||||
console.log(finalText)
|
||||
text(finalText, 10, height / 2)
|
||||
textSize(19);
|
||||
console.log(finalText);
|
||||
text(finalText, width / 2-400, height / 2, 800, height);
|
||||
}
|
||||
|
||||
function splitText(text) {
|
||||
for (i = 0; i < text.length; i++) {
|
||||
cleanedText = "";
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
cleanedText += " " + text[i];
|
||||
}
|
||||
cleanedText = cleanedText
|
||||
.replaceAll(".", " . ")
|
||||
.replaceAll("!", " ! ")
|
||||
.replaceAll("?", " ? ")
|
||||
.replaceAll(",", " , ")
|
||||
.replaceAll(";", " ; ")
|
||||
.replaceAll(":", " : ")
|
||||
.replaceAll('"', ' " ')
|
||||
.replaceAll("'", " ' ")
|
||||
.replaceAll(" "," ")
|
||||
.replaceAll(" "," ")
|
||||
.split(" ");
|
||||
.replaceAll(".", " . ")
|
||||
.replaceAll("!", " ! ")
|
||||
.replaceAll("?", " ? ")
|
||||
.replaceAll(",", " , ")
|
||||
.replaceAll(";", " ; ")
|
||||
.replaceAll(":", " : ")
|
||||
.replaceAll('"', ' " ')
|
||||
.replaceAll("'", " ' ")
|
||||
.replaceAll("`", " ` ")
|
||||
.replaceAll("´", " ´ ")
|
||||
.replaceAll(" ", " ")
|
||||
.replaceAll(" ", " ")
|
||||
.split(" ");
|
||||
return cleanedText;
|
||||
}
|
||||
|
||||
function generateNextWord(currentWord) {
|
||||
let currentWordIndex = getWordIndex(currentWord);
|
||||
let nextWordIndex = round(
|
||||
random(
|
||||
0, wordList[currentWordIndex][1].length - 1
|
||||
)
|
||||
);
|
||||
//retun . if end of text
|
||||
if (wordList[currentWordIndex][1][nextWordIndex] == undefined){
|
||||
// If no possible next words, return "."
|
||||
if (wordList[currentWordIndex][1].length === 0) {
|
||||
return '.';
|
||||
}
|
||||
return wordList[currentWordIndex][1][nextWordIndex];
|
||||
|
||||
let nextWordIndex = floor(random(0, wordList[currentWordIndex][1].length));
|
||||
let nextWord = wordList[currentWordIndex][1][nextWordIndex];
|
||||
return nextWord !== undefined ? nextWord : '.';
|
||||
}
|
||||
|
||||
function getWordIndex(word) {
|
||||
return wordListPlain.indexOf(word);
|
||||
return wordToIndex[word];
|
||||
}
|
||||
|
||||
function draw() {
|
||||
|
||||
Reference in New Issue
Block a user