PLAY HOUSIE:
package housiegame;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Playhousie1 extends Frame implements ActionListener{
public static int n;
private static final long serialVersionUID = 1L;
public static Button [] board=new Button[100];
public Ticket1 t[];
public int jflag[],r1flag[],r2flag[],r3flag[],hflag[],count[];
public static boolean fjflag,frflag[],fhflag;
public Playhousie1(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];
t=new Ticket1[n+1];
for(int i=1;i<=n;i++)
t[i]=new Ticket1();
Button start=new Button("start");
this.setSize(500,500);
this.add(start);
start.addActionListener(this);
start.setVisible(true);
for(int i=1;i<=99;i++)
this.add(board[i]);
this.setLayout(new FlowLayout());
this.setVisible(true);
}
public static void main(String[] args){
frflag=new boolean[4];
for(int i=1;i<=99;i++){
board[i]=new Button(String.valueOf(i));
board[i].setBackground(Color.red);
board[i].setVisible(true);
}
Scanner cin=new Scanner(System.in);
System.out.println("enter no of players");
n=cin.nextInt();
new Playhousie1(n);
}
public void actionPerformed(ActionEvent e){
Button b=(Button)e.getSource();
if(b.getLabel().equals("start")){
System.out.println("start");
return;
}
else{
int r=Integer.parseInt(b.getLabel());
int row=0;
if((board[r].getBackground()).equals(Color.green)){return;}
else{ //checking number in every ticket
board[r].setBackground(Color.green); //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(Integer.parseInt(t[i].ticket[j][0].getLabel())==r){
row=j;
t[i].ticket[j][0].setLabel(String.valueOf(0)); //p[i].t.t[j][0])=0;
count[i]=count[i]+1;
break;
}
}
}
else {
for(int j=1;j<=3;j++){
if(Integer.parseInt(t[i].ticket[j][k].getLabel())==r){//(p[i].t.t[j][k])==r){
row=j;
t[i].ticket[j][k].setLabel(String.valueOf("0"));//(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){
//number.setText("player"+ i +"wins jaldee");//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)){
r1flag[i]=1;
//l1.setText("player"+ i +"wins row" + row); //System.out.println("player" + i + "wins row"+ row);
}
} break;
case 2: if(!frflag[row]){ //second row
if(check(i,row)){
r2flag[i]=1;
//l2.setText("player"+ i +"wins row" + row); //System.out.println("player" + i + "wins row"+ row);
}
}
break;
case 3: if(!frflag[row]){ //third row
if(check(i,row)){
r3flag[i]=1;
//l3.setText("player"+ i +"wins row" + row);//System.out.println("player" + i + "wins row "+row);
}
}
break;
}
if(count[i]==15){
hflag[i]=1;
this.setTitle("PLAYER"+i+"WINS HOUSIE");//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;
}
}
}
int gameflag=0;
for(int i=1;i<=n;i++){
if(hflag[i]==1){
this.setBackground(Color.green);//System.out.println("game completed");
gameflag=1;//System.exit(0);
}
}
if(gameflag==1){}
//break;
}
}
}
public boolean check(int i,int row){
for(int column=0;column<=9;column++){
if(Integer.parseInt(t[i].ticket[row][column].getLabel())!=0) //(p[i].t.t[row][column])!=0)
return false;
}
return true;
}
}
TICKET:
package housiegame;
import java.awt.*;
import java.util.*;
public class Ticket1 extends Frame {
/**
*
*/
private static final long serialVersionUID = 1L;
public Button [][] ticket=new Button[4][10];
public Ticket1(){
for(int i=1;i<=3;i++){
for(int j=0;j<=9;j++){
ticket[i][j]=new Button("0");
this.add(ticket[i][j]);
ticket[i][j].setVisible(true);
}
this.setVisible(true);
this.setLayout(new GridLayout(3,9));
}
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:
ticket[i][j].setLabel(String.valueOf(r)); //ticket[i][j]=r;
countcolumn[j]=1;
rowcount++;
break;
case 2: if (Integer.parseInt(ticket[i-1][j].getLabel())!=r) { //t[i-1][j]!=r)
ticket[i][j].setLabel(String.valueOf(r)); //t[i][j]=r;
countcolumn[j]=1;
rowcount++;
}
break;
case 3:
if((Integer.parseInt(ticket[i-1][j].getLabel())!=r)&& (Integer.parseInt(ticket[i-2][j].getLabel())!=r)){
ticket[i][j].setLabel(String.valueOf(r)); //t[i][j]=r;
countcolumn[j]=1;
rowcount++;
}
break;
}
}
}
}
}
public static void main(String[] args){
Frame f=new Frame();
Scanner cin=new Scanner(System.in);
System.out.println("enter no of tickets");
int n=cin.nextInt();
Ticket1 t[]=new Ticket1[n];
for(int i=0;i<n;i++){
t[i]=new Ticket1();
f.add(t[i]);
}
f.setLayout(new FlowLayout());
f.setVisible(true);
}
}