Java 中的猪游戏2024 年 9 月 10 日 | 阅读 7 分钟 “猪游戏”(Pig Game),也称为“猪骰子游戏”(Pig Dice Game)或“传猪”(Pass the Pigs),是一款简单有趣的骰子游戏,可以用Java编程语言实现。游戏涉及掷一对骰子,并根据结果累积分数。游戏的目标是成为第一个达到预定分数总和的玩家,通常是100分。在本文中,我们将探讨“猪游戏”的规则,并演示如何在Java中实现它。 猪游戏规则游戏通常由两名或两名以上玩家进行。
Java 实现要在Java中实现“猪游戏”,我们可以遵循以下步骤: 步骤1: 设置游戏
步骤2: 实现骰子滚动
步骤3: 实现玩家回合
步骤4: 实现游戏循环
步骤5: 宣布获胜者
这是一个实现“猪游戏”的完整Java程序。 PigGame.java 输出 Enter the number of players: 2 Player 1's turn. Current Score: 0 Dice 1: 4 Dice 2: 3 Turn score: 7 Do you want to roll again (R) or hold (H)? r Player 1's turn. Current Score: 7 Dice 1: 2 Dice 2: 6 Turn score: 8 Do you want to roll again (R) or hold (H)? h Player 2's turn. Current Score: 15 Dice 1: 1 Dice 2: 3 Oops! You rolled a 1. Turn score reset to 0. Player 1's turn. Current Score: 15 Dice 1: 5 Dice 2: 5 Turn score: 10 Do you want to roll again (R) or hold (H)? h Player 2's turn. Current Score: 0 Dice 1: 4 Dice 2: 4 Turn score: 8 Do you want to roll again (R) or hold (H)? r Player 2's turn. Current Score: 8 Dice 1: 6 Dice 2: 6 Oops! You rolled two 1's. Turn score reset to 0. Player 1's turn. Current Score: 25 Dice 1: 1 Dice 2: 6 Oops! You rolled a 1. Turn score reset to 0. Player 2's turn. Current Score: 0 Dice 1: 3 Dice 2: 5 Turn score: 8 Do you want to roll again (R) or hold (H)? h Player 1's turn. Current Score: 25 Dice 1: 2 Dice 2: 1 Turn score: 3 Do you want to roll again (R) or hold (H)? r Player 1's turn. Current Score: 28 Dice 1: 6 Dice 2: 3 Turn score: 9 Do you want to roll again (R) or hold (H)? r Player 1's turn. Current Score: 37 Dice 1: 4 Dice 2: 6 Turn score: 10 Do you want to roll again (R) or hold (H)? h Player 2's turn. Current Score: 8 Dice 1: 2 Dice 2: 3 Turn score: 5 Do you want to roll again (R) or hold (H)? r Player 2's turn. Current Score: 13 Dice 1: 1 Dice 2: 4 Oops! You rolled a 1. Turn score reset to 0. Player 1's turn. Current Score: 47 Dice 1: 3 Dice 2: 1 Turn score: 4 Do you want to roll again (R) or hold (H)? h Player 1's turn. Current Score: 51 Dice 1: 6 Dice 2: 2 Turn score: 8 Do you want to roll again (R) or hold (H)? h Player 2's turn. Current Score: 13 Dice 1: 2 Dice 2: 4 Turn score: 6 Do you want to roll again (R) or hold (H)? r Player 2's turn. Current Score: 19 Dice 1: 5 Dice 2: 2 Turn score: 7 Do you want to roll again (R) or hold (H)? r Player 2's turn. Current Score: 26 Dice 1: 3 Dice 2: 4 Turn score: 7 Do you want to roll again (R) or hold (H)? r Player 2's turn. Current Score: 33 Dice 1: 4 Dice 2: 1 Turn score: 5 Do you want to roll again (R) or hold (H)? r Player 2's turn. Current Score: 38 Dice 1: 5 Dice 2: 3 Turn score: 8 Do you want to roll again (R) or hold (H)? h Player 1's turn. Current Score: 59 Dice 1: 1 Dice 2: 2 Turn score: 3 Do you want to roll again (R) or hold (H)? h Player 2's turn. Current Score: 46 Dice 1: 2 Dice 2: 4 Turn score: 6 Do you want to roll again (R) or hold (H)? h Player 1's turn. Current Score: 62 Dice 1: 5 Dice 2: 4 Turn score: 9 Do you want to roll again (R) or hold (H)? h Player 2's turn. Current Score: 52 Dice 1: 5 Dice 2: 1 Turn score: 6 Do you want to roll again (R) or hold (H)? h Player 1's turn. Current Score: 71 Dice 1: 3 Dice 2: 4 Turn score: 7 Do you want to roll again (R) or hold (H)? h Player 2's turn. Current Score: 58 Dice 1: 6 Dice 2: 2 Turn score: 8 Do you want to roll again (R) or hold (H)? h Player 1's turn. Current Score: 78 Dice 1: 1 Dice 2: 2 Turn score: 3 Do you want to roll again (R) or hold (H)? h Player 2's turn. Current Score: 66 Dice 1: 5 Dice 2: 3 Turn score: 8 Do you want to roll again (R) or hold (H)? r Player 2's turn. Current Score: 74 Dice 1: 6 Dice 2: 4 Turn score: 10 Do you want to roll again (R) or hold (H)? h Player 1's turn. Current Score: 81 Dice 1: 2 Dice 2: 2 Turn score: 4 Do you want to roll again (R) or hold (H)? r Player 1's turn. Current Score: 85 Dice 1: 1 Dice 2: 4 Turn score: 5 Do you want to roll again (R) or hold (H)? h Player 2's turn. Current Score: 84 Dice 1: 5 Dice 2: 4 Turn score: 9 Do you want to roll again (R) or hold (H)? r Player 2's turn. Current Score: 93 Dice 1: 2 Dice 2: 6 Turn score: 8 Do you want to roll again (R) or hold (H)? r Player 2's turn. Current Score: 101 Dice 1: 3 Dice 2: 5 Turn score: 8 Player 2 wins with a score of 101! “猪游戏”是一个简单但有趣的游戏,可以在Java中轻松实现。通过遵循本文概述的规则和步骤,您可以创建一个功能齐全的“猪游戏”程序。这个项目为您提供了在享受乐趣的同时练习Java编程技能的绝佳机会。所以,叫上你的朋友们,掷骰子,一起享受Java版的“猪游戏”吧! 下一主题Java中的不可达代码错误 |
绳索的最小成本是计算机科学和竞争性编程中的一个经典问题。它基于合并绳索以最小化总成本的概念。想象一下,你有几根不同长度的绳索,需要将它们合并成一根...
阅读 8 分钟
? File: SwapWithoutTemp .java public class SwapWithoutTemp { public static void main(String args[]) { String a = "Love"; String b = "You"; System.out.println("Before swap: " + a + " " + b); a = a + b; b = a.substring(0, a.length() - b.length()); a = a.substring(b.length()); System.out.println("After : " + a + " " +...
阅读1分钟
到目前为止,我们一直专注于 Java 中的对象。自 Java 8 以来,函数式编程方面得到了更多的重视。JavaSoft 的人意识到,一切都使用对象变得麻烦,在某些情况下使用函数可能更有效。Lambda 表达式...
阅读 4 分钟
创建 Java 身体质量指数 (BMI) 计算器需要实施多种使用不同公式计算 BMI 的方法。身体质量指数 (BMI) 是一种工具,用于根据身高和体重确定个人的身体脂肪。修改后的 BMI 公式,...
阅读 4 分钟
FileNotFoundException是java.io包中提供的另一个异常类。当我们尝试访问系统中不存在的文件时,就会发生此异常。它是一个受检异常,因为它发生在运行时,而不是编译时,并且它由一个...
阅读 4 分钟
在名为 Str 的字符串列表中查找最大的字符串。具有最多不同字符的字符串就是最大的字符串。示例 1:输入:字符串 str[] = {"AN KOW", "LO JO", "ZEW DO RO"} 输出:最多唯一字符的字符串是 "ZEW DO RO"。解释:"AN KOW" 具有唯一字符...
阅读9分钟
维吉尼亚密码使用一种基本的 polyalphabetic 替换技术来加密字母文本。它比传统的凯撒密码更安全,因为它使用关键字通过不同的量来移动明文中的字母。在本节中,我们将解释维吉尼亚...
5 分钟阅读
众所周知,Java 是一种知名且广泛使用的编程语言,遵循面向对象编程(OOPs)概念。要在 32 位操作系统上下载和安装 Java,用户需要访问 Oracle 官方网站。应该知道 Windowsx86...
阅读 2 分钟
将数据映射到固定大小哈希的算法称为哈希算法。Java 中的哈希算法是加密哈希函数。哈希算法或哈希函数的设计方式使其表现得像一个单向...
阅读9分钟
可以使用 MD5、SHA-1、SHA-256 等各种算法生成文件校验和值。校验和是一个数字签名,有助于确保文件的完整性和真实性。通过生成校验和值,您可以将其与原始校验和进行比较……
11 分钟阅读
我们请求您订阅我们的新闻通讯以获取最新更新。
我们提供所有技术(如 Java 教程、Android、Java 框架)的教程和面试问题
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India