public class client extends JPanel {
private final static long serialVersionUID = 2000L;
Server g1 = new Server();
private int[][] board = new int[3][3];
private String message = "";
JButton oneButton, twoButton, threeButton, fourButton, fiveButton, sixButton, sevenButton, eightButton, nineButton, endButton;
JPanel buttonPanel = new JPanel();
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
//reset the button so that they are enabled and have no text
public void reset(){
oneButton.setText("");
twoButton.setText("");
threeButton.setText("");
fourButton.setText("");
fiveButton.setText("");
sixButton.setText("");
sevenButton.setText("");
eightButton.setText("");
nineButton.setText("");
oneButton.setEnabled(true);
twoButton.setEnabled(true);
threeButton.setEnabled(true);
fourButton.setEnabled(true);
fiveButton.setEnabled(true);
sixButton.setEnabled(true);
sevenButton.setEnabled(true);
eightButton.setEnabled(true);
nineButton.setEnabled(true);
message = "";
for(int x=0;x<3;x++){
for(int j=0;j<3;j++){
board[x][j]=0;
}
}
}
//generate grid
public client() {
if (RIGHT_TO_LEFT) {
buttonPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
buttonPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
oneButton = new JButton("");
if (shouldWeightX) {
c.weightx = 4;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 50; //height
c.gridx = 0; //grid location
c.gridy = 0;//grid location
buttonPanel.add(oneButton,c); //add to panel
twoButton = new JButton("");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
buttonPanel.add(twoButton,c);
threeButton = new JButton("");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 0;
buttonPanel.add(threeButton,c);
fourButton = new JButton("");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
buttonPanel.add(fourButton,c);
fiveButton = new JButton("");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
buttonPanel.add(fiveButton,c);
sixButton = new JButton("");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 1;
buttonPanel.add(sixButton,c);
sevenButton = new JButton("");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
buttonPanel.add(sevenButton,c);
eightButton = new JButton("");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 2;
buttonPanel.add(eightButton,c);
nineButton = new JButton("");
c.gridx = 2;
c.gridy = 2;
buttonPanel.add(nineButton,c);
endButton = new JButton("End the game");
c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 1.0; //request any extra vertical space
c.anchor = GridBagConstraints.PAGE_END; //bottom of space
c.insets = new Insets(10,0,0,0); //top padding
c.gridwidth = 3;
c.ipady = 0;
c.gridx = 0;
c.gridy = 3;
buttonPanel.add(endButton,c);
//set the board array to all 0s
for(int x=0;x<3;x++){
for(int j=0;j<3;j++){
board[x][j]=0;
}
}
//if button 1 is clicked
oneButton.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent ae) {
oneButton.setText(g1.getPlayer()); //set the text of the button
oneButton.setEnabled(false); // disable button
board[0][0] = g1.getBoard(); //set the array to the specific player
message = g1.checkWin(board); // check if someone won
if(message.equals("")){
}else{
JOptionPane.showMessageDialog(null, message); //output if someone won or tied
reset(); //reset
g1.reset();
}
}
});
//if click two button
twoButton.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent ae) {
twoButton.setText(g1.getPlayer());
twoButton.setEnabled(false);
board[0][1] = g1.getBoard();
message = g1.checkWin(board);
if(message.equals("")){
}else{
JOptionPane.showMessageDialog(null, message);
reset();
g1.reset();
}
}
});
//if click three button
threeButton.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent ae) {
threeButton.setText(g1.getPlayer());
threeButton.setEnabled(false);
board[0][2] = g1.getBoard();
message = g1.checkWin(board);
if(message.equals("")){
}else{
JOptionPane.showMessageDialog(null, message);
reset();
g1.reset();
}
}
});
//if click four button
fourButton.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent ae) {
fourButton.setText(g1.getPlayer());
fourButton.setEnabled(false);
board[1][0] = g1.getBoard();
message = g1.checkWin(board);
if(message.equals("")){
}else{
JOptionPane.showMessageDialog(null, message);
reset();
g1.reset();
}
}
});
//if click five button
fiveButton.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent ae) {
fiveButton.setText(g1.getPlayer());
fiveButton.setEnabled(false);
board[1][1] = g1.getBoard();
message = g1.checkWin(board);
if(message.equals("")){
}else{
JOptionPane.showMessageDialog(null, message);
reset();
g1.reset();
}
}
});
//if click six button
sixButton.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent ae) {
sixButton.setText(g1.getPlayer());
sixButton.setEnabled(false);
board[1][2] = g1.getBoard();
message = g1.checkWin(board);
if(message.equals("")){
}else{
JOptionPane.showMessageDialog(null, message);
reset();
g1.reset();
}
}
});
//if click seven button
sevenButton.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent ae) {
sevenButton.setText(g1.getPlayer());
sevenButton.setEnabled(false);
board[2][0] = g1.getBoard();
message = g1.checkWin(board);
if(message.equals("")){
}else{
JOptionPane.showMessageDialog(null, message);
reset();
g1.reset();
}
}
});
//if click eight button
eightButton.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent ae) {
eightButton.setText(g1.getPlayer());
eightButton.setEnabled(false);
board[2][1] = g1.getBoard();
message = g1.checkWin(board);
if(message.equals("")){
}else{
JOptionPane.showMessageDialog(null, message);
reset();
g1.reset();
}
}
});
//if click nine button
nineButton.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent ae) {
nineButton.setText(g1.getPlayer());
nineButton.setEnabled(false);
board[2][2] = g1.getBoard();
message = g1.checkWin(board);
if(message.equals("")){
}else{
JOptionPane.showMessageDialog(null, message);
reset();
g1.reset();
}
}
});
//if click end button
endButton.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent ae) {
JOptionPane.showMessageDialog(null, "You have ended the game. Goodbye");
System.exit(0);//end
}
});
add(buttonPanel, BorderLayout.PAGE_START);
add (buttonPanel, BorderLayout.CENTER);
setSize(700,700);
}
/**
* Creates and displays the GUI
*/
private static void createAndShowGUI() {
JFrame frame = new JFrame("Tick Tack Toe");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add( new client());
frame.pack();
frame.setVisible(true);
frame.setSize(150,310);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}