Never exit while loop

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Never exit while loop

thesaxo
Hello, i'm trying to develop a very simple rock, paper, scissors game, but i'm struggling with the syntax of Jack, and i can't understand why this loop never ends:

class User {

    function void choose() {
        var String choice;
        while (~((choice = "ROCK") | (choice = "PAPER") | (choice = "SCISSORS"))) {
            let choice = Keyboard.readLine("Pick ROCK, PAPER or SCISSORS: ");
            do Output.printString(choice);
            do Output.println();
        }
        do Output.printString("You choosed ");
        do Output.printString(choice);
        do Output.println();
        do String.dispose(choice);
        return;
    }
}


I tryed typing 'ROCK', or 'rock' and so on but it doesn't change.
Reply | Threaded
Open this post in threaded view
|

Re: Never exit while loop

WBahn
Administrator
thesaxo wrote
Hello, i'm trying to develop a very simple rock, paper, scissors game, but i'm struggling with the syntax of Jack, and i can't understand why this loop never ends:

class User {

    function void choose() {
        var String choice;
        while (~((choice = "ROCK") | (choice = "PAPER") | (choice = "SCISSORS"))) {
            let choice = Keyboard.readLine("Pick ROCK, PAPER or SCISSORS: ");
            do Output.printString(choice);
            do Output.println();
        }
        do Output.printString("You choosed ");
        do Output.printString(choice);
        do Output.println();
        do String.dispose(choice);
        return;
    }
}


I tryed typing 'ROCK', or 'rock' and so on but it doesn't change.
You've got a few issues.

First, when you declare a variable you are only allocating memory for that variable. An variable of object type (such as String) is a one-word (16-bit) variable whose value is the address where the object is stored. Jack initializes local variables to 0, so at the start of your loop 'choice' is equal to 0 (and there is no String object stored there -- in fact it's where the Stack Pointer is located).

Next, when you use an expression such as

while (choice = "ROCK")

The compiler inserts code to create a string object and replaces the literal string in the source code a call to the String object constructor and additional code to copy the string into the allocated object. Since a constructor returns the address where the object it constructed is stored, what you are doing is comparing the addresses of two objects. Even if the contents are identical, they are not the same object and their address will only be equal if they are.


Reply | Threaded
Open this post in threaded view
|

Re: Never exit while loop

thesaxo
So how one can make strings comparison?
Reply | Threaded
Open this post in threaded view
|

Re: Never exit while loop

WBahn
Administrator
The same way that every programming language does it. You write a function. Since Strings know how long they are, the first thing you check is if they are the same length. If they aren't, then they aren't the same. If they are, then you walk down both strings one character at a time and compares the characters at that position. If they are different, the strings aren't the same. If you get to the end of the strings and all the characters have matched, then they are the same.
Reply | Threaded
Open this post in threaded view
|

Re: Never exit while loop

Lozminda
Also you'll have to destroy any string objects you create too. Even "const" strings you might use in output.
(Something I forgot.. That causes memory overflow or something similar. Been working on something else for a couple of months, my jack is getting a bit rusty).

Reply | Threaded
Open this post in threaded view
|

Re: Never exit while loop

WBahn
Administrator
I think that's one of the deficiencies of the language definition -- since there is now way for a function to know whether it can destroy a String or not, there is no way to free up string literals used as arguments to functions or methods -- thus ensuring a memory leak. In most languages this isn't an issue because string literals are either embedded in the code or allocated once on the heap no matter how many times they are used in the program. That's actually something that a Jack compiler could do without too much difficulty.
Reply | Threaded
Open this post in threaded view
|

Re: Never exit while loop

thesaxo
All makes sense.. anyway in the end i used char comparisons instead of strings, using 'p' for paper, 'r' for rock and so on..