八重豆子 发表于 2023-4-5 13:03:29

【非mc】ChatGPT搞的猜拳2.0

https://tc.52chye.cn/i/2023/04/05/ktwfrb.png全部由chatgpt开发,我只是翻译成了中文

这次加入了积分系统,赢了就加一积分,输了不会减少。也有会减少的版本。
源码:
以下为index.html
DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>石头剪刀布游戏</title>
    <style>
      body {
            font-family: Arial, sans-serif;
            text-align: center;
      }
      h1 {
            margin-top: 50px;
      }
      button {
            padding: 10px;
            margin: 20px;
            font-size: 20px;
            cursor: pointer;
      }
      #result {
            font-size: 24px;
            font-weight: bold;
            margin-top: 50px;
      }
    </style>
</head>
<body>
    <h1>石头剪刀布游戏</h1>
    <p>请选择:</p>
    <button onclick="play('石头')">石头</button>
    <button onclick="play('剪刀')">剪刀</button>
    <button onclick="play('布')">布</button>
    <p id="result"></p>
    <p>积分:<span id="score">0</span></p>
    <button onclick="reset()">重新开始</button>
    <script src="game.js"></script>
    <p>如果积分为Nan,点击重新开始即可。</p>
</html>以下为game.js,输了不降分(与index.html在同一个目录)
// 定义选择函数
function computerPlay() {
    const choices = ['石头', '剪刀', '布'];
    const randomIndex = Math.floor(Math.random() * 3);
    return choices;
}

function play(playerSelection) {
    const computerSelection = computerPlay();
    let result;

    // 比较选择结果
    if (playerSelection === computerSelection) {
      result = "平局!";
    } else if (
      (playerSelection === "石头" && computerSelection === "剪刀") ||
      (playerSelection === "剪刀" && computerSelection === "布") ||
      (playerSelection === "布" && computerSelection === "石头")
    ) {
      result = "你赢了!";
      // 加分
      score++;
         document.getElementById("score").textContent = score;
    } else {
      result = "你输了!";

    }

    // 显示结果
    document.getElementById("result").textContent = `你出了${playerSelection},电脑出了${computerSelection}。${result}`;
}

function reset() {
    score = 0;
    document.getElementById("score").textContent = score;
    document.getElementById("result").textContent = "";
}以下为输了会减分版本的game.js
// 定义选择函数
function computerPlay() {
    const choices = ['石头', '剪刀', '布'];
    const randomIndex = Math.floor(Math.random() * 3);
    return choices;
}

function play(playerSelection) {
    const computerSelection = computerPlay();
    let result;

    // 比较选择结果
    if (playerSelection === computerSelection) {
      result = "平局!";
    } else if (
      (playerSelection === "石头" && computerSelection === "剪刀") ||
      (playerSelection === "剪刀" && computerSelection === "布") ||
      (playerSelection === "布" && computerSelection === "石头")
    ) {
      result = "你赢了!";
      // 加分
      score++;
    } else {
      result = "你输了!";
      //减分
      score--;
                document.getElementById("score").textContent = score;
    }

    // 显示结果
    document.getElementById("result").textContent = `你出了${playerSelection},电脑出了${computerSelection}。${result}`;
}

function reset() {
    score = 0;
    document.getElementById("score").textContent = score;
    document.getElementById("result").textContent = "";
}



压缩答辩人 发表于 2023-4-29 17:46:38

srfkzrfksdvkzdrfkserfk
页: [1]
查看完整版本: 【非mc】ChatGPT搞的猜拳2.0