Java中的刽子手游戏
Hangman是一种流行的猜词游戏,玩家通过一次推测一个字母来努力构建一个丢失的单词。经过一定数量的偏离基地猜测后,游戏结束,玩家输了。当玩家准确区分丢失单词的所有字母时,游戏也会结束。下面给出了这个游戏在Java中的实现。
例子:
Output:
Welcome to HANGMAN GAME
let’s play the game
_ _ _ _ _ _
Your Guess:
Input: s
Output:
S is not present in the word.
_ _ _ _ _ _
You have 4 guesses left.
Your Guess:
Input: m
Output:
M is not present in the word.
_ _ _ _ _ _
You have 3 guesses left.
Your Guess:
Input: d
Output:
D is not present in the word.
_ _ _ _ _ _
You have 2 guesses left.
Your Guess:
Input: t
Output:
T is present in the word.
T_ _ _ T_
You have 2 guesses left.
Your Guess:
Input: o
Output:
O is present in the word.
TO_ OT_
You have 2 guesses left.
Your Guess:
Input: y
Output:
Y is present in the word.
TOYOT_
You have 2 guesses left.
Your Guess:
Input: a
Output:
A is present in the word.
The word is: TOYOTA
Well Played, you did it!!
Java是一种通用的计算机编程方言,它是并发的、基于类的、面向对象的,并且专门设计为具有尽可能少的依赖关系。 Java为随机数提供了一个完整的库,以了解它在日常编程中的重要性。 本文的代码中使用了 nextInt()函数。这个游戏适合初学者学习用Java编写代码,并向他们简要介绍如何使用字符串、循环和条件语句。
了解游戏
玩家必须猜出一个词。因此,输出屏幕将显示代表要猜测的字母的破折号数。然后玩家会猜出一个字母。如果该字母出现在单词中,则程序将在它出现的每个位置用该字母替换破折号。如果单词中不存在该字母,那么生命线的数量就会减少(由有限的机会组成)。一旦单词的所有字母都猜对了,玩家就赢得了游戏。
比赛计划:
用户应该首先猜测单词中出现次数最多的字母是元音( a , e , i , o , u )。除了元音,其他最常用的字母是t 、 n 、 s 、 h 、 r 、 d和l 。
游戏的实现
在程序中,创建了一个类游戏,其中创建了一个由单词组成的字符串列表。在单词列表中,将使用随机模块(Java.util.Random)随机选择一个单词,供用户猜测其字母。选择单词后,使用 toUpperCase()函数将所有字母变为大写,然后将这些字母替换为破折号。游戏中允许的最大猜错次数为5 ,如果用户超过该次数,则用户将输掉游戏。在 while 循环中,当用户开始猜测字母时,正确的猜测将用正确的字母替换破折号,而错误的猜测会将计算错误猜测次数的变量增加1 。
- 第一个条件用于通知用户当用户猜错字母时剩余的生命线。
- 第二个条件用于告诉用户输入的字母已经被猜到了。
- 第三个条件是检查猜测的新字母是否存在于单词中,如果它们正确则将破折号替换为那些正确的字母
- 如果单词中不存在该字母,则生命线减 1。
如果满足以下任一条件,则游戏结束:
- 用户已正确猜出整个单词。
- 用户的生命线已经结束。
Java
// Java program to implement
// Hangman game
import java.util.Scanner;
import java.util.Random;
class Game {
static Scanner input;
public static void hangman()
{
input = new Scanner(System.in);
// array of strings containing words
String[] company = { "Maruti", "Tata", "Suzuki",
"Ducati", "Toyota" };
System.out.println(
" Welcome to HANGMAN GAME ");
Random obj = new Random();
int Ran_num = obj.nextInt(5);
// takes input of the word
String word = (company[Ran_num]);
word = word.toUpperCase();
// To show the word in underscores
String word1 = word.replaceAll("[A-Z]", "_ ");
// play the game
System.out.println("let's play the game");
startGame(word, word1);
}
public static void startGame(String word, String word1)
{
// total guesses
int guess_ = 0;
// number of wrong guesses
int wrong = 0;
// for each guess
String guess;
// stores the guessed letter
char letter;
// stores if the letter was
// already guessed
boolean guessescontainsguess;
String guesses = "";
boolean guessinword;
// while loop starts here
while (wrong < 5 && word1.contains("_")) {
System.out.println(word1 + "\n");
int temp = 5 - wrong;
if (wrong != 0) {
// for picture 1
System.out.println("You have " + temp
+ " guesses left.");
}
System.out.print("Your Guess:");
// takes guess input
guess = input.nextLine();
// converts to uppercase
// for comparison
guess = guess.toUpperCase();
// gets the first letter
// as guessed letter
letter = guess.charAt(0);
guessescontainsguess
= (guesses.indexOf(letter)) != -1;
// stores every letter
// guessed in guesses
guesses += letter;
// converts to uppercase for
// comparison
letter = Character.toUpperCase(letter);
System.out.println();
// if letter already guessed
if (guessescontainsguess == true) {
// already guessed letter
System.out.println("You ALREADY guessed "
+ letter + ". \n");
}
// guessed letter is in the word
guessinword = (word.indexOf(letter)) != -1;
// if statement begins
if (guessinword == true) {
// print the letter
System.out.println(
letter + " is present in the word.");
System.out.print("\n");
// find the letter positions
// replace dashes with those
// letter at valid positions
for (int position = 0;
position < word.length(); position++) {
// guessed letter is equal to
// letter at position in word
// and word1 has previously does not
// have that letter
if (word.charAt(position) == letter
&& word1.charAt(position)
!= letter) {
word1 = word1.replaceAll("_ ", "_");
String word2;
word2 = word1.substring(0, position)
+ letter
+ word1.substring(position
+ 1);
word2 = word2.replaceAll("_", "_ ");
word1 = word2;
}
}
}
// if statement ends, else if begins
else {
// prints
// wrong = wrong + 1, after every
// wrong answer
System.out.println(
letter
+ " is not present in the word.");
wrong++;
}
// guess_ = guess_ + 1, after every
// attempt
guess_++;
} // while loop ends
// if the lifelines finishes
if (wrong == 5) {
System.out.println(
"YOU LOST!, maximum limit of incorrect guesses reached.");
}
else {
// when solved
System.out.print(
"The word is: " + word1
+ "\n Well Played, you did it!!");
}
}
public static void main(String[] args)
{
// play hangman game
hangman();
}
}
输出:
注意:这是一个互动游戏,所以在控制台中玩。