简体   繁体   English

方法“dibujarTablero”和方法“pulsado”

[英]Method 'dibujarTablero' and method 'pulsado'

I'm trying to make a 4 in line in Android Studio, I have to complete one method (dibujarTablero() --> drawBoard() in english), the thing is, I have to assign the correct drawable to the id depending on the status of each ring on the board, I don't know if I completed the method in the right way.我正在尝试在 Android Studio 中制作 4 行,我必须完成一种方法(dibujarTablero() --> 英语中的 drawBoard()),问题是,我必须根据以下情况将正确的 drawable 分配给 id棋盘上每一个戒指的状态,不知道是不是我方法对了。

By the other hand, the method 'pulsado' is never used, I want to know where it must be called or used :/.另一方面,从未使用过“pulsado”方法,我想知道必须在何处调用或使用它:/。 I attach the code of the Game.java and MainActivity.java, the android code is right, I just need the java code.我附上Game.java和MainActivity.java的代码,android代码是对的,我只需要java代码。

Game.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主活动.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;
}
}

Para llamar al método "pulsado" hazlo desde el XML activity_main.xml o directamente desde styles.xml asignando el mismo estilo a cada botón. XML的XML文件activity_main.xml或styles.xml的XML注释。

Lo de las ids de los botones para imageButton creo que está bien así. 图片按钮的图像按钮。

In the method sePuedeColocarFicha() you are asking if that spot tablero[i][j] is empty, but not if is the last row / the lowest position.在方法 sePuedeColocarFicha() 中,您询问该位置 tablero[i][j] 是否为空,但不是如果是最后一行/最低位置。 Have you added some code to check it?您是否添加了一些代码来检查它?

Also, in case you leave it like it, you are doing the same as in the method estaVacio (in estaVacio you should ask in the IF if that position is equal to 0/vacio, for that is called that method Vacio, you are doing the same as in esJugador) (which also checks if that position in tablero is empty)此外,如果你喜欢它,你正在做与方法 estaVacio 相同的事情(在 estaVacio 中,你应该在 IF 中询问该位置是否等于 0/vacio,因为它被称为方法 Vacio,你正在做与 esJugador 相同)(它还检查 tablero 中的该位置是否为空)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM