Search code examples
javaswingevent-driven-design

Turns issue in java game


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:


Solution

  • 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 :)