package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
public class TicTacToe {
JButton[][] gameButtons;
private char gameChar = 'X';
private enum gameStatus {IN_PROGRESS, WON, DRAW}
GameButton button = new GameButton();
public TicTacToe() {
//setup buttons
gameButtons = new JButton[3][3];
for (JButton[] row : gameButtons) {
for (int i = 0; i < row.length; i++) {
row[i] = new JButton();
}
}
}
public void startGame() {
loadFrontEnd();
}
private void loadFrontEnd() {
JFrame jframe = new JFrame("Tic Tac Toe");
JPanel outer = new JPanel();
outer.setLayout(new GridLayout(3, 3, 10, 10));
for (JButton[] row : gameButtons) {
for (JButton aButton : row) {
aButton.addActionListener(button);
outer.add(aButton);
}
}
outer.setPreferredSize(new Dimension(300, 300));
jframe.add(outer);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.pack();
jframe.setVisible(true);
}
public char getAndFlipGameChar() {
gameChar = gameChar == 'X' ? 'O' : 'X';
return gameChar;
}
public gameStatus getGameStatus() {
//check horizontal
for (JButton[] row : gameButtons) {
if (row[0].getText() != "") {
if ((row[0].getText().equals(row[1].getText())) && row[0].getText().equals(row[2].getText()))
return gameStatus.WON;
}
}
//check vertical
for (int i = 0; i < gameButtons.length; i++) {
if (gameButtons[0][i].getText() != ""
&&(gameButtons[0][i].getText().equals(gameButtons[1][i].getText()))
&& (gameButtons[2][i].getText().equals(gameButtons[0][i].getText())))
return gameStatus.WON;
}
//check diagonal
if ( gameButtons[0][0].getText() != ""
&& (gameButtons[0][0].getText().equals(gameButtons[1][1].getText()))
&& (gameButtons[0][0].getText().equals(gameButtons[2][2].getText())))
return gameStatus.WON;
if ( gameButtons[2][0].getText() != ""
&& (gameButtons[0][2].getText().equals(gameButtons[1][1].getText()))
&& (gameButtons[0][2].getText().equals(gameButtons[2][0].getText())))
return gameStatus.WON;
//check no empties
ArrayList<JButton> buttonList = new ArrayList<>(9);
for (JButton[] row : gameButtons) {
Collections.addAll(buttonList, row);
}
buttonList.removeIf( a -> a.getText() != "");
if (!buttonList.isEmpty())
return gameStatus.IN_PROGRESS;
else
return gameStatus.DRAW;
}
private void resetGame() {
for (JButton[] row : gameButtons) {
for (JButton aButton : row) {
aButton.setText("");
}
}
}
class GameButton implements ActionListener {
public void actionPerformed (ActionEvent e) {
JButton theButtonPressed = (JButton) e.getSource();
// if button not used write text to it
if (theButtonPressed.getText().equals("") && getGameStatus() == gameStatus.IN_PROGRESS ) {
theButtonPressed.setText( Character.toString(getAndFlipGameChar()));
}
gameStatus currentStatus = getGameStatus();
if (currentStatus != gameStatus.IN_PROGRESS) {
String conclusionMessage = currentStatus == gameStatus.WON ? gameChar + " won the game!" : "DRAW!!!";
JOptionPane.showMessageDialog(new JFrame(), conclusionMessage);
resetGame();
}
}
}
}
No comments:
Post a Comment