Click here to Skip to main content
16,018,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Aactually i am developing Bingo card game, after distributing bingo card to each client, game begins and each client will play one by one and send a number to server and then server will send that number to all clients this is the repeatedly process,
as i said all client will send number one by one if client follow this flow then the execution of the program is as i want **i am stucked on a situation that is if client enters two numbers at a time** **then after completing all other clients chance ,when chance comes to that particular client who entered two numbers will not get chance and the second number entered by this client will be considered as his played number. i want to stop listening from server side to accept numbers if a client enters more than one number at a time.

What I have tried:

Java
<pre>
**Multiple clients and on ne server for bingo game**

**<pre lang="java">
Server.java**
```import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.*;
import java.util.ArrayList;

public class Server {

    int[][] card = new int[5][5];
    int[][] bingo = new int[5][5];

    public int[][] bingoCard() {

        ArrayList<Integer> alreadyUsed = new ArrayList<>();
        boolean valid = false;
        int tmp = 0;

        for (int i = 0; i <= 4; i++) {
            for (int row = 0; row < card.length; row++) {
                while (!valid) {
                    tmp = (int) (Math.random() * 25) + 1;
                    if (!alreadyUsed.contains(tmp)) {
                        valid = true;
                        alreadyUsed.add(tmp);
                    }
                }
                card[row][i] = tmp;
                bingo[row][i] = tmp;
                valid = false;
            }
        }

        //create array to make title.  
        String title[] = {"B", "I", "N", "G", "O"};

        for (int i = 0; i < title.length; i++) {
            System.out.print(title[i] + "\t");
        }

        System.out.println();

        for (int row = 0; row < card.length; row++) {
            for (int col = 0; col < card[row].length; col++) {
                System.out.print(card[row][col] + "\t");

            }
            System.out.println();

        }
        return bingo;

    }

    public static void main(String args[]) {
        try {

            ServerSocket serversct = new ServerSocket(1000);
            int counter = 0;
            System.out.println("Server Started ....");
            while (counter <= 2) {
                counter++;
                Socket serverClient = serversct.accept();  //server accept the client connection request
                System.out.println(" >> " + "Client No:" + counter + " started!");
                DataInputStream inStream = new DataInputStream(serverClient.getInputStream());
                DataOutputStream outStream = new DataOutputStream(serverClient.getOutputStream());
                //bingo=Server.bingoCard();
                ObjectOutputStream os = new ObjectOutputStream(serverClient.getOutputStream());

                ServerClientThread sct = new ServerClientThread(serverClient, counter, inStream, outStream, os, new Server().bingoCard()); //send  the request to a separate thread

                ServerClientThread.sockets.add(sct);
                if (counter == 1) {
                    ServerClientThread.chance.add(1);
                } else {
                    ServerClientThread.chance.add(0);
                }

            }


            ServerClientThread.distribute();

            //ServerClientThread.sockets.get(1).start();
            ServerClientThread.transferring();

            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

class ServerClientThread extends Thread {

    Socket serverClient;
    int clientNo;
    DataInputStream inStream;
    DataOutputStream outStream;
    ObjectOutputStream oos;
    static ArrayList<ServerClientThread> sockets = new ArrayList<>();
    static ArrayList<Integer> chance = new ArrayList<>();

    static ArrayList<ObjectOutputStream> oosal = new ArrayList<>();
    static ArrayList<DataInputStream> datainputal = new ArrayList<>();
    static ArrayList<DataOutputStream> dataoutputal = new ArrayList<>();
    static int received;
    int[][] card = new int[5][5];
    int[][] bingo = new int[5][5];

    ServerClientThread(Socket inSocket, int counter, DataInputStream dis, DataOutputStream dos, ObjectOutputStream oos, int abc[][]) {//,int abc[][]) {
        this.serverClient = inSocket;
        this.clientNo = counter;

        this.oos = oos;
        oosal.add(oos);

        this.inStream = dis;
        this.outStream = dos;
        ServerClientThread.datainputal.add(dis);
        ServerClientThread.dataoutputal.add(dos);
        this.bingo = abc;
    }
//i will use this run method later for checking winner of bingo card
    @Override
    public void run() {
        try {


            System.out.println("thread is running");


        } catch (Exception ex) {
            System.out.println(ex);
        } finally {
            System.out.println("Client :" + clientNo + " exit!! ");
        }
    }

    public static void distribute() throws Exception {
        System.out.println("distributing");

        for (int i = 0; i <= ServerClientThread.sockets.size() - 1; i++) {
            oosal.get(i).writeObject(ServerClientThread.sockets.get(i).bingo);
            System.out.println("i is +" + i);
        }

    }

    public static synchronized void transferring() throws Exception {



        while (true) {
                int chance = 1;
            int received = 10000;
            for (int i = 0; i <= ServerClientThread.datainputal.size() - 1; i++) {
                
                ServerClientThread.dataoutputal.get(i).writeInt(chance);
                ServerClientThread.dataoutputal.get(i).flush();
                
                for (int l = 0; l <= ServerClientThread.chance.size() - 1; l++) {

                    
                    if (ServerClientThread.chance.get(l) == 1)
                {
                    received = ServerClientThread.datainputal.get(i).readInt();
                }
                    if (l == i + 1)
                    {
                        ServerClientThread.chance.set(l, 1);
                    } 
                    else if (i == ServerClientThread.chance.size() - 1) {
                        ServerClientThread.chance.set(l, 0);
                        ServerClientThread.chance.set(0, 1);
                    } else {
                        ServerClientThread.chance.set(l, 0);
                    }


                }

                
                for (int n = 0; n< ServerClientThread.datainputal.size(); n++)
                {
                    for (int j = 0; j< ServerClientThread.datainputal.size(); j++)
                if(ServerClientThread.chance.get(n)==1 && ServerClientThread.datainputal.get(j).available()>0)
                {
                        received=ServerClientThread.datainputal.get(j).read();
                }}
                
               System.out.println("this is received" + received);
                

                chance = 0;
                for (int j = 0; j <= ServerClientThread.dataoutputal.size() - 1; j++) {

                    ServerClientThread.dataoutputal.get(j).writeInt(chance);
                    
                    ServerClientThread.dataoutputal.get(j).writeInt(received);
                    
                }

                chance = 1;
                for (int k = 0; k <= ServerClientThread.chance.size() - 1; k++) {
                    System.out.println("here is given chance" + ServerClientThread.chance.get(k));
                }
            }

        }
    }



    }

}
```

**Client.java**
```
import java.io.*;
import java.net.*;
import java.util.Scanner;

class Client {

    static String completed[] = new String[5];

    static int tosend;
    static int received1;

    public static void main(String args[]) throws Exception {

        try {

            // Scanner scn = new Scanner(System.in);
            // getting localhost ip 
            InetAddress ip = InetAddress.getByName("localhost");
            int flag = 1;
            // establish the connection with server port 9999 
            Socket s = new Socket(ip, 1000);
            DataInputStream discl = new DataInputStream(s.getInputStream());
            DataOutputStream doscl = new DataOutputStream(s.getOutputStream());

            ObjectInputStream is = new ObjectInputStream(s.getInputStream());
            int[][] array = (int[][]) is.readObject();
        
            //create array to make title.  
            String title[] = {"B", "I", "N", "G", "O"};

            for (int i = 0; i < title.length; i++) {
                System.out.print(title[i] + "\t");
            }

            System.out.println();

            for (int row = 0; row < array.length; row++) {
                for (int col = 0; col < array[row].length; col++) {
                    System.out.print(array[row][col] + "\t");

                }
                System.out.println();

            }
        
            while (true) {

                int i = discl.readInt();
                    
                if (i == 1 ) 
                {
                       
                    System.out.println("inside i==1");
                    System.out.println("in if condition 1");
                    System.out.println("opening  scanner");
                   
                    tosend = new Scanner(System.in).nextInt();
                    
                    
                    doscl.writeInt(tosend);
 
                } else if (i == 0) {

                    //sendComplete();
                    System.out.println("closing scanner");

                    System.out.println("inside i==2");
                    //completed = Bingo;
                    System.out.println("in if condition 0");

                    received1 = discl.readInt();
                    System.out.println(received1);


                }

            }

        } catch (IOException | ClassNotFoundException e) {
        }
    }

}
Posted
Updated 12-Jul-20 17:40pm
v2
Comments
karma2447 13-Jul-20 0:16am    
Well, your code is extremely complex to comprehend, add comments also, Please break it down and point to location where issue is.
nitinjain9619 13-Jul-20 6:51am    
okk i will do the changes
Richard MacCutchan 13-Jul-20 3:47am    
You just need a table or list to hold all the clients. Each client entry should contain information about whether they are allowed to enter a number or not. Then as each message arrives you just need to check the client's table entry.
nitinjain9619 13-Jul-20 6:56am    
okk i am trying what u said... thanks for your comment

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900