Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this project, you will submit two .java files. You will need to run two clas

ID: 3719946 • Letter: F

Question

For this project, you will submit two .java files. You will need to run two classes simultaneously for this activity. Make sure you have a compiler that is capable of this.

The game we’re going to make will be a simple competitive Memory game. To begin, the Server will create a 4x4 grid of 16 integers – 1 through 8. These integers will be stored in a 2D array and held on the Server. Neither Client should be given access to this array. Instead, the Server will ask the first player for a grid tile (row and column). Then, the Server will display the tile that the user has chosen and then ask the same player for another grid tile. If the two match, they remain revealed. If they don’t, they are hidden again. Afterwards, the process should repeat with the second player this time. If either player reveals a match, they are given a point (also kept only on the Server). At the end of the game, the player with the highest number of points wins (or both players tie if their scores are the same).

To simplify the process of sending data and to keep the data flow open, you might want to create a method on your Server to return a single String that represents the current state of the board and send that to each Client when the need arises. Be sure to include the players’ scores in the String that you send. Also remember to catch any possible exceptions, close all forms of I/O, and prevent the connection from being terminated early.

Explanation / Answer

/*

Client side program... for multiple clients, run this program in seperate windows.

*/

import java.io.*;

import java.net.*;

public class clientside implements Runnable{

{

  

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

{

System.out.println("connection to server....");

try

{

static Socket clientSocket = new Socket("xyz",1010);

static PrintStream out = new PrintStream(clientScocket.getOutputStream());

static BufferedReader in =new BufferedReader(new InputStreamReader(clientSocket.getInputStream());

static BufferedReader userin = new BufferedReader(new InputStreamReader(System.in));

static Flag=false;

}

catch(Exception e)

{

System.out.println("Error:"+e.getMessage());

}

if(clientSocket != null)

{

try

{

new thread(new clientside()).start(); // client side thread starts here

  

while(!Flag)

{

out.println(userin.readLine());

}

out.close();

in.close();

clientSocket.close(); // close the client socket

}

  

catch(Exception e)

{

System.out.println("Error:"+e.getMessage());

}

  

}   

}

public void run()

{

String ID;

try

{

while((ID=in.readLine())!= null)

System.out.println("user ID:" +ID);

Flag= true; //session ends

}

catch(Exception e)

{

System.out.println("Error:"+e.getMessage());

}

  

}   

}

/* Server Side program */

import java.io.*;

import java.net.*;

import.java.util.Random;

public class ServerSide

{

static Socket cs= null; // client socket to listen

static clientThread ct[] = new clientThread[3];

,

public static void main(String arg[]) throws Exception

{

system.out.println("Server side is active....");

try

{

static ServerSocket ss =new ServerSocket(1010); // server socket to connect with client

}

catch(Exception e)

{

System.out.println("Error:"+e.getMessage());

}

while(true)

{

try

{

cs=ss.accept();

for(int i=0;i<2;i++)

{

if(ct[i]==null)

{

(ct[i]= new clientThread(cs,ct)).start();break;

}

}

catch(Exception e)

{

System.out.println("Error:"+e.getMessage());

}

}   

}

}

class clientThread extends Thread

{

static Socket cs = null;

clientThread ct[];

public clientThread(Socket cs,clientThread[] ct)

{

this.cs=cs;

this.ct=ct;

}

public void run()

{

Random rand = new Random();

int id = rand.nextInt(9999) + 1111;

  

String name;

try

{

static PrintStream out = new PrintStream(cs.getOutputStream());

static BufferedReader in =new BufferedReader(new InputStreamReader(cs.getInputStream());

out.println("your Unique Id :" + id);

out.println("Enter your name:");

name= in.readLine();

for(int i=0;i<2;i++)

{

if(ct[i]!= null && ct[i]!=this)

System.out.println(id +":" + name);

}

in.close();

out.close();

cs.close();

}

catch(Exception e)

{

System.out.println("Error:"+e.getMessage());

}

}   

}

  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote