Skip to content

Instantly share code, notes, and snippets.

@PabloSanchezMartinez
Created May 8, 2017 17:07
Show Gist options
  • Save PabloSanchezMartinez/000902dccba151157981eb123e79dae6 to your computer and use it in GitHub Desktop.
Save PabloSanchezMartinez/000902dccba151157981eb123e79dae6 to your computer and use it in GitHub Desktop.
Juego Piedra Papel Tijera con JAVA FX (by Pablo Sanchez)
package bo.ubi.lapaz;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
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 {
private int valor;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Image imagPiedra = new Image(getClass().getResourceAsStream("piedra.jpg"));
Image imagPapel = new Image(getClass().getResourceAsStream("papel.jpg"));
Image imagTijera = new Image(getClass().getResourceAsStream("tijera.jpg"));
Image imagJugar = new Image(getClass().getResourceAsStream("jugar.jpg"));
Image imagWin = new Image(getClass().getResourceAsStream("ganar.jpg"));
Image imagLose = new Image(getClass().getResourceAsStream("perder.jpg"));
Image imagEmpate = new Image(getClass().getResourceAsStream("empate.jpg"));
ImageView viewPiedra = new ImageView(imagPiedra);
viewPiedra.setFitHeight(200);
viewPiedra.setFitWidth(200);
ImageView viewPapel = new ImageView(imagPapel);
viewPapel.setFitHeight(200);
viewPapel.setFitWidth(200);
ImageView viewTijera = new ImageView(imagTijera);
viewTijera.setFitHeight(200);
viewTijera.setFitWidth(200);
ImageView viewJugar = new ImageView(imagJugar);
viewJugar.setFitHeight(50);
viewJugar.setFitWidth(200);
ImageView viewWin =new ImageView(imagWin);
viewWin.setFitHeight(60);
viewWin.setFitWidth(120);
ImageView viewLose=new ImageView(imagLose);
viewLose.setFitHeight(60);
viewLose.setFitWidth(120);
ImageView viewEmpate=new ImageView(imagEmpate);
viewEmpate.setFitHeight(60);
viewEmpate.setFitWidth(120);
Button bPiedra = new Button(null, viewPiedra);
Button bPapel = new Button(null, viewPapel);
Button bTijera = new Button(null, viewTijera);
Button bJugar = new Button(null, viewJugar);
bPiedra.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
valor = 0;
System.out.println(valor);
}
});
bPapel.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
valor = 1;
System.out.println(valor);
}
});
bTijera.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
valor = 2;
System.out.println(valor);
}
});
VBox resul = new VBox();
bJugar.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Random random = new Random();
int numeroAleatorio = random.nextInt(3);
System.out.println("PC" + numeroAleatorio);
if (numeroAleatorio != valor) {
if (valor == 0 && numeroAleatorio == 2) {
//gana piedra usuario
Label resultado = new Label(null,viewWin);
resul.getChildren().add(resultado);
} else if (valor == 0 && numeroAleatorio == 1) {
//gana papel compu
Label resultado = new Label(null,viewLose);
resul.getChildren().add(resultado);
} else if (valor == 1 && numeroAleatorio == 0) {
//gana papel usuario
Label resultado = new Label(null,viewWin);
resul.getChildren().add(resultado);
} else if (valor == 1 && numeroAleatorio == 2) {
//gana tijera compu
Label resultado = new Label(null,viewLose);
resul.getChildren().add(resultado);
} else if (valor == 2 && numeroAleatorio == 0) {
//gana piedra compu
Label resultado = new Label(null,viewLose);
resul.getChildren().add(resultado);
} else if (valor == 2 && numeroAleatorio == 1) {
//gana tijera usuario
Label resultado = new Label(null,viewWin);
resul.getChildren().add(resultado);
}
} else {
Label resultado = new Label(null,viewEmpate);
resul.getChildren().add(resultado);
}
}
});
HBox fila = new HBox();
fila.getChildren().add(bPiedra);
fila.getChildren().add(bPapel);
fila.getChildren().add(bTijera);
fila.setAlignment(Pos.BOTTOM_CENTER);
VBox columna = new VBox();
columna.getChildren().add(fila);
columna.getChildren().add(bJugar);
columna.getChildren().add(resul);
columna.setAlignment(Pos.TOP_CENTER);
Scene grafico = new Scene(columna, 650, 650);
primaryStage.setScene(grafico);
primaryStage.setTitle("PIEDRA | PAPEL | TIJERA");
primaryStage.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment