samedi 7 novembre 2020

How to have the same random results

Im trying to write a program that generate random pairs(without repetition), it would be used for my family to shuffle who would get who for giving him/her Christmas gift :) I want to have one, random result and send it to 6 different people, however, whenever someone is loading a page, it randomize results. I want to be able to shuffle it once, and then send the link for everyone to see their pair. How can i separate randomizing from reloading a code?

my code so far (im starting learning .js)

let irena
let jola;
let maciek;
let marta;
let ula;
let lukasz;

let jola_2
let maciek_2
let marta_2
let irena_2
let ula_2
let lukasz_2

var names = ["lukasz", "jola", "ula", "maciek", "marta", "irena"]

var guziki = []
var obrazki = []

var arr1 = names.slice(),
  arr2 = names.slice(); 



function preload(){
  irena = loadImage('irena.png');
  jola = loadImage('jola.png');
  maciek = loadImage('maciek.png');
  marta = loadImage('marta.png');
  ula = loadImage('ula.png');
  lukasz = loadImage('lukasz.png');  
}


arr1.sort(function() {
  return 0.5 - Math.random();
});

arr2.sort(function() {
  return 0.5 - Math.random();
});


while (arr1.length) {
  var name1 = arr1.pop(),
    name2 = arr2[0] == name1 ? arr2.pop() : arr2.shift();
  guziki.push(name1)
  obrazki.push(name2)
}

//szukanie gdzie kto
const found_irena = guziki.findIndex(element => element === "irena")
const found_jola = guziki.findIndex(element => element === "jola")
const found_maciek = guziki.findIndex(element => element === "maciek")
const found_marta = guziki.findIndex(element => element === "marta")
const found_ula = guziki.findIndex(element => element === "ula")
const found_lukasz = guziki.findIndex(element => element === "lukasz")


function linia(){
  //irena
  line(140,70,280,70); //prosta
  line(200,50,280,70); //strzalka gorna
  line(200,90,280,70); //strzalka dolna
  //jola
  line(140,170,280,170);
  line(200,150,280,170);
  line(200,190,280,170);
  //maciek
   line(140,270,280,270);
  line(200,250,280,270);
  line(200,290,280,270);
  //marta
   line(140,370,280,370);
  line(200,350,280,370);
  line(200,390,280,370);
  //ula
   line(140,470,280,470);
  line(200,450,280,470);
  line(200,490,280,470);
  //lukasz
   line(140,570,280,570);
  line(200,550,280,570);
  line(200,590,280,570);
}


function setup() {
  createCanvas(400, 800);
}

function draw() {
  background(200,200,0);
  linia();
  image(irena,20,20,irena.width/10,irena.height/10);
  image(jola,20,120,jola.width/10,jola.height/10);
  image(maciek,20,220,maciek.width/11,maciek.height/11);
  image(marta,20,320,marta.width/12,marta.height/12);
  image(ula,20,420,ula.width/10,ula.height/10);
  image(lukasz,20,520,lukasz.width/10,lukasz.height/10);
  if (irena_2 >0){
    textSize(30)
    text(obrazki[found_irena], 300, 75);
    fill(40,100,100);
  } else if(jola_2 >0){
    textSize(30)
    text(obrazki[found_jola], 300, 175);
    fill(40,100,100);
  }else if(maciek_2 >0){
    textSize(30)
    text(obrazki[found_maciek], 300, 275);
    fill(40,100,100);
  }
  else if(marta_2 >0){
    textSize(30)
    text(obrazki[found_marta], 300, 375);
    fill(40,100,100);
  }
  else if(ula_2 >0){
    textSize(30)
    text(obrazki[found_ula], 300, 475);
    fill(40,100,100);
  }
  else if(lukasz_2 >0){
    textSize(30)
    text(obrazki[found_lukasz], 300, 575);
    fill(40,100,100);
  }
    
}

function mousePressed() {
  if (mouseY >20 && mouseY<120 && mouseX > 20 && mouseX<120) {
    console.log("kliknales irenke" + "a ona wylosowala ");
    irena_2 =1;
  } else if (mouseY >120 && mouseY<220 && mouseX > 20 && mouseX<120){
    console.log("kliknales jolke a ona wylosowala ")
    jola_2 =1;
  } else if (mouseY >220 && mouseY<320 && mouseX > 20 && mouseX<120){
    console.log("kliknales macka a on wylosowal")
    maciek_2 =1;
  } else if (mouseY >320 && mouseY<420 && mouseX > 20 && mouseX<120){
    console.log("kliknales marte")
    marta_2=1;
  } else if (mouseY >420 && mouseY<520 && mouseX > 20 && mouseX<120){
    console.log("kliknales ule")
    ula_2= 1;
  } else if (mouseY >520 && mouseY<620 && mouseX > 20 && mouseX<120){
    console.log("kliknales lukaszka")
    lukasz_2 =1;
  }
  
}

thank you in advance, Lukasz




Aucun commentaire:

Enregistrer un commentaire