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();
}
}