0

我正在尝试在 Android Studio 中制作 4 行,我必须完成一种方法(dibujarTablero() --> drawBoard() 英文),问题是,我必须根据板子上每个环的状态,不知道是不是用正确的方式完成了方法。

另一方面,从未使用过“pulsado”方法,我想知道必须在哪里调用或使用它:/。我附上Game.java和MainActivity.java的代码,android代码是对的,我只需要java代码。

游戏.java

package android.esteban.conecta4;

/**
* Created by Esteban on 19/10/2015.
*/

import java.util.Random;

public class Game {
static final int NFILAS = 6;
static final int NCOLUMNAS = 7;
static final int VACIO = 0;
static final int MAQUINA = 1;
static final int JUGADOR = 2;

private int tablero[][];

public Game() {
    tablero = new int[NFILAS][NCOLUMNAS];

    for (int i = 0; i < NFILAS; i++)
        for (int j = 0; j < NCOLUMNAS; j++)
            tablero[i][j] = VACIO;
}

/*************************************************************************
 Completa este metodo.
 Parametros: indices i y j del tablero.
 Retorno: cierto si la posicion tablero[i][j] esta vacia (su valor
 es VACIO) y falso en caso contrario
 *************************************************************************/
public boolean estaVacio(int i, int j) {
    // Aqui debes incluir tu codigo

    if(tablero[i][j] == JUGADOR){
        return false;
    }

    else {
        return true;
    }
}

/*************************************************************************
 Completa este metodo.
 Parametros: indices i y j del tablero.
 Retorno: cierto si la posicion tablero[i][j] esta ocupada por el
 jugador (su valor es JUGADOR) y falso en caso contrario
 *************************************************************************/
public boolean estaJugador(int i, int j) {

    // Aqui debes incluir tu codigo

    if(tablero[i][j] == JUGADOR){
        return true;
    }

    else {
        return false;
    }
}

public void ponerJugador(int i, int j) {
    tablero[i][j] = JUGADOR;
}

public boolean tableroLleno() {
    for (int i=0; i<NFILAS; i++)
        for (int j=0; j<NCOLUMNAS; j++)
            if (tablero[i][j] == VACIO)
                return false;

    return true;
}

/*************************************************************************
 Completa este metodo.
 Parametros: indices i y j del tablero.
 Retorno: cierto si se puede colocar ficha en la posicion (i,j) del
 tablero. Debes comprobar que esa posicion del tablero esta vacia
 (su valor es VACIO) y que es la posicion vacia mas baja del tablero.
 En caso contrario, la funcion debe devolver false.
 *************************************************************************/
public boolean sePuedeColocarFicha(int i, int j) {
    // Aqui debes incluir tu codigo

    if(tablero[i][j] == VACIO){
        return true;
    }

    else {
        return false;
    }
}

public void juegaMaquina() {
    int i;
    int fila = -1, columna;
    Random r = new Random();

    do {
        columna = r.nextInt(NCOLUMNAS);

        for (i = 0; i < NFILAS; i++)
            if (tablero[i][columna] == VACIO) {
                fila = i;
                break;
            }
    } while (fila < 0);

    tablero[fila][columna] = MAQUINA;
}
}

MainActivity.java

package android.esteban.conecta4;
//Creado por Esteban Fajardo Muñoz

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

