Results 1 to 3 of 3
  1. #1
    jolt is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default Help with a massive problem

    OK so now i have been doing a tic-tac-toe program for a class and have been stuck for a while now on a problem that has been taunting me. I have gone to my professor but he is horrible, wont let me speak when i think i have a solution and keep s going off on tangents that have no meaning or end , I understand that he should not give me the answer but he does not give me any thing to think about so i need you guys to help if you could .
    The program is designed so the user can play a one person game of tic-tac-toe ex. if the user enters a spot the computer will calculate and enter a spot randomly.
    below is the program now i believe the main reason it wont run properly is because currMove must be assigned to playerMove I think i am not to sure any help would be welcome. thanks

    import java.util.Scanner;

    public class Hmwk1 {

    public static final char SPACE = ' ';
    // Tic-tac-toe board showing move numbers
    public static final char[][] MOVE_INFO = {{'1', '2', '3'},
    {'4', '5', '6'},
    {'7', '8', '9'}};

    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    char[][] board = new char[3][3];
    char currPlayer = 'X';
    int playerMove, currMove;
    currMove=0;

    fill2D(board, SPACE);
    printBoard(MOVE_INFO);
    while (!gameOver(board)) {
    System.err.print("Move for " + currPlayer + "? ");
    makeMove(currPlayer, currMove, board);
    currMove = playerMove(currPlayer, currMove);
    printBoard(board);
    currPlayer = nextPlayer(currPlayer);
    }
    reportResults(board);
    }

    /**
    * Fill a given two-dimensional character array with a given
    * character
    *
    * @param a the array to fill
    * @param c the character to use
    */
    public static void fill2D(char[][] a, char c) {
    for (int i = 0; i < a.length; i++) {
    for (int j = 0; j < a[i].length; j++) {
    a[i][j] = c;
    }
    }
    }

    public static final String VERT = " * * ";
    public static final String HORZ = "***********";

    /**
    * Print the contents of a 3x3 array representing a tic-tac-toe
    * board
    *
    * @param board the board configuration to be printed
    */
    public static void printBoard(char[][] board) {
    System.out.println();
    for (int i = 0; i < 3; i++) {
    System.out.println(VERT);
    for (int j = 0; j < 3; j++) {
    System.out.print(" " + board[i][j] + " ");
    if (j < 2) {
    System.out.print("*");
    } else {
    System.out.println();
    }
    }
    System.out.println(VERT);
    if (i < 2) {
    System.out.println(HORZ);
    }
    }
    System.out.println();
    }

    /**
    * Determine if the game is over; i.e., if a player has won or if
    * there are no more moves possible
    *
    * @param board current board configuration
    * @return true if game is over, false otherwise
    */
    public static boolean gameOver(char[][] board) {
    return noMovesLeft(board)
    || isWinner('X', board)
    || isWinner('O', board);
    }

    /**
    * Determine if no more moves are possible; i.e., if there
    * are no more SPACE's left in the board array
    *
    * @param board the current board configuration
    * @return true if there are no more legal moves, false otherwise
    */
    public static boolean noMovesLeft(char[][] board) {
    for (int i = 0; i < board.length; i++) {
    for (int j = 0; j < board[i].length; j++) {
    if (board[i][j] == SPACE) {
    return false;
    }
    }
    }
    return true;
    }
    /**
    * Determine if a given player is a winner; i.e., has 3 marks in a
    * row, either horizontally, vertically, or diagonally
    *
    * @param player the player to check
    * @param board the current board configuration
    * @return true if player has 3 marks in a row, false otherwise
    */
    public static boolean isWinner(char player, char[][] board) {
    // check rows (horizontal)
    for (int row = 0; row < 3; row++) {
    if (board[row][0] == player && board[row][1] == player
    && board[row][2] == player) {
    return true;
    }
    }
    // check cols (vertical)
    for (int col = 0; col < 3; col++) {
    if (board[0][col] == player && board[1][col] == player
    && board[2][col] == player) {
    return true;
    }
    }
    // check diagonals
    if (board[0][0] == player && board[1][1] == player
    && board[2][2] == player) {
    return true;
    } else if (board[0][2] == player && board[1][1] == player
    && board[2][0] == player) {
    return true;
    } else {
    return false;
    }
    }
    public static int playerMove(char currPlayer, int currMove)
    {
    if( currPlayer == 'X')
    {
    //currmove must be assined to playerMove:confused:
    return currMove;
    }
    else
    {
    currMove = (int)(Math.random() * 9) + 1;
    return currMove;
    }
    }
    /**
    * Mark the specified move for the specified player on the given
    * board configuration (move == 1 => [0][0], move == 2 => [0][1],
    * move == 3 => [0][2], move == 4 => [1][0], move == 5 => [1][1],
    * move == 6 => [1][2], move == 7 => [2][0], move == 8 => [2][1],
    * move == 9 => [2][2])
    *
    * @param player the player making the move
    * @param move the move to be made
    * @param board the current board configuration
    */
    public static void makeMove(char player, int move, char[][] board) {
    int row = (move - 1) / board.length;
    int col = (move - 1) % board.length;
    board[row][col] = player;
    }

    /**
    * Determine the next player, for cycling X, then O, then X, etc.
    *
    * @param current the current player ('X' or 'O')
    * @return the next player to play ('O' or 'X')
    */
    public static char nextPlayer(char current) {
    if (current == 'X') {
    return 'O';
    } else {
    return 'X';
    }
    }

    /**
    * Report the results of the game, whether X or O won or the game
    * ended in a tie.
    *
    * @param board the board configuration to be described
    */
    public static void reportResults(char[][] board) {
    if (isWinner('O', board)) {
    System.out.println("O is the winner!");
    } else if (isWinner('X', board)) {
    System.out.println("X is the winner!");
    } else {
    System.out.println("The game ends in a tie.");
    }
    }

    }
    Last edited by jolt; 02-25-2009 at 07:46 PM.

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

    Default

    can you please explain what isn't "running properly?"

  3. #3
    jolt is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default

    it compliels but i get an error when running
    * *
    1 * 2 * 3
    * *
    ***********
    * *
    4 * 5 * 6
    * *
    ***********
    * *
    7 * 8 * 9
    * *

    Move for X? Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at Hmwk1.makeMove(Hmwk1.java:175)
    at Hmwk1.main(Hmwk1.java:30)

Similar Threads

  1. Massive Hit Deletion Help please
    By quickfingers in forum Java 2D
    Replies: 1
    Last Post: 12-24-2007, 08:49 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
  •