Justin
Justin

Reputation: 11

How to return an two dimensional array index from a method?

Im creating a tic tac toe program from here:

http://programmingbydoing.com/a/tic-tac-toe.html

I'm having trouble finding out how to return an array index from a method. I want to user to input 2 integers, ( row, column) and have that index returned and have either char 'O', or 'X' replace the blank corresponding index.

I'm a bit new to java, and am trying to learn it as fast and efficiently as possible to catch up in my class.

import java.util.Scanner;

public class ticTacToe{
    public static Scanner in = new Scanner(System.in);
    private static int r, c;
    private static char[][] board = new char[3][3];
    //create a 3x3 array of characters

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

        displayBoard();
        do{
            // user inputs two numbers as indexes
            //turn index into "o"
            //prompt other user to make move, repeat with "x"

        }while(checkLoser());

    }

    public static void displayBoard() {
        System.out.println("  0  " + board[0][0] + "|" + board[0][1] + "|" + board[0][2]);
        System.out.println("    --+-+--");
        System.out.println("  1  " + board[1][0] + "|" + board[1][1] + "|" + board[1][2]);
        System.out.println("    --+-+--");
        System.out.println("  2  " + board[2][0] + "|" + board[2][1] + "|" + board[2][2]);
        System.out.println("     0 1 2 ");
    }

    public static int[] getArrayIndex(){
        System.out.println("'O'. Choose your Row...(row, column)");
        int r = in.nextInt();
        int c = in.nextInt();
        return new int[] {r,c};
    }

    public static void userMove(int[][] ){
        board[r][c] = 'O';
    }

    public static boolean checkLoser(){
            if (board[0][0] == board[0][1]){
            if (board [0][1] == board[0][2]){
                return false;
            }
            }
            else if (board[1][0] == board[1][1]){
            if (board [1][1] == board[1][2]){
                return false;
            }
            }
            else if (board[2][0] == board[2][1]){
            if (board [2][1] == board[2][2]){
                return false;
            }
            }
            else if (board[0][0] == board[1][0]){
            if (board [1][0] == board[2][0]){
                return false;
            }
            }
            else if (board[0][1] == board[1][1]){
            if (board [1][1] == board[1][2]){
                return false;
            }
            }
            else if (board[0][2] == board[1][2]){
            if (board [1][2] == board[2][2]){
                return false;
            }
            }

            else if (board[0][0] == board[1][1]){
            if (board [1][1] == board[2][2]){
                return false;
            }
            }
            else if (board[0][2] == board[1][1]){
            if (board [1][1] == board[2][0]){
                return true;
            }
            }else{
                return false;
            }

    }

}

Upvotes: 1

Views: 4785

Answers (3)

Kamal Kumar
Kamal Kumar

Reputation: 11

you may want to consider using java.awt.Point.

Upvotes: 0

Alfro
Alfro

Reputation: 517

Well, to answer your question, since it's a two-dimensional array, you obviously need two indices to be returned (as you probably thought). Since a function can only return one Object, as you correcty meant to do given your function:

public static int[] getArrayIndex(){
        System.out.println("'O'. Choose your Row...(row, column)");
        int r = in.nextInt();
        int c = in.nextInt();
        return new int[] {r,c};
    }

you returned a two-element array with both the indices you need. That is ONE way you can accomplish it. Other forms include creating an Object with two int elements and using it instead or storing both variables in the same int somehow (keeping them one digit long for example). Anyways, your way to returning those indices is alright. How you use that information is up to you then. I guess you'd have to store it in a temporary int array like this int[] temp=getArrayIndex(); and then access each element to get the corresponding indices.

However, your code has other errors, like forgetting to put a name to the parameter in this function (or the fact that such parameter isn't used anyways):

public static void userMove(int[][] ){
    board[r][c] = 'O';
}

I suggest you check on those. Maybe the program you compiled gave you an error and you thought it was because of the new int[] {r,c} thing, but naaah, that works. If that's the case, check the rest, it's something else. I'll be glad to help though, so feel free to ask. ^^

Upvotes: 1

5gon12eder
5gon12eder

Reputation: 25419

The Java-way to do this is define a class to hold the row and column index.

final class BoardIndex {

    final int row;
    final int col;

    BoradIndex(final int r, final int c) {
        this.row = r;
        this.col = c;
    }
}

You can then return it from a function.

BoardIndex getIt() {
    return new BoradIndex(1, 2);
}

It's quite a bit of typing but many Java folks prefer this kind of being explicit about types over using, say, an array of integers.

Upvotes: 3

Related Questions