Skip to content

Instantly share code, notes, and snippets.

@sometowngeek
Created May 27, 2019 16:29
Show Gist options
  • Save sometowngeek/5f0de509a1c93bb87dffcb3f06918a95 to your computer and use it in GitHub Desktop.
Save sometowngeek/5f0de509a1c93bb87dffcb3f06918a95 to your computer and use it in GitHub Desktop.
public class TicTacToe extends JFrame {
private boolean initialized; // Default = false
private XOButtons buttons;
private int scorePlayer1;
private int scorePlayer2;
// Reset the board
// Reset the markers, etc.
public void resetGame() {
this.scorePlayer1 = 0;
this.scorePlayer2 = 0;
this.buttons.resetMarkers();
}
// Set up frame, buttons, score, etc.
public void newGame(){
// If the application has not been initialized for the first time,
// setup the frame with everything else
if (!initialized){
buttons = new XOButtons();
// Set up frame
}
// Otherwise, reset the game
else {
buttons.resetMarkers();
// Do other stuff...
}
}
// Play the game
public void play(){
// For each round,
// a player chooses where to put their "X" or "O"
// add scores, etc.
// Then at the end of each round
if (this.buttons.winCheck())
this.resetGame();
}
}
public class XOButtons {
List<JButton> buttons;
public XOButtons(){
this.buttons = new ArrayList<JButton>();
for (int i = 0; i < 9; i++){
this.buttons.add(new JButton());
// Do what you want with the buttons
}
}
// Check to see if there is a winner
// If so, announce the winner and return true
// Otherwise, return false.
public boolean winCheck() {}
public void resetMarkers(){
for (JButton button : buttons) {
button.setText("");
}
}
// Getters and setters
public String getMarker(){
return this.marker;
}
public void setMarker(String marker, int row, int column){
int location;
try {
location = (column * 3) == 0 ? 0 : column * 3 - 1; // Adding extra logic here to disallow negative values.
location += row;
this.buttons.get(location).setText(marker);
} catch (NumberFormatException | NullPointerException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment