aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes tictacttoe project1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "tictacttoe project1" Watch "tictacttoe project1" New topic
Author

tictacttoe project1

ten doeschate
Greenhorn

Joined: Jun 22, 2013
Posts: 16
the code :
login page:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

class Login {

public static boolean authenticate(String username, String password) {
// hardcoded username and password
if (username.equals("tendoeschate") && password.equals("yomahesh")) {
return true;
}
return false;
}
}
class LoginDialog extends JDialog {

private JTextField tfUsername;
private JPasswordField pfPassword;
private JLabel lbUsername;
private JLabel lbPassword;
private JButton btnLogin;
private JButton btnCancel;
private boolean succeeded;

public LoginDialog(final Frame parent) {
super(parent, "Login", true);
//
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints cs = new GridBagConstraints();

cs.fill = GridBagConstraints.HORIZONTAL;

lbUsername = new JLabel("Username: ");
cs.gridx = 0;
cs.gridy = 0;
cs.gridwidth = 1;
panel.add(lbUsername, cs);

tfUsername = new JTextField(20);
cs.gridx = 1;
cs.gridy = 0;
cs.gridwidth = 2;
panel.add(tfUsername, cs);

lbPassword = new JLabel("Password: ");
cs.gridx = 0;
cs.gridy = 1;
cs.gridwidth = 1;
panel.add(lbPassword, cs);

pfPassword = new JPasswordField(20);
cs.gridx = 1;
cs.gridy = 1;
cs.gridwidth = 2;
panel.add(pfPassword, cs);
panel.setBorder(new LineBorder(Color.GRAY));

btnLogin = new JButton("Login");

btnLogin.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
if (Login.authenticate(getUsername(), getPassword())) {
parent.setVisible(false);
game g=new game();



} else {
JOptionPane.showMessageDialog(LoginDialog.this,
"Invalid username or password",
"Login",
JOptionPane.ERROR_MESSAGE);
// reset username and password
tfUsername.setText("");
pfPassword.setText("");
succeeded = false;

}
}
});
btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
dispose();
}
});
JPanel bp = new JPanel();
bp.add(btnLogin);
bp.add(btnCancel);

getContentPane().add(panel, BorderLayout.CENTER);
getContentPane().add(bp, BorderLayout.PAGE_END);

pack();
setResizable(false);
setLocationRelativeTo(parent);
}

public String getUsername() {
return tfUsername.getText().trim();
}

public String getPassword() {
return new String(pfPassword.getPassword());
}

public boolean isSucceeded() {
return succeeded;
}
}
public class re {
public static void main(String[] args) {
final JFrame frame = new JFrame("TicTacToe Login Page");
final JButton btnLogin = new JButton("Click to login");

btnLogin.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e) {
LoginDialog loginDlg = new LoginDialog(frame);
frame.setVisible(false);
loginDlg.setVisible(true);
// if logon successfully

}
});

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLayout(new FlowLayout());
frame.getContentPane().add(btnLogin);
frame.setVisible(true);
}
}

Select game :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class game extends JFrame {
game()
{

final JFrame f=new JFrame();
JPanel panel=new JPanel();
JLabel label=new JLabel("Select Game Mode");
JButton button1=new JButton("AI");
JButton button2=new JButton("TWO PLAYER");
f.setVisible(true);
f.setTitle("GameMode");
f.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

label.setLayout(new BorderLayout(5,5));
panel.add(label);

panel.add(button1);
panel.add(button2);
f.getContentPane().add(panel);
button2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
f.setVisible(false);
new TicTacToe();

}
});
button1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent g)
{
f.setVisible(false);
new TicTacToeV4();
}
});
}



public static void main(String args[])
{
}
}

Two player:
/**
* AWT Sample application
*
* @author
* @version 1.00 13/06/21
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import java.text.*;
import java.util.*;


public class TicTacToe implements ActionListener {


/*Instance Variables*/
private JFrame window = new JFrame("Tic-Tac-Toe");
private JButton button1 = new JButton("");
private JButton button2 = new JButton("");
private JButton button3 = new JButton("");
private JButton button4 = new JButton("");
private JButton button5 = new JButton("");
private JButton button6 = new JButton("");
private JButton button7 = new JButton("");
private JButton button8 = new JButton("");
private JButton button9 = new JButton("");
private JButton button10 = new JButton("score");
private String letter = "";
private int count = 0;
private boolean win = false;




