1

I've created a simple game which a player plays against computer. I've got an issue in the timing of the turns, the computer should be the first to make a move than the real player should make his move by clicking LeftChoice or RightChoice button.

Here's my problem at code:

public class GameForm extends javax.swing.JFrame {

/**
 * Creates new form GameForm
 */
final int SIZE = 10;
int CurrentSize = 10;
int PC_SUM=0;
int HUMAN_SUM=0;
boolean PC_TURN = true;
int[] RandArr = new int[SIZE];
public GameForm() {
    initComponents();
}
public void init(){
    for(int i = 0 ; i<SIZE;i++){
        RandArr[i] = (int)(Math.random()*100)+1;
    }
    jTextField3.setText("");
    jTextField4.setText(Integer.toString(PC_SUM));
    jTextField5.setText(Integer.toString(HUMAN_SUM));
}
public void HUMAN_updateLeft(){
    HUMAN_SUM+=RandArr[0];
    jTextField5.setText(Integer.toString(HUMAN_SUM));
    jTextField1.setText(Arrays.toString(RandArr));
    CurrentSize--;
    int [] NewRand = new int[CurrentSize];
     for(int i = 1 ; i<=CurrentSize;i++){
        NewRand[i-1] = RandArr[i];
    }
     RandArr = NewRand;
     jTextField2.setText(Arrays.toString(RandArr));
     PC_TURN = true;
}
public void HUMAN_updateRight(){
    HUMAN_SUM+=RandArr[CurrentSize-1];
    jTextField5.setText(Integer.toString(HUMAN_SUM));
    jTextField1.setText(Arrays.toString(RandArr));
    CurrentSize--;
    int [] NewRand = new int[CurrentSize];
     for(int i = CurrentSize-1 ; i>=0;i--){
        NewRand[i] = RandArr[i];
    }
     RandArr = NewRand;
     jTextField2.setText(Arrays.toString(RandArr));
     PC_TURN = true;
}
public static boolean WhoIsBigger(int[] arr){
int even=0,odd=0;

for(int i=0;i<arr.length;i+=2){
    if(i%2==0){
    even+=arr[i];
    odd+=arr[i+1];
    }
    else{
        odd+=arr[i];
        even+=arr[i+1];
    }
  }
return even>odd;
}
public void PC_updateLeft(){
    PC_SUM+=RandArr[0];
    jTextField4.setText(Integer.toString(PC_SUM));
    jTextField1.setText(Arrays.toString(RandArr));
    CurrentSize--;
    int [] NewRand = new int[CurrentSize];
     for(int i = 1 ; i<=CurrentSize;i++){
        NewRand[i-1] = RandArr[i];
    }
     RandArr = NewRand;
     jTextField2.setText(Arrays.toString(RandArr));
}
public void PC_updateRight(){
    PC_SUM+=RandArr[CurrentSize-1];
    jTextField4.setText(Integer.toString(PC_SUM));
    jTextField1.setText(Arrays.toString(RandArr));
    CurrentSize--;
    int [] NewRand = new int[CurrentSize];
     for(int i = CurrentSize-1 ; i>=0;i--){
        NewRand[i] = RandArr[i];
    }
     RandArr = NewRand;
     jTextField2.setText(Arrays.toString(RandArr));
}
public void PC_TURN(){
    if(WhoIsBigger(RandArr))
        PC_updateLeft();
    PC_updateRight();
}
public void FullGame(){
    while(RandArr.length>0){
        if(PC_TURN){
            PC_TURN();
            PC_TURN = false;
        }
    }
}
  //start button listener
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    init();
    jTextField2.setText(Arrays.toString(RandArr));
    jTextField1.setText("-");
    jButton1.setEnabled(false);
    FullGame();
}                                        
   //left button listener
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
HUMAN_updateLeft();
}                                        
//right button listener
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
HUMAN_updateRight();
}                

How can i know that the real player has made his move so it'll change PC_TURN to True and the game will move on?

here's a picture of how my GUI looks like:

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
gil
  • 2,388
  • 1
  • 21
  • 29

3 Answers3

4

Firstly, you should follow Java Naming Conventions when it comes to naming your variables. As it stands, PC_TURN looks as though it is a constant, although it is not a constant since you are changing it's value. So a more appropriate name would be pcTurn.

You also seem to have a method called PC_TURN() which I am assuming is the method which causes the computer to take it's turn and do something. So it would be better if this was named something descriptive like takePCTurn(). Notice the use of camelCase throughout. Capital letters at the start of a name should be reserved for classes and interfaces, not variables or methods.

