Showing posts with label balance scale. Show all posts
Showing posts with label balance scale. Show all posts

Sunday, May 14, 2023

HP Prime: Drawing a Balance Scale and an Updated High Low Game

HP Prime:   Drawing a Balance Scale and an Updated High Low Game



Introduction


The program DSCALE draws a balance scale given two weights:  one placed on the left side (A) and one on the right side.  The length of the bar, which is measured from the center to one of the weights can be set by the user.   Assume that the length is equal on both sides.


The scale in this program is not meant to bend more than 45°.   The program will not draw the scale if the bend is greater than 45°.  


The program is made to simulate balancing the scales.   


Note that the HP Prime PPL  (Prime Programming Language) does not have goto and label commands.  To allow for the user to draw more than one scale, a while loop and a seeding value are used.



HP Prime Program:  dscale


EXPORT dscale()

BEGIN

STARTAPP("Function");

HAngle:=1;  // degrees

W:=4;

K:=1;

INPUT(L,"Balance Scale","L:",

"Length-Center");

 

  

WHILE K==1 DO

INPUT({A,B},"Balance-Scale",

{"<-","->"},{"Left Weight","Right

Weight"});

D:=(B-A)/2;

θ:=ABS(ATAN((B-A)/L));

S:=W*(B-A)/L;

IF θ>45 THEN

MSGBOX("CANNOT DRAW");

CONTINUE;

END;

Function.Xmin:=−W-1;

Function.Xmax:=W+1;

Function.Xtick:=1;

Function.Ymin:=−W-1;

Function.Ymax:=W+1;

Function.Ytick:=1;

RECT(#FFFFFFh);

LINE(0,W,0,−W,#DAA520h);

LINE(−.5,W,.5,W,#FF8000h);

LINE(−1,−W,1,−W,#FF8000h);

LINE(−.5,W,−W,S,#AAAAAAh);

LINE(.5,W,W,−S,#AAAAAAh);

LINE(−W,S,W,−S,0);

WAIT(0);


CHOOSE(K,"Again?","Yes","No"); 

END;

STARTVIEW(−1);

 

END;






The Hi-Low Game Comes to the HP Prime



The program HILODS is the classic high-low guessing game, but instead of text, it draws a balance scale to indicate whether you are higher and lower from the target number.  You work with the left weight (A), with the goal of balancing with the right weight (B).  The right weight is set with a random integer from 10 to 99.  


The game has three difficulty levels:  easy (25 guesses), medium (15 guesses), and hard (10 guesses).






HP Prime Program:  hilods


lose()

BEGIN

STARTVIEW(−1);

MSGBOX("The number is "+STRING(B)+".\n

"+STRING(ABS(A-B))+" away.\n

Better luck next time.");

// backslash n: new line

END;


win() 

BEGIN

STARTVIEW(−1);

MSGBOX("You win! \n

# Guesses: "+STRING(N-C));

END;

 

EXPORT hilods()

BEGIN

// in progress

 

  

STARTAPP("Function");

HAngle:=1;  // degrees

W:=4;

L:=100;

B:=RANDINT(10,99);

Function.Xmin:=−W-1;

Function.Xmax:=W+1;

Function.Xtick:=1;

Function.Ymin:=−W-1;

Function.Ymax:=W+1;

Function.Ytick:=1;

CHOOSE(I,"Different Levels",

"Easy","Medium","Hard");

C:=2.5*I^2-17.5*I+40;

N:=C;

A:=0; // starting seed


WHILE A≠B DO

INPUT(A,

"Guesses left: "+STRING(C),

"Guess? ",

"From 10 to 99");

D:=(B-A)/2;

S:=W*(B-A)/L;

RECT(#FFFFFFh);

LINE(0,W,0,−W,#DAA520h);

LINE(−.5,W,.5,W,#FF8000h);

LINE(−1,−W,1,−W,#FF8000h);

LINE(−.5,W,−W,S,#AAAAAAh);

LINE(.5,W,W,−S,#AAAAAAh);

LINE(−W,S,W,−S,0);

WAIT(0);

C:=C-1;

IF A==B THEN

win();

END;

IF C==0 THEN

lose(); B:=A; // to exit loop

END;

// while end

END;

// main end

END;





This a slightly different approach to the same program than I gave last week.  


Eddie 



All original content copyright, © 2011-2023.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Sunday, May 7, 2023

TI-84 Plus CE: Drawing a Balance Scale and an Updated High Low Game

TI-84 Plus CE:   Drawing a Balance Scale and an Updated High Low Game



Drawing a Scale



The program DSCALE draws a balance scale given two weights:  one placed on the left side (A) and one on the right side.  The length of the bar, which is measured from the center to one of the weights can be set by the user.   Assume that the length is equal on both sides.





The scale in this program is not meant to bend more than 45°.   The program will not draw the scale if the bend is greater than 45°.  


DSCALE was programmed with a TI-84 Plus CE, but can be programmed on a monochrome calculator with the color commands taken out.


The program repeats for different weights at the user's choice.  



TI-84 Plus CE Program:   DSCALE



Degree:Func:FnOff 

Disp "BALANCE SCALE"

Disp "LENGTH-CENTER?"

Prompt L

4→W

Lbl 1

ClrHome

Disp "LENGTH-CENTER:",L,""

Input "LEFT WEIGHT? ",A

Input "RIGHT WEIGHT? ",B

(B-A)/2→D

abs(tan^-1((B-A)/L))→θ

W*(B-A)/L→S

If θ>45:Then:Disp "CANNOT DRAW":Goto 2:End

­-W-1→Xmin:W+1→Xmax:1→Xscl

­-W-1→Ymin:W+1→Ymax:1→Yscl

AxesOff:LabelOff

ClrDraw

Line(0,W,0,-­W,ORANGE)

Line(-­.5,W,.5,W,BROWN)

Line(­-1,­-W,1,­-W,BROWN)

Line(­-.5,W,-­W,S,LTGRAY)

Line(.5,W,W,­-S,LTGRAY)

Line(­-W,S,W,­-S,BLACK)

Lbl 2

Pause 

Menu("AGAIN?","YES",1,"NO",0)

Lbl 0

Disp "THANK YOU"







Updating a Game



The program HILODS is the classic high-low guessing game, but instead of text, it draws a balance scale to indicate whether you are higher and lower from the target number.  You work with the left weight (A), with the goal of balancing with the right weight (B).  The right weight is set with a random integer from 10 to 99.  


The game has three difficulty levels:  easy (25 guesses), medium (15 guesses), and hard (10 guesses).



TI-84 Plus CE Program:   HILODS


Degree:Func:FnOff 

Disp "HIGH-LOW GAME"

AxesOff:LabelOff

100→L

randInt(10,99)→B

4→W

­-W-1→Xmin:W+1→Xmax:1→Xscl

­-W-1→Ymin:W+1→Ymax:1→Yscl

Menu("DIFFICULTY LEVEL","EASY",10,"MEDIUM",11,"HARD",12)

Lbl 10:25→C:Goto 20

Lbl 11:15→C:Goto 20

Lbl 12:10→C:Goto 20

Lbl 20

Disp toString(C)+" GUESSES"

Disp "BETWEEN 10 AND 99"

C→N


Lbl 1

ClrHome

Disp "GUESSES LEFT: "+toString(C)

Disp "{10,99}"

Input "GUESS? ",A

(B-A)/2→D

W*(B-A)/L→S

ClrDraw

Line(0,W,0,-­W,ORANGE)

Line(-­.5,W,.5,W,BROWN)

Line(­-1,­-W,1,­-W,BROWN)

Line(­-.5,W,-­W,S,LTGRAY)

Line(.5,W,W,­-S,LTGRAY)

Line(­-W,S,W,­-S,BLACK)

Pause 

C-1→C

If A=B:Goto 9

If C=0:Goto 8

Goto 1


Lbl 8

ClrHome

Disp "THE NUMBER IS "+toString(B)

Disp toString(abs(A-B))+" AWAY"

Disp "BETTER LUCK NEXT TIME"

Stop



Lbl 9

ClrHome

Disp "THE NUMBER IS "+toString(A)

Disp "YOU WIN!"

Disp "NUMBER OF GUESSES: "+toString(N-C)

Stop







Download both programs here (as a zip file):  https://drive.google.com/file/d/1hJwyqSZJ-aCQ36Wh2cCfMVy6BpAzmmPZ/view?usp=share_link



Enjoy,


Eddie 


All original content copyright, © 2011-2023.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


HP 32SII: Volume of a Square Frustum (Three Approaches in RPN)

HP 32SII: Volume of a Square Frustum (Three Approaches in RPN ) The volume of a square frustum is: v = height ÷ ...