Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added out/production/Game/META-INF/Game.kotlin_module
Binary file not shown.
Binary file added out/production/Game/actions/Move.class
Binary file not shown.
Binary file added out/production/Game/controller/ChatMessage.class
Binary file not shown.
Binary file not shown.
Binary file added out/production/Game/controller/Client.class
Binary file not shown.
Binary file added out/production/Game/controller/Controller.class
Binary file not shown.
Binary file not shown.
Binary file added out/production/Game/controller/Server.class
Binary file not shown.
Binary file added out/production/Game/model/Model.class
Binary file not shown.
Binary file added out/production/Game/model/Player.class
Binary file not shown.
Binary file added out/production/Game/model/Tile.class
Binary file not shown.
Binary file added out/production/Game/resources/0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added out/production/Game/resources/1024.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added out/production/Game/resources/128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added out/production/Game/resources/16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added out/production/Game/resources/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added out/production/Game/resources/2048.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added out/production/Game/resources/256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added out/production/Game/resources/32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added out/production/Game/resources/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added out/production/Game/resources/512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added out/production/Game/resources/64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added out/production/Game/resources/8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added out/production/Game/resources/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added out/production/Game/view/KeyView.class
Binary file not shown.
Binary file added out/production/Game/view/View.class
Binary file not shown.
12 changes: 0 additions & 12 deletions src/Main_client.java

This file was deleted.

10 changes: 0 additions & 10 deletions src/Main_server.java

This file was deleted.

35 changes: 35 additions & 0 deletions src/controller/ChatMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package controller;


import java.io.*;
/*
* This class defines the different type of messages that will be exchanged between the
* Clients and the Server.
* When talking from a Java Client to a Java Server a lot easier to pass Java objects, no
* need to count bytes or to wait for a line feed at the end of the frame
*/

public class ChatMessage implements Serializable {

// The different types of message sent by the Client
// WHOISIN to receive the list of the users connected
// MESSAGE an ordinary text message
// LOGOUT to disconnect from the Server
static final int WHOISIN = 0, MESSAGE = 1, LOGOUT = 2, START=3, SCORE=4, WHOISWIN=5;
private int type;
private String message;

// constructor
ChatMessage(int type, String message) {
this.type = type;
this.message = message;
}

int getType() {
return type;
}

String getMessage() {
return message;
}
}
254 changes: 254 additions & 0 deletions src/controller/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
package controller;

import model.Player;

import java.net.*;
import java.io.*;
import java.util.*;


