Skip to content

Instantly share code, notes, and snippets.

@alvareztech
Created May 14, 2017 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alvareztech/ea3f3db64acce18ca47381df11fc4f25 to your computer and use it in GitHub Desktop.
Save alvareztech/ea3f3db64acce18ca47381df11fc4f25 to your computer and use it in GitHub Desktop.
JavaFX: Jankenpon
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.util.Random;
public class Main extends Application {
public static final int PAPEL = 1;
public static final int PIEDRA = 2;
public static final int TIJERAS = 3;
private Label seleccionUsuarioLabel;
private Label seleccionMaquinaLabel;
private Label mensajeLabel;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Image papelImage = new Image(getClass().getResourceAsStream("papel.png"));
Image piedraImage = new Image(getClass().getResourceAsStream("piedra.png"));
Image tijerasImage = new Image(getClass().getResourceAsStream("tijeras.png"));
ImageView papelImageView = new ImageView(papelImage);
ImageView piedraImageView = new ImageView(piedraImage);
ImageView tijerasImageView = new ImageView(tijerasImage);
papelImageView.setFitWidth(60);
papelImageView.setPreserveRatio(true);
piedraImageView.setFitWidth(60);
piedraImageView.setPreserveRatio(true);
tijerasImageView.setFitWidth(60);
tijerasImageView.setPreserveRatio(true);
Button papelButton = new Button(null, papelImageView);
Button piedraButton = new Button(null, piedraImageView);
Button tijeraButton = new Button(null, tijerasImageView);
HBox hBox = new HBox();
hBox.getChildren().addAll(papelButton, piedraButton, tijeraButton);
hBox.setSpacing(20);
seleccionUsuarioLabel = new Label();
seleccionMaquinaLabel = new Label();
mensajeLabel = new Label();
VBox vBox = new VBox();
vBox.getChildren().addAll(hBox, seleccionUsuarioLabel, seleccionMaquinaLabel, mensajeLabel);
vBox.setSpacing(20);
vBox.setPadding(new Insets(20));
// Acciones de los botones
papelButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
calcularGanador(PAPEL);
}
});
piedraButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
calcularGanador(PIEDRA);
}
});
tijeraButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
calcularGanador(TIJERAS);
}
});
Scene scene = new Scene(vBox, 320, 220);
primaryStage.setScene(scene);
primaryStage.show();
}
private void calcularGanador(int seleccionUsuario) {
int seleccionMaquina = obtenerSeleccionMaquina();
seleccionUsuarioLabel.setText("Tu seleccionaste: " + obtenerNombreSeleccion(seleccionUsuario));
seleccionMaquinaLabel.setText("La máquina seleccionó: " + obtenerNombreSeleccion(seleccionMaquina));
if (seleccionUsuario == seleccionMaquina) {
mensajeLabel.setText("¡Empate!");
} else {
if (ganoUsuario(seleccionUsuario, seleccionMaquina)) {
mensajeLabel.setText("¡Ganaste!");
} else {
mensajeLabel.setText("¡Ganó la máquina!");
}
}
}
/**
* Método que retorna un true o false si el usuario gano, caso contrario gano la máquina
*/
private boolean ganoUsuario(int seleccionUsuario, int seleccionMaquina) {
if (seleccionUsuario == PAPEL && seleccionMaquina == PIEDRA) {
return true;
} else if (seleccionUsuario == PAPEL && seleccionMaquina == TIJERAS) {
return false;
} else if (seleccionUsuario == PIEDRA && seleccionMaquina == PAPEL) {
return false;
} else if (seleccionUsuario == PIEDRA && seleccionMaquina == TIJERAS) {
return true;
} else if (seleccionUsuario == TIJERAS && seleccionMaquina == PAPEL) {
return true;
} else if (seleccionUsuario == TIJERAS && seleccionMaquina == PIEDRA) {
return false;
}
return false;
}
private String obtenerNombreSeleccion(int seleccion) {
if (seleccion == PAPEL) {
return "PAPEL";
} else if (seleccion == PIEDRA) {
return "PIEDRA";
} else if (seleccion == TIJERAS) {
return "TIJERAS";
}
return "";
}
private int obtenerSeleccionMaquina() {
Random random = new Random();
int numeroAleatorio = random.nextInt(3);
return numeroAleatorio + 1; // para que sea del 1 al 3 y no del 0 al 2
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment