Results 41 to 52 of 52
- 04-15-2011, 04:37 AM #41
Member
- Join Date
- Mar 2011
- Posts
- 42
- Rep Power
- 0
- 04-15-2011, 04:53 AM #42
Member
- Join Date
- Apr 2011
- Location
- Canada!
- Posts
- 30
- Rep Power
- 0
Java Code:import java.awt.*; import java.awt.event.*; import java.awt.font.*; import java.util.ArrayList; import javax.swing.*; //import java.text.*; public class tictactoe extends Frame { //Color red = new Color(153,255,255);; Font font = new Font("Dialog", Font.BOLD, 18); Font font2 = new Font("Dialog", Font.ITALIC, 20); String turn = "O"; //value is either "O" or "X" Panel board; TicTacToeButton button[][]; Label playerTurn; ButtonHandler bH = new ButtonHandler(); int usedCells = 0; //number of cells in use public static ArrayList<TicTacToeButton> allButtons = new ArrayList<TicTacToeButton>(); [COLOR="Red"] public static ArrayList<tictactoe> allGames = new ArrayList<tictactoe>();[/COLOR] tictactoe(String title) { super(title); board = new Panel(); button = new TicTacToeButton[3][]; playerTurn = new Label("Player 1's Turn"); playerTurn.setFont(font2); playerTurn.setForeground(Color.white); [COLOR="Red"] allGames.add(this);[/COLOR] } public void launchGame() { setBackground(Color.red); board.setLayout(new GridLayout(3, 3, 6, 6)); //setMenuBar// MenuBar menuBar = new MenuBar(); setMenuBar(menuBar); Menu fileMenu = new Menu("File"); menuBar.add(fileMenu); MenuItem clearAction = new MenuItem("Clear"); MenuItem aboutAction = new MenuItem("About"); MenuItem exitAction = new MenuItem("Exit"); fileMenu.add(clearAction); fileMenu.add(aboutAction); fileMenu.add(exitAction); exitAction.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String arg = e.getActionCommand(); { if (arg.equals("Exit")) { System.exit(0); } } } }); aboutAction.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent f) { String arg = f.getActionCommand(); { if (arg.equals("About")) { String message = " Shawn's TicTacToe \n Structured Programming Java"; JOptionPane.showMessageDialog(null, message, "About Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE); } } } }); clearAction.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent g) { String arg = g.getActionCommand(); { if (arg.equals("Clear")) { // for (int i = 0, count = 0; i < 3; i++) { // button[i] = new TicTacToeButton[3]; // for (int j = 0; j < 3; j++, count++) { // button[i][j].setLabel(""); // // // //} //} for (TicTacToeButton current : allButtons) { current.setLabel(" "); } [COLOR="Red"] allGames.get(0).reinitialize(); allGames.get(0).launchGame();[/COLOR] } } } }); //clearAction.setActionCommand("Clear"); //aboutAction.setActionCommand("About"); //exitAction.setActionCommand("Exit"); /* initialize buttons */ for (int i = 0, count = 0; i < 3; i++) { button[i] = new TicTacToeButton[3]; for (int j = 0; j < 3; j++, count++) { button[i][j] = new TicTacToeButton(); allButtons.add(button[i][j]); /* inialize button values to 0,1, 2,...,8 */ button[i][j].value = (new Integer(count)).toString(); board.add(button[i][j]); /* add listeners to the buttons */ button[i][j].addActionListener(bH); button[i][j].setFont(font); button[i][j].setForeground(Color.green); button[i][j].setBackground(Color.black); } } add(board); board.setSize(300, 300); add(playerTurn, BorderLayout.SOUTH); /* add listeners for closing the frame */ addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent event) { System.exit(0); } }); /* display */ pack(); setVisible(true); } class ButtonHandler implements ActionListener { void changeButtonLabel(TicTacToeButton b) { b.setLabel(turn); b.value = turn; /* remove listener */ b.removeActionListener(bH); usedCells++; } public void actionPerformed(ActionEvent ae) { boolean win = false; boolean draw = false; int player = 0; TicTacToeButton b = (TicTacToeButton) ae.getSource(); /* change content of button to O or X */ changeButtonLabel(b); /* check for a win */ /* horizontal */ for (int i = 0; i < 3; i++) { if ((button[i][0].value).equals(button[i][1].value) && (button[i][0].value).equals(button[i][2].value)) { win = true; } } /* vertical */ for (int i = 0; i < 3; i++) { if ((button[0][i].value).equals(button[1][i].value) && (button[0][i].value).equals(button[2][i].value)) { win = true; } } /* diagonal */ if ((button[0][0].value).equals(button[1][1].value) && (button[0][0].value).equals(button[2][2].value)) { win = true; } else if ((button[0][2].value).equals(button[1][1].value) && (button[0][2].value).equals(button[2][0].value)) { win = true; } /* check for a draw */ if (!win) { if (usedCells == 9) { draw = true; } } /* Change message */ String message = ""; //continued on next page if (win) { if (turn.equals("O")) { player = 1; } else { player = 2; } message += "Player " + player + " wins!"; /* remove all listeners */ for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { button[i][j].removeActionListener(bH); } } } else if (draw) { message += "It's a draw!"; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { button[i][j].removeActionListener(bH); } } } else { /* change turn */ if (turn.equals("O")) { //setBackground(Color.red); turn = new String("X"); player = 2; message += "Player 2's Turn"; } else { turn = new String("O"); player = 1; message += "Player 1's Turn"; } } playerTurn.setText(message); } } class TicTacToeButton extends Button { String value; } public static void main(String args[]) { tictactoe game = new tictactoe("Tic-Tac-Toe"); game.launchGame(); } [COLOR="Red"] public void reinitialize(){ this.dispose(); this.board.removeAll(); allButtons.clear(); }[/COLOR] }
I've fixed it, but its a hack on a hack. Now you can continue playing after pressing Clear. I highlighted my new additions in red.
Thx for the hint on CODE tags, whoever it was. I think I saw you on another gaming forum as well, a fellow game development enthusiast I see :D!Last edited by Maximus-EVG; 04-15-2011 at 05:00 AM. Reason: forgot to highlight change in constructor. Also, this whole class can benefit from being a singleton.
- 04-15-2011, 05:08 AM #43
Member
- Join Date
- Mar 2011
- Posts
- 42
- Rep Power
- 0
Cool thanks. One hint..when the game restarts I need it to add playerTurn.setText (" ");
So it says Player 1's Turn when the game shows.
- 04-15-2011, 05:44 AM #44
Member
- Join Date
- Mar 2011
- Posts
- 42
- Rep Power
- 0
I basically have it so whoever wins it sets the text to Player " " Wins! and itcarries over to the next board. Do you know how I can fix this so it just says Player 1's Turn
- 04-15-2011, 06:18 AM #45
- 04-15-2011, 06:55 AM #46
Member
- Join Date
- Mar 2011
- Posts
- 42
- Rep Power
- 0
Duhh :p ugghh long night!
- 04-15-2011, 07:12 AM #47
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
@maximus, please don't spoonfeed. I've seen a couple of posts where you gave the answer. While it may appear helpful, it really doesn't help posters to give them the direct answer. Copy/paste is not learning.
- 04-15-2011, 07:21 AM #48
Member
- Join Date
- Mar 2011
- Posts
- 42
- Rep Power
- 0
Im glad he could help. Now I have learned some new code I would have never seen before!
- 04-15-2011, 07:21 AM #49
- 04-16-2011, 01:57 AM #50
Member
- Join Date
- Apr 2011
- Location
- Canada!
- Posts
- 30
- Rep Power
- 0
Hey sunde887,
If you look at the first page the guy wasn't able to understand where the import was missing when I first helped him fix the game. How will an absolute beginner who's trying to mod someones game going to understand how to do it without any examples? I don't believe I spoon fed him per say, but I should have included detailed comments outlining why my changes fixed it, as that definitely aids understanding more than just looking at it by yourself. I'm a believer in step-by-step learning :P. Is there a forum rule against guiding people by showing the fix, and explaining how to do it :/?
Also on the first page, your suggestion to his questions was to format the code properly. This isn't the prettiest code I saw, but, did you even bother to go through it once? That's not the kind of suggestion I would say to my boss on the job man :D.
- 04-16-2011, 02:14 AM #51
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I didn't read it entirely. I scanned it, I am not going to waste time going through that much unformatted code. My suggestion was meant to show him a way to receive more help. While I do not know whether it's a rule to not spoonfeed people, I personally(as well as many others, I'm sure) tend to favor helping them figure it out, rather then giving them the answer. Call me lazy, but lack of formatting tends to bother me, I will read through formatted code, but when I come across un formatted code on my phone my first comment tends to ask the user to properly format code.
Of course he is going to say he understands what he was missing, maybe he does. The thing is, if you helped him come to this realization without simply highlighting and correcting the code perhaps he would also have picked up a deeper understanding of the fix. Spoonfeeding leads to people coming here with every little problem they have. Why figure it out yourself if someone will eventually give you the answer?
Step by step learning is different then what you did. Perhaps I am nitpicking, and I do occasionally spoon feed a little, but in the long run, I believe it will hurt the user more than it will help.
- 04-16-2011, 02:34 AM #52
Member
- Join Date
- Apr 2011
- Location
- Canada!
- Posts
- 30
- Rep Power
- 0
I somewhat agree with you, but then, in how many steps could I have divided those 3 lines of fix to make him really understand what is happening? Real learning is always up to the individual himself, the onus is on Bundy to experiment with the game/fix and figure it out. The thing is, if he were to get no help at all, or purely hints for 3 pages of the thread, that's pretty disheartening to go through. As for crappily formatted code, man...Im used to it :( ;)
Similar Threads
-
connection = DriverManager.getConnection(DATABASE_URL,'"+userid +"','"+password+"');
By renu in forum New To JavaReplies: 3Last Post: 10-12-2010, 04:21 PM -
How to change my form design from "metal" to "nimbus" in Netbeans 6.7.1?
By mlibot in forum New To JavaReplies: 1Last Post: 01-21-2010, 09:20 AM -
problem with argument list and precedence "(" and ")"
By helpisontheway in forum Advanced JavaReplies: 6Last Post: 12-24-2009, 07:50 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks