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.