import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
@SuppressWarnings("serial")
public class SimpleTicTacToe1 extends JPanel {
private static final int BUTTON_FONT_POINTS = 64;
private static final Font TTT_BTN_FONT = new Font(Font.SANS_SERIF, Font.BOLD, BUTTON_FONT_POINTS);
private static final String[] XO = {"X", "O"};
private JButton[][] tttButtons = new JButton[3][3];
private JFrame window = new JFrame("Tic-Tac-Toe");
private int buttonCount = 0;
public SimpleTicTacToe1() {
TttButtonListener tttBtnListener = new TttButtonListener();
/*Creates the menu bar*/
JMenuBar menuBar = new JMenuBar();
window.setJMenuBar(menuBar);
/*Creates "File" Button to Menu*/
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
JMenuItem aboutAction = new JMenuItem("About");
JMenuItem exitAction = new JMenuItem("Exit");
fileMenu.add(aboutAction);
fileMenu.addSeparator();
fileMenu.add(exitAction);
JPanel tttPanel = new JPanel(new GridLayout(3, 3, 5, 5));
for (int row = 0; row < tttButtons.length; row++) {
for (int col = 0; col < tttButtons[row].length; col++) {
JButton btn = new JButton(" ");
btn.setFont(TTT_BTN_FONT);
btn.addActionListener(tttBtnListener); // add listener to ttt button
tttPanel.add(btn);
tttButtons[row][col] = btn;
}
}
//all other non tictactoe buttons use anonymous inner classes
JButton resetButton = new JButton("Reset");
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
resetActionPerformed();
}
});
JButton aboutButton = new JButton("About");
aboutButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
aboutActionPerformed();
}
});
JButton exitButton = new JButton("Exit");
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Window win = SwingUtilities.getWindowAncestor(SimpleTicTacToe1.this);
win.dispose();
}
});
JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0));
buttonPanel.add(resetButton);
buttonPanel.add(aboutButton);
buttonPanel.add(exitButton);
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setLayout(new BorderLayout(5, 5));
add(tttPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.NORTH);
}
private void resetActionPerformed() {
buttonCount = 0;
for (JButton[] buttonRow : tttButtons) {
for (JButton button : buttonRow) {
button.setText("");
}
}
}
private void aboutActionPerformed() {
// TODO: show about action
String message = "Ryan's TicTacToe \nStructured Programming\nCopyright 2011\nAll rights reserved";
JOptionPane.showMessageDialog(null,message,"About TicTacToe", JOptionPane.INFORMATION_MESSAGE);
}
public void checkForWin() {
// TODO: complete this method
}
// private listener class, only for the tictactoe buttons
private class TttButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
if (button.getText().trim().isEmpty()) {
button.setText(XO[buttonCount]);
buttonCount++;
buttonCount %= 2;
checkForWin();
}
}
}
private static void createAndShowUI() {
JFrame frame = new JFrame("Simple TicTacToe");
frame.getContentPane().add(new SimpleTicTacToe());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
8 replies to this topic
#1
Posted 03 April 2011 - 09:13 PM
I have a code here that i have made and all of the features working fine like all the buttons and my menu items but when there is "three is a row" i want to try and have a text field on the bottom of the app that says who won either X OR O. Here is what i have so far lets see if you guys can help me out.
|
|
|
#2
Posted 04 April 2011 - 03:25 PM
It's really simple, check each row, each column and the 2 diagonals for matching symbols. There are numerous ways to do this. Here is 1 idea:
Similar code can be created for the columns, and the diagonal.
boolean checkRow(int rowNum) {
String x,y,z;
x = matrix[rowNum][0].getText().trim();
y = matrix[rowNum][1].getText().trim();
z = matrix[rowNum][2].getText().trim();
if( x.length() > 0 && x.equals(y) && y.equals(z) )
return true;
else
return false;
}
Similar code can be created for the columns, and the diagonal.
#3
Posted 04 April 2011 - 03:34 PM
lethalwire said:
It's really simple, check each row, each column and the 2 diagonals for matching symbols. There are numerous ways to do this. Here is 1 idea:
Similar code can be created for the columns, and the diagonal.
boolean checkRow(int rowNum) {
String x,y,z;
x = matrix[rowNum][0].getText().trim();
y = matrix[rowNum][1].getText().trim();
z = matrix[rowNum][2].getText().trim();
if( x.length() > 0 && x.equals(y) && y.equals(z) )
return true;
else
return false;
}
Similar code can be created for the columns, and the diagonal.
How would i output the result though.
#4
Posted 04 April 2011 - 03:55 PM
You've created the gui so well, I'm sure you can figure this part out too.
Just add a JTextField to the bottom of your frame/jpanel. Whatever you're using.
Make sure to call JTextField's setEditable(false) method.
After you've tested the winning solution AND if you have a winning solution
then call JTextField's method: setText( "x or o");
Just add a JTextField to the bottom of your frame/jpanel. Whatever you're using.
Make sure to call JTextField's setEditable(false) method.
After you've tested the winning solution AND if you have a winning solution
then call JTextField's method: setText( "x or o");
#5
Posted 04 April 2011 - 07:14 PM
*sigh* Oh Ryan.
Look what I've found.
Ryan you must learn to take people's advice a little better than you have been. In many cases, when people offer you pointers, they really are being helpful. When Fubarable informed you that == would not work with String comparisons, and to instead use .equals(), you did not heed this advice. You made no modification to you source code, and thus far seem to have only copied from source examples and snippets from the other users, occasionally performing some arbitrary transformation such as "TicTacToe" to "TicTacToe5", causing another bug someone helpfully mentioned. The reason people didn't try to help you in a way you found to be helpful is because we assumed you knew what the code you started with meant. Your original source from Java Forums was ultimately sourced from various other Java sources on the internet. I was satisfied by finding the first two, as sufficient evidence you weren't even sure what you had constructed.
You also seem to have misunderstood what I believe is the prevalent objective of people who offer help to others on forums. The reason there are so many who dutifully assist is that we want you to learn something. The objective is that you gain an understanding of what the code actually means, and not that "block of code X does this, and block of code Y does that." When you see a simple code example:
So, do you really want to learn this?
Look what I've found.
Ryan you must learn to take people's advice a little better than you have been. In many cases, when people offer you pointers, they really are being helpful. When Fubarable informed you that == would not work with String comparisons, and to instead use .equals(), you did not heed this advice. You made no modification to you source code, and thus far seem to have only copied from source examples and snippets from the other users, occasionally performing some arbitrary transformation such as "TicTacToe" to "TicTacToe5", causing another bug someone helpfully mentioned. The reason people didn't try to help you in a way you found to be helpful is because we assumed you knew what the code you started with meant. Your original source from Java Forums was ultimately sourced from various other Java sources on the internet. I was satisfied by finding the first two, as sufficient evidence you weren't even sure what you had constructed.
You also seem to have misunderstood what I believe is the prevalent objective of people who offer help to others on forums. The reason there are so many who dutifully assist is that we want you to learn something. The objective is that you gain an understanding of what the code actually means, and not that "block of code X does this, and block of code Y does that." When you see a simple code example:
JFrame frame = new JFrame();You should have a conceptual understanding of what actually occurred in the virtual machine. You should understand that a JFrame reference was created on the stack, called 'frame', and has been assigned the value of the address of a new JFrame object created at runtime. To understand programming, and to really learn this, the terminology I've been using should not be foreign to you. If you are willing to learn how this all actually works, there will be those willing to teach you. All I can hope to achieve with this post is to motivate you enough, through either realization, a sense of pride, spite, or frankly a little embarrassment, to try and really learn programming. To write your own novel code, and quite frankly of a few smaller projects than what you're tackling right now. It's okay to write small programs when you're learning, that's how you learn.
So, do you really want to learn this?
Wow I changed my sig!
#6
Posted 04 April 2011 - 07:21 PM
well im majoring in comp tech and this is one of the classes that was needed so literally i've had 0 experience with java and any coding involved it takes years to learn everything about java and obviously i havent had the time to even start looking at coding.
#7
Posted 04 April 2011 - 07:42 PM
Ryan09 said:
well im majoring in comp tech and this is one of the classes that was needed so literally i've had 0 experience with java and any coding involved it takes years to learn everything about java and obviously i havent had the time to even start looking at coding.
So what do you need? For everyone to do the work/thinking for you? :crying:
#8
Posted 04 April 2011 - 08:28 PM
Are you in a beginning Java course? These courses generally assume someone with no former programming experience, though I will admit they do tend to gloss over the basics far too quickly.
Wow I changed my sig!
#9
Posted 05 April 2011 - 06:18 AM
pretty much..no one teaches us they just give us codes to rewrite and then when task comes we have to take some of the things we went over and make a new application on our own. They dont teach us the basics thats why i have no idea what to do or where to even start.
Edited by Ryan09, 05 April 2011 - 11:56 AM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









