1+ package Games ;
2+ /*
3+ Wordle Game
4+
5+ Description:
6+ A simple Wordle game where the player has to guess a randomly selected word
7+ from a dictionary. After each guess, feedback is provided:
8+ - 'O' : Correct letter in the correct position
9+ - 'N' : Correct letter in the wrong position
10+ - '*' : Incorrect letter
11+
12+ Rules:
13+ 1. Each guess must be a 5-letter word.
14+ 2. Player has 6 attempts to guess the correct word.
15+ */
16+ //take word from dictionary
17+ //give random word to user
18+ //make array of the words given by user
19+ //check if the word is valid or not and satisfy the game condition
20+ //check if the word is already used or not
21+ //find score
22+
23+ //dictionary is used
24+
25+ import java .io .*;
26+ import java .util .*;
27+ public class Antakshari {
28+ static Scanner sc = new Scanner (System .in );
29+ static Random rand = new Random (); // ✅ Only one Random instance
30+ static List <String > dictionary = new ArrayList <>();
31+ static Set <String > usedWords = new HashSet <>();
32+ static Set <Character > specialLetters = new HashSet <>();
33+
34+ // ✅ No longer passing usedWords here
35+ public static List <String > loadDictionary (String filename ) {
36+ List <String > dict = new ArrayList <>();
37+ try (BufferedReader br = new BufferedReader (new FileReader (filename ))) {
38+ String word ;
39+ while ((word = br .readLine ()) != null ) {
40+ word = word .trim ().toLowerCase ();
41+ if (!word .isEmpty ()) {
42+ dict .add (word );
43+ }
44+ }
45+ } catch (IOException e ) {
46+ System .out .println ("Failed to load dictionary: " + e .getMessage ());
47+ }
48+ return dict ;
49+ }
50+
51+ public static List <Character > getSpecialCharacters () {
52+ List <Character > special = new ArrayList <>();
53+ while (special .size () < 3 ) {
54+ char c = (char ) (rand .nextInt (26 ) + 'a' );
55+ if (!special .contains (c )) {
56+ special .add (c );
57+ }
58+ }
59+ return special ;
60+ }
61+
62+ public static int getWordScore (String word ) {
63+ int score = 0 ;
64+ for (char c : word .toCharArray ()) {
65+ if (specialLetters .contains (c )) {
66+ score += 3 ;
67+ } else {
68+ score += 1 ;
69+ }
70+ }
71+ return score ;
72+ }
73+
74+ public static String getPCWord (String prevWord ) {
75+ for (int i = 0 ; i < 100 ; i ++) {
76+ String candidate = dictionary .get (rand .nextInt (dictionary .size ()));
77+ if (!usedWords .contains (candidate )) {
78+ if (prevWord .equals ("" ) || candidate .charAt (0 ) == prevWord .charAt (prevWord .length () - 1 )) {
79+ usedWords .add (candidate );
80+ return candidate ;
81+ }
82+ }
83+ }
84+ return "" ; // No suitable word found
85+ }
86+
87+ public static boolean isValidWord (String word , String prevWord ) {
88+ if (!dictionary .contains (word )) {
89+ System .out .println ("Invalid word! Not in dictionary." );
90+ return false ;
91+ }
92+ if (usedWords .contains (word )) {
93+ System .out .println ("Word already used! Try another." );
94+ return false ;
95+ }
96+ if (!prevWord .equals ("" ) && word .charAt (0 ) != prevWord .charAt (prevWord .length () - 1 )) {
97+ System .out .println ("Word must start with '" + prevWord .charAt (prevWord .length () - 1 ) + "'" );
98+ return false ;
99+ }
100+ return true ;
101+ }
102+
103+ public static void main (String [] args ) {
104+ System .out .println ("🎮 Welcome to the world of Antakshari!" );
105+
106+ dictionary = loadDictionary ("dictionary.txt" );
107+
108+ if (dictionary .isEmpty ()) {
109+ System .out .println ("Dictionary is empty or not found. Exiting." );
110+ return ;
111+ }
112+
113+ specialLetters .addAll (getSpecialCharacters ());
114+ System .out .println ("Special letters worth 3 points: " + specialLetters + "\n " );
115+
116+ String prevWord = "" ;
117+ int userScore = 0 ;
118+ int pcScore = 0 ;
119+
120+ for (int i = 1 ; i <= 5 ; i ++) {
121+ System .out .println ("\n --------------------- Round " + i + " ---------------------" );
122+ System .out .print ("Your turn! Enter a word: " );
123+ String userWord = sc .nextLine ().trim ().toLowerCase ();
124+
125+ if (!isValidWord (userWord , prevWord )) {
126+ System .out .println ("Try again." );
127+ i --; // Don't advance round
128+ continue ;
129+ }
130+
131+ usedWords .add (userWord );
132+ int uScore = getWordScore (userWord );
133+ userScore += uScore ;
134+ prevWord = userWord ;
135+
136+ System .out .println ("Your word: " + userWord + " (+ " + uScore + " points) | Total: " + userScore );
137+
138+ String pcWord = getPCWord (prevWord );
139+ if (pcWord .isEmpty ()) {
140+ System .out .println ("PC couldn't find a valid word. You win this round!" );
141+ break ;
142+ }
143+
144+ int pcScoreAdd = getWordScore (pcWord );
145+ pcScore += pcScoreAdd ;
146+ prevWord = pcWord ;
147+
148+ System .out .println ("PC's word: " + pcWord + " (+ " + pcScoreAdd + " points) | Total: " + pcScore );
149+ }
150+
151+ System .out .println ("\n --------------------- Game Over ---------------------" );
152+ System .out .println ("Your score: " + userScore );
153+ System .out .println ("PC score: " + pcScore );
154+
155+ if (userScore > pcScore ) {
156+ System .out .println ("Congratulations! You win!" );
157+ } else if (userScore < pcScore ) {
158+ System .out .println ("PC wins! Better luck next time." );
159+ } else {
160+ System .out .println ("It's a tie!" );
161+ }
162+ }
163+ }
0 commit comments