You are on page 1of 2

import java.util.

Random;
import java.util.Scanner;

public class Tesoro {


// se definen constantes para representar el
// contenido de las celdas
static final int VACIO = 0;
static final int MINA = 1;
static final int TESORO = 2;
static final int INTENTO = 3;
static final int FILAS = 3;
static final int COLUMNAS = 4;

public static void main(String[] args) {


int[][] board = new int[FILAS][COLUMNAS];

// Initialize the board with 'VACIO'


for (int i = 0; i < FILAS; i++) {
for (int j = 0; j < COLUMNAS; j++) {
board[i][j] = VACIO;
}
}

// Place the mine and treasure at random positions


Random random = new Random();
int mineRow = random.nextInt(FILAS);
int mineColumn = random.nextInt(COLUMNAS);
board[mineRow][mineColumn] = MINA;

int treasureRow = random.nextInt(FILAS);


int treasureColumn = random.nextInt(COLUMNAS);
while (mineRow == treasureRow && mineColumn == treasureColumn) {
treasureRow = random.nextInt(FILAS);
treasureColumn = random.nextInt(COLUMNAS);
}
board[treasureRow][treasureColumn] = TESORO;

// User input
Scanner scanner = new Scanner(System.in);
boolean foundTreasure = false;
int guessRows;
int guessColumns;

while (!foundTreasure) {
System.out.println("Enter the row and column of your guess (0-" +
(FILAS - 1) + "):");
guessRows = scanner.nextInt();
guessColumns = scanner.nextInt();

if (guessRows >= 0 && guessRows < FILAS && guessColumns >= 0 &&
guessColumns < COLUMNAS) {
if (board[guessRows][guessColumns] == TESORO) {
System.out.println("Congratulations! You found the treasure!");
foundTreasure = true;
} else {
System.out.println("Sorry, you didn't find the treasure. Try
again!");
board[guessRows][guessColumns] = INTENTO;
}
} else {
System.out.println("Invalid input. Please try again.");
}
}

scanner.close();

// Print the board


System.out.println("Game board:");
for (int i = 0; i < FILAS; i++) {
for (int j = 0; j < COLUMNAS; j++) {
if (board[i][j] == MINA) {
System.out.print(" M ");
} else if (board[i][j] == TESORO) {
System.out.print(" T ");
} else if (board[i][j] == INTENTO) {
System.out.print(" X ");
} else {
System.out.print(" . ");
}
}
System.out.println();
}
}
}

You might also like