🔒 Closed Java tic tac toe code!

Status
Not open for further replies.
subukan moto sa eclipse... for eclipse na yang snipets...

private void gameScore()
{
txtCountX.setText(Integer.toString(xCount));
txtCountO.setText(Integer.toString(oCount));

}
 
aus na yong gamescore yan na yong code nya pero may mali naman dun sa main() nya.... subukan mo sa compiler mo baka gumana sayo kasi dito sa web na yan gumana....
 
akala ko sa netbean kaya gumawa ako ng sa netbean. drag and drop din ba ang gui ng eclipse?
yan ang diko alam ehh kasi di ko pa nasubukan ang eclipse... netbeans kasi ako ehh at yan din ang ginagamit namin sa school..... at isa pa hindi ako mahilig sa drag and drop ehhh... kasi mas gumagaling ako sa purong code lang... indi sa nagmamayabang ahhh... ganung kasi pag mga programmer ehhh.....
 
gumana po?

My Tic Tac Toe Program.....
My Own Program...
*No Bugs
*No Error
*2 Players
*Scoreboard
*Player Name
*Automatic Player Name (If name leave or set as blank)
*Its Working......


package tictactoe;

/**
*
* @author DicksonPaL21
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TicTacToe {

private int xCount = 0, oCount = 0;
private JButton btn1 = new JButton();
private JButton btn2 = new JButton();
private JButton btn3 = new JButton();
private JButton btn4 = new JButton();
private JButton btn5 = new JButton();
private JButton btn6 = new JButton();
private JButton btn7 = new JButton();
private JButton btn8 = new JButton();
private JButton btn9 = new JButton();
private JLabel Player = new JLabel("Player");
private JLabel Player1 = new JLabel();
private JLabel Player2 = new JLabel();
private JLabel Score = new JLabel("Score");
private JLabel ScoreP1 = new JLabel("0");
private JLabel ScoreP2 = new JLabel("0");
private JFrame frame = new JFrame();
private JPanel T_Panel = new JPanel();
private JPanel D_Panel = new JPanel();

public TicTacToe() {

Customization();

Player1.setText(JOptionPane.showInputDialog(null, "Player 1", "Player Name", JOptionPane.OK_OPTION));
Player2.setText(JOptionPane.showInputDialog(null, "Player 2", "Player Name", JOptionPane.OK_OPTION));

if (Player1.getText().equalsIgnoreCase("")) {
Player1.setText("Player 1");
}
if (Player2.getText().equalsIgnoreCase("")) {
Player2.setText("Player 2");
}

Player1.setForeground(Color.green);
Player2.setForeground(Color.green);
Player1.setEnabled(true);
Player2.setEnabled(false);

}
p

public void Customization() {
frame.setTitle("Tic Tac Toe");
frame.setSize(700, 550);
frame.setLayout(new BorderLayout(0, 1));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.add(T_Panel);
frame.add(D_Panel, BorderLayout.SOUTH);

T_Panel.setLayout(new GridLayout(3, 2));
T_Panel.add(Player);
T_Panel.add(Score);
T_Panel.add(Player1);
T_Panel.add(ScoreP1);
T_Panel.add(Player2);
T_Panel.add(ScoreP2);

D_Panel.setPreferredSize(new Dimension(frame.getWidth(), 450));
D_Panel.setLayout(new GridLayout(3, 3, 2, 2));
D_Panel.add(btn1); D_Panel.add(btn2); D_Panel.add(btn3);
D_Panel.add(btn4); D_Panel.add(btn5); D_Panel.add(btn6);
D_Panel.add(btn7); D_Panel.add(btn8); D_Panel.add(btn9);

btn1.setForeground(Color.LIGHT_GRAY);
btn1.setFont(new Font("Tahoma", Font.BOLD, 90));
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn1.setText("X");
btn1.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn1.setText("O");
btn1.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});

btn2.setForeground(Color.LIGHT_GRAY);
btn2.setFont(new Font("Tahoma", Font.BOLD, 90));
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn2.setText("X");
btn2.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn2.setText("O");
btn2.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});

btn3.setForeground(Color.LIGHT_GRAY);
btn3.setFont(new Font("Tahoma", Font.BOLD, 90));
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn3.setText("X");
btn3.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn3.setText("O");
btn3.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});

btn4.setForeground(Color.LIGHT_GRAY);
btn4.setFont(new Font("Tahoma", Font.BOLD, 90));
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn4.setText("X");
btn4.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn4.setText("O");
btn4.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});

btn5.setForeground(Color.LIGHT_GRAY);
btn5.setFont(new Font("Tahoma", Font.BOLD, 90));
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn5.setText("X");
btn5.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn5.setText("O");
btn5.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});

btn6.setForeground(Color.LIGHT_GRAY);
btn6.setFont(new Font("Tahoma", Font.BOLD, 90));
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn6.setText("X");
btn6.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn6.setText("O");
btn6.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});

btn7.setForeground(Color.LIGHT_GRAY);
btn7.setFont(new Font("Tahoma", Font.BOLD, 90));
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn7.setText("X");
btn7.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn7.setText("O");
btn7.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});

btn8.setForeground(Color.LIGHT_GRAY);
btn8.setFont(new Font("Tahoma", Font.BOLD, 90));
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn8.setText("X");
btn8.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn8.setText("O");
btn8.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});

btn9.setForeground(Color.LIGHT_GRAY);
btn9.setFont(new Font("Tahoma", Font.BOLD, 90));
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn9.setText("X");
btn9.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn9.setText("O");
btn9.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});


}

public void Scoring() {
if ( btn1.getText() == "X" && btn2.getText() == "X" && btn3.getText() == "X" ||
btn4.getText() == "X" && btn5.getText() == "X" && btn6.getText() == "X" ||
btn7.getText() == "X" && btn8.getText() == "X" && btn9.getText() == "X" ||
btn1.getText() == "X" && btn4.getText() == "X" && btn7.getText() == "X" ||
btn2.getText() == "X" && btn5.getText() == "X" && btn8.getText() == "X" ||
btn3.getText() == "X" && btn6.getText() == "X" && btn9.getText() == "X" ||
btn1.getText() == "X" && btn5.getText() == "X" && btn9.getText() == "X" ||
btn3.getText() == "X" && btn5.getText() == "X" && btn7.getText() == "X") {
xCount++; ScoreP1.setText(Integer.toString(xCount));
JOptionPane.showMessageDialog(null, "Player 1 wins", "TIC TAC TOE" ,JOptionPane.INFORMATION_MESSAGE);
Reset();
} else
if ( btn1.getText() == "O" && btn2.getText() == "O" && btn3.getText() == "O" ||
btn4.getText() == "O" && btn5.getText() == "O" && btn6.getText() == "O" ||
btn7.getText() == "O" && btn8.getText() == "O" && btn9.getText() == "O" ||
btn1.getText() == "O" && btn4.getText() == "O" && btn7.getText() == "O" ||
btn2.getText() == "O" && btn5.getText() == "O" && btn8.getText() == "O" ||
btn3.getText() == "O" && btn6.getText() == "O" && btn9.getText() == "O" ||
btn1.getText() == "O" && btn5.getText() == "O" && btn9.getText() == "O" ||
btn3.getText() == "O" && btn5.getText() == "O" && btn7.getText() == "O") {
oCount++; ScoreP2.setText(Integer.toString(oCount));
JOptionPane.showMessageDialog(null, "Player 2 wins", "TIC TAC TOE" ,JOptionPane.INFORMATION_MESSAGE);
Reset();
}
}

public void Reset() {
btn1.setText(null);
btn2.setText(null);
btn3.setText(null);
btn4.setText(null);
btn5.setText(null);
btn6.setText(null);
btn7.setText(null);
btn8.setText(null);
btn9.setText(null);
}

public static void main(String[] args) {
try {
new TicTacToe().frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
 
kung meron kang ibang alam na code about for free internet like shadowsocks ung back-end ung mismong pagprocess nya kahit wag na ung GUI share mo naman sakin.... para matulungan din kita..... kasi balak ko sanang gumawa ng GUI program ehhhh for pc..... balak ko sanang pagsamahin ung shadowsocks, postern config, ehi files, hpi files at SSH sa iisang program ehh..... para maganda...
 
Try
sir, satingin ko dito ka lang nag kamali sa gamescore mo........ mali yong pagkuha mo ng score ehhh....


//gawa mo sir

private void gameScore()
{
txtCountX.setText(String.valueOf(xCount++));
txtCountO.setText(String.valueOf(oCount++));

}



ito yong tama para makuha yong score ng mga winner at para macount din sila....
//gawa ko naman to sir

private void gameScore()
{
txtCountX.setText(xCount.toString());
txtCountO.setText(oCount.toString());

}


//// subukan mo nalang kc di ko pa na test o ehhh pero sa tingin ko gagana yan... kc kabisado ko na rin ang java ehhh kahit di ko na itest yan... hindi sa nagmamayabang ahhh... ang ibig ko lang sabihin may experience na rin ako sa java....




pero hindi ko rin sinasabi na expert na ako ahhh.....
Ito Kasi parang sakto SA calculation ko gawa Ng Yan Rin Yung project ko dati...if I remember ha..
 
My Tic Tac Toe Program.....
My Own Program...
*No Bugs
*No Error
*2 Players
*Scoreboard
*Player Name
*Automatic Player Name (If name leave or set as blank)
*Its Working......


package tictactoe;

/**
*
* @author DicksonPaL21
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TicTacToe {

private int xCount = 0, oCount = 0;
private JButton btn1 = new JButton();
private JButton btn2 = new JButton();
private JButton btn3 = new JButton();
private JButton btn4 = new JButton();
private JButton btn5 = new JButton();
private JButton btn6 = new JButton();
private JButton btn7 = new JButton();
private JButton btn8 = new JButton();
private JButton btn9 = new JButton();
private JLabel Player = new JLabel("Player");
private JLabel Player1 = new JLabel();
private JLabel Player2 = new JLabel();
private JLabel Score = new JLabel("Score");
private JLabel ScoreP1 = new JLabel("0");
private JLabel ScoreP2 = new JLabel("0");
private JFrame frame = new JFrame();
private JPanel T_Panel = new JPanel();
private JPanel D_Panel = new JPanel();

public TicTacToe() {

Customization();

Player1.setText(JOptionPane.showInputDialog(null, "Player 1", "Player Name", JOptionPane.OK_OPTION));
Player2.setText(JOptionPane.showInputDialog(null, "Player 2", "Player Name", JOptionPane.OK_OPTION));

if (Player1.getText().equalsIgnoreCase("")) {
Player1.setText("Player 1");
}
if (Player2.getText().equalsIgnoreCase("")) {
Player2.setText("Player 2");
}

Player1.setForeground(Color.green);
Player2.setForeground(Color.green);
Player1.setEnabled(true);
Player2.setEnabled(false);

}
p

public void Customization() {
frame.setTitle("Tic Tac Toe");
frame.setSize(700, 550);
frame.setLayout(new BorderLayout(0, 1));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.add(T_Panel);
frame.add(D_Panel, BorderLayout.SOUTH);

T_Panel.setLayout(new GridLayout(3, 2));
T_Panel.add(Player);
T_Panel.add(Score);
T_Panel.add(Player1);
T_Panel.add(ScoreP1);
T_Panel.add(Player2);
T_Panel.add(ScoreP2);

D_Panel.setPreferredSize(new Dimension(frame.getWidth(), 450));
D_Panel.setLayout(new GridLayout(3, 3, 2, 2));
D_Panel.add(btn1); D_Panel.add(btn2); D_Panel.add(btn3);
D_Panel.add(btn4); D_Panel.add(btn5); D_Panel.add(btn6);
D_Panel.add(btn7); D_Panel.add(btn8); D_Panel.add(btn9);

btn1.setForeground(Color.LIGHT_GRAY);
btn1.setFont(new Font("Tahoma", Font.BOLD, 90));
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn1.setText("X");
btn1.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn1.setText("O");
btn1.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});

btn2.setForeground(Color.LIGHT_GRAY);
btn2.setFont(new Font("Tahoma", Font.BOLD, 90));
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn2.setText("X");
btn2.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn2.setText("O");
btn2.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});

btn3.setForeground(Color.LIGHT_GRAY);
btn3.setFont(new Font("Tahoma", Font.BOLD, 90));
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn3.setText("X");
btn3.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn3.setText("O");
btn3.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});

btn4.setForeground(Color.LIGHT_GRAY);
btn4.setFont(new Font("Tahoma", Font.BOLD, 90));
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn4.setText("X");
btn4.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn4.setText("O");
btn4.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});

btn5.setForeground(Color.LIGHT_GRAY);
btn5.setFont(new Font("Tahoma", Font.BOLD, 90));
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn5.setText("X");
btn5.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn5.setText("O");
btn5.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});

btn6.setForeground(Color.LIGHT_GRAY);
btn6.setFont(new Font("Tahoma", Font.BOLD, 90));
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn6.setText("X");
btn6.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn6.setText("O");
btn6.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});

btn7.setForeground(Color.LIGHT_GRAY);
btn7.setFont(new Font("Tahoma", Font.BOLD, 90));
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn7.setText("X");
btn7.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn7.setText("O");
btn7.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});

btn8.setForeground(Color.LIGHT_GRAY);
btn8.setFont(new Font("Tahoma", Font.BOLD, 90));
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn8.setText("X");
btn8.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn8.setText("O");
btn8.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});

btn9.setForeground(Color.LIGHT_GRAY);
btn9.setFont(new Font("Tahoma", Font.BOLD, 90));
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(Player1.isEnabled()) {
btn9.setText("X");
btn9.setForeground(Color.RED);
Player1.setEnabled(false);
Player2.setEnabled(true);
Scoring();
} else
if (Player2.isEnabled()) {
btn9.setText("O");
btn9.setForeground(Color.BLUE);
Player1.setEnabled(true);
Player2.setEnabled(false);
Scoring();
}
}
});


}

public void Scoring() {
if ( btn1.getText() == "X" && btn2.getText() == "X" && btn3.getText() == "X" ||
btn4.getText() == "X" && btn5.getText() == "X" && btn6.getText() == "X" ||
btn7.getText() == "X" && btn8.getText() == "X" && btn9.getText() == "X" ||
btn1.getText() == "X" && btn4.getText() == "X" && btn7.getText() == "X" ||
btn2.getText() == "X" && btn5.getText() == "X" && btn8.getText() == "X" ||
btn3.getText() == "X" && btn6.getText() == "X" && btn9.getText() == "X" ||
btn1.getText() == "X" && btn5.getText() == "X" && btn9.getText() == "X" ||
btn3.getText() == "X" && btn5.getText() == "X" && btn7.getText() == "X") {
xCount++; ScoreP1.setText(Integer.toString(xCount));
JOptionPane.showMessageDialog(null, "Player 1 wins", "TIC TAC TOE" ,JOptionPane.INFORMATION_MESSAGE);
Reset();
} else
if ( btn1.getText() == "O" && btn2.getText() == "O" && btn3.getText() == "O" ||
btn4.getText() == "O" && btn5.getText() == "O" && btn6.getText() == "O" ||
btn7.getText() == "O" && btn8.getText() == "O" && btn9.getText() == "O" ||
btn1.getText() == "O" && btn4.getText() == "O" && btn7.getText() == "O" ||
btn2.getText() == "O" && btn5.getText() == "O" && btn8.getText() == "O" ||
btn3.getText() == "O" && btn6.getText() == "O" && btn9.getText() == "O" ||
btn1.getText() == "O" && btn5.getText() == "O" && btn9.getText() == "O" ||
btn3.getText() == "O" && btn5.getText() == "O" && btn7.getText() == "O") {
oCount++; ScoreP2.setText(Integer.toString(oCount));
JOptionPane.showMessageDialog(null, "Player 2 wins", "TIC TAC TOE" ,JOptionPane.INFORMATION_MESSAGE);
Reset();
}
}

public void Reset() {
btn1.setText(null);
btn2.setText(null);
btn3.setText(null);
btn4.setText(null);
btn5.setText(null);
btn6.setText(null);
btn7.setText(null);
btn8.setText(null);
btn9.setText(null);
}

public static void main(String[] args) {
try {
new TicTacToe().frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
I try your codes sir, it worked! but I already passed my project last week. But thank you for your help and concern. I really appreciate it. If I need help, can I ask you again hehe.
 
Status
Not open for further replies.

About this Thread

  • 45
    Replies
  • 2K
    Views
  • 8
    Participants
Last reply from:
DicksonPaL_021

Trending Topics

Online now

Members online
500
Guests online
1,203
Total visitors
1,703

Forum statistics

Threads
2,273,054
Posts
28,947,292
Members
1,236,562
Latest member
primojace19
Back
Top