Hi,
Here is the code for noughts and crosses game I made.
java Code:
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Nightwalker83 http://aaronspehr.net/
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//Button array in grid layout
private static int TILES = 9;
int shotcounter;
int j;
int k;
public noughtsandcrosses(){
for (int i = 0; i < tArray.length; i++){
con.add(tArray[i]);
tArray[i].addActionListener(this);
}
}
for (j = 0; j < tArray.length; j++) {
if (e.getSource() == tArray[j]) {
temp =
Integer.
toString(++shotcounter
);
if (shotcounter % 2 == 0) {
temp = "0";
tArray[j].setText("0");
} else {
temp = "X";
tArray[j].setText("X");
}
tArray[j].setEnabled(false);
showWinner(j, temp);
}
}
}
public void showWinner
( int j,
String temp
) { if (tArray[0].getText() == tArray[1].getText() && tArray[0].getText() == tArray[2].getText() && tArray[0].getText() != "") {
displayDialog(k);
} else if (tArray[3].getText() == tArray[4].getText() && tArray[3].getText() == tArray[5].getText() && tArray[3].getText() != "") {
displayDialog(k);
} else if (tArray[6].getText() == tArray[7].getText() && tArray[6].getText() == tArray[8].getText() && tArray[6].getText() != "") {
displayDialog(k);
} else if (tArray[0].getText() == tArray[3].getText() && tArray[0].getText() == tArray[6].getText() && tArray[0].getText() != "") {
displayDialog(k);
} else if (tArray[1].getText() == tArray[4].getText() && tArray[1].getText() == tArray[7].getText() && tArray[1].getText() != "") {
displayDialog(k);
} else if (tArray[2].getText() == tArray[5].getText() && tArray[2].getText() == tArray[8].getText() && tArray[2].getText() != "") {
displayDialog(k);
} else if (tArray[0].getText() == tArray[4].getText() && tArray[0].getText() == tArray[8].getText() && tArray[0].getText() != "") {
displayDialog(k);
} else if (tArray[2].getText() == tArray[4].getText() && tArray[2].getText() == tArray[6].getText() && tArray[2].getText() != "") {
displayDialog(k);
} else if (shotcounter > 8) {
}
}
public void displayDialog(int k) {
int result =
JOptionPane.
showConfirmDialog(null,
"Well done! Player " + temp +
" is Victorious!!!!!!! Excellent!!! Do you wish to close the game?",
"Do you wish to close?",
JOptionPane.
YES_NO_OPTION);
}
}
public static void main
(String args
[]) { noughtsandcrosses window = new noughtsandcrosses();
window.setTitle("Noughts And Crosses");
window.
setPreferredSize(new Dimension(500,
250));
window.setVisible(true);
window.pack();
}
}
Just add the code to a *.java file and that is that.
Nightwalker