private final int ids[][] = {
        { R.id.primerBoton5, R.id.segundoBoton5, R.id.tercerBoton5, R.id.cuartoBoton5, R.id.quintoBoton5, R.id.sextoBoton5, R.id.septimoBoton5 },
        { R.id.primerBoton4, R.id.segundoBoton4, R.id.tercerBoton4, R.id.cuartoBoton4, R.id.quintoBoton4, R.id.sextoBoton4, R.id.septimoBoton4 },
        { R.id.primerBoton3, R.id.segundoBoton3, R.id.tercerBoton3, R.id.cuartoBoton3, R.id.quintoBoton3, R.id.sextoBoton3,
                R.id.septimoBoton3 },
        { R.id.primerBoton2, R.id.segundoBoton2, R.id.tercerBoton2, R.id.cuartoBoton2, R.id.quintoBoton2, R.id.sextoBoton2,
                R.id.septimoBoton2 },
        { R.id.primerBoton1, R.id.segundoBoton1, R.id.tercerBoton1, R.id.cuartoBoton1, R.id.quintoBoton1, R.id.sextoBoton1,
                R.id.septimoBoton1 },
        { R.id.primerBoton, R.id.segundoBoton, R.id.tercerBoton, R.id.cuartoBoton, R.id.quintoBoton, R.id.sextoBoton,
                R.id.septimoBoton } };

private Game game;
private TextView resultadoTextView;

/*************************************************************************
 Completa este metodo.
 Despues de inflar la interfaz especificada en el fichero activity_main.xml,
 este metodo debe instanciar un objeto de tipo Game y asignar la referencia
 al miembro privado game.
 *************************************************************************/
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Aqui debes incluir tu codigo
    game = new Game();
    resultadoTextView = (TextView) findViewById(R.id.resultadoTextView);
}

/*************************************************************************
 Completa este metodo.
 Dependiendo del estado de cada casilla del tablero, debes asignar al
 identificador id el dibujable adecuado:
 - R.drawable.c4_button
 - R.drawable.c4_human_pressed_button
 - R.drawable.c4_machine_pressed_button
 *************************************************************************/
public void dibujarTablero() {
    int id = 0;

    for (int i = 0; i < Game.NFILAS; i++)
        for (int j = 0; j < Game.NCOLUMNAS; j++) {

            // Aqui debes incluir tu codigo

            if(game.estaJugador(i,j) == true){
                id = R.drawable.c4_human_pressed_button;
            }

            if(game.estaVacio(i,j) == true){
                id = R.drawable.c4_button;
            }

            if(game.estaVacio(i,j) == false && game.estaJugador(i,j) == false){
                id = R.drawable.c4_machine_pressed_button;
            }

            ImageButton imageButton = (ImageButton) findViewById(ids[i][j]);
            imageButton.setImageResource(id);
        }
}

public void pulsado(View v) {
    int fila, columna, id = v.getId();

    if (game.tableroLleno()) {
        resultadoTextView.setText(R.string.fin_del_juego);
        return;
    }

    fila = deIdentificadorAFila(id);
    columna = deIdentificadorAColumna(id);

    if (game.sePuedeColocarFicha(fila, columna) != true) {
        Toast.makeText(this, R.string.nosepuedecolocarficha,
                Toast.LENGTH_SHORT).show();
        return;
    }

    game.ponerJugador(fila, columna);
    game.juegaMaquina();
    dibujarTablero();
}

private int deIdentificadorAFila(int id) {
    for (int i = 0; i < Game.NFILAS; i++)
        for (int j = 0; j < Game.NCOLUMNAS; j++)
            if (ids[i][j] == id)
                return i;
    return -1;
}

private int deIdentificadorAColumna(int id) {
    for (int i = 0; i < Game.NFILAS; i++)
        for (int j = 0; j < Game.NCOLUMNAS; j++)
            if (ids[i][j] == id)
                return j;
    return -1;
}
}
4

1 回答 1

0

在 sePuedeColocarFicha() 方法中,您询问的是那个点 tablero[i][j] 是否为空,但不是最后一行/最低位置。您是否添加了一些代码来检查它?

此外,如果您不喜欢它,您将执行与 estaVacio 方法相同的操作(在 estaVacio 中,您应该在 IF 中询问该位置是否等于 0/vacio,因为这称为该方法 Vacio,您正在做与 esJugador 相同)(它还会检查 tablero 中的该位置是否为空)

于 2016-08-17T08:50:11.267 回答