public TicTacToe(){
/*Create Window*/
JPanel p = new JPanel();


window.setSize(500,500);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));


/*Add Buttons To The Window*/
window.add(button1);
window.add(button2);
window.add(button3);
window.add(button4);
window.add(button5);
window.add(button6);
window.add(button7);
window.add(button8);
window.add(button9);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("Game");
fileMenu.setMnemonic('G');

JMenu editMenu = new JMenu("Help");
editMenu.setMnemonic('H');
JMenuItem newMenuItem = new JMenuItem("NewGame");
JMenuItem openMenuItem = new JMenuItem("Scores");
JMenuItem saveMenuItem = new JMenuItem("Save");
JMenuItem exitMenuItem = new JMenuItem("Exit");
JMenuItem new1MenuItem = new JMenuItem("help");
JMenuItem new2MenuItem = new JMenuItem("about");
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.add(exitMenuItem);
editMenu.add(new1MenuItem);

editMenu.add(new2MenuItem);


menuBar.add(fileMenu);
menuBar.add(editMenu);

window.setJMenuBar(menuBar);

/*Add The Action Listener To The Buttons*/
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
newMenuItem.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent ae)
{new TicTacToe();
}
});
exitMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent m)
{
System.exit(0);
}
});
new2MenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent n)
{
JOptionPane.showMessageDialog(null,"this game is developed by tendoeschate");
}
});
openMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent x)
{
SimpleTableExample mainFrame = new SimpleTableExample();
mainFrame.setVisible( true );

}

} );

new1MenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent u)
{
JOptionPane.showMessageDialog(null,"simple game played by two players with the help of mouse click");
}
});
saveMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent z)

{
JFrame frame=new JFrame();
JFileChooser fileSave = new JFileChooser();
fileSave.showSaveDialog(frame);
SaveFile s=new SaveFile();
s.saveFile(fileSave.getSelectedFile());
}
});

/*Make The Window Visible*/
window.setVisible(true);
}

public void actionPerformed(ActionEvent a) {
count++;

/*Calculate Who's Turn It Is*/
if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){

letter = "X";


} else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){

letter = "O";

}

/*Display X's or O's on the buttons*/
if(a.getSource() == button1){
button1.setText(letter);
button1.setEnabled(false);
button1.setBackground(Color.WHITE);
} else if(a.getSource() == button2){
button2.setText(letter);
button2.setEnabled(false);
button2.setBackground(Color.WHITE);
} else if(a.getSource() == button3){
button3.setText(letter);
button3.setEnabled(false);
button3.setBackground(Color.WHITE);
} else if(a.getSource() == button4){
button4.setText(letter);
button4.setEnabled(false);
button4.setBackground(Color.WHITE);
} else if(a.getSource() == button5){
button5.setText(letter);
button5.setEnabled(false);
button5.setBackground(Color.WHITE);
} else if(a.getSource() == button6){
button6.setText(letter);
button6.setEnabled(false);
button6.setBackground(Color.WHITE);
} else if(a.getSource() == button7){
button7.setText(letter);
button7.setEnabled(false);
button7.setBackground(Color.WHITE);
} else if(a.getSource() == button8){
button8.setText(letter);
button8.setEnabled(false);
button8.setBackground(Color.WHITE);
} else if(a.getSource() == button9){
button9.setText(letter);
button9.setEnabled(false);
button9.setBackground(Color.WHITE);
}

/*Determine who won*/
//horizontal wins
if( button1.getText() == button2.getText() && button2.getText() == button3.getText() && button1.getText() != ""){
win = true;
}
else if(button4.getText() == button5.getText() && button5.getText() == button6.getText() && button4.getText() != ""){
win = true;
}
else if(button7.getText() == button8.getText() && button8.getText() == button9.getText() && button7.getText() != ""){
win = true;
}

//virticle wins
else if(button1.getText() == button4.getText() && button4.getText() == button7.getText() && button1.getText() != ""){
win = true;
}
else if(button2.getText() == button5.getText() && button5.getText() == button8.getText() && button2.getText() != ""){
win = true;
}
else if(button3.getText() == button6.getText() && button6.getText() == button9.getText() && button3.getText() != ""){
win = true;
}

//diagonal wins
else if(button1.getText() == button5.getText() && button5.getText() == button9.getText() && button1.getText() != ""){
win = true;
}
else if(button3.getText() == button5.getText() && button5.getText() == button7.getText() && button3.getText() != ""){
win = true;
}
else {
win = false;
}

/*Show a dialog if someone wins or the game is tie*/
if(win == true){
if(letter=="X")

{
Global.total_games++;
Global.check_wins++;





}
else if(letter=="O")
{
Global.total_games++;
Global.check_wins1++;


}
JOptionPane.showMessageDialog(null, letter + " WINS!");


} else if(count == 9 && win == false){
JOptionPane.showMessageDialog(null, "Tie Game!");
Global.total_games++;
}



}

public static void main(String[] args){

new TicTacToe();

}
}
class SimpleTableExample extends JFrame
{
// Instance attributes used in this example
private JPanel topPanel;
private JTable table;
private JScrollPane scrollPane;




public SimpleTableExample()
{
// Set the frame characteristics
setTitle( "Simple Table Application" );
setSize( 300, 200 );
setBackground( Color.gray );

// Create a panel to hold all other components
topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
Global.percentage_games=(((Global.check_wins)/(Global.total_games)))*100;
Global.percentage_games1=(((Global.check_wins1)/(Global.total_games)))*100;

// Create columns names
String columnNames[] = { "Player", "Wins","PercentageWin","TotalGames" };

// Create some data
Double dataValues[][] =
{
{ 1.0,Global.check_wins,Global.percentage_games,Global.total_games },
{ 2.0,Global.check_wins1,Global.percentage_games1,Global.total_games },

};

// Create a new table instance
table = new JTable( dataValues, columnNames );

// Add the table to a scrolling pane
scrollPane = new JScrollPane( table );
topPanel.add( scrollPane, BorderLayout.CENTER );

}

// Main entry point for this example
public static void main( String args[] )
{
// Create an instance of the test application

}
}
class Global {

public static double check_wins=0;
public static double check_wins1=0;
public static double total_games=0;
public static double percentage_games;
public static double percentage_games1;
}

class SaveFile
{
JFrame frame=new JFrame();
JTextArea area=new JTextArea();
void saveFile(File file) {
frame.setTitle(file.getName() );
try {
FileWriter writer = new FileWriter(file);
writer.write(area.getText() );
writer.close();
} catch (IOException ex) {
System.out.println("cannot save file");
ex.printStackTrace();
}
}
public static void main(String h[])
{
}
}
AI game:
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.io.*;
import java.util.*;
import java.text.*;
import javax.swing.*;

public class TicTacToeV4 implements ActionListener {
/*Instance Variables*/
private int[][] winCombinations = new int[][] {
{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, //horizontal wins
{1, 4, 7}, {2, 5, 8}, {3, 6, 9}, //virticle wins
{1, 5, 9}, {3, 5, 7} //diagonal wins
};
private JFrame window = new JFrame("Tic-Tac-Toe");
private JButton buttons[] = new JButton[10];
private int count = 0;
private String letter = "";
private boolean win = false;

public TicTacToeV4(){
/*Create Window*/
window.setPreferredSize(new Dimension(300,300));
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));

/*Add Buttons To The Window*/
for(int i = 1; i<=9; i++){
buttons[i] = new JButton();
window.add(buttons[i]);
buttons[i].addActionListener(this);
}
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("Game");
fileMenu.setMnemonic('G');

JMenu editMenu = new JMenu("Help");
editMenu.setMnemonic('H');
JMenuItem newMenuItem = new JMenuItem("NewGame");
JMenuItem openMenuItem = new JMenuItem("Scores");
JMenuItem saveMenuItem = new JMenuItem("Save");
JMenuItem exitMenuItem = new JMenuItem("Exit");
JMenuItem new1MenuItem = new JMenuItem("help");
JMenuItem new2MenuItem = new JMenuItem("about");
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.add(exitMenuItem);
editMenu.add(new1MenuItem);

editMenu.add(new2MenuItem);


menuBar.add(fileMenu);
menuBar.add(editMenu);

window.setJMenuBar(menuBar);
/*Make The Window Visible*/
window.setVisible(true);
window.pack();

newMenuItem.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent ae)
{new TicTacToeV4();
}
});
openMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent x)
{
SimpleTableExample1 mainFrame = new SimpleTableExample1();
mainFrame.setVisible( true );

}

} );
exitMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent m)
{
System.exit(0);
}
});
new2MenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent n)
{
JOptionPane.showMessageDialog(null,"this game is developed by tendoeschate");
}
});
new1MenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent u)
{
JOptionPane.showMessageDialog(null,"simple game played by one player and AI with the help of mouse click");
}
});

saveMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent z)

{
JFrame frame=new JFrame();
JFileChooser fileSave = new JFileChooser();
fileSave.showSaveDialog(frame);
SaveFile1 s=new SaveFile1();
s.saveFile(fileSave.getSelectedFile());
}
});
}

public void actionPerformed(ActionEvent a) {


/*Write the letter to the button and deactivate it*/
for(int i = 1; i<= 9; i++){
if(a.getSource() == buttons[i]){
buttons[i].setText("X");
buttons[i].setEnabled(false);
buttons[i].setBackground(Color.WHITE);

}
}

count++;
AI();

}

public void AI(){
count++;
if(buttons[1].getText().equals("O") && buttons[2].getText().equals("O") && buttons[3].getText().equals("")){
buttons[3].setText("O");
buttons[3].setEnabled(false);
} else if(buttons[4].getText().equals("O") && buttons[5].getText().equals("O") && buttons[6].getText().equals("")){
buttons[6].setText("O");
buttons[6].setEnabled(false);
} else if(buttons[7].getText().equals("O") && buttons[8].getText().equals("O") && buttons[9].getText().equals("")){
buttons[9].setText("O");
buttons[9].setEnabled(false);
}

else if(buttons[2].getText().equals("O") && buttons[3].getText().equals("O") && buttons[1].getText().equals("")){
buttons[1].setText("O");
buttons[1].setEnabled(false);
} else if(buttons[5].getText().equals("O") && buttons[6].getText().equals("O") && buttons[4].getText().equals("")){
buttons[4].setText("O");
buttons[4].setEnabled(false);
} else if(buttons[8].getText().equals("O") && buttons[9].getText().equals("O") && buttons[7].getText().equals("")){
buttons[7].setText("O");
buttons[7].setEnabled(false);
}

else if(buttons[1].getText().equals("O") && buttons[3].getText().equals("O") && buttons[2].getText().equals("")){
buttons[2].setText("O");
buttons[2].setEnabled(false);
} else if(buttons[4].getText().equals("O") && buttons[6].getText().equals("O") && buttons[5].getText().equals("")){
buttons[5].setText("O");
buttons[5].setEnabled(false);
} else if(buttons[7].getText().equals("O") && buttons[9].getText().equals("O") && buttons[8].getText().equals("")){
buttons[8].setText("O");
buttons[8].setEnabled(false);
}

else if(buttons[1].getText().equals("O") && buttons[4].getText().equals("O") && buttons[7].getText().equals("")){
buttons[7].setText("O");
buttons[7].setEnabled(false);
} else if(buttons[2].getText().equals("O") && buttons[5].getText().equals("O") && buttons[8].getText().equals("")){
buttons[4].setText("O");
buttons[4].setEnabled(false);
} else if(buttons[3].getText().equals("O") && buttons[6].getText().equals("O") && buttons[9].getText().equals("")){
buttons[9].setText("O");
buttons[9].setEnabled(false);
}

else if(buttons[4].getText().equals("O") && buttons[7].getText().equals("O") && buttons[1].getText().equals("")){
buttons[1].setText("O");
buttons[1].setEnabled(false);
} else if(buttons[5].getText().equals("O") && buttons[8].getText().equals("O") && buttons[2].getText().equals("")){
buttons[2].setText("O");
buttons[2].setEnabled(false);
} else if(buttons[6].getText().equals("O") && buttons[9].getText().equals("O") && buttons[3].getText().equals("")){
buttons[3].setText("O");
buttons[3].setEnabled(false);
}

else if(buttons[1].getText().equals("O") && buttons[7].getText().equals("O") && buttons[4].getText().equals("")){
buttons[4].setText("O");
buttons[4].setEnabled(false);
} else if(buttons[2].getText().equals("O") && buttons[8].getText().equals("O") && buttons[5].getText().equals("")){
buttons[5].setText("O");
buttons[5].setEnabled(false);
} else if(buttons[3].getText().equals("O") && buttons[9].getText().equals("O") && buttons[6].getText().equals("")){
buttons[6].setText("O");
buttons[6].setEnabled(false);
}

else if(buttons[1].getText().equals("O") && buttons[5].getText().equals("O") && buttons[9].getText().equals("")){
buttons[9].setText("O");
buttons[9].setEnabled(false);
} else if(buttons[5].getText().equals("O") && buttons[9].getText().equals("O") && buttons[1].getText().equals("")){
buttons[1].setText("O");
buttons[1].setEnabled(false);
} else if(buttons[1].getText().equals("O") && buttons[9].getText().equals("O") && buttons[5].getText().equals("")){
buttons[5].setText("O");
buttons[5].setEnabled(false);
}

else if(buttons[3].getText().equals("O") && buttons[5].getText().equals("O") && buttons[7].getText().equals("")){
buttons[7].setText("O");
buttons[7].setEnabled(false);
} else if(buttons[7].getText().equals("O") && buttons[5].getText().equals("O") && buttons[3].getText().equals("")){
buttons[3].setText("O");
buttons[3].setEnabled(false);
} else if(buttons[7].getText().equals("O") && buttons[3].getText().equals("O") && buttons[5].getText().equals("")){
buttons[5].setText("O");
buttons[5].setEnabled(false);
}

else if(buttons[1].getText().equals("X") && buttons[2].getText().equals("X") && buttons[3].getText().equals("")){
buttons[3].setText("O");
buttons[3].setEnabled(false);
} else if(buttons[4].getText().equals("X") && buttons[5].getText().equals("X") && buttons[6].getText().equals("")){
buttons[6].setText("O");
buttons[6].setEnabled(false);
} else if(buttons[7].getText().equals("X") && buttons[8].getText().equals("X") && buttons[9].getText().equals("")){
buttons[9].setText("O");
buttons[9].setEnabled(false);
}

else if(buttons[2].getText().equals("X") && buttons[3].getText().equals("X") && buttons[1].getText().equals("")){
buttons[1].setText("O");
buttons[1].setEnabled(false);
} else if(buttons[5].getText().equals("X") && buttons[6].getText().equals("X") && buttons[4].getText().equals("")){
buttons[4].setText("O");
buttons[4].setEnabled(false);
} else if(buttons[8].getText().equals("X") && buttons[9].getText().equals("X") && buttons[7].getText().equals("")){
buttons[7].setText("O");
buttons[7].setEnabled(false);
}

else if(buttons[1].getText().equals("X") && buttons[3].getText().equals("X") && buttons[2].getText().equals("")){
buttons[2].setText("O");
buttons[2].setEnabled(false);
} else if(buttons[4].getText().equals("X") && buttons[6].getText().equals("X") && buttons[5].getText().equals("")){
buttons[5].setText("O");
buttons[5].setEnabled(false);
} else if(buttons[7].getText().equals("X") && buttons[9].getText().equals("X") && buttons[8].getText().equals("")){
buttons[8].setText("O");
buttons[8].setEnabled(false);
}

else if(buttons[1].getText().equals("X") && buttons[4].getText().equals("X") && buttons[7].getText().equals("")){
buttons[7].setText("O");
buttons[7].setEnabled(false);
} else if(buttons[2].getText().equals("X") && buttons[5].getText().equals("X") && buttons[8].getText().equals("")){
buttons[8].setText("O");
buttons[8].setEnabled(false);
} else if(buttons[3].getText().equals("X") && buttons[6].getText().equals("X") && buttons[9].getText().equals("")){
buttons[9].setText("O");
buttons[9].setEnabled(false);
}

else if(buttons[4].getText().equals("X") && buttons[7].getText().equals("X") && buttons[1].getText().equals("")){
buttons[1].setText("O");
buttons[1].setEnabled(false);
} else if(buttons[5].getText().equals("X") && buttons[8].getText().equals("X") && buttons[2].getText().equals("")){
buttons[2].setText("O");
buttons[2].setEnabled(false);
} else if(buttons[6].getText().equals("X") && buttons[9].getText().equals("X") && buttons[3].getText().equals("")){
buttons[3].setText("O");
buttons[3].setEnabled(false);
}

else if(buttons[1].getText().equals("X") && buttons[7].getText().equals("X") && buttons[4].getText().equals("")){
buttons[4].setText("O");
buttons[4].setEnabled(false);
} else if(buttons[2].getText().equals("X") && buttons[8].getText().equals("X") && buttons[5].getText().equals("")){
buttons[5].setText("O");
buttons[5].setEnabled(false);
} else if(buttons[3].getText().equals("X") && buttons[9].getText().equals("X") && buttons[6].getText().equals("")){
buttons[6].setText("O");
buttons[6].setEnabled(false);
}

else if(buttons[1].getText().equals("X") && buttons[5].getText().equals("X") && buttons[9].getText().equals("")){
buttons[9].setText("O");
buttons[9].setEnabled(false);
} else if(buttons[5].getText().equals("X") && buttons[9].getText().equals("X") && buttons[1].getText().equals("")){
buttons[1].setText("O");
buttons[1].setEnabled(false);
} else if(buttons[1].getText().equals("X") && buttons[9].getText().equals("X") && buttons[5].getText().equals("")){
buttons[5].setText("O");
buttons[5].setEnabled(false);
}

else if(buttons[3].getText().equals("X") && buttons[5].getText().equals("X") && buttons[7].getText().equals("")){
buttons[7].setText("O");
buttons[7].setEnabled(false);
} else if(buttons[7].getText().equals("X") && buttons[5].getText().equals("X") && buttons[3].getText().equals("")){
buttons[3].setText("O");
buttons[3].setEnabled(false);
} else if(buttons[7].getText().equals("X") && buttons[3].getText().equals("X") && buttons[5].getText().equals("")){
buttons[5].setText("O");
buttons[5].setEnabled(false);
}

else if(buttons[1].getText().equals("X") && buttons[5].getText().equals("O") && buttons[9].getText().equals("X")) {
buttons[6].setText("O");
buttons[6].setEnabled(false);
}

else if(buttons[3].getText().equals("X") && buttons[5].getText().equals("O") && buttons[7].getText().equals("X")) {
buttons[4].setText("O");
buttons[4].setEnabled(false);
}

else if(buttons[5].getText().equals("")){
buttons[5].setText("O");
buttons[5].setEnabled(false);
}

else if(buttons[1].getText().equals("")){
buttons[1].setText("O");
buttons[1].setEnabled(false);
}
else {
if(count >= 9)
checkWin();
else
RandomMove();
}

checkWin();

}

