JavaFX实验2 石头剪刀布

编写一个和计算机交互的“石头-剪刀-布”游戏程序。当两个人玩的时候,每个人要同时选择一项(用手势表示),然后决出胜负。规则为石头赢剪刀,剪刀赢布,布赢石头。程序必须随机选取一项但不显示给玩家,然后提示用户选择。当用户选择后,程序同时显示出计算机和用户的选择并且输出游戏结果。游戏一直进行到用户退出为止,然后输出双方输赢的次数及和局的次数。



实验过程

1. 目录结构
2. 实验代码
3. 实验效果



1 . 目录结构

在这里插入图片描述



2. 实验代码

MainController

package tech.zger.www.controller;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author zg
 * @create 2021/11/16 0:45
 */
public class MainController implements Initializable {
    private static final int SCISSOR = 0;
    private static final int CLOTH = 1;
    private static final int STONE = 2;
    /**
     * 电脑玩家的选择
     */
    @FXML
    private ImageView computer;
    /**
     * 玩家的选择
     */
    @FXML
    private ImageView people;
    /**
     * 平局
     */
    private int draw;
    private int win;
    private int fail;
    /**
     * 完成一局游戏需要点击 再来一次 才能进行下一次游戏
     */
    private boolean changeable = true;

    @FXML
    private void clickScissors() {
        if (changeable) {
            this.people.setImage(new Image(getClass().getResourceAsStream("/tech/zger/www/images/scissor.png")));
            int computerInput = this.generateRandomSign();
            if (computerInput == SCISSOR) {
                this.draw++;
                showDraw();
            } else if (computerInput == CLOTH) {
                this.win++;
                showWin();
            } else {
                this.fail++;
                showFail();
            }
            changeable = false;
        }
    }

    @FXML
    private void clickCloth() {
        if (changeable) {
            this.people.setImage(new Image(getClass().getResourceAsStream("/tech/zger/www/images/cloth.png")));
            int computerInput = this.generateRandomSign();
            if (computerInput == CLOTH) {
                this.draw++;
                showDraw();
            } else if (computerInput == STONE) {
                this.win++;
                showWin();
            } else {
                this.fail++;
                showFail();
            }
            changeable = false;
        }
    }

    @FXML
    private void clickStone() {
        if (changeable) {
            this.people.setImage(new Image(getClass().getResourceAsStream("/tech/zger/www/images/stone.png")));
            int computerInput = this.generateRandomSign();
            if (computerInput == STONE) {
                this.draw++;
                showDraw();
            } else if (computerInput == SCISSOR) {
                this.win++;
                showWin();
            } else {
                this.fail++;
                showFail();
            }
            changeable = false;
        }
    }

    @FXML
    private void reset() {
        this.computer.setImage(new Image("/tech/zger/www/images/judge.png"));
        this.people.setImage(new Image("/tech/zger/www/images/judge.png"));
        this.changeable = true;
    }

