Edit: check next post, I didn't realize I posted twice sorry about this!
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Edit: check next post, I didn't realize I posted twice sorry about this!
I am here because I don't understand how to proceed with the code I need to set up.
I would like to know the best way to get this code running the easiest way possible.
I am not just looking for a solved code but how and why the code was written the way it was.
This program needs to be in the applet format, thank you all in advance!
A few requirements are:
- Use grid layout
- add a random method to simulate another player
- use X's and O's to display who moved where.
- program tells you who wins.
Edit:I updated the code in the top post for all new people who hasn't seen it yet.
All I need now is to implement the computer simulation.
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JApplet; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.LineBorder; public class TicTacToe extends JApplet { private char whoseTurn = 'X'; private Cell[][] cells = new Cell[3][3]; private JLabel jlblStatus = new JLabel("X's turn to play"); public TicTacToe() { JPanel p = new JPanel(new GridLayout(3, 3, 0, 0)); for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) p.add(cells[i][j] = new Cell()); p.setBorder(new LineBorder(Color.red, 1)); jlblStatus.setBorder(new LineBorder(Color.yellow, 1)); add(p, BorderLayout.CENTER); add(jlblStatus, BorderLayout.SOUTH); } //Check to see if the spot is filled public boolean isFull() { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) if (cells[i][j].getToken() == ' ') return false; return true; } //Check for a winner public boolean isWon(char token) { for (int i = 0; i < 3; i++) if ((cells[i][0].getToken() == token) && (cells[i][1].getToken() == token) && (cells[i][2].getToken() == token)) { return true; } for (int j = 0; j < 3; j++) if ((cells[0][j].getToken() == token) && (cells[1][j].getToken() == token) && (cells[2][j].getToken() == token)) { return true; } if ((cells[0][0].getToken() == token) && (cells[1][1].getToken() == token) && (cells[2][2].getToken() == token)) { return true; } if ((cells[0][2].getToken() == token) && (cells[1][1].getToken() == token) && (cells[2][0].getToken() == token)) { return true; } return false; } public class Cell extends JPanel { private char token = ' '; public Cell() { setBorder(new LineBorder(Color.black, 1)); addMouseListener(new MyMouseListener()); } public char getToken() { return token; } public void setToken(char c) { token = c; repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); if (token == 'X') { g.drawLine(10, 10, getWidth() - 10, getHeight() - 10); g.drawLine(getWidth() - 10, 10, 10, getHeight() - 10); } else if (token == '0') { g.drawOval(10, 10, getWidth() - 20, getHeight() - 20); } } private class MyMouseListener extends MouseAdapter { public void mouseClicked(MouseEvent e) { if (token == ' ' && whoseTurn != ' ') { setToken(whoseTurn); if (isWon(whoseTurn)) { jlblStatus.setText(whoseTurn + " won! The game is over"); whoseTurn = ' '; } else if (isFull()) { jlblStatus.setText("Draw! The game is over"); whoseTurn = ' '; } else { whoseTurn = (whoseTurn == 'X') ? '0' : 'X'; jlblStatus.setText(whoseTurn + "'s turn"); } } } } } }
Last edited by bulletsster; February 22nd, 2013 at 11:40 AM. Reason: I edited the code this is the new updated code.
This first problem I see is that the code is not written with the methods an applet requires.
See the tutorial:
Lesson: Java Applets (The Java™ Tutorials > Deployment)
If you don't understand my answer, don't ignore it, ask a question.
I guess I have no idea about this my professor didn't really go into the difference between an applet and an application window. I will read up on my book and try to figure out what all I need as well as read more on the link you posted thank you.
Edit:I updated the code in the top post for all new people who hasn't seen it yet.
All I need now is to implement the computer simulation.
I went ahead and updated it, I'm still working on it but any suggestions would be good
Edit: I created the computer but not sure how to implement it. Can anyone help me figure out how to fit it in there properly?
I suggest that you do what Norm has already suggested: read the applet tutorial that he's already linked to, because you're still way off the mark. Don't just make up code. Read the tutorial first and then if anything in the tutorial confuses you, please come and ask us about it. Until you understand the information in the tutorial, I don't think we'll be able to help you much.