Java 中的猪游戏

2024 年 9 月 10 日 | 阅读 7 分钟

“猪游戏”(Pig Game),也称为“猪骰子游戏”(Pig Dice Game)或“传猪”(Pass the Pigs),是一款简单有趣的骰子游戏,可以用Java编程语言实现。游戏涉及掷一对骰子,并根据结果累积分数。游戏的目标是成为第一个达到预定分数总和的玩家,通常是100分。在本文中,我们将探讨“猪游戏”的规则,并演示如何在Java中实现它。

猪游戏规则

游戏通常由两名或两名以上玩家进行。

  • 在每个回合中,玩家掷一对六面骰子。
  • 玩家根据骰子点数之和累积分数。
  • 如果玩家掷出1点(无论哪一个骰子),则他们的回合结束,并且他们在此回合中累积的所有分数作废。
  • 如果玩家掷出两个骰子都为1点,则他们将失去所有分数,并且回合结束。
  • 玩家可以在回合的任何时候选择“hold”(暂停),这将把累积的分数添加到他们的总得分中。
  • 第一个达到或超过预定分数总和(例如100分)的玩家赢得游戏。

Java 实现

要在Java中实现“猪游戏”,我们可以遵循以下步骤:

步骤1: 设置游戏

  • 定义玩家人数和目标分数。
  • 将每个玩家的总得分初始化为零。

步骤2: 实现骰子滚动

  • 生成1到6之间的随机数来模拟掷骰子。
  • 将掷出的点数加到玩家当前回合的分数中。
  • 检查是否掷出1点(无论哪一个骰子),并处理回合结束的条件。

步骤3: 实现玩家回合

  • 提示当前玩家掷骰子或暂停。
  • 调用骰子滚动函数并相应地更新玩家得分。
  • 重复此过程,直到玩家决定暂停或掷出1点。

步骤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版的“猪游戏”吧!