 |
|
|
|
|
 |
|
|
Guest
|
Printing variables? -
05-01-2008, 01:47 PM
I have been playing with Alice all semester, and have only done the examples in the book, sticking to the limitations of the assignment. I have a question for my final project, which is due Tuesday.
This project is based upon a simple Java program I wrote, that emulates the combat stage of a RPG (role-playing game), with turn based combat based upon the user's input. Now, I have the variables "playerHP", "playerMP", and "enemyHP", standing for the player's hit points (health), the player's mana points (magic power), and the enemy's hit points (health) respectively.
I want them to show on the screen the entire duration of the program, so that the user can see the variables change through each pass of the while loop. My Java program is written something like this (not typing all Java, this is just an outline)...
int playerHP = 5
int playerMP = 3
int enemyHP = 5
while (playerHP != 0 && enemyHP != 0)
{print (playerHP,playerMP,enemyHP);
print "what do you want to do?"
if click icon1, then attack { roll a random number (1-6)
if odd, enemyHP - 1}
if click icon2, cast magic spell (playerMP - 1) { roll a random number (1-6)
if odd, enemyHP - 2}
else click icon3, cast heal spell (playerHP + 3; playerMP - 1)
if enemyHP == 0
{print "you win!"}
if enemyHP != 0
{print "enemy's turn"
roll a random number (1-6)
if odd, playerHP - 1}
if playerHP == 0
{print "you have died"}
} I know it is messy, so if you have any questions, feel free to ask. Any help in getting the variables to constantly appear in Alice, whether in a 3D object or in the debug window, is greatly appreciated! Thanks!
Last edited by Nuinethir; 05-02-2008 at 09:33 AM.
|
|
|
|
|
Guest
|

05-02-2008, 09:29 AM

