Created
March 1, 2012 00:52
-
-
Save gAmUssA/1946300 to your computer and use it in GitHub Desktop.
simple tick-tack-toe game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.practicaljava.lesson11; | |
import java.awt.Component; | |
import java.awt.Font; | |
import java.awt.event.MouseEvent; | |
import java.awt.event.MouseListener; | |
import javax.swing.JApplet; | |
import javax.swing.JButton; | |
import javax.swing.JOptionPane; | |
import javax.swing.SwingConstants; | |
@SuppressWarnings("serial") | |
public class TicTacToeApplet extends JApplet implements MouseListener { | |
private static final String PLAYERX = "Player X"; | |
private static final String PLAYERO = "Player O"; | |
private int moves = 0; | |
private String playerName = PLAYERX; | |
private javax.swing.JButton button1; | |
private javax.swing.JButton button2; | |
private javax.swing.JButton button3; | |
private javax.swing.JButton button4; | |
private javax.swing.JButton button5; | |
private javax.swing.JButton button6; | |
private javax.swing.JButton button7; | |
private javax.swing.JButton button8; | |
private javax.swing.JButton button9; | |
private javax.swing.JLabel playerNumber; | |
private java.awt.Panel buttonsPanel; | |
public void init() { | |
initComponents(); | |
} | |
private void initComponents() { | |
buttonsPanel = new java.awt.Panel(); | |
button1 = new javax.swing.JButton(); | |
button2 = new javax.swing.JButton(); | |
button3 = new javax.swing.JButton(); | |
button4 = new javax.swing.JButton(); | |
button5 = new javax.swing.JButton(); | |
button6 = new javax.swing.JButton(); | |
button7 = new javax.swing.JButton(); | |
button8 = new javax.swing.JButton(); | |
button9 = new javax.swing.JButton(); | |
playerNumber = new javax.swing.JLabel(playerName, SwingConstants.CENTER); | |
buttonsPanel.setLayout(new java.awt.GridLayout(4, 3)); | |
buttonsPanel.add(button1); | |
buttonsPanel.add(button2); | |
buttonsPanel.add(button3); | |
buttonsPanel.add(button4); | |
buttonsPanel.add(button5); | |
buttonsPanel.add(button6); | |
buttonsPanel.add(button7); | |
buttonsPanel.add(button8); | |
buttonsPanel.add(button9); | |
Font buttonFont = new Font("Times New Roman", Font.PLAIN, 60); | |
for (Component component : buttonsPanel.getComponents()) { | |
if (component instanceof JButton) { | |
((JButton) component).addMouseListener(this); | |
((JButton) component).setFont(buttonFont); | |
} | |
} | |
setPlayerName(PLAYERX); | |
buttonsPanel.add(playerNumber); | |
add(buttonsPanel); | |
} | |
private void setPlayerName(String playerName) { | |
this.playerName = playerName; | |
playerNumber.setText(playerName + " your turn. "); | |
} | |
private void reset() { | |
for (Component component : buttonsPanel.getComponents()) { | |
if (component instanceof JButton) { | |
((JButton) component).setText(""); | |
} | |
} | |
setPlayerName(PLAYERX); | |
moves = 0; | |
} | |
public void checkForWinner() { | |
String[] str = { "OK" }; | |
switch (findThreeInARow()) { | |
case 1: | |
String winnerName = (playerName == PLAYERX) ? PLAYERO : PLAYERX; | |
JOptionPane.showOptionDialog(this, | |
winnerName.concat(" won!!! Congratulations!!!"), | |
"Congratulations!", JOptionPane.YES_OPTION, | |
JOptionPane.PLAIN_MESSAGE, null, str, "OK"); | |
reset(); | |
break; | |
case 3: | |
JOptionPane.showOptionDialog(this, " ohh!!1 Tie!!! Try again!!!", | |
"Try again!", JOptionPane.YES_OPTION, | |
JOptionPane.PLAIN_MESSAGE, null, str, "OK"); | |
reset(); | |
break; | |
default: | |
break; | |
} | |
} | |
public void mouseClicked(MouseEvent e) { | |
javax.swing.JButton currentButton = (JButton) e.getComponent(); | |
if (currentButton.getText() == "") { | |
if (playerName == PLAYERX) { | |
currentButton.setText("X"); | |
setPlayerName(PLAYERO); | |
} else if (playerName == PLAYERO) { | |
currentButton.setText("O"); | |
setPlayerName(PLAYERX); | |
} | |
} | |
moves++; | |
checkForWinner(); | |
} | |
public void mousePressed(MouseEvent e) { | |
} | |
public void mouseReleased(MouseEvent e) { | |
} | |
public void mouseEntered(MouseEvent e) { | |
} | |
public void mouseExited(MouseEvent e) { | |
} | |
private int findThreeInARow(){ | |
if ((button1.getText() == button2.getText() && button2.getText() == button3.getText() && button1.getText() != "") || | |
(button4.getText() == button5.getText() && button5.getText() == button6.getText() && button4.getText() != "") || | |
(button7.getText() == button8.getText() && button8.getText() == button9.getText() && button7.getText() != "") || | |
(button1.getText() == button4.getText() && button4.getText() == button7.getText() && button1.getText() != "") || | |
(button2.getText() == button5.getText() && button5.getText() == button8.getText() && button2.getText() != "") || | |
(button3.getText() == button6.getText() && button6.getText() == button9.getText() && button3.getText() != "") || | |
(button1.getText() == button5.getText() && button5.getText() == button9.getText() && button1.getText() != "") || | |
(button3.getText() == button5.getText() && button5.getText() == button7.getText() && button3.getText() != "") | |
) | |
return 1; | |
else if (moves == 9) return 3; | |
else | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment