-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStarWarsGame.java
More file actions
133 lines (113 loc) · 4.38 KB
/
Copy pathStarWarsGame.java
File metadata and controls
133 lines (113 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
import java.util.Random;
public class StarWarsGame {
static final int SIZE = 10;
static final char YODA = 'Y';
static final char VADER = 'V';
static final char DARTH_MAUL = 'D';
static final char R2D2 = 'R';
static final char WALL = 'M';
static final char EMPTY = 'L';
static final int START_LIVES = 3;
char[][] yodaBoard = new char[SIZE][SIZE];
char[][] vaderBoard = new char[SIZE][SIZE];
int yodaRow, yodaCol, vaderRow, vaderCol;
int yodaLives = START_LIVES, vaderLives = START_LIVES;
Random random = new Random();
public StarWarsGame() {
initializeBoard(yodaBoard, YODA, DARTH_MAUL, WALL);
initializeBoard(vaderBoard, VADER, R2D2, WALL);
}
private void initializeBoard(char[][] board, char player, char badCharacter, char wall) {
// Rellenar con 'L' inicialmente
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
board[i][j] = EMPTY;
}
}
// Colocar al jugador aleatoriamente
placeCharacter(board, player);
// Colocar 5 personajes malos
for (int i = 0; i < 5; i++) {
placeCharacter(board, badCharacter);
}
// Colocar 5 muros
for (int i = 0; i < 5; i++) {
placeCharacter(board, wall);
}
// Definir la casilla final
board[SIZE - 1][SIZE - 1] = 'F';
}
private void placeCharacter(char[][] board, char character) {
int row, col;
do {
row = random.nextInt(SIZE);
col = random.nextInt(SIZE);
} while (board[row][col] != EMPTY);
board[row][col] = character;
// Guardar posición inicial de los jugadores
if (character == YODA) {
yodaRow = row;
yodaCol = col;
} else if (character == VADER) {
vaderRow = row;
vaderCol = col;
}
}
public void movePlayer(char player, int newRow, int newCol) {
char[][] board = (player == YODA) ? yodaBoard : vaderBoard;
int currentRow = (player == YODA) ? yodaRow : vaderRow;
int currentCol = (player == YODA) ? yodaCol : vaderCol;
int lives = (player == YODA) ? yodaLives : vaderLives;
if (newRow < 0 || newRow >= SIZE || newCol < 0 || newCol >= SIZE || board[newRow][newCol] == WALL) {
System.out.println("Movimiento no válido.");
return;
}
// Actualizar la posición del jugador en el tablero
board[currentRow][currentCol] = EMPTY;
if (board[newRow][newCol] == DARTH_MAUL || board[newRow][newCol] == R2D2) {
lives--;
System.out.println(player + " perdió una vida. Vidas restantes: " + lives);
}
if (lives <= 0) {
System.out.println(player + " ha perdido todas sus vidas. ¡Perdió el juego!");
System.exit(0);
}
board[newRow][newCol] = player;
if (player == YODA) {
yodaRow = newRow;
yodaCol = newCol;
yodaLives = lives;
} else {
vaderRow = newRow;
vaderCol = newCol;
vaderLives = lives;
}
// Verificar si el jugador ha ganado
if (newRow == SIZE - 1 && newCol == SIZE - 1) {
System.out.println(player + " ha llegado a la casilla final. ¡Ganó el juego!");
System.exit(0);
}
}
public void displayBoard(char[][] board) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
public static void main(String[] args) {
StarWarsGame game = new StarWarsGame();
System.out.println("Tablero de Yoda:");
game.displayBoard(game.yodaBoard);
System.out.println("Tablero de Darth Vader:");
game.displayBoard(game.vaderBoard);
// Aquí puedes agregar lógica para mover a los jugadores
// Ejemplo de movimientos:
game.movePlayer(YODA, 1, 1); // Cambia las coordenadas según las reglas del juego
game.movePlayer(VADER, 2, 2); // Cambia las coordenadas según las reglas del juego
}
}