I am very surprised no one has answered this. I am at my wit's end, and am ready to chuck the project I have been working on for over a month. Isn't there anyone that knows how to print a variable to the debug window? Please help!
(In the educational book "An Introduction to Programming Using Alice" by Charles W. Herbert, on page 169, figure 6-17 clearly shows variable data being printed to the debug window. In the chapter summary, they reiterate that the "print" function sends variables to that same debug window. However, they don't explain how it is done, or how the code should be entered, but only that it CAN be done. Now tell me, how is this supposed to be educational?)
|
|
|
|
|
Guest
|

05-02-2008, 09:51 AM
Quote:
Originally Posted by Nuinethir

I am very surprised no one has answered this. I am at my wit's end, and am ready to chuck the project I have been working on for over a month. Isn't there anyone that knows how to print a variable to the debug window? Please help!
(In the educational book "An Introduction to Programming Using Alice" by Charles W. Herbert, on page 169, figure 6-17 clearly shows variable data being printed to the debug window. In the chapter summary, they reiterate that the "print" function sends variables to that same debug window. However, they don't explain how it is done, or how the code should be entered, but only that it CAN be done. Now tell me, how is this supposed to be educational?) 
|
I have always been of the impression that the print tile on the bottom of the screen causes material to be printed in a white area at the bottom of the running world. Nothing special is required to do that (see http://www.dickbaldwin.com/alice/Alice0135.htm#Figure_4 and http://www.dickbaldwin.com/alice/Alice0195.htm)
I do recall something about being able to create a "watch box" somewhere else on the screen, but I have never been able to get the debug watch box to work reliably under Windows XP. (see http://www.alice.org/community/archi...php/t-620.html)
Dick Baldwin
Free Alice tutorials: http://www.dickbaldwin.com/tocalice.htm
Free programming tutorials: http://www.dickbaldwin.com/toc.htm
Last edited by DickBaldwin; 05-02-2008 at 09:55 AM.
Reason: Added a reference
|
|
|
|
|
Guest
|

05-02-2008, 10:48 AM
I must be missing something here, but in the textbook that you cite there are indeed instructions for printing variables. Your post refered to pg 169 from the book. Turn the page, you should now be on page 170, and midway on the page is a paragraph beginning "Another useful debugging technique is to display the value of a variable or parameter while the world is running." If you continue to read you will see that he asks you to open the triple jump loop 2.a2w world and then he walks you through how to display variables using the print instruction.
Basically you drag the print instruction into the Editor and drop it where you need it. When the print menu appears you select expressions and then the variable you wish to print.
You may have to create your variable at the world level (i.e., a public variable) in order to have access to it throughout your entire program.
I hope this helps, and good luck.
Last edited by lanceA; 05-02-2008 at 10:52 AM.
Reason: typo
|
|
|
|
|
Guest
|

05-02-2008, 05:49 PM
My apologies, I should have been more specific. If you look at the picture closely, it shows formatted text, saying the variable's name, and the variable's value after an equals sign. But when I try it, instead it shows it in the literal sense...
the value of DuckPrince is DuckPrince
the value of world.my first method.duckHP is 10.0 (double, not int)
How can I get it to print like this...
DuckPrince's HP = 10
DuckPrince's MP = 3
Zombie's HP = 10 I want it to show an integer value, not a double value, so that all numbers are whole numbers without decimals.
If there is a way I can throw a print command, and type it out, using whatever formatting Alice uses, I would be a happy camper. In Java, all I would need was this:
int duckHP = 10
System.out.println("DuckPrince's HP = " + duckHP);
|
|
|
|
|
Guest
|

05-02-2008, 07:34 PM
Not a problem. Most of us forget that we need to go into 'great' detail when trying to explain to another person our programming issue.
Anyway, Thank you for providing the additional information concerning your problem.
Refer back to page 169 of the book. It appears that Mr. Herbert possibly wrote a method which his print statement calls. (The "print" instruction always displays at the bottom of your screen.) He appears to use this as a way to display the information depicted in the Figure.
Looking at Figure 6-17 it appears as though he is concatenating two Strings within his method to print the information. This in itself is not easy because in Alice he has to cast an Integer to a String before he can 'join' it and then print the String. (Remember, Alice is not pure Java 6.0- you still have to 'cast') This requires familiarity with several of Alice's functions and the use of 'place holders' when constructing statements. Are you familiar with this process of "place holders" when constructing statements in Alice?
The bottom line is that it can be done, however it requires some thought before proceeding.
I suggest you test my theory by first writing a method and testing it using the Print instruction. If it shows in your "Debug" window then proceed accordingly.
Good luck.
Last edited by lanceA; 05-02-2008 at 08:31 PM.
Reason: to add additional comments
|
|
|
|
|
Guest
|

05-03-2008, 05:07 AM
Hello again.
I decided to check over at Dick Baldwin's website again, with one of the links he gave me, directing me to programming tutorial 0135a, showing the source code itself. It was all written with french braces, so I was dumbfounded. "How do you get that inside Alice?!" I asked myself.
But this time, I found a download link on the page, and checked out the file. Inside, there are no french braces or such, so I don't know why he shows them there? (shrug)
At any rate, had I read the comments more closely, I would have seen the instructions for getting it to work as I intend. I saw the code itself, and through seeing what he used, I now have a better understanding, and have gotten it to print properly. Yay!
Step by step, this is what I did:
1. Added print, typed in "DuckPrince's HP: ".
2. Added world's string function "a joined with b", choosing "dummy" as a temporary placeholder for b.
3. Added world's string function "what as a string" on top of the "dummy" placeholder.
4. Added DuckPrince's HP variable "duckHP" to "what" as a string.
Executed, I get this:
DuckPrince's HP: 10.0
I would have liked for it to return a integer value and not a double value, but at this point I could care less. I know how to translate data types in Java, but I have plenty of things on my plate as is. (I am working on an Associates in Systems & Network Administration with a Cisco Systems certification, but I am very interested in software development, more specifically, game design, so I might change my course as my experience grows.)
DickBaldwin, lanceA: I have always felt that forums are the best place to discuss matters such as these, and wanted to let you know I really appreciate your assistance. Thank you for all your help!
Last edited by Nuinethir; 05-03-2008 at 07:13 AM.
Reason: I needed coffee, hehe...
|
|
|
|
|
Guest
|

05-03-2008, 07:31 AM
Quote:
Originally Posted by Nuinethir
Hello again.
I decided to check over at Dick Baldwin's website again, with one of the links he gave me, directing me to programming tutorial 0135a, showing the source code itself. It was all written with french braces, so I was dumbfounded. "How do you get that inside Alice?!" I asked myself.
But this time, I found a download link on the page, and checked out the file. Inside, there are no french braces or such, so I don't know why he shows them there? (shrug)
|
You can cause your Alice code to be displayed in any one of the following three formats by selecting the format under the Edit/Preferences menu:
Alice Style
Java Style in Color
Java Style in Black and White
Although Alice style is the default, I try to remember to keep my preferences set to Java Style in Color in hopes that students will move on to learning Java after they complete an Alice course.
When you downloaded and examined my code, you probably had your preferences set to Alice Style which caused it to look different from the code in my lesson.
The source code listings that you will find in my tutorials were produced using the File/Export Code for Printing... menu items. Assuming that I remembered to select Java Style, the code that is produced by those menu items is very close to what you would see in a comparable Java program.
Occasionally the format of code listings produced in this way will differ from what you see in the Alice programming screen, but if you have your preferences set to the same style that was used to produce the listings, the differences will be minor (except where events and the mouse are concerned, in which case they are major).
I don't know any other way to produce source code listings in Alice short of doing screen capture to capture the programming area as an image.
Anyway, I'm glad that you were able to solve your problem. Since Alice doesn't have a true integer type, it isn't easy to display integers in Alice.
Dick Baldwin
Free Alice tutorials: http://www.dickbaldwin.com/tocalice.htm
Free programming tutorials: http://www.dickbaldwin.com/toc.htm
Last edited by DickBaldwin; 05-03-2008 at 07:36 AM.
Reason: Clarification
|
|
|
|
|
Guest
|

05-03-2008, 09:16 AM
Quote:
Originally Posted by DickBaldwin
You can cause your Alice code to be displayed in any one of the following three formats by selecting the format under the Edit/Preferences menu:
Alice Style
Java Style in Color
Java Style in Black and White
Although Alice style is the default, I try to remember to keep my preferences set to Java Style in Color in hopes that students will move on to learning Java after they complete an Alice course.
|
Thank you for the tip, I had no idea Alice could work like this! I switched over and love it. I will be sure to mention this to my classmates.
|
|
|
Thread Tools |
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Copyright ©2023, Carnegie Mellon University
Alice 2.x © 1999-2012, Alice 3.x © 2008-2012, Carnegie Mellon University. All rights reserved.
|
 |