PS the reason the names of the classes are tic tac toe and such is because i just changed a tic tac toe program i made
/**
* Simon Kapiamba
* 4/7/10
*
*/
package tic;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class TicTacToe extends JFrame implements ActionListener{
Container contentPane;
JButton rePlay;
JButton show;
JLabel message;
int tempVal;
int count;
int tempN;
int first;
char tempC;
int second;
int turn;
int pairs = 0;
int r = 0;
char winner = '*';
char [] clickVal = new char[16];
Color c = new Color(255, 255, 200);
JButton [] board = new JButton[16];
char [] value = new char[16];
Font fonts = new Font("Arial", Font.BOLD, 72);
public static void main(String[] args){
TicTacToe window = new TicTacToe();
window.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public TicTacToe(){
super("Tic Tac Toe");
setBounds(0, 0, 900, 400);
contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
message = new JLabel("");
rePlay = new JButton("reset");
show = new JButton("solution");
rePlay.addActionListener(this);
JPanel panelBoard = new JPanel(new GridLayout(4,4));
JPanel panel = new JPanel(new FlowLayout());
panel.add(show);
panel.add(message);
panel.add(rePlay);
int count = 0;
for(int i = 0; i < board.length; i++){
if(i%2 == 0)
count++;
value[i] = (char)(count+64);
clickVal[i] = '*';
board[i] = new JButton();
board[i].addActionListener(this);
r = i + 1;
String temp = new String (" " + r);
board[i].setText(temp);
panelBoard.add(board[i]);
board[i].setFont(fonts);
winner = '*';
c = new Color(165, 211, 69);
board[i].setBackground(c);
}
shuffle(value);
contentPane.add(panelBoard,BorderLayout.CENTER);
contentPane.add(panel,BorderLayout.SOUTH);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
String a = e.getActionCommand();
a = a.substring(1);
tempC = a.charAt(0);
//System.out.println(tempC);
if(a.equals("eset")){
shuffle(value);
message.setText("");
for(int i = 0; i < board.length; i++){
pairs = 0;
clickVal[i] = '*';
board[i].setText(new String(" " + (i+1)));
}
}else if(a.equals("olution")){
for(int i = 0; i < board.length; i++){
board[i].setText(new String(" ") + value[i]);
}
message.setText("Press 'reset' to start a new game!");
}else if(tempC != 'A' && tempC != 'B' && tempC != 'C' && tempC != 'D' && tempC != 'E' && tempC != 'F' && tempC != 'G' && tempC != 'H'){
tempN = new Integer(a);
if(turn%2 == 0 && clickVal[tempN-1] == '*'){
first = tempN - 1;
clickVal[first] = ' ';
board[first].setText(new String(" " + value[first]));
turn++;
}
else if(turn%2 == 1 && first != tempN-1){
second = tempN - 1;
clickVal[second] = ' ';
board[second].setText(new String(" " + value[second]));
repaint();
turn++;
if(value[first] != value[second]){
board[first].setText(new String(" " + (first+1)));
clickVal[first] = '*';
board[second].setText(new String(" " + (second+1)));
clickVal[second] = '*';
}else{
pairs++;
}
if(pairs == 8){
message.setText("You win!");
}
}
}
}
static void wait(int n){
long t0,t1;
t0=System.currentTimeMillis();
do{
t1 = System.currentTimeMillis();
}
while(t1-t0<n);
}
static void shuffle(char [] a){
int f;
int s;
char hold;
for(int i = 0; i < 100; i++){
f = (int) (Math.random()*16);
s = (int) (Math.random()*16);
hold = a[f];
a[f] = a[s];
a[s] = hold;
}
}
}

New Topic/Question
Reply
MultiQuote












|