JAVA WHat is wrong with my code?


#1

What is wrong with my code?

WHen I type Y, it run well, but when I type in any number (1 to 4) then the program just keep loading non stop.


`


#2

Consider,

if (user==KVARTAL) {
                switch (comp) {
                    case 4:
                        result = 1;
                        break;
                    case 2:
                    case 1:
                        result = -1;
                        break;
                    default:
                        result = -1;
                        break;
                }
            }
    // ...

putting the switch on the outside, and deal with each case in a win or lose manner.

switch (user) {
case FLURG:
    return comp == 2 || comp == 3 ? 1 : -1;
case STUGA:
    return comp == 3 ? 1 : -1;
case KVARTAL:
    return comp == 4 ? 1 : -1;
case LERBERG:
    return comp == 1 || comp == 2 ? 1 : -1;
default:
    return -1;
}

This is pseudo code, mind. Correct as necessary.

Not really saying much, though. Just thinking along a different line as a way to condense and narrow in on the actual fault. I'm not a Java programmer, but JavaScript resembles it in many ways, once we get down to the constructs. That's why I call this pseudo code. It is JS.


#3

I actually figure out what was wrong.
It was because I input another user input without knowing. This made me thought my program just keep loading.


#4

Does the above look doable in Java? If so, it will sure trim things.


#5

Not speaking on any level of expertise, if we have garbage collection working, meaning we are exiting functions, then it follows that we can create new objects at each round. They would have output methods built in which would eliminate the repeated Java-centered syntax seen strewn throughout this code. If I can spot this deficiency, then so will any teacher.


#6

Yeah, at this part:

System.out.print("\nENTER A MOVE: ");
                choice = readChoice();
                while (choice != QUIT) {
                    int compCh = compChoice();
                    int userCh = readChoice();

choice and userCh is supposed to be the same thing which only need one user enter. However, I put it in twice which it will ask the user to enter twice instead. That was why I thought it still loading, but in fact it was actually waiting for me to input in another number LOL
And no problem, I'm still learning. It took me nearly three hours to spot this problem :frowning:


#7

That did look a little off to me, as well. Just not astute enough to point that out, though we might have come down to it, eventually, the way I bird-dog things.

If you haven't studied JavaScript, then parallel-learn it with Java. They are two completely different languages (Java is class based; JS is prototypal) but yet they share so many common traits in the core constructs. The logic simply overlaps. It might be that writing concise code in JS helps you port over to class based Java. I seem to get that impression.


#8

In a parting aside on this project or any similar to Rock, Paper, Scissors, may I recommend a more fair minded version that mitigates ties (give the computer another go) and that evens the odds by adding one more case (similar to Sheldon's game). Two win, two lose. The scale always balances no matter what you pick. Odd sample space. 3, 5, 7, 9 will always give equal probability for any outcome.


#9

Sure, you may! I'm even more excited to hear about other people's solution on how they will code this. But the way you describe it sound really interesting to me. Just curious how you are going code that.


#10


Scissors cuts paper;
Paper covers rock;
Rock crushes lizard;
Lizard poisons Spock;
Spock smashes scissors;
Scissors decapitates lizard;
Lizard eats paper;
Paper disproves Spock;
Spock vaporizes rock; and as it always has,
Rock crushes scissors;

var SCISSORS, LIZARD, SPOCK, PAPER, ROCK;
SCISSORS = 1;
LIZARD = 2;
SPOCK = 3;
PAPER = 4;
ROCK = 5;

and in the compare() function...

switch (user) {
case SCISSORS:
    return comp == 4 || comp == 2 ? 1 : -1;
case LIZARD:
    return comp == 3 || comp == 4 ? 1 : -1;
case SPOCK:
    return comp == 1 || comp == 5 ? 1 : -1;
case PAPER:
    return comp == 5 || comp == 3 ? 1 : -1;
case ROCK:
    return comp == 2 || comp == 1 ? 1 : -1;
default:
    return -1;
}

I'll dig around for a complete JS version of this game that I wrote a couple of years ago.


#11

Still haven't done any digging but did play around with this some more...

var names = {
    '1': { name:'SCISSORS', wins: [2,4] },
    '2': { name:'LIZARD', wins: [3,4] },
    '3': { name:'SPOCK', wins: [1,5] },
    '4': { name:'PAPER', wins: [3,5] },
    '5': { name:'ROCK', wins: [1,2] }
};
var toss = function () {
    return Math.floor(Math.random() * 5 + 1);
};
var compare = function (u, c) {
    while (c === u) {
        c = toss();
    }
    m = names[c];
    n = names[u];
    if (c == n.wins[0] || c == n.wins[1]) {
        console.log("User beats " + m.name + " with " + n.name);
    } else {
        console.log("Computer beats " + n.name + " with " + m.name);
    }
};
compare(2,3);    // User beats SPOCK with LIZARD
compare(4,2);    // Computer beats PAPER with LIZARD
compare(toss(), toss());

#13

Decided to take the time to round this program out to a working game. Still very simple and in the console. I made it a study on self-documenting code. It seems very readable to me...

// Sheldon's Game - Rock, Paper, Scissors, Lizard, Spock
// self-documenting code study

var names = {
    '1': { name:'SCISSORS', wins: [2,4] },
    '2': { name:'LIZARD', wins: [3,4] },
    '3': { name:'SPOCK', wins: [1,5] },
    '4': { name:'PAPER', wins: [3,5] },
    '5': { name:'ROCK', wins: [1,2] }
};

var PROMPT = (function (n) {
    var str = "";
    for (var key in n) {
        str += " [" + key + "] " + n[key].name;
    }
    return str;
})(names);

var toss = function () {
    return Math.floor(Math.random() * 5 + 1);
};

var user = function () {
    var u;
    do {
        u = Number(prompt(PROMPT));
    } while (isNaN(u));
    return u;
};

var compare = function (u, c){
    while (c === u) {
        c = toss();
    }
    m = names[c];
    n = names[u];
    if (c == n.wins[0] || c == n.wins[1]) {
        console.log("User beats " + m.name + " with " + n.name);
    } else {
        console.log("Computer beats " + n.name + " with " + m.name);
    }
};

var play = function () {
    var usr;
    do {
        usr = user();
        if (usr) {
            if (usr > 0 && usr < 6) {
                compare(usr, toss());
            } else {
                console.log("Input Error. Try again.");
            }
        }
    } while (usr);
    console.log("Bye!");
};

play();

#14

I would argue that this is stateful in that it allows names to change.