Hi,

I created a game in which players take turns entering their symbol by clicking on a box that is implemented on a grid window. I am having a hard time being able to check if the player has four symbols in a row diagonal or up or down or 3 in a corner. What I want to do is create 25 variables corresponding to each box so then I can compare each four in a row or three in a row position. If anyone has a more efficent idea to compare 25 variables than the use of many if statments feel free to post it. I will have to let you know if it is not helpful(if my knowledge is not great enough to understand your suggestions/code).


PLEASE NOTE: THIS CODE DOES NOT COMPILE. AS MANY OF YOU ALREADY NOTICE THERE IS A LARGE SUM OF CODE MISSING. IF YOU REQUIRE THE REST OF THE CODE TO HELP ME PLEASE LET ME KNOW.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Game implements ActionListener {
	
	
	private JButton buttons[][];
	private int boardSize=0;
	private int count = -1;
	private String letter = "";
	private boolean win = false;

	public Game(){
	//Create Window
	
	inputPanel.add(button5);
	button5.addActionListener(this);
	inputPanel.setSize(500,200);
	window.add(inputPanel,"North");
	
	
	
	//Make The Window Visible
	window.setVisible(true);
	}
	
	/**
	 When an object is clicked, perform an action.
	 @param a action event object
	 */
	public void actionPerformed(ActionEvent move) {
		if(count==-1){
			letter="-";
			count=0;
		}
		else{
		count++;
		
		//Calculate whose turn it is
		
		if(count % 2 == 0){
			letter = "O";
		} else {
			letter = "X";
		}
		}
		
	    if (move.getSource() == button5){
			boardSize=5;
		}
		if ((move.getSource()==button5)){
			button5.setEnabled(false);
			buttons = new JButton[boardSize][boardSize];
			for(int i=0; i<boardSize; i++)
				for (int j=0; j<boardSize; j++){
					buttons[i][j] = new JButton();
					boardPanel.add(buttons[i][j]);
					buttons[i][j].addActionListener(this);
			

				}
			
			boardPanel.setSize(300,300);
			boardPanel.setLayout(new GridLayout(boardSize,boardSize));
			window.add(boardPanel,"Center");
		}
		
		//Write the letter to the button and deactivate(CAN NO LONGER BE CLICKED) it
		 JButton pressedButton = (JButton)move.getSource(); 
		 pressedButton.setText(letter);
		 pressedButton.setEnabled(false);
			
		//Determine winner
	
		//Show a dialog when game is over
		if(win == true){
			JOptionPane.showMessageDialog(null, letter + " wins the game!");
			System.exit(0);
		} else if(count 	}
	
	private void getName() {
		// TODO Auto-generated method stub
		
	}

	public static void main(String[] args){
		Game starter = new Game();
	}
}

If anyone has a more efficent idea to compare 25 variables than the use of many if statments feel free to post it

Could you post the part of the program where you use those 25 variables so we can have a better idea on how to the program works

anyway I think you should use a 2d array to store those variables then use a loop to compare each element instead of many if statement

Sorry if I was not clear but I need help to somehow recognize a click at a specific position on the board to be assigned to one of 25 variables, and then as the variables are added I was thinking to continuously loop until there are four of the same variables in a row.

I know this is not what you are asking for, but why not just compare a clicked spot every time a move is made? then only a few if-statements will be needed instead of covering the whole board every time.

Would each of the previously clicked spots be stored in a variable to be compared to the most currently click spot? Can you please be a little more specific if this is not what you meant?

yeah.. store a 2D array of the spots in the game board, and after each turn you know which one has been clicked, so compare each of the immediately adjacent spots and if the if-statement returns a yes, the spot(s) in line with both of the affirmed spots to that one to look for a win.

1. Player 2 will never win on player 1's turn aka you can't make a move to 'lose' aka No player will win not on his/her turn
2. A player will never win during his/her turn with a spot non-adjacent to the clicked spot (or through a chain of clicked spots) as nothing has changed anywhere during that turn.
therefore: you only have to deal with a player's win/loss status during his turn + within that turn, only the adjacent spots (or adjacent through a series of affirmative if-statements) from the clicked spot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.