diff --git a/README.md b/README.md new file mode 100644 index 0000000..c259fa8 --- /dev/null +++ b/README.md @@ -0,0 +1,22 @@ +# Lufar check +![](https://img.shields.io/badge/using-org.json-blue?logo=json) +## What is it? +Well it's a game where you play 3 in a row but its 5 in a row and on a non-standard size map, so it's playable on a 5x5 to inf x inf grid +## How do I play it? +So the game rules is that you place your marker (a blue square or a red circle) and try to get 5 in a row while blocking the other player +from getting 5 in a row +## How do I play this with my friend? +So this game requires command line to run +- Star the server + 1. Open a command line + - Windows: press win key(or cmd) then type `cmd` and press enter + - macOS: press cmd(win key)+space and type `termianl` + - Ubuntu(might work on other Linux distros too) press ctrl+alt+t + 2. Now go to the directory where you downloaded the latest version + 3. run `$ java -jar './lufar chack.jar'- server` NOTE: `$` is to indicate that it's a command, `` is the latest version + 4. now you need your ip for the computer that host the server(if you and your friend is not on the same network as the server then you need the public ip by visiting this [site](https://www.whatismyip.com/)) +- Join the server + 1. follow step 1 and 2 from `Start the server` + 2. run `$ java -jar './lufar chack.jar' -client -ip ` NOTE: `$` is to indicate that it's a command, `` is the latest version, the `-ip ` is not needed if you ar on the same computer as the server, + `` is the local(if you ar on the same network as the server) or public(if you ar not on the same network as the server) ip of the server + 3. ENJOY! \ No newline at end of file diff --git a/build.gradle b/build.gradle index 8ba84be..0278f76 100644 --- a/build.gradle +++ b/build.gradle @@ -18,7 +18,7 @@ dependencies { jar { manifest { - attributes "Main-Class": "me.alien.game.Game" + attributes "Main-Class": "me.alien.lufar.chack.Main" } from { diff --git a/src/main/java/me/alien/lufar/chack/client/Client.java b/src/main/java/me/alien/lufar/chack/client/Client.java index 58272f8..03a197b 100644 --- a/src/main/java/me/alien/lufar/chack/client/Client.java +++ b/src/main/java/me/alien/lufar/chack/client/Client.java @@ -2,10 +2,7 @@ import javax.swing.*; import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; +import java.awt.event.*; import java.io.*; import java.net.Socket; import java.util.ArrayList; @@ -18,7 +15,7 @@ import static me.alien.lufar.chack.Main.VERSION; -public class Client extends JPanel implements MouseListener, ActionListener { +public class Client extends JPanel implements MouseListener, ActionListener, KeyListener { public static void main(ArrayList args) { String hostname = "localhost"; if(args.contains("-ip")){ @@ -32,6 +29,7 @@ public static void main(ArrayList args) { } static ArrayList> dataIn = new ArrayList<>(); + static ArrayList> lines = new ArrayList<>(); JFrame frame; @@ -46,6 +44,8 @@ public static void main(ArrayList args) { static Tile[][] map = new Tile[60][60]; + Vector2I leftTop = new Vector2I(map.length/2-30, map[0].length/2-30); + public Client(String hostname){ frame = new JFrame(); @@ -80,10 +80,8 @@ public Client(String hostname){ System.exit(1); } - map[0][0] = new Tile(); - - for(int x = 0; x < 20; x++){ - for(int y = 0; y < 20; y++){ + for(int x = 0; x < map.length; x++){ + for(int y = 0; y < map[x].length; y++){ map[x][y] = new Tile(); } } @@ -115,12 +113,17 @@ public void doDraw(Graphics2D g2d){ g2d.drawLine(0,y, getWidth(), y); } - for(int x = 0; x < map.length; x++){ + for(int x = 0; x < 60; x++){ int xPos = x*10; - for(int y = 0; y < map[x].length; y++){ + for(int y = 0; y < 60; y++){ int yPos = y*10; try { - map[x][y].draw(g2d, xPos, yPos); + final Tile tile = map[x][y]; + if(tile.isFinished()){ + g2d.setColor(Color.GRAY); + g2d.fillRect(xPos, yPos, 10, 10); + } + tile.draw(g2d, xPos, yPos); }catch (NullPointerException e){ e.printStackTrace(); System.out.println("x: "+x+" y: "+y); @@ -128,6 +131,17 @@ public void doDraw(Graphics2D g2d){ } } } + + for(Line line : lines){ + g2d.setColor(Color.GREEN); + if(line.getType() == LineType.HORIZONTAL){ + g2d.drawLine(line.getKey().getX()*10+5, line.getKey().getY()*10+5, line.getValue().getX()*10-5, line.getValue().getY()*10+5); + }else if(line.getType() == LineType.VERTICAL){ + g2d.drawLine(line.getKey().getX()*10+5, line.getKey().getY()*10+5, line.getValue().getX()*10+5, line.getValue().getY()*10-5); + }else if(line.getType() == LineType.DIAGONAL){ + g2d.drawLine(line.getKey().getX()*10+5, line.getKey().getY()*10+5, line.getValue().getX()*10-5, line.getValue().getY()*10-5); + } + } } @Override @@ -160,30 +174,53 @@ public void actionPerformed(ActionEvent e) { repaint(); } + @Override + public void keyTyped(KeyEvent e) { + + } + + @Override + public void keyPressed(KeyEvent e) { + + } + + @Override + public void keyReleased(KeyEvent e) { + + } + public class InputDataThread extends Thread{ @Override public void run() { while (true){ try { String dataIn = in.readLine(); - System.out.print(dataIn); + System.out.println(dataIn); JSONObject obj = new JSONObject(dataIn); Pair tmp = new Pair<>(Type.valueOf(obj.getString("type")), obj.getJSONObject("data")); //dataIn.add(tmp); - if(tmp.getKey() == Type.TILE){ + final Type type = tmp.getKey(); + if(type == Type.TILE){ Pair pair1 = Tile.fromJSON(tmp.getValue()); int x = pair1.getKey().getX(), y = pair1.getKey().getY(); map[x][y] = pair1.getValue(); //dataIn.remove(pair); - }else if(tmp.getKey() == Type.MAP){ + }else if(type == Type.MAP){ JSONObject tiles = tmp.getValue(); for(String name : tiles.keySet()){ Pair pair1 = Tile.fromJSON(tiles.getJSONObject(name).getJSONObject("value")); int x = pair1.getKey().getX(), y = pair1.getKey().getY(); map[x][y] = pair1.getValue(); } + }else if(type == Type.LINE){ + JSONObject startJSON = tmp.getValue(); + Vector2I start = new Vector2I(startJSON.getJSONObject("start").getInt("x"), startJSON.getJSONObject("start").getInt("y")); + Vector2I end = new Vector2I(startJSON.getJSONObject("end").getInt("x"), startJSON.getJSONObject("end").getInt("y")); + LineType lineType = startJSON.getEnum(LineType.class, "type"); + + lines.add(new Line<>(start, end, lineType)); } } catch (IOException e) { e.printStackTrace(); diff --git a/src/main/java/me/alien/lufar/chack/server/Server.java b/src/main/java/me/alien/lufar/chack/server/Server.java index 346b84f..b638056 100644 --- a/src/main/java/me/alien/lufar/chack/server/Server.java +++ b/src/main/java/me/alien/lufar/chack/server/Server.java @@ -1,9 +1,6 @@ package me.alien.lufar.chack.server; -import me.alien.lufar.chack.util.Pair; -import me.alien.lufar.chack.util.Tile; -import me.alien.lufar.chack.util.Type; -import me.alien.lufar.chack.util.Version; +import me.alien.lufar.chack.util.*; import me.alien.lufar.chack.util.math.Vector2I; import me.alien.lufar.chack.util.networking.DataPacket; import org.json.JSONObject; @@ -28,7 +25,8 @@ public static void main(String[] args) { ServerSocket serverSocket; - static Tile[][] map = new Tile[20][20]; + static Tile[][] map = new Tile[60][60]; + static ArrayList> currentPlating = new ArrayList<>(); Client player1, player2; @@ -56,8 +54,8 @@ public Server(){ dataHandler = new DataHandler(); dataHandler.start(); - for(int x = 0; x < 20; x++){ - for(int y = 0; y < 20; y++){ + for(int x = 0; x < map.length; x++){ + for(int y = 0; y < map[x].length; y++){ map[x][y] = new Tile(); } } @@ -89,7 +87,7 @@ public void run() { player1 = new Client(socket, 1, server); id = 1; }else if(player2 == null){ - player2 = new Client(socket, 1, server); + player2 = new Client(socket, 2, server); id = 2; }else{ out.println(new DataPacket(Type.ERROR, new JSONObject().put("error", "Server full"), "Server full").toJSON()); @@ -98,9 +96,9 @@ public void run() { out.println(new DataPacket(Type.SUCCES, new JSONObject().put("info", "Ready to play"), "").toJSON()); //out.println(new DataPacket(Type.TILE, new Tile().place(id).toJSON(0,id), "").toJSON()); - JSONObject map = new JSONObject(); - for(int x = 0; x < 20; x++){ - for(int y = 0; y < 20; y++){ + /*JSONObject map = new JSONObject(); + for(int x = 0; x < Server.map.length; x++){ + for(int y = 0; y < Server.map[x].length; y++){ final JSONObject value = new Pair<>(new Vector2I(x, y).toJSON(), Server.map[x][y].toJSON(x, y)).toJSON(x, y); map.put(x+":"+y, value); } @@ -108,7 +106,7 @@ public void run() { System.out.println(map); final JSONObject mapJson = new DataPacket(Type.MAP, map, "").toJSON(); System.out.println(mapJson); - out.println(mapJson); + out.println(mapJson);*/ } catch (IOException e) { e.printStackTrace(); } @@ -132,19 +130,87 @@ public void run() { int id = dataIn.get(0).getKey(); Pair data = dataIn.get(0).getValue(); + dataIn.remove(0); if(data.getKey() == Type.CLICK){ + if(player1 == null || player2 == null) continue; if(turn == id) { final JSONObject tile = data.getValue(); int x = tile.getInt("x")/10; int y = tile.getInt("y")/10; + if(map[x][y].isPlaced()) continue; map[x][y].place(id); - if(player1 != null){ - player1.out.println(new DataPacket(Type.TILE, map[x][y].toJSON(x,y), "").toJSON()); + currentPlating.add(new Pair<>(new Vector2I(x,y), map[x][y])); + Vector2I firstTile = null; + Vector2I lastTile = null; + boolean finished = false; + for(Pair checkTile : currentPlating){ + int id1 = checkTile.getValue().getId(); + Vector2I pos = checkTile.getKey(); + firstTile = pos; + for(int x1 = -1; x1 <= 1; x1++){ + for(int y1 = -1; y1 <= 1; y1++){ + try{ + if(y1 == 0 && x1 == 0) continue; + int y2 = pos.getY()+y1, x2 = pos.getX()+x1; + Tile t = map[x2][y2]; + int stack = 1; + while (t.getId() == id1 && stack < 5){ + if(t.isFinished()) break; + x2+=x1; + y2+=y1; + t = map[x2][y2]; + stack++; + } + if(stack == 5){ + finished = true; + lastTile = new Vector2I(x2, y2); + break; + } + }catch (IndexOutOfBoundsException e){ + + } + } + if(finished){ + break; + } + } + if(finished){ + break; + } } - if(player2 != null){ - player2.out.println(new DataPacket(Type.TILE, map[x][y].toJSON(x,y), "").toJSON()); + if(finished) { + JSONObject line = new JSONObject(); + JSONObject finishedMap = new JSONObject(); + for (Pair checkTile : currentPlating) { + checkTile.getValue().setFinished(true); + + line.put("start", firstTile.toJSON()); + line.put("end", lastTile.toJSON()); + LineType lineType = LineType.DIAGONAL; + if(firstTile.getY() == lastTile.getY()){ + lineType = LineType.HORIZONTAL; + }else if(firstTile.getX() == lastTile.getX()){ + lineType = LineType.VERTICAL; + } + line.put("type", lineType); + + final Vector2I pos = checkTile.getKey(); + final int y1 = pos.getY(); + final int x1 = pos.getX(); + finishedMap.put(x1 +":"+ y1, new Pair<>(pos.toJSON(), checkTile.getValue().toJSON(x1, y1)).toJSON(x1, y1)); + } + turn = (turn==1?2:1); + player1.out.println(new DataPacket(Type.MAP, finishedMap, "").toJSON()); + player2.out.println(new DataPacket(Type.MAP, finishedMap, "").toJSON()); + + player1.out.println(new DataPacket(Type.LINE, line, "").toJSON()); + player2.out.println(new DataPacket(Type.LINE, line, "").toJSON()); + currentPlating = new ArrayList<>(); + continue; } + player1.out.println(new DataPacket(Type.TILE, map[x][y].toJSON(x,y), "").toJSON()); + player2.out.println(new DataPacket(Type.TILE, map[x][y].toJSON(x,y), "").toJSON()); turn = (turn==1?2:1); } } diff --git a/src/main/java/me/alien/lufar/chack/util/Line.java b/src/main/java/me/alien/lufar/chack/util/Line.java new file mode 100644 index 0000000..37b8857 --- /dev/null +++ b/src/main/java/me/alien/lufar/chack/util/Line.java @@ -0,0 +1,50 @@ +package me.alien.lufar.chack.util; + +public class Line { + K key; + V value; + T type; + + public Line(K key, V value, T type) { + this.key = key; + this.value = value; + this.type = type; + } + + public Line(){ + + } + + public V getValue() { + return value; + } + + public K getKey() { + return key; + } + + public T getType() { + return type; + } + + public void setType(T type) { + this.type = type; + } + + public void setValue(V value) { + this.value = value; + } + + public void setKey(K key) { + this.key = key; + } + + @Override + public String toString() { + return "Line{" + + "key=" + key + + ", value=" + value + + ", type=" + type + + '}'; + } +} diff --git a/src/main/java/me/alien/lufar/chack/util/LineType.java b/src/main/java/me/alien/lufar/chack/util/LineType.java new file mode 100644 index 0000000..54478ff --- /dev/null +++ b/src/main/java/me/alien/lufar/chack/util/LineType.java @@ -0,0 +1,7 @@ +package me.alien.lufar.chack.util; + +public enum LineType { + VERTICAL, + HORIZONTAL, + DIAGONAL +} diff --git a/src/main/java/me/alien/lufar/chack/util/Pair.java b/src/main/java/me/alien/lufar/chack/util/Pair.java index 7fd2214..46f3837 100644 --- a/src/main/java/me/alien/lufar/chack/util/Pair.java +++ b/src/main/java/me/alien/lufar/chack/util/Pair.java @@ -69,12 +69,15 @@ public JSONObject toJSON(int x, int y){ } catch (IllegalAccessException ex) { ex.printStackTrace(); } + } catch(NullPointerException e) { + //Hantera detta på rätt sätt + System.out.println("Breakpoint"); } } } - Method[] valueMethods = key.getClass().getDeclaredMethods(); - Object valueData = key.toString(); + Method[] valueMethods = value.getClass().getDeclaredMethods(); + Object valueData = value.toString(); for(Method m : valueMethods){ if(m.getName().equals("toJSON")){ try { diff --git a/src/main/java/me/alien/lufar/chack/util/Tile.java b/src/main/java/me/alien/lufar/chack/util/Tile.java index a53a3ea..f6e2bf1 100644 --- a/src/main/java/me/alien/lufar/chack/util/Tile.java +++ b/src/main/java/me/alien/lufar/chack/util/Tile.java @@ -8,6 +8,7 @@ public class Tile { boolean placed = false; int id = 0; + boolean finished = false; public Tile(){} @@ -25,6 +26,14 @@ public boolean isPlaced() { return placed; } + public boolean isFinished(){ + return finished; + } + + public void setFinished(boolean finished) { + this.finished = finished; + } + public void draw(Graphics2D g2d, int x, int y){ if(placed) { if (id == 1) { @@ -51,6 +60,7 @@ public JSONObject toJSON(int x, int y){ obj.put("placed", placed); obj.put("x", x); obj.put("y", y); + obj.put("finished", finished); return obj; } @@ -59,6 +69,7 @@ public static Pair fromJSON(JSONObject data){ if(data.getBoolean("placed")){ tile.place(data.getInt("id")); } + tile.setFinished(data.getBoolean("finished")); return new Pair<>(new Vector2I(data.getInt("x"), data.getInt("y")), tile); } } diff --git a/src/main/java/me/alien/lufar/chack/util/Type.java b/src/main/java/me/alien/lufar/chack/util/Type.java index 6db1430..017e55c 100644 --- a/src/main/java/me/alien/lufar/chack/util/Type.java +++ b/src/main/java/me/alien/lufar/chack/util/Type.java @@ -8,5 +8,6 @@ public enum Type { TILE, INFO, ERROR, - CLICK + CLICK, + LINE }