-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordChain
38 lines (28 loc) · 1.3 KB
/
WordChain
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.Scanner;
public class WordChainGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the Word Chain Game!");
String[] words = {"apple", "banana", "cherry", "orange", "grape"};
String previousWord = words[(int) (Math.random() * words.length)];
System.out.println("The starting word is: " + previousWord);
while (true) {
System.out.print("Enter a word starting with the last letter of the previous word (or type 'quit' to end): ");
String input = scanner.nextLine().toLowerCase();
if (input.equals("quit")) {
break;
}
if (isValidWord(previousWord, input)) {
System.out.println("Valid word! Next player's turn.");
previousWord = input;
} else {
System.out.println("Invalid word! Please enter a word starting with the last letter of the previous word.");
}
}
System.out.println("Thanks for playing the Word Chain Game!");
scanner.close();
}
public static boolean isValidWord(String previousWord, String currentWord) {
return previousWord.charAt(previousWord.length() - 1) == currentWord.charAt(0);
}
}