简体   繁体   中英

min and max functions with objects

Dear everyone this is my first post here and I've only recently started trying to code at all so please be kind.

I'm currently doing a course and on that course they have given us tasks to do. I'm having a problem with my current task.

I will post the whole code below with my current attempt. I feel like I've tried this in every logical way but it keeps coming back as failed.

Any help at all would be so, so appreciated.

/*

Officer: 1585754
CaseNum: 303-2-49385492-1585754

Case 303 - The Case of the Crooked Attorney
Stage 3 - The Gates Bank

I’ve made an appointment for you at the Gates Bank to retrieve your safe deposit box from the vault.
Actually you will break into Torvalds’ one.

Crack the safe by doing the following:

    Whilst the mouse is moving:
    - Make CrypticSafeCombination_0 equal to the value of mouseY
    - Use the 'min' function to prevent CrypticSafeCombination_0 from going above 11

    Whilst the mouse is moving:
    - Decrement CrypticSafeCombination_1 by 2
    - Use the 'max' function to prevent CrypticSafeCombination_1 from falling below 4

    Whilst the mouse is being dragged:
    - Make CrypticSafeCombination_2 equal to the value of mouseX
    - Use the 'max' function to prevent CrypticSafeCombination_2 from falling below 2

    Whilst the mouse is moving:
    - Decrement CrypticSafeCombination_3 by 1
    - Use the 'max' function to prevent CrypticSafeCombination_3 from falling below 3

    Whilst the mouse is moving:
    - Make CrypticSafeCombination_4 equal to the value of mouseX
    - Use the 'max' function to prevent CrypticSafeCombination_4 from falling below 13



This time you'll need to create the relevant event handlers yourself.

There are many possible ways of investigating this case, but you
should use ONLY the following commands:

    - The assignment operator aka. the equals sign !
    - mouseX, mouseY
    - Incrementing +=
    - Decrementing -=
    - min, max
    - constrain

*/

//declare the variables

var CrypticSafeCombination_0;
var CrypticSafeCombination_1;
var CrypticSafeCombination_2;
var CrypticSafeCombination_3;
var CrypticSafeCombination_4;


function preload()
{
    //IMAGES WILL BE LOADED HERE

}

function setup()
{
    createCanvas(512,512);

    //initialise the variables
    CrypticSafeCombination_0 = 0;
    CrypticSafeCombination_1 = 0;
    CrypticSafeCombination_2 = 0;
    CrypticSafeCombination_3 = 0;
    CrypticSafeCombination_4 = 0;

}

///////////////////EVENT HANDLERS///////////////////

//Create event handlers here to open the safe ...

**This is my current attempt**

function mouseMoved()
{

    CrypticSafeCombination_0 = mouseY;
    min = (mouseY, 11);

    CrypticSafeCombination_1 -= 2;
    max = (2, 4);

    CrypticSafeCombination_3 -=1;
    max = (1, 3);

    CrypticSafeCombination_4 = mouseX;
    max = (mouseX, 13);
}

function mouseDragged()
{

    CrypticSafeCombination_2 = mouseX;
    max = (mouseX, 2);
}


///////////////DO NOT CHANGE CODE BELOW THIS POINT///////////////////

function draw()
{

    //Draw the safe door
    background(70);
    noStroke();
    fill(29,110,6);
    rect(26,26,width-52,width-52);

    //Draw the combination dials
    push();
    translate(120,170);
    drawDial(140,CrypticSafeCombination_0, 16);
    pop();

    push();
    translate(120,380);
    drawDial(140,CrypticSafeCombination_1, 23);
    pop();

    push();
    translate(280,170);
    drawDial(140,CrypticSafeCombination_2, 19);
    pop();

    push();
    translate(280,380);
    drawDial(140,CrypticSafeCombination_3, 15);
    pop();

    //Draw the lever
    push();
    translate(width - 125,256);
    drawLever(CrypticSafeCombination_4);
    pop();


}

function drawDial(diameter,num,maxNum)
{
    //the combination lock

    var r = diameter * 0.5;
    var p = r * 0.6;

    stroke(0);
    fill(255,255,200);
    ellipse(0,0,diameter,diameter);
    fill(100);
    noStroke();
    ellipse(0,0,diameter*0.66,diameter*0.66);
    fill(150,0,0);
    triangle(
        -p * 0.4,-r-p,
        p * 0.4,-r-p,
        0,-r-p/5
    );

    noStroke();

    push();
    var inc = 360/maxNum;

    rotate(radians(-num * inc));
    for(var i = 0; i < maxNum; i++)
    {
        push();
        rotate(radians(i * inc));
        stroke(0);
        line(0,-r*0.66,0,-(r-10));
        noStroke();
        fill(0);
        text(i,0,-(r-10));
        pop();
    }

    pop();
}

function drawLever(rot)
{
    push();
    rotate(radians(-rot))
    stroke(0);
    fill(100);
    rect(-10,0,20,100);
    ellipse(0,0,50,50);
    ellipse(0,100,35,35);
    pop();
}

You've misunderstood how to use function s. min() and max() are functions which receive 2 arguments and return 1 argument.

eg x = min(a, b) pass the arguments a and b to the function min() . min() finds the minimum of a and b and returns it. The return value is assigned to x .

eg y = max(y, c) finds the maximum of y and c and assigns it to y .

Apply that to your code:

function mouseMoved()
{ 
    // Make CrypticSafeCombination_0 equal to the value of mouseY  
    CrypticSafeCombination_0 = mouseY;
    // Use the 'min' function to prevent CrypticSafeCombination_0 from going above 11
    CrypticSafeCombination_0 = min(CrypticSafeCombination_0, 11);

    // Decrement CrypticSafeCombination_1 by 2
    CrypticSafeCombination_1 -= 2;
    // Use the 'max' function to prevent CrypticSafeCombination_1 from falling below 4
    CrypticSafeCombination_1 = max(CrypticSafeCombination_1, 4);

    // Decrement CrypticSafeCombination_3 by 1
    CrypticSafeCombination_3 -=1;
    //Use the 'max' function to prevent CrypticSafeCombination_3 from falling below 3
    CrypticSafeCombination_3 = max(CrypticSafeCombination_3, 3);

    //Make CrypticSafeCombination_4 equal to the value of mouseX
    CrypticSafeCombination_4 = mouseX;
    // Use the 'max' function to prevent CrypticSafeCombination_4 from falling below 13
    CrypticSafeCombination_4 = max(CrypticSafeCombination_4, 13);
}

function mouseDragged()
{
    // Make CrypticSafeCombination_2 equal to the value of mouseX
    CrypticSafeCombination_2 = mouseX;
    // Use the 'max' function to prevent CrypticSafeCombination_2 from falling below 2
    CrypticSafeCombination_2 = max(CrypticSafeCombination_2, 2);
}

or

function mouseMoved()
{ 
    // Make CrypticSafeCombination_0 equal to the value of mouseY  
    // Use the 'min' function to prevent CrypticSafeCombination_0 from going above 11
    CrypticSafeCombination_0 = min(mouseY, 11);

    // Decrement CrypticSafeCombination_1 by 2
    // Use the 'max' function to prevent CrypticSafeCombination_1 from falling below 4
    CrypticSafeCombination_1 = max(CrypticSafeCombination_1-2, 4);

    // Decrement CrypticSafeCombination_3 by 1
    //Use the 'max' function to prevent CrypticSafeCombination_3 from falling below 3
    CrypticSafeCombination_3 = max(CrypticSafeCombination_3-1, 3);

    // Make CrypticSafeCombination_4 equal to the value of mouseX
    // Use the 'max' function to prevent CrypticSafeCombination_4 from falling below 13
    CrypticSafeCombination_4 = max(mouseX, 13);
}

function mouseDragged()
{
    // Make CrypticSafeCombination_2 equal to the value of mouseX
    // Use the 'max' function to prevent CrypticSafeCombination_2 from falling below 2
    CrypticSafeCombination_2 = max(mouseX, 2);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM