HOUSIE GAME
package housiegame;
import java.util.*;
public class Playhousie{
public Player p[];
public static int board[];
public int jflag[],r1flag[],r2flag[],r3flag[],hflag[],count[];
public static boolean fjflag,frflag[],fhflag;
public Playhousie(int n){
jflag=new int[n+1];
r1flag=new int[n+1];
r2flag=new int[n+1];
r3flag=new int[n+1];
hflag=new int[n+1];
count=new int[n+1];
p=new Player[n+1];
for(int i=1;i<=n;i++)
p[i]=new Player();
}
public static void main(String[] args){
frflag=new boolean[4];
board=new int[100];
int n;
Scanner cin=new Scanner(System.in);
System.out.println("enter no of players");
n=cin.nextInt();
Playhousie game=new Playhousie(n);
for(int i=1;i<=n;i++){
game.p[i].showTicket();
}
game.start(n);
}
public void start(int n){
Random random=new Random();
int r;
int row=0;
while(true){
r=(random.nextInt(99))+1;
if(Playhousie.board[r]==1)
continue;
System.out.println("number"+r); //checking number in every ticket
board[r]=1;
for(int i=1;i<=n;i++){
int k=r/10;
//searching number
if(k==0){
for(int j=1;j<=3;j++){
if((p[i].t.t[j][0])==r){
row=j;
(p[i].t.t[j][0])=0;
count[i]=count[i]+1;
break;
}
}
}
else {
for(int j=1;j<=3;j++){
if((p[i].t.t[j][k])==r){
row=j;
(p[i].t.t[j][k])=0;
count[i]=count[i]+1;
break;
}
}
}
//checking for any jaldee
if(count[i]<5){
continue;
}
if(!fjflag && count[i]==5){
System.out.println("player" + i + "wins jaldee \n");
jflag[i]=1;
}
//checking for row
if(count[i]>=5){
switch(row) {
case 1: if(!frflag[row]){ //first row
if(check(i,row,p)){
r1flag[i]=1;
System.out.println("player" + i + "wins row"+ row);
}
} break;
case 2: if(!frflag[row]){ //second row
if(check(i,row,p)){
r2flag[i]=1;
System.out.println("player" + i + "wins row"+ row);
}
}
break;
case 3: if(!frflag[row]){ //third row
if(check(i,row,p)){
r3flag[i]=1;
System.out.println("player" + i + "wins row "+row);
}
}
break;
}
if(count[i]==15){
hflag[i]=1;
System.out.println("player "+ i + "wins housie");
}
}
}
//checking any thing happened after the random number "r"
if(!fjflag){
for(int i=1;i<=n;i++){ //jaldee
if(jflag[i]==1){
fjflag=true;
break;
}
}
}
if(!frflag[1]){ //row1
for(int i=1;i<=n;i++){
if(r1flag[i]==1){
frflag[1]=true;
break;
}
}
}
if(!frflag[2]){
for(int i=1;i<=n;i++){ //row2
if(r2flag[i]==1){
frflag[2]=true;
break;
}
}
}
if(!frflag[3]){
for(int i=1;i<=n;i++){ //row3
if(r3flag[i]==1){
frflag[3]=true;
break;
}
}
}
for(int i=1;i<=n;i++){
if(hflag[i]==1){
System.out.println("game completed");
System.exit(0);
}
}
}
}
public boolean check(int i,int row,Player p[]){
for(int column=0;column<=9;column++){
if((p[i].t.t[row][column])!=0)
return false;
}
return true;
}
}
palyer in the housie game:
package housiegame;
public class Player{
public Ticket t;
public Player(){
t=new Ticket();
}
public void getTicket(){
t.generateTicket();
}
public void showTicket(){
t.printTicket();
}
public static void main(String[] args){
Player p=new Player();
p.getTicket();
p.showTicket();
}
}
TICKET IN THE HOUSIE GAME:
package housiegame;
import java.util.*;
public class Ticket{
public int t[][]=new int[4][10];
public Ticket(){
this.generateTicket();
}
public void generateTicket(){
Random random = new Random();
int r,j;
for(int i=1;i<=3;i++){
int countcolumn[]=new int[10];
for(int rowcount=0;rowcount<5; ){
j=random.nextInt(10); //random(max-min+1)+min
if(countcolumn[j]==0){
if(j==0)
r=random.nextInt(9-1+1)+1;
else
r=random.nextInt(9+1)+(j)*10;
switch(i){
case 1:
t[i][j]=r;
countcolumn[j]=1;
rowcount++;
break;
case 2: if(t[i-1][j]!=r){
t[i][j]=r;
countcolumn[j]=1;
rowcount++;
}
break;
case 3:
if((t[i-1][j]!=r)&& (t[i-2][j]!=r)){
t[i][j]=r;
countcolumn[j]=1;
rowcount++;
}
break;
}
}
}
}
}
void printTicket(){
for(int i=1;i<=3;i++){
for(int j=0;j<=9;j++){
System.out.println(t[i][j]+"\t");
}
System.out.println("\n");
}
}
public static void main(String[] args){
Ticket t=new Ticket();
t.printTicket();
}
}
No comments:
Post a Comment