Results 1 to 3 of 3

Thread: Help ....!!!

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

    Default Help ....!!!

    please i need help with the part of computer , means , i wanna make this game now , computer vs player....
    this is simple code , player vs player , which is also restarting the game , after the one player wins , or gets tie , need help with computer one
    package TicTacToe;
    import javax.swing.Timer;
    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.Timer;

    just the starting

    public class TicTacToe extends Frame
    {
    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, 350);
    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;
    }

    myFrame.requestFocus();
    // add listener for closing frame
    myFrame.addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    });
    the timer method to get the lines

    timerGame = new Timer(25, new ActionListener ()
    {
    public void actionPerformed(ActionEvent e)
    {
    Graphics g = myFrame.getGraphics();

    g.clearRect(0,0,myFrame.getWidth(), myFrame.getHeight());
    page1(g);

    timerGame.stop();
    }});

    timerGame.start();




    // 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()
    if(!checkWinner())
    {
    xTurn = false;
    myFrame.setTitle("TicTacTOE - o's turn");
    }}
    else
    {
    // draw O
    gridMark[gridSelected] = 5;
    myGraphics.setColor(Color.red);
    myGraphics.drawOval(x, y, 60, 60);
    if(!checkWinner())
    {
    xTurn = true;
    myFrame.setTitle("TicTacTOE - x's turn");
    }



    }
    if (numberClicks == 9 )
    {
    myFrame.setTitle("Tic-Tac-Toe - its a tie");

    Graphics g = myFrame.getGraphics();
    Restart();
    } }// for the second one
    }
    //Method to reset

    if(numberClicks == 9 || numberClicks== 10)
    {
    if(x> myFrame.getWidth()/ 2 - 50 &&
    x < myFrame.getWidth()/2 + 50 &&
    y > 300 && y < 340)
    {
    reset();
    }
    }}
    });
    }
    drawing lines method
    public static void page1(Graphics g)

    {
    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);
    }// end of main

    method just to show the restart button on the screen
    public static void Restart()
    {
    Graphics g = myFrame.getGraphics();
    g.setColor(Color.red);
    g.fillRect((myFrame.getWidth()/ 2 - 50), 300 ,100, 40);
    g.setColor(Color.black);
    g.drawRect((myFrame.getWidth()/ 2 - 50) ,300,100,40);
    g.setColor(Color.white);
    g.setFont(new Font ("Arial", Font.BOLD,16));
    g.drawString("Restart",myFrame.getWidth()/2 - 37,327);
    }
    method to reset , after pressing restart game will start again
    public static void reset()
    {
    timerGame.start();
    for (int i = 0; i < 9; i++)
    {
    gridMark[i] = 0;
    }
    xTurn = true;
    numberClicks = 0;
    myFrame.setTitle("Tic Tac Toe - x 's turn");
    }

    //method to check winner for x and o
    public static boolean checkWinner()
    {
    if((gridMark[0] + gridMark[1] + gridMark[2] == 9 )||
    (gridMark[3] + gridMark[4] + gridMark[5] == 9) ||
    ( gridMark[6] + gridMark[7] + gridMark[8] == 9)||
    (gridMark[0] + gridMark[3] + gridMark[6] == 9 )||
    (gridMark[1] + gridMark[4] + gridMark[7] == 9 )||
    (gridMark[2] + gridMark[5] + gridMark[8] == 9) ||
    (gridMark[0] + gridMark[4] + gridMark[8] == 9) ||
    (gridMark[2] + gridMark[4] + gridMark[6] == 9))
    {
    // x, turn
    numberClicks = 10;
    myFrame.setTitle("X WINS!!!");

    Restart();
    return true;
    }
    if(((gridMark[0] + gridMark[1] + gridMark[2] == 15 )||(gridMark[3] + gridMark[4] + gridMark[5] == 15) ||
    ( gridMark[6] + gridMark[7] + gridMark[8] == 15) || (gridMark[0] + gridMark[3] + gridMark[6] == 15 )||
    (gridMark[1] + gridMark[4] + gridMark[7] == 15 )|| (gridMark[2] + gridMark[5] + gridMark[8] == 15)||
    (gridMark[0] + gridMark[4] + gridMark[8] == 15) || (gridMark[2] + gridMark[4] + gridMark[6] == 15)))
    {
    //o's turn
    numberClicks = 10;
    myFrame.setTitle("O WINS!!!");

    Restart();
    return true;
    }
    return false;

    }



    }
    Last edited by akshaygoel96; 01-10-2013 at 09:57 PM.

  2. #2
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: Help ....!!!

    This reminds me of a chess program that I made one time and I wanted to be able to play against the computer.

    What I did is I made three additional classes:

    1. Player which was an abstract class which had a method called getMove().
    2. HumanPlayer which was a concrete method that implemented getMove() by waiting until the user indicated his or her move in the GUI.

    and

    3. ComputerPlayer which was a concrete method that implementned getMove by calling findBestMove() which was a recursive method that would search ahead and find the best move.

    findBestMove() would use and algorithm called mini-max. findBestMove would evaluate each possible move in turn trying to find the best move. At each level, the player would be switched and findBestMove() would be called again finding the best move for the other player.

    With Tic-Tac-Toe, you can search all the way to the end of every possibility, always finding the best move.
    Last edited by kaydell2; 01-10-2013 at 07:06 AM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Help ....!!!

    akshaygoel96, you were told about the code tags in your previous thread. Does this advice have to be repeated every time you post code?

    Also go through the Forum Rules, particularly the third paragraph. Then edit your post accordingly. To change the subject line, click 'Edit Post' and then 'Go Advanced'

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Posting Permissions

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