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.

Results 1 to 6 of 6

Thread: Tic Tac Toe GUI applet help!

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Tic Tac Toe GUI applet help!

    Edit: check next post, I didn't realize I posted twice sorry about this!


  2. #2
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Tic Tac Toe GUI applet help!

    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 10:40 AM. Reason: I edited the code this is the new updated code.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,327
    Thanks
    49
    Thanked 2,194 Times in 2,167 Posts

    Default Re: Tic Tac Toe GUI applet help!

    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.

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tic Tac Toe GUI applet help!

    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.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tic Tac Toe GUI applet help!

    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?

    Random comp = new Random();		
    		for (int i = 0; i < 99; i++) {
    			int compMoves = (int) comp.nextInt(10);
    		}

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Tic Tac Toe GUI applet help!

    Quote Originally Posted by bulletsster View Post
    I went ahead and updated it, I'm still working on it but any suggestions would be good
    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.

Similar Threads

  1. [SOLVED] Tic Tac Toe GUI program
    By TSPARR in forum AWT / Java Swing
    Replies: 10
    Last Post: May 7th, 2012, 07:09 PM
  2. Easy Tic Tac Toe game and Applet Assignment
    By Jmastersam in forum Java Theory & Questions
    Replies: 2
    Last Post: April 28th, 2012, 10:06 AM
  3. Tic-Tac-Toe Help
    By coke32 in forum Object Oriented Programming
    Replies: 13
    Last Post: March 12th, 2012, 07:59 AM
  4. Need help with Tic Tac Toe assignment
    By BAL1990 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 28th, 2011, 12:33 PM
  5. [SOLVED] Tic Toe java code exists when we enter 0 row and 0 column
    By big_c in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 13th, 2009, 07:16 AM