Implementing the classic word game,in a multi-player word game. If you don't know about the rules.Then its as follows ,a player begins the game by telling a random word,the next player must tell a word begins with the last letter of the word the previous player,& also he also must use a word that hasn't been used before for not getting disqualified.This process goes on repeating until till the last player,& again loops back to the first player.This process goes on,until only one player remains,& all others get disqualified.
Here i have implemented this thing using a class named wordGame. The class wordGame have a parameterized constructor through which we initialize the number of players that will be playing. Here a i have used a database string to hold all the words that have been used. Matching the last letters of the previous words & first letters of the present letters is fairly simple,and can be easily done.
Now implementing the wordGame class i.e implementing the game
Here i have implemented this thing using a class named wordGame. The class wordGame have a parameterized constructor through which we initialize the number of players that will be playing. Here a i have used a database string to hold all the words that have been used. Matching the last letters of the previous words & first letters of the present letters is fairly simple,and can be easily done.
package Word_Game;
import java.io.*;
public class wordGame
{
public static int players,p[];
public wordGame(int pl)
{
p=new int[pl];
players=pl;
}
public void play()throws Exception
{
String dictionary="",curr_word="";
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i=0,ctr=0;
while(true)
{
if(i==players)
i=0;
if(ctr==(players-1))
break;
if(p[i]==0)
{
if(dictionary.equals(""))
{
System.out.println("Player "+(i+1)+" :Enter a word--->");
curr_word=br.readLine();
dictionary=curr_word+" ";
}
else
{
int dl=dictionary.length()-2;
System.out.println("Player "+(i+1)+" :Enter a word that begins with "+dictionary.charAt(dl)+ "--->");
curr_word=br.readLine();
if(check(dictionary,curr_word) || (dictionary.charAt(dl))!=curr_word.charAt(0))
{
p[i]=1;ctr++;
System.out.println("Player "+(i+1)+" :Sorry you have been eliminated.\nBetter luck next time.....");
}
else
dictionary=dictionary+curr_word+" ";
}
}
i++;
}
int k=0;
while(k<players)
if(p[k++]==0)
System.out.println("The winner is Player "+k);
}
public boolean check(String db,String w)
{
boolean b=false;
int i=0,j=0,l=db.length();
while(j<l && b==false)
{
if(db.charAt(j)==' ')
{
if((db.substring(i, j)).equalsIgnoreCase(w))
b=true;
i=j+1;
}
j++;
}
return b;
}
}
Now implementing the wordGame class i.e implementing the game
package Word_Game;
import java.io.*;
public class Main
{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of players for this classic word game::");
int p=Integer.parseInt(br.readLine());
wordGame wd=new wordGame(p);
wd.play();
}
}
0 Comments On This Entry
Recent Entries
-
Implementing Gauss-Elimination Method to solve simultaneous linear equations
on Jun 27 2012 11:19 PM
-
-
-
-
Recent Comments
My Blog Links
0 user(s) viewing
0 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)
|
|



Leave Comment









|