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 4 of 4

Thread: Tic-Tac-Toe (loops, arrays, and methods) help please

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Tic-Tac-Toe (loops, arrays, and methods) help please

    So I am having trouble with a few of things. We have to create an empty array filled with '-' characters to start. I have finished the parts where it randomly pics player1 or player2 to start and displaying an empty board. There are 3 things I am having problems with (will post code below with sample output). 1, I am having trouble getting the input from the user and replacing the '-' with an x or an o. next, after the player pics it will show the board and ask the other player to pic. The last method scans it to see if there is a winner, i know how to get it to scan the rows and columns but not diagonal. Any help would be useful, thanks Smile
    oh and i left the methods i need help with empty with descriptions above them. it got kind of messy trying out different things.

    Code:

    package lab07;
    import java.util.Scanner;
    import java.util.Random;
    /**
    *
    * @author
    */
    public class Lab07 {

    /**
    * @param args the command line arguments
    * Do not change main()
    */
    public static void main(String[] args) {
    char [] [] board; char win; char player;
    int size = getBoardSize();

    while(size >= 3) {
    board = createEmptyBoard( size );
    player = getStartingPlayer();
    do {
    getMove(board, player);
    player = player=='O'?'X':'O'; // switch players
    win = checkForWin(board);
    } while(win == 'N'); // must be F, O or X to leave loop
    if(win == 'F'){
    System.out.printf("\nThe board is Full: no Winner\n");
    } else {
    System.out.printf("\nThe winner is %c\n", win);
    }
    displayBoard(board);
    size = getBoardSize();
    } // end play games loop
    } // end main

    /*
    * returns 3, 4 or 5 for the size of the play board
    * or -1 to quit
    * This is complete, no changes necessary
    */
    public static int getBoardSize() {
    int retval;
    Scanner keyboard = new Scanner(System.in);
    do {
    System.out.printf("Enter board size 3, 4, 5 or -1 to quit: ");
    retval = keyboard.nextInt();
    } while(retval != 3 && retval != 4 && retval != 5 && retval != -1);

    keyboard.nextLine(); // you should know why by now

    return retval; // this MUST BE LAST LINE in this method()
    } // getBoardSize

    /*
    * randomly selects X or O as the starting player
    *
    */
    public static char getStartingPlayer(){
    char retval = 'X';

    Random randomNumbers = new Random();
    int player = randomNumbers.nextInt(2);

    switch (player) {
    case 0:
    retval = 'O';
    break;

    case 1:
    retval = 'X';
    break;
    }//end switch


    System.out.printf("\n\tNew Game. Starting player is %c\n", retval);
    return retval; // this MUST BE LAST LINE in this method()
    } // end getStartingPlayer

    /**
    * gets new memory for a board and fills all cells with a minus sign '-'
    * @param size the board if 3 is 3 x 3, if 4 is 4 x 4 etc.
    */
    public static char [][] createEmptyBoard(int size) {
    char [] [] board = new char [size][size];

    for (char row = 0; row < board.length; row++) {
    System.out.println();
    for (char col = 0; col < board[row].length; col++) {
    board[row][col] = '-';
    System.out.print(board[row][col] + "\t");

    }//end col
    }//end row

    return board; // this MUST BE LAST LINE in this method()
    } // end createEmptyBoard

    /**
    * 1. displays board and
    * 2. gets row column indexes for next move
    * 3. makes sure the cell has a '-' is a legal move
    * 4. loops till legal
    * 5. puts the player's X or O at that cell
    * @param board the current play board
    * @param the X or O of the current player
    */
    public static void getMove(char [][] board, char player){
    System.out.printf("Player %c move\n", player);
    displayBoard(board);




    } // end getMove

    /*
    * displays the current play board to the console
    * @param board the current play board to be displayed
    */
    public static void displayBoard(char [][] board) {

    } // end displayBoard

    /* returns X, O, F or N
    * returns X for X wins
    * O for O wins
    * F for board Full no winner
    * or N for No winner yet
    * @param board, the current play board to be checked
    */
    public static char checkForWin(char [][] board){
    char retval = 'F'; //


    return retval; // this MUST BE LAST LINE in this method()
    } // end checkForWin


    }


    Sample Output:

    Enter board size 3, 4, 5 or -1 to quit: 3

    New Game. Starting player is X

    Player X move
    - - -
    - - -
    - - -
    Enter Row: >=0 and <= 2 1
    Enter Col: >=0 and <= 2 1
    Player O move
    - - -
    - X -
    - - -
    Enter Row: >=0 and <= 2 2
    Enter Col: >=0 and <= 2 2
    Player X move
    - - -
    - X -
    - - O
    Enter Row: >=0 and <= 2 1
    Enter Col: >=0 and <= 2 0
    Player O move
    - - -
    X X -
    - - O
    Enter Row: >=0 and <= 2 0
    Enter Col: >=0 and <= 2 0
    Player X move
    O - -
    X X -
    - - O
    Enter Row: >=0 and <= 2 1
    Enter Col: >=0 and <= 2 2

    The winner is X
    O - -
    X X X
    - - O
    Enter board size 3, 4, 5 or -1 to quit: -1


  2. #2
    Junior Member
    Join Date
    Nov 2012
    Location
    Germany
    Posts
    20
    My Mood
    Bored
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Tic-Tac-Toe (loops, arrays, and methods) help please

    [code]-tags?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Tic-Tac-Toe (loops, arrays, and methods) help please

    Please edit your post and properly format the code and wrap it in code tags.

    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Tic-Tac-Toe (loops, arrays, and methods) help please

    Which is the user? Is the user always 'X' or is the user always 'O' or what?

    I mean, your getMove() method must know whether it is supposed to get a move from the user or get a move from the computer.

    If it is getting from the user, then there would be a loop that prompts the user and reads row and column number and sees whether they correspond to a valid move. The loop repeats until it sees a valid move (move to a place presently occupied by '-').

    If it is getting from the computer, then one way would be to get two random numbers and see whether they correspond to a valid move. The loop repeats until it sees a valid move (move to a place presently occupied by '-').

    On the other hand...

    I would probably have two methods: One for user move and one for computer move. Then the main() loop would decide which to call for a given move. Or some such thing.


    Cheers!

    Z

Similar Threads

  1. Tic-Tac-Toe Help
    By coke32 in forum Object Oriented Programming
    Replies: 13
    Last Post: March 12th, 2012, 07:59 AM
  2. Need help with Tic Tac Toe assignment
    By BAL1990 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 28th, 2011, 01:33 PM
  3. TIC-TAC-TOE GAME
    By umerahmad in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 29th, 2011, 12:15 PM
  4. [SOLVED] Tic-Tac-Toe program
    By Actinistia in forum Java Theory & Questions
    Replies: 2
    Last Post: April 28th, 2011, 11:18 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