public void RandomMove(){
Random x = new Random();
int y = 1 + x.nextInt(9);
if(buttons[y].getText().equals("O") || buttons[y].getText().equals("X") ){
RandomMove();
} else {
buttons[y].setText("O");
buttons[y].setEnabled(false);
}
}

public void checkWin(){

/*Determine who won*/
for(int i=0; i<=7; i++){
if( buttons[winCombinations[i][0]].getText().equals(buttons[winCombinations[i][1]].getText()) &&
buttons[winCombinations[i][1]].getText().equals(buttons[winCombinations[i][2]].getText()) &&
!buttons[winCombinations[i][0]].getText().equals("")) {
win = true;
}
}

if(count % 2 == 0)
letter = "O";
else
letter = "X";

/*Show a dialog when game is over*/
if(win == true){
JOptionPane.showMessageDialog(null, letter + " wins!");
if(letter=="X")
{
Global1.check_wins++;
Global1.total_games++;
}
else if(letter=="O"){
Global1.check_wins1++;
Global1.total_games++;
}

} else if(count >= 9 && win == false){
Global1.total_games++;
JOptionPane.showMessageDialog(null, "The game was tie!");

}
}
public static void main(String[] args){
new TicTacToeV4();
}
}
class SaveFile1
{
JFrame frame=new JFrame();
JTextArea area=new JTextArea();
void saveFile(File file) {
frame.setTitle(file.getName() );
try {
FileWriter writer = new FileWriter(file);
writer.write(area.getText() );
writer.close();
} catch (IOException ex) {
System.out.println("cannot save file");
ex.printStackTrace();
}
}
public static void main(String h[])
{
}
}
class SimpleTableExample1 extends JFrame
{
// Instance attributes used in this example
private JPanel topPanel;
private JTable table;
private JScrollPane scrollPane;




public SimpleTableExample1()
{
// Set the frame characteristics
setTitle( "Simple Table Application" );
setSize( 300, 200 );
setBackground( Color.gray );

// Create a panel to hold all other components
topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
Global1.percentage_games=(((Global1.check_wins)/(Global1.total_games)))*100;
Global1.percentage_games1=(((Global1.check_wins1)/(Global1.total_games)))*100;

// Create columns names
String columnNames[] = { "Player", "Wins","PercentageWin","TotalGames" };

// Create some data
Double dataValues[][] =
{
{ 1.0,Global1.check_wins,Global1.percentage_games,Global1.total_games },
{ 2.0,Global1.check_wins1,Global1.percentage_games1,Global1.total_games },

};

// Create a new table instance
table = new JTable( dataValues, columnNames );

// Add the table to a scrolling pane
scrollPane = new JScrollPane( table );
topPanel.add( scrollPane, BorderLayout.CENTER );

}

// Main entry point for this example
public static void main( String args[] )
{
// Create an instance of the test application

}
}
class Global1 {

public static double check_wins=0;
public static double check_wins1=0;
public static double total_games=0;
public static double percentage_games;
public static double percentage_games1;
}

can yu suggest me changes in the project
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
UseCodeTags
ten doeschate wrote:can yu suggest me changes in the project

Changes to do what ? What do you think is wrong with the code ?
ten doeschate
Greenhorn

Joined: Jun 22, 2013
Posts: 16
i mean formatting changes
Tim Cooke
Bartender

Joined: Mar 28, 2008
Posts: 1001
    
  47

I'm not sure this is the right place to come for a free code review. If you have a specific problem or are unclear on any particular aspect of your code formatting then we will surely be able to help you. Otherwise I don't think you're going to get the response you were hoping for.


Tim Driven Development
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2409
    
    7

Swing GUI projects are practically putting components on a page/window. Grouping the interacting components together in a single java file is a good start.

One approach can be one file per the following:
JFrame main window
JMenuBar with JMenu and JMenuItems
JPanel represent whatever part of the main window
Action can be separated too (class can implement ActionListener or extends AbstractAction)
JDialog for popup messages
Status bar (a JPanel really) for status messages

The list can go on forever really depending on how "small" a distinct component you want.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
ten doeschate
Greenhorn

Joined: Jun 22, 2013
Posts: 16
can yu suggest me how to draw status bar
i tried it but not working
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2409
    
    7

ten doeschate wrote:can yu suggest me how to draw status bar
i tried it but not working


Simple (maybe for me). Like I said, it's just a JPanel.



Then somewhere else in your code probably the main JFrame need to have a getStatusBar() method to return an instance of this StatusBar then call the setMessage to update the message.
ten doeschate
Greenhorn

Joined: Jun 22, 2013
Posts: 16
error is coming : cannot find symbol
setMessage
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
ten doeschate wrote:error is coming : cannot find symbol
setMessage

TellTheDetails. All anyone can tell you from that information is that somewhere in your code you use the symbol setMessage in a context where the compiler doesn't know what setMessage is.
ten doeschate
Greenhorn

Joined: Jun 22, 2013
Posts: 16
public class StatusBar extends JPanel {
private JLabel messageLabel;
private String message;

public StatusBar() {
setLayout(new BorderLayout());
messageLabel = new JLabel();
messageLabel.setMessage("..."); // your default message
}

public String setMessage(String msg) {
messageLabel.setText(msg);
add(messageLabel, BorderLayout.WEST); // left side of JPanel
}

public String getMessage() {
return message;
}
}


here setMessage is used how to use it in the above program in the first thread enclosed
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41803
    
  62
JLabel has no method called setMessage. If you want to call your own method, remove the "messageLabel." in front of it.


Ping & DNS - my free Android networking tools app
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
As I said before - UseCodeTags

There's a small error in the code that K. Tsang provided. I'm sure (s)he put it in deliberately so that you had to understand the code and not just blindly copy it.
It calls the setMessage method on the JLabel variable, when it's actually a method of the StatusBar class.
So how do you think you need to change this line

so that it calls the StatusBar method ?
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2409
    
    7

ten doeschate wrote:here setMessage is used how to use it in the above program in the first thread enclosed


Actually I did give you a hint how to setMessage: get an instance of StatusBar class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: tictacttoe project1