Results 1 to 7 of 7
  1. #1
    bobmasta5 is offline Member
    Join Date
    Dec 2008
    Location
    North America
    Posts
    29
    Rep Power
    0

    Exclamation need help creating my Tic Tac Toe game!

    This is what i have so far for creating a simple tic tac toe game (user vs computer).
    Im not sure how to determine the winner propely by using an if '3 x's or o's are in a row then that person/computer wins.

    Java Code:
    // The "TicTacToe" class.
    import java.awt.*;
    import hsa.Console;
    public class TicTacToe
    {
        static Console c;           // The output console
        public static void main (String[] args)
        {
            c = new Console ();
            // Place your program here.  'c' is the output console
            String board[];
            board = new String [9];
            int playerMove = 0;
            int compMove;
            c.print ("Tic Tac Toe Game!");
            c.println ();
            c.println ();
            //prompt user for their move
            c.print ("Enter the location of your next move (0-8): ");
            playerMove = c.readInt ();
            board [playerMove] = "X";
            //the computer must now make a move choosing a random number 0-8 and printing an "O"
            for (int counter = 0 ; counter < 1 ; counter++)
            {
                compMove = (int) (Math.random () * (8 - 0 + 1)) + 0;
                board [compMove] = "O";
            }
            //make an if statement to determine if there is a winner
            if (board[playerMove]= "XXX") //if player gets three X's in a row
            {
                c.println ("Player Wins!");
            }
            else if (board[compMove]= "OOO") //if player gets three O's in a row
            {
                c.println ("Computer Wins!");
            }
            //make a loop so the game goes on until the maximum moves (9) have occured
            while (board[playerMove]+ [compMove]<9)
            {
            
            }
        } // main method
    } // TicTacToe class

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Plan ahead ...

    Bob... before you start committing grey matter to code I would suggest that you sit down and write down all the issues involved with this task. Write the tasks needed for a tic-tac-toe game... some come mind:
    • Are you going to use a single dimension array ?

    Java Code:
    gameBoard [] = {0,1,2,3,4,5,6,7,8,9}
    • or are you going to use a double dimension array?

    Java Code:
    gameBoard [3][3]
    • What combinations within the array will make a win?
    • How do you avoid overwriting an "X" with an "O" or visaversa?
    • How are you going to show the game board status to the player?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    bobmasta5 is offline Member
    Join Date
    Dec 2008
    Location
    North America
    Posts
    29
    Rep Power
    0

    Post Instructions, steps

    Here are the steps giving to me in order to complete my program. I have showed where I am stuck, and this is what is expected of my tic tac toe program:
    Tic-Tac-Toe

    Were going to make the game Tic-Tac-Toe. Our goal is to play against the computer.

    My initial thoughts on this program are that:

    We will need a 9 element array of type String called board that represents the playing board.



    The user will make their move by entering a number between 0 and 8 inclusive (see the sample code). In the location that the user chose, the program will place an X.

    The computer will choose its move be generating a random number between 0 and 8 inclusive. In the spot that the computer chose, the program will place an O.

    The program will need if statements to determine if there is a winner (three Xs or three Os in a row) --this where i am roughly


    The program will need a loop so that the user and computer can continue playing each other until the game ends. (What is the maximum number of moves that can occur before all of the spaces have been used?)


    I think that we should pre-load every element in the array with a . so that the user can see which squares are still available. Also, we need to show the user the 9 element array; we should display it in a grid so that I looks more like the real game. Pre-loaded and arranged in a grid, it should look like this:
    Notes:

    Your program will have to ensure that both the user and the computer can only choose spots that are still available. (How can you tell if an array element is still available?)

    The following is a screen shot from a sample version of the program. Notice that you don't have to ask the computer to move; it should automatically move after the user enters their move.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    We will need a 9 element array of type String called board that represents the playing board.
    Hm, I would think that a 3 x 3 two-dimensional array would be a more natural fit here, but YMMV.

    The user will make their move by entering a number between 0 and 8 inclusive (see the sample code). In the location that the user chose, the program will place an X.
    Or some type of ordered pair.

    The computer will choose its move be generating a random number between 0 and 8 inclusive. In the spot that the computer chose, the program will place an O.
    ditto.

    The program will need if statements to determine if there is a winner (three X’s or three O’s in a row) --this where i am roughly
    Do the logic out on paper. In fact plan on going through lots of paper and lead til you find a decent solution. Then and only then start coding.

  5. #5
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    just draw it out and number the squares. this is one of those problems where the solution can be programmed similarly to how you solve the problem by hand.

  6. #6
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    There are 8 possible winning solutions. Once you get one if statement coded the other 7 are pretty much the same thing.

    When in doubt pseudo-code something and show us and we can tell you if it's going to work or not.

    P.S. Whats YMMV mean?

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Ymmv

    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 05:49 AM
  2. Looking for team members for creating the applet game.
    By M77 in forum Reviews / Advertising
    Replies: 10
    Last Post: 05-28-2008, 09:25 AM
  3. game
    By amith in forum AWT / Swing
    Replies: 0
    Last Post: 05-19-2008, 06:16 PM
  4. creating a text based game
    By Phobos0001 in forum New To Java
    Replies: 1
    Last Post: 02-12-2008, 05:35 PM
  5. Programming a Game?
    By gt123 in forum New To Java
    Replies: 4
    Last Post: 01-01-2008, 01:41 PM

Posting Permissions

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