By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,060 Members | 1,878 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,060 IT Pros & Developers. It's quick & easy.

Problem with a rock, paper, scissors game

P: 1
I am trying to create a Rock, Paper, Scissors game (where you play against a bot) using four different classes (SSPViewer, SSPController, SSPPlayer and SSPUserInput) + one main class. The game is supposed to use two different windows/JPanels. One with buttons that the user can click on and one showing the score etc.

The current problem that I have is that I cannot get the game to count score at all. Everything else about the design within the program is working as it should. I think that it has to do with some logic in the code, but I cannot seem to find the problem.

Here is some code from the Viewer and Controller-classes:

Controller:

public void game(int Humanchoice){
if (pointsBot < 3 && pointsHuman < 3){
int computer = bot.newChoice();
// rock = 1, paper = 2, scissors = 3;

//reglerna - pošng utdelning
if (Humanchoice == 1 && computer == 3){
pointsHuman++;
viewer.setScoreHuman(pointsHuman);
}
else if (Humanchoice == 2 && computer == 3){
pointsBot++;
viewer.setScoreBot(pointsBot);
}
else if (Humanchoice == 1 && computer == 2){
pointsBot++;
viewer.setScoreBot(pointsBot);
}
else if (Humanchoice == 3 && computer == 2){
pointsHuman++;
viewer.setScoreHuman(pointsHuman);
}
else if (Humanchoice == 2 && computer == 1){
pointsHuman++;
viewer.setScoreHuman(pointsHuman);
}
else if (Humanchoice == 3 && computer == 1){
pointsBot++;
viewer.setScoreBot(pointsBot);
}
}
}


Viewer:

public void setHumanChoice(String ChoiceHuman) {
Choice.setText(ChoiceHuman);
}

public void setBotChoice(String ChoiceBot) {
Choice2.setText(ChoiceBot);
}

public void setScoreHuman(int pointsHuman) {
HumanNbr.setText("" + pointsHuman);
}

public void setScoreBot(int pointsBot) {
ComputerNbr.setText("" + pointsBot);
}
}
Jan 6 '19 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 785
Please use code tags to make it easier for us free helpers to read your code.

You have two variables "pointsHuman" and "pointsBot", but you have not listed the code where they are defined and where they are printed out. Maybe you count up, but at the end you reset them to 0 by accident?

So the first thing you should do is to add two methods
Expand|Select|Wrap|Line Numbers
  1. increaseHumanPoints() {
  2.    pointsHuman++;
  3.    System.out.println("Human points after increase:" + pointsHuman);
  4. }
  5. resetHumanPoints() {
  6.    pointsHuman= 0;
  7.    System.out.println("Human points were resetted!");
  8. }
  9.  
Then replace every occurence of "pointsHuman" with these methods everywhere. You should not have a single occurrence of humanPoints anywhere in your code except in these two methods.
Then let your program run and post the result here.
If everything is OK, then do it similar with "pointsBot" and post the results here.
Apr 5 '19 #2

Post your reply

Sign in to post your reply or Sign up for a free account.