Create a board in Java

2

They let me do a project in Java that resembles the Minesweeper game, I have to make a board that has to take a square dimension, and also a character without uncovering and uncovered character, it has to look like the following:

So far I have done the board with the characters, but I need to add the coordinates of the a-z and 1-26, I'm stuck in that part, I hope you can help me.

public class Tablero {
    int dimension;
    char caracterSinDestapar;
    char caracterDestapado;
    char tablero[][];
    //Mina minas;

    Tablero(int dimension, char caracterSinDestapar, char caracterDestapado)
    {
        this.dimension = dimension;
        this.caracterSinDestapar = caracterSinDestapar;
        this.caracterDestapado = caracterDestapado;

        tablero = new char[dimension][dimension];

        for(int i = 1; i < dimension; i++)
            for(int j=1; j < dimension; j++)
                tablero[i][j] = caracterSinDestapar;
        for(int i = 1; i < dimension; i++)
            tablero[i][0] = '+';
        for(int j = 1; j < dimension; j++)
            tablero[0][j] = 'b';

    }

    void imprimeTablero()
    {
        for(int i=0; i < dimension; i++)
        {
            for(int j=0; j < dimension; j++)
            {
                System.out.print(tablero[i][j] + "\t");
            }
            System.out.println("\n");
        }
    }

    void actualizaTablero(int x, int y)
    {
        tablero[x][y] = caracterDestapado;
    }

    public static void main(String[] args) 
    {
        Tablero tablero = new Tablero(4, '#', 'O');
        Tablero tablero2 = new Tablero(3, '-', 'x');

        tablero.imprimeTablero();
        tablero2.imprimeTablero();

        tablero.actualizaTablero(2, 2);
        tablero2.actualizaTablero(0, 0);

        tablero.imprimeTablero();
        tablero2.imprimeTablero();
}

This is how it is shown:

    
asked by Jonathan Ortz 18.05.2016 в 00:26
source

2 answers

2

I regret to inform you that, as far as I know, you will not be able to show beyond row 9 since the Tablero array is of the char type, which only supports a single character. If you have the opportunity to change the array type to String, the way to do it would be as simple as this:

class Tablero
{
    int dimension;
    String caracterSinDestapar;
    String caracterDestapado;
    String tablero[][];
    String [] abc ={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};

Tablero(int dimension, String caracterSinDestapar, String caracterDestapado)
{
    this.dimension = dimension;
    this.caracterSinDestapar = caracterSinDestapar;
    this.caracterDestapado = caracterDestapado;

    tablero = new String[dimension][dimension];

    for(int i = 1; i < dimension; i++)
        for(int j=1; j < dimension; j++)
            tablero[i][j] = caracterSinDestapar;


    for(int i = 1; i < dimension; i++)
        tablero[i][0] = String.valueOf(i); //Aquí se añaden los números de la
                                           //izquierda aprovechando el indice del for

    for(int j = 1; j < dimension; j++)     //Aquí rellenamos con las vocales del array
        tablero[0][j] = abc[j - 1];       

//para eliminar el null que queda en la posicion [0][0] tan fácil como:
    tablero[0][0] = "";
    }

I hope it serves you!

    
answered by 18.05.2016 в 11:55
2

The information you must show does not necessarily have to be part of the information you have stored. Based on this, I would recommend that the information of a b c ... and number 1 2 3 ... not be part of the board.

With that in mind, your builder would change to:

Tablero(int dimension, char caracterSinDestapar, char caracterDestapado)
{
    this.dimension = dimension;
    this.caracterSinDestapar = caracterSinDestapar;
    this.caracterDestapado = caracterDestapado;

    tablero = new char[dimension][dimension];

    for(int i = 1; i < dimension; i++)
        for(int j=1; j < dimension; j++)
            tablero[i][j] = caracterSinDestapar;
}

And in the method where you print the board you add the relevant information:

void imprimeTablero()
{
    //primero imprimimos los caracteres de la parte superior
    System.out.print(" \t"); //dejar un espacio en blanco
    for(int i=0; i < dimension; i++) {
        System.out.print( (char)(i + 97) + "\t" ); //97 es el valor de 'a'
    }
    System.out.println();
    //ahora imprimimos todas las filas y columnas del tablero
    //este bucle for controla la impresión de las líneas
    for(int i=0; i < dimension; i++)
    {
        //lo primero que se imprime en la línea es el número
        //System.out.printf permite imprimir una cadena con formato
        //%2d significa que se imprimirá un número de dos dígitos
        //si solo tiene un dígito, se imprimen espacios en blanco a la izquierda
        //los demás argumentos del método son los elementos que se
        //imprimirán en las posiciones de %<algo>
        //en este caso, (i+1) resulta en 1, 2, 3 ...
        System.out.printf("%2d\t", i+1);
        //ahora continuamos con la impresión del tablero
        for(int j=0; j < dimension; j++)
        {
            System.out.print(tablero[i][j] + "\t");
        }
        //ya imprime el salto de línea, no es necesario agregar
        //un salto de línea adicional con \n
        System.out.println();
        //en caso que realmente requieras el salto de línea adicional
        //te recomiendo que vuelvas a llamar a System.out.println()
    }
}
    
answered by 18.05.2016 в 17:23