Results 1 to 7 of 7
Like Tree3Likes
  • 1 Post By JosAH
  • 1 Post By kaydell2
  • 1 Post By JosAH

Thread: Tic Tac Toe Game

  1. #1
    akshaygoel96 is offline Member
    Join Date
    Jan 2013
    Location
    Kent , Washington
    Posts
    7
    Rep Power
    0

    Post Tic Tac Toe Game

    i really don't know , how I am going to check if any player wins or not..... please help me


    package TicTacToe;
    import javax.swing.Timer;


    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.Timer;

    // just the class method public class TicTacToe extends Frame
    {
    // static methods
    static Frame myFrame;
    static int[] gridMark = new int[9];
    static boolean xTurn = true;
    static int numberClicks = 0;
    static Timer timerGame;

    public static void main(String[] args)
    {
    // create frame
    myFrame = new Frame();
    myFrame.setSize(300, 400);
    myFrame.setTitle("Tic-Tac-Toe - X's Turn");
    myFrame.setVisible(true);


    // Grid selected
    // draw and initialize grid

    for (int i = 0; i < 9; i++)
    {
    gridMark[i] = 0;
    }

    // add listener for closing frame
    myFrame.addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }});

    // timer to display lines
    timerGame = new Timer(25, new ActionListener ()
    {
    public void actionPerformed(ActionEvent e)
    {
    Graphics g = myFrame.getGraphics();
    g.setColor(Color.black);
    g.drawLine(110, 40, 110, 280);
    g.drawLine(190, 40, 190, 280);
    g.drawLine(30, 120, 270, 120);
    g.drawLine(30, 200, 270, 200);

    timerGame.stop();
    }});
    timerGame.start();

    // timer methods ends



    // add listener for mouse press
    myFrame.addMouseListener(new MouseAdapter()
    {
    public void mousePressed(MouseEvent e)
    {


    int gridSelected;
    int x = e.getX();
    int y = e.getY();
    // if we haven't clicked 9 times, can still click
    if (numberClicks < 9 && x > 30 && x < 270 && y > 40 && y < 280)
    {
    /* determine which grid location was clicked
    * each square is 80 pixels x 80 pixels
    * offset by 30 on right and 40 on top
    * number system:
    * 0 | 1 | 2
    * -----------
    * 3 | 4 | 5
    * -----------
    * 6 | 7 | 8
    */

    if (y > 200)
    {
    // one of three bottom grids
    gridSelected = 6 + (x - 30) / 80;
    y = 210;
    }
    else if (y > 120)
    {
    // one of three middle grids
    gridSelected = 3 + (x - 30) / 80;
    y = 130;
    }
    else
    {
    // one of three top grids
    gridSelected = (x - 30) / 80;
    y = 50;
    }

    // again if else statement starts..
    // if nothing there, can draw new mark

    if (gridMark[gridSelected] == 0)
    {
    numberClicks = numberClicks + 1;
    Graphics myGraphics = myFrame.getGraphics();
    // decide where to draw mark
    x = 40 + (gridSelected % 3) * 80;
    if (xTurn)
    {
    // draw X
    gridMark[gridSelected] = 3;
    myGraphics.setColor(Color.blue);
    myGraphics.drawLine(x, y, x + 60, y + 60);
    myGraphics.drawLine(x, y + 60, x + 60, y);
    // check winner()
    xTurn = false;
    myFrame.setTitle("Tic-Tac-Toe - O's Turn");
    }
    else
    {
    // draw O
    gridMark[gridSelected] = 5;
    myGraphics.setColor(Color.red);
    myGraphics.drawOval(x, y, 60, 60);
    // check winner
    xTurn = true;
    myFrame.setTitle("Tic-Tac-Toe - X's Turn");
    }
    // methods to check if there is no move remain
    if (numberClicks == 9)
    {
    myFrame.setTitle("Tic-Tac-Toe - Game Over");


    }

    }// for the second one
    }}
    } );
    }

    public void checkWinner()
    {

    }
    public void displayGrid()
    {

    }// end of main
    }
    Last edited by akshaygoel96; 01-07-2013 at 06:08 PM.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Tic Tac Toe Game

    Hello and welcome! Please use [code][/code] tags when posting code so we can easily read it!

    Forum Rules
    Guide For New Members
    BB Code List - Java Programming Forum
    How to ask smart questions

  3. #3
    akshaygoel96 is offline Member
    Join Date
    Jan 2013
    Location
    Kent , Washington
    Posts
    7
    Rep Power
    0

    Default Re: Tic Tac Toe Game

    i really don't understand , can you show me please , how to post

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: Tic Tac Toe Game

    Quote Originally Posted by akshaygoel96 View Post
    i really don't know , how I am going to check if any player wins or not..... please help me
    Why don't you define an array with the winning combination positions? Something like this will do:

    Java Code:
    private int[][] wp=  { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 0, 3, 6 }, { 1, 4, 7 }, { 2, 5, 8 }, { 0, 5, 8 }, { 2, 5, 7 } };
    kind regards,

    Jos
    akshaygoel96 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    akshaygoel96 is offline Member
    Join Date
    Jan 2013
    Location
    Kent , Washington
    Posts
    7
    Rep Power
    0

    Default Re: Tic Tac Toe Game

    Quote Originally Posted by JosAH View Post
    Why don't you define an array with the winning combination positions? Something like this will do:

    Java Code:
    private int[][] wp=  { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 0, 3, 6 }, { 1, 4, 7 }, { 2, 5, 8 }, { 0, 5, 8 }, { 2, 5, 7 } };
    kind regards,

    Jos

    , for every one , like diagonally , horizontally , vertically

  6. #6
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    107
    Rep Power
    0

    Default Re: Tic Tac Toe Game

    I think that some things about this Tic-Tac-Toe program would have been easier if the board were declared as a 3 by 3 array instead of a one-dimensional array, but I know that you have a lot of code written that you don't want to change at this point.

    I can see you having the following methods for the program to determine who the winner is:

    Java Code:
    	/**
    	 * @param index0 the index of the first square to be checked
    	 * @param index1 the index of the second square to be checked
    	 * @param index2 the index of the third square to be checked
    	 * @return either the int for X or Oh if they won, otherwise zero
    	 */
    	private int threeInARow(int index0, int index1, int index2) {
    	}
    	
    	/**
    	 * @return return the int for X or Oh if they won by having three in a row by columns, otherwise zero is returned
    	 * This method calls threeInARow() three times to search for a winner
    	 */
    	private int winningColumns() {
    	}
    	
    	/**
    	 * @return returns the int for X or Oh if they won by three in a row, otherwise zero is returned
    	 * This method calls threeInARow() three times to search for a winner
    	 */
    	private int winningRows() {
    	}
    	
    	/**
    	 * @return return the int for X or Oh if th ey won, otherwise zero is returned
    	 * This method calls threeInARow() two times to search for a win on one of the diagonals
    	 */
    	private int winningDiagonals() {
    	}
    
    	/*
    	 * @return returns the int for X or Oh if they won by having three in a row by any means, otherwise a zero is returned
    	 * This method calls winningColumns() winningRows and winningDiagonals to find a winner
    	 */
    	public int checkWinner() {
    	}
    akshaygoel96 likes this.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: Tic Tac Toe Game

    Quote Originally Posted by kaydell2 View Post
    I think that some things about this Tic-Tac-Toe program would have been easier if the board were declared as a 3 by 3 array instead of a one-dimensional array, but I know that you have a lot of code written that you don't want to change at this point.
    The two indexing methods are completely equivalent, i.e. array1D[x] == array2D[x/3][x%3] and array2D[x][y] == array1D[3*x+y]

    kind regards,

    Jos
    akshaygoel96 likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 4
    Last Post: 07-08-2012, 09:07 AM
  2. Replies: 1
    Last Post: 06-02-2011, 09:29 AM
  3. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  4. game code for any game
    By deathnote202 in forum Java Gaming
    Replies: 4
    Last Post: 06-10-2010, 08:06 AM
  5. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 06:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •