3 Replies - 539 Views - Last Post: 19 November 2012 - 03:05 PM Rate Topic: -----

#1 mario6699  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-November 12

Tic Tac Toe using 2d Arrays in Java

Posted 15 November 2012 - 04:31 PM

I have create a tic tac toe using methods. For the playGame method I am having trouble with trying to
Initialize the board
While there is not a winner and it is not a draw
Display the board
Get the player’s move and set the corresponding board position to “X”
Determine if the player wins
Compute the computer’s move and set the corresponding board position to “O”
Determine if the computer wins
The player will win if the current move will result in 3 “X”s in a row, column or diagonal (if the position) is on a diagonal.
The computer will win if the current move will result in 3 “O”s in a row, column or diagonal (if the position) is on a diagonal.


package tictactoe;

import java.util.Arrays;
import java.util.Scanner;

/**
 *
 * @
 */
public class TicTacToe {

    private String board[][] = new String[3][3]; // to hold board contents
    private int moveBoard[][] = new int[3][3];   // used to evaluate the computer move
    private Scanner input = new Scanner(System.in);
    private int playerX;  // holds the row choice of the user
    private int playerY;  // holds the column choice of the user

    public TicTacToe() {
        initializeBoard();

    }

    // This method sets all positions in the board to " "
    private void initializeBoard() {
        for (String[] row : board) {
            Arrays.fill(row, " ");
        }
    }

    // This method will initialize the moveBoard prior to determining
    // the next move. If the board contains a blank the position is set to 0
    // otherwise the position is set to -10 because the position is filled and we 
    // don't want to be able to select the position
    
    private void initializeMoveBoard() {
        for (int row = 0; row < moveBoard.length; row++) {
            for (int col = 0; col < moveBoard[row].length; col++) {
                if (board[row][col].equals(" ")) {
                    moveBoard[row][col] = 0;
                } else {
                    moveBoard[row][col] = -10;
                }
            }
        }
    }

    // this method displays the board
    public void displayBoard() {
        System.out.println("\t  1   2   3");
        System.out.println("\t-------------");
        for (int row = 0; row < board.length; row++) {
            System.out.print("  " + (row + 1) + "\t");
            for (int col = 0; col < board[row].length; col++) {
                System.out.print("| " + board[row][col] + " ");
            }
            System.out.println("|");
            System.out.println("\t-------------");
        }
    }

    // this method displays the moveBoard values. This is added for debugging
    // purposes if required.
    private void displayMoveBoard() {

        for (int row = 0; row < moveBoard.length; row++) {
            for (int col = 0; col < moveBoard[row].length; col++) {
                System.out.printf("%2d ", moveBoard[row][col]);
            }
            System.out.println();
        }
    }
// This method gets a valid player move

    private void getPlayerMove() {

        do {
            do {
                System.out.print("Move (row col separated by blanks): ");
                playerX = input.nextInt();
                if (playerX < 1 || playerX > 3) {
                    System.out.println("Invalid row");
                }
                playerY = input.nextInt();
                if (playerY < 1 || playerY > 3) {
                    System.out.println("Invalid column");
                }
            } while (playerX < 1 || playerX > 3 || playerY < 1 || playerY > 3);
            if (!board[playerX - 1][playerY - 1].equals(" ")) {
                System.out.println("Board at position " + playerX + " " + playerY
                        + " is occupied");
            }
        } while (!board[playerX - 1][playerY - 1].equals(" "));
    }

    public void playGame() {
        initializeBoard();  // reset the board for this game
        initializeMoveBoard();
        displayMoveBoard();
        displayBoard();
        getPlayerMove();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        TicTacToe game = new TicTacToe();
        game.playGame();
    }
}



It should be I have created so far

Is This A Good Question/Topic? 0
  • +

Replies To: Tic Tac Toe using 2d Arrays in Java

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3911
  • View blog
  • Posts: 11,448
  • Joined: 18-April 07

Re: Tic Tac Toe using 2d Arrays in Java

Posted 18 November 2012 - 11:57 AM

So in essence you are having trouble with the entire game? I am not entirely sure why you have two boards setup. You should only need one. Why complicate things with having to play with two boards? When you start a game, you initialize the board to be empty. Then you ask the player for a move. They tell you were to place their mark, you mark the array, run through row/column/diagonal checks to see if there is a win. You then check if the board is full. If it is not, you switch to the other player and do it all over again.

:)
Was This Post Helpful? 1
  • +
  • -

#3 farrell2k  Icon User is offline

  • 1.21 Jiggawatts!
  • member icon

Reputation: 572
  • View blog
  • Posts: 1,752
  • Joined: 29-July 11

Re: Tic Tac Toe using 2d Arrays in Java

Posted 18 November 2012 - 12:33 PM

This seems to be the week of tic tac toe questions. There's a great tutorial in the game programming tutorials section that covers everything!
Was This Post Helpful? 0
  • +
  • -

#4 mario6699  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-November 12

Re: Tic Tac Toe using 2d Arrays in Java

Posted 19 November 2012 - 03:05 PM

I need help with where to put board[playerX][playerY]='X';in the getPlayerMove Method to set the board position to X

Also I am trying to determine if the player wins. I have code started at the end of my program but I am not sure if I am doing this right or where it goes. The player wins if the current move will result in 3 Xs in a row column or diagonal (if the position) is on a diagonal.

The computer will win if the current move will result in 3 Os in a row column or diagonal (if the position) is on a diagonal.

I created a getComputerMove method. I am trying to follow the following algorithm:

• Initialize the moveBoard
• For each position in the board 2D array
o If the position contains a blank “ “
For each row and column the position occupies
• If there are 2 “O”s in the row or column add 50 to the corresponding position in the moveBoard array
• If there are 2 “X”s in the row or column add 25 to the corresponding position in the moveBoard array
• If there is 1 “O” and no “X”s in the row or column add 10 to the corresponding position in the moveBoard array

But i am getting errors. Any help would be appreciated. Thanks


package tictactoe;

import java.util.Arrays;
import java.util.Scanner;

/**
 *
 * @
 */
public class TicTacToe {

    private String board[][] = new String[3][3]; // to hold board contents
    private int moveBoard[][] = new int[3][3];   // used to evaluate the computer move
    private Scanner input = new Scanner(System.in);
    private int playerX;  // holds the row choice of the user
    private int playerY;  // holds the column choice of the user

    public TicTacToe() {
        initializeBoard();

    }

    // This method sets all positions in the board to " "
    private void initializeBoard() {
        for (String[] row : board) {
            Arrays.fill(row, " ");
        }
    }

    // This method will initialize the moveBoard prior to determining
    // the next move. If the board contains a blank the position is set to 0
    // otherwise the position is set to -10 because the position is filled and we 
    // don't want to be able to select the position
    
    private void initializeMoveBoard() {
        for (int row = 0; row < moveBoard.length; row++) {
            for (int col = 0; col < moveBoard[row].length; col++) {
                if (board[row][col].equals(" ")) {
                    moveBoard[row][col] = 0;
                } else {
                    moveBoard[row][col] = -10;
                }
            }
        }
    }

    // this method displays the board
    public void displayBoard() {
        System.out.println("\t  1   2   3");
        System.out.println("\t-------------");
        for (int row = 0; row < board.length; row++) {
            System.out.print("  " + (row + 1) + "\t");
            for (int col = 0; col < board[row].length; col++) {
                System.out.print("| " + board[row][col] + " ");
            }
            System.out.println("|");
            System.out.println("\t-------------");
        }
    }

    // this method displays the moveBoard values. This is added for debugging
    // purposes if required.
    private void displayMoveBoard() {

        for (int row = 0; row < moveBoard.length; row++) {
            for (int col = 0; col < moveBoard[row].length; col++) {
                System.out.printf("%2d ", moveBoard[row][col]);
            }
            System.out.println();
        }
    }
// This method gets a valid player move

    private void getPlayerMove() {

       
        do {
            do {
                System.out.print("Move (row col separated by blanks): ");
                playerX = input.nextInt();
                if (playerX < 1 || playerX > 3) {
                    System.out.println("Invalid row");
                }
                playerY = input.nextInt();
                if (playerY < 1 || playerY > 3) {
                    System.out.println("Invalid column");
                }
            } while (playerX < 1 || playerX > 3 || playerY < 1 || playerY > 3);
            if (!board[playerX - 1][playerY - 1].equals(" ")) {
                System.out.println("Board at position " + playerX + " " + playerY
                        + " is occupied");
            }
            
        } while (!board[playerX - 1][playerY - 1].equals(" "));
       // board[playerX][playerY]='x';
    }
    

    public void playGame() {
        initializeBoard();  // reset the board for this game
        initializeMoveBoard();
        displayMoveBoard();
        displayBoard();
        getPlayerMove();
        getComputerMove();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        TicTacToe game = new TicTacToe();
        game.playGame();
    }
    
    public void getComputerMove(){
        int xcount =1;
       int ocount=0;
         initializeMoveBoard();
       //board[playerX][playerY]='O'; 
         for (int row = 0; row < board.length; row++) {
            for (int col = 0; col < board[row].length; col++) {
                if (board[row][col].equals(" ")) {
           for (int r=0; r< board.length; r++){         
            for ( int c=0; c <board.length; c++) {
                if ((board [r][c]='0'){
                        ocount++;
                        if (board[r][c]='x'){
                            xcount++;
                            
                            if(ocount = 2){
                                moveBoard[row][col]+=50;
                                 if(ocount = 2){
                                moveBoard[row][col]+=25;
                                if(ocount+=1 && xcount=0) )
                                moveboard[row][col]+=10
                            }
                        }
            }   
                    
                }
            }
        
    
           }
                }
            }
         
         for(int ro=0){
                 for(int co=0{
                     if(board[ro][co]=0
                             system.out.println('winner')
                 }
)
)
         }
         

    

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1