0

I started making this extremely simple 2048 game, without graphics and stuff (only array), and made the board :

import java.util.*;

public class GameBoard
{
String [][] GameBoard;
void justprint(String s, int n) //(ignore, just a function that makes next steps easier)
{
    for(int i = 0; i<n;i++)
    {
        System.out.print(s);
    }
    System.out.println();
}
public GameBoard(int n)
{
    GameBoard = new String [n][n];
    Random num = new Random();
    for(int i = 0; i<n;i++)
    {
        for(int j = 0; j<n;j++)
        {
            GameBoard[i][j] = "    ";
        }
    }
    System.out.print("-");
    justprint("-----", n);
    int r1 = num.nextInt(n);
    int r2 = num.nextInt(n);
    int r3 = num.nextInt(n);
    int r4 = num.nextInt(n);
    GameBoard[r1][r2] = "   2";
    GameBoard[r3][r4] = "   2";
    for(int i = 0; i<n;i++)
    {
        System.out.print("|");
        for(int j = 0; j<n;j++)
        {
            System.out.print(GameBoard[i][j] + "|");
        }
        System.out.println();
        System.out.print("-");
        justprint("-----", n);
    }
    this.GameBoard = GameBoard;
}

}

Now, I want to make the functions to check if the neighbouring places are blank, or to add if they are the same number, etc.; but I have to make them in another class. I can probably make the functions to check all of that, but can someone tell me just how I could implement all of it in different classes?

I already tried doing it, but I want the user to input the size of the game board, and I can't do that as if I make a new class I have to write like

GameBoard gb = new GameBoard[4][4];

I don't want to define the size, but if I don't then I can't define my movement functions... Any ideas?

Kian Shah
  • 1
  • 1
  • Can you write us more? Why you need array's size? Can't you use .lenght? Also why you "have to" implement this in diffrenet class? – HeroesVII Nov 06 '20 at 13:55
  • 1
    Instead of a String array for the game board, use an int array. Empty positions would be represented by zeros. It'll make additions much easier. Convert the int array to Strings when printing. If I remember correctly, you can move in all 4 directions, but the "piece" moves as far as possible until it gets to the edge or another "piece". The movement methods should be in the same class as the game board. – Gilbert Le Blanc Nov 06 '20 at 14:05
  • @GilbertLeBlanc Thanks for that! Would try in int now. – Kian Shah Nov 06 '20 at 15:07
  • @HeroesVII I need the array's size so I can write the function to check if the neighbouring cell is empty. I'm planning to make a for loop that sees if the next cell is empty, but I can't tell it where to end, cause I don't know the size of the array. And ya I tried length but it isn't working for some reason. I've been given this project actually where I have to make the functions in different classes. If it doesn't work I will eventually just make them in the same class, but for now I'm trying :) – Kian Shah Nov 06 '20 at 15:10

0 Answers0