The same goes for your method name

public void FullGame()

should be written as

public void fullGame()

Keeping to coding conventions like this make it easier for others to read and understand your code and keep everything neat and tidy. It's a good habit to get into. :)

I don't know where your left and right buttons are being declared or what you have named them, but you will need to add an event listener to each button which causes something to happen when they are clicked. I also am unsure about the purpose of RandArr.length > 0. You really don't need a loop here since this is an application with a GUI, it is never going to close unless you explicitly tell it to (e.g by clicking the close button). So I will just give you a generic solution.

You basically want the players turn to trigger the computer to take it's turn until some game over condition is met.

Example:

//Assuming this is called when the Start button is clicked
public void fullGame() {
    takePCTurn();
}

public void takePCTurn() {
    //Do PC's turn logic
    //You might want to check if the Computer Won the game here        
}


class PlayerTurnListener implements ActionListener() {
    public void actionPerformed(ActionEvent e) {
        //Do Player's turn logic            
        //You might want to check if the Player Won the game here
        takePCTurn();
    }
}

//We can create an instance of our listener and add it to both buttons
PlayerTurnListener playerTurnListener = new PlayerTurnListener();

leftButton.addActionListener(playerTurnListener);
rightButton.addActionListener(playerTurnListener);

So the first thing that happens is fullGame() is called by your Start button which then calls takePCTurn(); causing the computer to take it's turn. Now nothing will happen until the player clicks the left or right button. When they do this, the PlayerTurnListener's actionPerformed() method is called and you can do some logic in there and then takePCTurn(); will be called once again.

Rinse and repeat until gameover.

Hope that helps :)

Redtama
  • 1,603
  • 1
  • 18
  • 35
2

You should attach a clickListener onto the left and right buttons. Then when the user clicks one that event will be fired. You should change PC_TURN to true there and re-run the loop if needed.

Example:

JButton yourButton = new JButton("Your Button");
yourButton.addActionListener(new ActionListener()
{
  public void actionPerformed(ActionEvent e)
  {
    PC_TURN = true;
    // extra code
  }
});
vguzzi
  • 2,420
  • 2
  • 15
  • 19
  • that's exactly what i've done at first , but the program getting stucked. @vguzzi – gil Dec 01 '15 at 16:43
  • Can you show the code where it gets stuck. – vguzzi Dec 01 '15 at 16:44
  • when i first initialize it by clicking "start" button – gil Dec 01 '15 at 16:46
  • 1
    You'll have to paste more of your code if you want sufficient help. – vguzzi Dec 01 '15 at 16:46
  • It looks like he's added the listener but never breaks out of the original loop. He initialises the loop, the PC takes one turn, then the loop sits waiting patiently for RandArr.length>0 to resolve as false... – Jon Story Dec 01 '15 at 16:49
  • your'e right, how can i fix this? @JonStory – gil Dec 01 '15 at 16:59
  • erm, I'm not sure tbh - I haven't touched Java for about 4 years! Can you update your code in the original post and I or someone else will have a chance to take a look? – Jon Story Dec 01 '15 at 17:02
1

If this is a Swing or other GUI then you will need to get rid of the while loop.

Remember that GUI programs are non-linear and are event-driven, and so rather than a restricting while loop which risks completely feezing your GUI, you would change the program's state depending on whose turn it is -- have a variable indicating whose turn it is -- and then change the behavior of the program based on the state.

If you need more specific help, then you're going to need to ask a much more complete question including one with enough code and explanation to allow us to understand your problem better (but not too much code).


Regarding the code you've posted:

  • Yes, definitely get rid of that while loop.
  • Your code is hard to understand, mainly because you're variable naming is poor. Rather than using variable names like jTextField1, use names that make logical sense, that make your code "self-debuggin", such as humanScoreField and computerScoreField, etc.
  • Also learn and follow standard Java naming practices including having all variables start with lower case letters, classes with upper case letters, use camelCase for all except for constants which are all upper-case.
  • Tell more about just what this code is supposed to be doing, what the buttons and the JTextfields represent, what behaviors you desire, etc...
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • according to what you said that's the problem, i've edited my code now so you can see the whole of it. i'm new with GUI and not sure how to solve the loop issue – gil Dec 01 '15 at 17:14
  • @gilleibovitz: again, you don't want nor do you need a loop. Please see edits to answer. If you're learning Java on your own, please do go through the introductory chapters on core principles first -- you won't regret doing so as it will help you later on (and help us better understand your code). – Hovercraft Full Of Eels Dec 01 '15 at 17:27