public class Client {
private static Player player;
private static Controller controller;

private String notif = " *** ";

private ObjectInputStream sInput; // читать из сокета
private ObjectOutputStream sOutput; // писать в сокет
private Socket socket; // socket object

private String server, username; // server and username
private int port; //port

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

/*
* Constructor to set below things
* server: the server address
* port: the port number
* username: the username
*/

Client(String server, int port, String username) {
this.server = server;
this.port = port;
this.username = username;
this.player = new Player(username);
}

public static String run_the_game() {

int s = 0;
player.controller.getView().run();
while (!player.controller.getView().isGameEnded) {
s = player.get_player_score();
}
return "" + s;
}

/*
* To start the chat
*/
public boolean start() {
// try to connect to the server
try {
socket = new Socket(server, port);
}
// exception handler if it failed
catch (Exception ec) {
display("Error connectiong to server:" + ec);
return false;
}

String msg = "Connection accepted " + socket.getInetAddress() + ":" + socket.getPort();
display(msg);


/* Creating both Data Stream */
try {
sInput = new ObjectInputStream(socket.getInputStream());
sOutput = new ObjectOutputStream(socket.getOutputStream());
} catch (IOException eIO) {
display("Exception creating new Input/output Streams: " + eIO);
return false;
}

// creates the Thread to listen from the server
new ListenFromServer().start();
// Send our username to the server this is the only message that we
// will send as a String. All other messages will be ChatMessage objects
try {
sOutput.writeObject(username);
} catch (IOException eIO) {
display("Exception doing login : " + eIO);
disconnect();
return false;
}
// success we inform the caller that it worked
return true;
}

/*
* To send a message to the console
*/
private void display(String msg) {

System.out.println(msg);

}

/*
* To send a message to the server
*/
void sendMessage(ChatMessage msg) {
try {
sOutput.writeObject(msg);
} catch (IOException e) {
display("Exception writing to server: " + e);
}
}

/*
* When something goes wrong
* Close the Input/Output streams and disconnect
*/
private void disconnect() {
try {
if (sInput != null) sInput.close();
} catch (Exception e) {
}
try {
if (sOutput != null) sOutput.close();
} catch (Exception e) {
}
try {
if (socket != null) socket.close();
} catch (Exception e) {
}

}

/*
* To start the Client in console mode use one of the following command
* > java Client
* > java Client username
* > java Client username portNumber
* > java Client username portNumber serverAddress
* at the console prompt
* If the portNumber is not specified 1234 is used
* If the serverAddress is not specified "localHost" is used
* If the username is not specified "Anonymous" is used
*/
public static void main(String[] args) {
// default values if not entered
int portNumber = 1234;
String serverAddress = "localhost";
String userName = "Anonymous";
Scanner scan = new Scanner(System.in);

System.out.println("Enter the username: ");
userName = scan.nextLine();

// different case according to the length of the arguments.
switch (args.length) {
case 3:
// for > javac Client username portNumber serverAddr
serverAddress = args[2];
case 2:
// for > javac Client username portNumber
try {
portNumber = Integer.parseInt(args[1]);
} catch (Exception e) {
System.out.println("Invalid port number.");
System.out.println("Usage is: > java Client [username] [portNumber] [serverAddress]");
return;
}
case 1:
// for > javac Client username
userName = args[0];
case 0:
// for > java Client
break;
// if number of arguments are invalid
default:
System.out.println("Usage is: > java Client [username] [portNumber] [serverAddress]");
return;
}
// create the Client object
Client client = new Client(serverAddress, portNumber, userName);
// try to connect to the server and return if not connected
if (!client.start())
return;

System.out.println("\nHello! Welcome to the game 2048.");
System.out.println("Instructions:");
System.out.println("1. Simply type the message to send broadcast to all active clients");
System.out.println("2. Type '@username<space>yourmessage' without quotes to send message to desired client");
System.out.println("3. Type 'WHOISIN' without quotes to see list of active clients");
System.out.println("4. Type 'LOGOUT' without quotes to logoff from server");
System.out.println("4. Type 'START' without quotes to start the game");
System.out.println("4. Type 'WHOISWIN' without quotes to know have won the game");
// infinite loop to get the input from the user
while (true) {
System.out.print("> ");
// read message from user
String msg = scan.nextLine();
// logout if message is LOGOUT
if (msg.equalsIgnoreCase("LOGOUT")) {
client.sendMessage(new ChatMessage(ChatMessage.LOGOUT, ""));
break;
}
// message to check who are present in chatroom
else if (msg.equalsIgnoreCase("WHOISIN")) {
client.sendMessage(new ChatMessage(ChatMessage.WHOISIN, ""));
}
// message to start the game
else if (msg.equalsIgnoreCase("START")) {
client.sendMessage(new ChatMessage(ChatMessage.START, ""));
String score = run_the_game();
client.sendMessage(new ChatMessage(ChatMessage.SCORE, score));

} else if (msg.equalsIgnoreCase("WHOISWIN")) {
client.sendMessage(new ChatMessage(ChatMessage.WHOISWIN, ""));

}
// regular text message
else {
client.sendMessage(new ChatMessage(ChatMessage.MESSAGE, msg));
}
}
// close resource
scan.close();
// client completed its job. disconnect client.
client.disconnect();
}

/*
* a class that waits for the message from the server
*/
class ListenFromServer extends Thread {

public void run() {
while (true) {
try {
// read the message form the input datastream
String msg = (String) sInput.readObject();
// print the message
System.out.println(msg);
System.out.print("> ");
} catch (IOException e) {
display(notif + "Server has closed the connection: " + e + notif);
break;
} catch (ClassNotFoundException ignored) {
}
}
}
}
}

7 changes: 4 additions & 3 deletions src/controller/Controller.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package controller;

import model.Player;
import model.Tile;
import model.Model;
import view.KeyView;
Expand All @@ -10,8 +11,9 @@


public class Controller extends KeyAdapter {
private Model model;
private View view;

private final Model model;
private final View view;

public Controller(Model model) {
this.model = model;
Expand All @@ -26,7 +28,6 @@ public int getScore() {
return model.score;
}


public View getView() {
return view;
}
Expand Down
Loading