Skip to content

Commit

Permalink
fixed nullpointer exeption
Browse files Browse the repository at this point in the history
made the game playible on a 60x60 field

and added README.md
  • Loading branch information
zaze06 committed Feb 12, 2022
1 parent 2a19e5e commit 0a5de0f
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 35 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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<VERSION>.jar'- server` NOTE: `$` is to indicate that it's a command, `<VERSION>` 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<VERSION>.jar' -client -ip <server ip>` NOTE: `$` is to indicate that it's a command, `<VERSION>` is the latest version, the `-ip <server ip>` is not needed if you ar on the same computer as the server,
`<server ip>` 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!
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies {

jar {
manifest {
attributes "Main-Class": "me.alien.game.Game"
attributes "Main-Class": "me.alien.lufar.chack.Main"
}

from {
Expand Down
67 changes: 52 additions & 15 deletions src/main/java/me/alien/lufar/chack/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> args) {
String hostname = "localhost";
if(args.contains("-ip")){
Expand All @@ -32,6 +29,7 @@ public static void main(ArrayList<String> args) {
}

static ArrayList<Pair<Type, JSONObject>> dataIn = new ArrayList<>();
static ArrayList<Line<Vector2I, Vector2I, LineType>> lines = new ArrayList<>();

JFrame frame;

Expand All @@ -46,6 +44,8 @@ public static void main(ArrayList<String> 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();

Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -115,19 +113,35 @@ 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);
System.exit(1);
}
}
}

for(Line<Vector2I, Vector2I, LineType> 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
Expand Down Expand Up @@ -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<Type, JSONObject> 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<Vector2I, Tile> 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<Vector2I, Tile> 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();
Expand Down
98 changes: 82 additions & 16 deletions src/main/java/me/alien/lufar/chack/server/Server.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Pair<Vector2I, Tile>> currentPlating = new ArrayList<>();

Client player1, player2;

Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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());
Expand All @@ -98,17 +96,17 @@ 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);
}
}
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();
}
Expand All @@ -132,19 +130,87 @@ public void run() {

int id = dataIn.get(0).getKey();
Pair<Type, JSONObject> 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<Vector2I, Tile> 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<Vector2I, Tile> 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);
}
}
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/me/alien/lufar/chack/util/Line.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package me.alien.lufar.chack.util;

public class Line<K, V, T> {
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 +
'}';
}
}
7 changes: 7 additions & 0 deletions src/main/java/me/alien/lufar/chack/util/LineType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.alien.lufar.chack.util;

public enum LineType {
VERTICAL,
HORIZONTAL,
DIAGONAL
}
Loading

0 comments on commit 0a5de0f

Please sign in to comment.