@@ -13,11 +13,89 @@ def __init__(self):
1313 "room" : random .choice (self .rooms )
1414 }
1515
16- def introduce_game (self ):
17- print ("Welcome, detective! I'm your AI assistant, here to help you solve a mysterious crime." )
18- print ("A murder has occurred at the mansion, and it's up to us to find the culprit, the murder weapon, and the room where it happened." )
19- print ("You can ask me questions, and I'll provide you with clues. When you're ready to make an accusation, just say 'Solve'." )
20- print ("Let's begin our investigation!\n " )
16+ // bad logic
17+ import java .util .* ;
18+
19+ class Main {
20+ public static void main (String args []) {
21+ Hang hm = new Hang ();
22+ hm .Generate ();
23+ }
24+ }
25+
26+ class Hang {
27+ Random rd = new Random ();
28+ Scanner sc = new Scanner (System .in );
29+ String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
30+ int n = s .length ();
31+ char [] c = new char [6 ]; // Array for random letters
32+ char [] c1 = {'_' , '_' , '_' , '_' , '_' , '_' }; // Array to display guessed letters
33+ int maxAttempts = 6 ; // Maximum allowed attempts
34+
35+ void Generate () {
36+ // Generate a random 6 - letter word
37+ for (int i = 0 ; i < 6 ; i + + ) {
38+ c [i ] = s .charAt (rd .nextInt (n ));
39+ }
40+
41+ System .out .println ("Guess the 6-letter word:" );
42+ displayWord ();
43+ Function ();
44+ }
45+
46+ void displayWord () {
47+ // Display the current state of the guessed word
48+ for (int i = 0 ; i < 6 ; i + + ) {
49+ System .out .print (c1 [i ] + " " );
50+ }
51+ System .out .println ();
52+ }
53+
54+ void Function () {
55+ int attempts = 0 ;
56+
57+ // Loop until the word is guessed or attempts are exhausted
58+ while (attempts < maxAttempts & & !isWordGuessed ()) {
59+ System .out .println ("Enter your guess (a single letter): " );
60+ char guess = sc .nextLine ().toUpperCase ().charAt (0 );
61+
62+ boolean correctGuess = false ;
63+
64+ // Check if the guessed letter is in the word
65+ for (int i = 0 ; i < 6 ; i + + ) {
66+ if (c [i ] == guess & & c1 [i ] == '_' ) {
67+ c1 [i ] = guess ;
68+ correctGuess = true ;
69+ }
70+ }
71+
72+ // If the guess was incorrect , increment attempts
73+ if (!correctGuess ) {
74+ attempts + + ;
75+ System .out .println ("Wrong guess! Attempts left: " + (maxAttempts - attempts ));
76+ }
77+
78+ // Display the current state of the word
79+ displayWord ();
80+ }
81+
82+ // Check if the word was fully guessed
83+ if (isWordGuessed ()) {
84+ System .out .println ("You've guessed the word correctly." );
85+ } else {
86+ System .out .println ("You've run out of attempts. The word was: " + Arrays .toString (c ));
87+ }
88+ }
89+
90+ boolean isWordGuessed () {
91+ for (char ch : c1 ) {
92+ if (ch == '_' ) {
93+ return false ;
94+ }
95+ }
96+ return true ;
97+ }
98+ }
2199
22100 def get_clue (self ):
23101 if len (self .clues ) < 5 :
@@ -62,4 +140,4 @@ def play(self):
62140
63141if __name__ == "__main__" :
64142 game = AIDetective ()
65- game .play ()
143+ game .play ()
0 commit comments