5 Replies - 1486 Views - Last Post: 17 November 2012 - 08:05 PM Rate Topic: -----

#1 mario6699   User is offline

  • New D.I.C Head

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

2d ArraysTic Tac Toe Game in Java

Posted 17 November 2012 - 12:51 PM

Hello, I need help with writing a 2D array for a tic tac toe game using methods. I already have several methods created. I have a algorithm I am trying to follow for the playGame Method. Here is the algorithm:

• Initialize the board

• While there is not a winner and it is not a draw

o Display the board
o Get the player’s move and set the corresponding board position to “X”
o Determine if the player wins
o Compute the computer’s move and set the corresponding board position to “O”
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.

This is what I've come up with so far:

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();
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: 2d ArraysTic Tac Toe Game in Java

#2 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: 2d ArraysTic Tac Toe Game in Java

Posted 17 November 2012 - 04:21 PM

Go read the Java tic tac toe tutorial in the game development tutorial section.
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: 2d ArraysTic Tac Toe Game in Java

Posted 17 November 2012 - 04:35 PM

What is your question?
Was This Post Helpful? 0
  • +
  • -

#4 mario6699   User is offline

  • New D.I.C Head

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

Re: 2d ArraysTic Tac Toe Game in Java

Posted 17 November 2012 - 06:25 PM

My question is how do I determine if the player won or the computer won?
Was This Post Helpful? 0
  • +
  • -

#5 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: 2d ArraysTic Tac Toe Game in Java

Posted 17 November 2012 - 06:30 PM

The tutorial explains that nicely. Follow my advice.
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: 2d ArraysTic Tac Toe Game in Java

Posted 17 November 2012 - 08:05 PM

First of all, put the question in terms that reflect your code. What does it mean when you say someone wins? Well, they've got three squares marked on the same horizontal, vertical, or diagonal. Very well, how could you express than in terms of your representation?

(and yes, you could probably get something useful out of the tutorials, if you wanted)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1