    @FXML
    private void showResult() {
        Stage stage = new Stage();
        stage.setTitle("结果预览");
        stage.setResizable(false);
        stage.getIcons().add(new Image(getClass().getResourceAsStream("/tech/zger/www/images/logo/zg.jpg")));
        try {
            Pane pane = FXMLLoader.load(getClass().getResource("/tech/zger/www/view/result.fxml"));
            // 总场次
            int all = this.draw + this.win + this.fail;
            // 获取id = xxx  的标签并赋值
            ((Label) (pane.lookup("#win"))).setText("" + this.win);
            ((Label) (pane.lookup("#lost"))).setText("" + this.fail);
            ((Label) (pane.lookup("#draw"))).setText("" + this.draw);
            ((Label) (pane.lookup("#all"))).setText("" + all);
            Scene scene = new Scene(pane, 400, 250);
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private int generateRandomSign() {
        int n = (int) (Math.random() * 10) % 3;
        int computerInput;
        if (n == SCISSOR) {
            this.computer.setImage(new Image("/tech/zger/www/images/scissor.png"));
            computerInput = SCISSOR;
        } else if (n == CLOTH) {
            this.computer.setImage(new Image("/tech/zger/www/images/cloth.png"));
            computerInput = CLOTH;
        } else {
            this.computer.setImage(new Image("/tech/zger/www/images/stone.png"));
            computerInput = STONE;
        }
        return computerInput;
    }

    private void showWin() {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setHeaderText("YOU WIN!");
        alert.setContentText("点击再来一次按钮可再次进行游戏");
        alert.setTitle("第" + (draw + win + fail) + "次游戏的结果");
        alert.show();
    }

    private void showFail() {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setHeaderText("YOU LOST!");
        alert.setContentText("点击再来一次按钮可再次进行游戏");
        alert.setTitle("第" + (draw + win + fail) + "次游戏的结果");
        alert.show();
    }

    private void showDraw() {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setHeaderText("Draw!");
        alert.setContentText("点击再来一次按钮可再次进行游戏");
        alert.setTitle("第" + (draw + win + fail) + "次游戏的结果");
        alert.show();
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        this.draw = 0;
        this.win = 0;
        this.fail = 0;
    }
}

MainApplication

package tech.zger.www;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

import java.io.IOException;

/**
 * @author zg
 * @create 2021/11/16 0:16
 */
public class MainApplication extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("石头剪刀布");
        primaryStage.setResizable(false);
        primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/tech/zger/www/images/logo/zg.jpg")));
        try {
            Pane pane = FXMLLoader.load(getClass().getResource("/tech/zger/www/view/index.fxml"));
            Scene scene = new Scene(pane, 600, 400);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

index.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
      prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1"
      fx:controller="tech.zger.www.controller.MainController">
    <children>
        <ImageView fx:id="computer" fitHeight="150.0" fitWidth="200.0" layoutX="106.0" layoutY="75.0"
                   pickOnBounds="true" preserveRatio="true">
            <image>
                <Image url="@../images/judge.png"/>
            </image>
        </ImageView>
        <Label layoutX="157.0" layoutY="26.0" text="电脑玩家"/>
        <Button layoutX="141.0" layoutY="282.0" mnemonicParsing="false" onAction="#clickStone" text="石头"/>
        <Button layoutX="288.0" layoutY="282.0" mnemonicParsing="false" onAction="#clickScissors"
                text="剪刀"/>
        <Button layoutX="411.0" layoutY="282.0" mnemonicParsing="false" onAction="#clickCloth"
                prefHeight="23.0" prefWidth="40.0" text=""/>
        <ImageView fx:id="people" fitHeight="150.0" fitWidth="200.0" layoutX="321.0" layoutY="75.0" pickOnBounds="true"
                   preserveRatio="true">
            <image>
                <Image url="@../images/judge.png"/>
            </image>
        </ImageView>
        <Label layoutX="364.0" layoutY="26.0" text="你的选择"/>
        <Button layoutX="364.0" layoutY="349.0" mnemonicParsing="false" onAction="#showResult" text="结果预览"/>
        <Button layoutX="181.0" layoutY="349.0" mnemonicParsing="false" onAction="#reset" text="再来一次"/>
    </children>
</Pane>

result.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="250.0"
      prefWidth="400.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <Label layoutX="112.0" layoutY="50.0" text=""/>
        <Label fx:id="win" layoutX="233.0" layoutY="50.0" text="0" id="win"/>
        <Label layoutX="112.0" layoutY="90.0" text=""/>
        <Label fx:id="fail" layoutX="233.0" layoutY="90.0" text="0" id="lost"/>
        <Label layoutX="112.0" layoutY="131.0" text="平局"/>
        <Label fx:id="draw" layoutX="233.0" layoutY="131.0" text="0" id="draw"/>
        <Label layoutX="112.0" layoutY="176.0" text="总场次"/>
        <Label fx:id="all" layoutX="233.0" layoutY="176.0" text="0" id="all"/>
    </children>
</Pane>



3. 结果展示
  • 初始界面
    在这里插入图片描述

  • 进行游戏
    在这里插入图片描述
    在这里插入图片描述

  • 再来一次
    在这里插入图片描述

  • 结果预览
    在这里插入图片描述

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值