Liang
Liang

Reputation: 383

Java - Defining a Method/function/subroutine within a class method

So basically the problem is that I have segment of code that needs to be re-used with very slight variations through out in a java method.

This section of code also needs access to a variable only defined within the class method.

I did a search, but found no way that I can add a subroutine within a method. I thought about adding a local class, but it seems you can't use static methods with these, which is what I really need.

If it helps at all, my goal is to initialise a gameboard, that basically consists of:

GameSquare[][] board = new GameSquare[15][15];

and for each call:

board[a][b] = new GameSquare(params);

there should be a corresponding call for:

board[b][a] = new GameSquare(params);
board[15-a][15-b] = new GameSquare(params);
board[15-b][15-a] = new GameSquare(params);

Ie any special square needs to mirrored across the other four corners.

I'm hoping to have all these included within a single method. If there were a way to have an method within a method, I could just have:

method( int a, int b, otherparams passed to GameSquare constructor){
  //initialise those 4 squares.
}

But, so far I have not found such a way of doing this.

Cheers.

Upvotes: 2

Views: 840

Answers (4)

Ray Tayek
Ray Tayek

Reputation: 10003

is this how you want your special squares to be?

class GameSquare {
    GameSquare(int x) {
        this.x = x;
    }
    public String toString() {
        return "" + x;
    }
    int x;
}
public class Main {
    void print() {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++)
                if (board[i][j] == null) System.out.print("oops");
                else if (board[i][j].x < 10) System.out.print(board[i][j] + "  ");
                else
                    System.out.print(board[i][j] + " ");
            System.out.println();
        }
    }
    void makeSpecial(int i,int j,int k) {
        board[i][j] = new GameSquare(k);
        board[j][i] = new GameSquare(k);
        board[n - 1 - i][n - 1 - j] = new GameSquare(k);
        board[n - 1 - j][n - 1 - i] = new GameSquare(k);
        board[i][n - 1 - j] = new GameSquare(k);
        board[n - 1 - j][i] = new GameSquare(k);
        board[n - 1 - i][j] = new GameSquare(k);
        board[j][n - 1 - i] = new GameSquare(k);
    }
    void init() {
        int k = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++, k++)
                board[i][j]=new GameSquare(0);
        makeSpecial(2,3,1);
        makeSpecial(5,6,2);
    }
    public static void main(String[] args) {
        Main main = new Main();
        main.init();
        main.print();
    }
    final int n = 7;
    GameSquare[][] board = new GameSquare[n][n];
}

Upvotes: 0

user949300
user949300

Reputation: 15729

It seems to me that you need a new class, GameBoard, which contains the array of GameSquares. It would have a method

setSquare(int a, int b, GameSquare gs)

which knows about the mirroring.

Upvotes: 1

irreputable
irreputable

Reputation: 45443

No we can't in Java, which is annoying sometimes.

You can declared the method outside as Kevin shows. It's not that bad.

Local class is also fine. The method doesn't have to be static.

final GameSquare[][] board = ...

class Util
    void method(...)
        board[][]...

Util util = new Util();

util.method(...);

this is a little more verbose.

Upvotes: 0

Kevin K
Kevin K

Reputation: 9584

Since it's an array, how about just passing the array as a parameter to the method? Except for primitives (int, float, etc.), parameters in Java are passed by reference so you are still modifying the same array.

method(GameSquare[][] board, int a, int b, otherparams passed to GameSquare constructor){
    board[a][b] = new GameSquare(params)
    board[b][a] = new GameSquare(params)
    board[15-a][15-b] = new GameSquare(params)
    board[15-b][15-a] = new GameSquare(params)
}

Using it:

GameSquare[][] board = new GameSquare[15][15];
method(board,a,b,params);

Upvotes: 1

Related Questions