Skip to content

Commit

Permalink
added displayment ofwins/losses to the title bar
Browse files Browse the repository at this point in the history
incresed the size of the map from 60x60 to 1000x1000
made it posible to move around the map whit arow key or WASD
fixed the win line displayer
  • Loading branch information
zaze06 committed Feb 13, 2022
1 parent 0a5de0f commit a1573f0
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 25 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ plugins {
id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'
group 'me.alien'
version '1.1-STABLE'

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/me/alien/lufar/chack/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public class Main {

public static final Version VERSION = new Version("0.1-DEV");
public static final Version VERSION = new Version("1.0-STABLE");
public static final ArrayList<Class<?>> listeners = new ArrayList<>();

public static void main(String[] args1) {
Expand Down
48 changes: 32 additions & 16 deletions src/main/java/me/alien/lufar/chack/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,23 @@ public static void main(ArrayList<String> args) {

InputDataThread inputDataThread;

static Tile[][] map = new Tile[60][60];
static Tile[][] map = new Tile[1000][1000];

Vector2I leftTop = new Vector2I(map.length/2-30, map[0].length/2-30);
Vector2I offset = new Vector2I(map.length/2-30, map[0].length/2-30);

public Client(String hostname){
frame = new JFrame();

frame.setSize(600,600);

addMouseListener(this);
addKeyListener(this);

frame.addKeyListener(this);
try {



socket = new Socket(hostname, 3030);

out = new PrintWriter(socket.getOutputStream(), true);
Expand Down Expand Up @@ -118,7 +123,7 @@ public void doDraw(Graphics2D g2d){
for(int y = 0; y < 60; y++){
int yPos = y*10;
try {
final Tile tile = map[x][y];
final Tile tile = map[x+offset.getX()][y+offset.getY()];
if(tile.isFinished()){
g2d.setColor(Color.GRAY);
g2d.fillRect(xPos, yPos, 10, 10);
Expand All @@ -134,19 +139,21 @@ public void doDraw(Graphics2D g2d){

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);
g2d.drawLine((line.getKey().getX()-offset.getX())*10+5, (line.getKey().getY()-offset.getY())*10+5,
(line.getValue().getX()-offset.getX())*10+5, (line.getValue().getY()-offset.getY())*10+5);
/*if(line.getType() == LineType.HORIZONTAL){
g2d.drawLine(line.getKey().getX()*10-offset.getX()+5, line.getKey().getY()*10-offset.getY()+5, line.getValue().getX()*10-offset.getX()-5, line.getValue().getY()*10-offset.getY()+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);
g2d.drawLine(line.getKey().getX()*10-offset.getX()+5, line.getKey().getY()*10-offset.getY()+5, line.getValue().getX()*10-offset.getX()+5, line.getValue().getY()*10-offset.getY()-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);
}
g2d.drawLine(line.getKey().getX()*10-offset.getX()+5, line.getKey().getY()*10-offset.getY()+5, line.getValue().getX()*10-offset.getX()-5, line.getValue().getY()*10-offset.getY()-5);
}*/
}
}

@Override
public void mouseClicked(MouseEvent e) {
out.println(new DataPacket(Type.CLICK, new JSONObject().put("x", e.getX()).put("y", e.getY()), "").toJSON());
out.println(new DataPacket(Type.CLICK, new JSONObject().put("x", e.getX()/10+offset.getX()).put("y", e.getY()/10+offset.getY()), "").toJSON());
}

@Override
Expand Down Expand Up @@ -176,12 +183,20 @@ public void actionPerformed(ActionEvent e) {

@Override
public void keyTyped(KeyEvent e) {

System.out.println("Key tyoed");
}

@Override
public void keyPressed(KeyEvent e) {

if(e.getKeyCode() == KeyEvent.VK_W || e.getKeyCode() == KeyEvent.VK_UP){
offset.addY(-1);
}else if(e.getKeyCode() == KeyEvent.VK_S || e.getKeyCode() == KeyEvent.VK_DOWN){
offset.addY(1);
}else if(e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_LEFT){
offset.addX(-1);
}else if(e.getKeyCode() == KeyEvent.VK_D || e.getKeyCode() == KeyEvent.VK_RIGHT){
offset.addX(1);
}
}

@Override
Expand All @@ -196,12 +211,11 @@ public void run() {
try {
String dataIn = in.readLine();
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);
JSONObject object = new JSONObject(dataIn);
JSONObject data = object.getJSONObject("data");
Type type = object.getEnum(Type.class, "type");
Pair<Type, JSONObject> tmp = new Pair<>(type, data);

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();
Expand All @@ -221,6 +235,8 @@ public void run() {
LineType lineType = startJSON.getEnum(LineType.class, "type");

lines.add(new Line<>(start, end, lineType));
}else if(type == Type.NAME){
frame.setTitle(tmp.getValue().getString("name"));
}
} catch (IOException e) {
e.printStackTrace();
Expand Down
29 changes: 24 additions & 5 deletions src/main/java/me/alien/lufar/chack/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void main(String[] args) {

ServerSocket serverSocket;

static Tile[][] map = new Tile[60][60];
static Tile[][] map = new Tile[1000][1000];
static ArrayList<Pair<Vector2I, Tile>> currentPlating = new ArrayList<>();

Client player1, player2;
Expand All @@ -36,6 +36,9 @@ public static void main(String[] args) {

DataHandler dataHandler;

int player1Wins = 0;
int player2Wins = 0;

int turn = 1;

public Server(){
Expand Down Expand Up @@ -85,9 +88,11 @@ public void run() {
int id;
if(player1 == null){
player1 = new Client(socket, 1, server);
player1.out.println(new DataPacket(Type.NAME, new JSONObject().put("name", "You have: "+player1Wins+" wins. Player 2 have: "+player2Wins+" wins"), "").toJSON());
id = 1;
}else if(player2 == null){
player2 = new Client(socket, 2, server);
player2.out.println(new DataPacket(Type.NAME, new JSONObject().put("name", "You have: "+player2Wins+" wins. Player 1 have: "+player1Wins+" wins"), "").toJSON());
id = 2;
}else{
out.println(new DataPacket(Type.ERROR, new JSONObject().put("error", "Server full"), "Server full").toJSON());
Expand Down Expand Up @@ -136,9 +141,11 @@ public void run() {
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;
int x = tile.getInt("x");
int y = tile.getInt("y");
if(map[x][y].isPlaced()) continue;
for(Pair<Vector2I, Tile> checkTile : currentPlating){
}
map[x][y].place(id);
currentPlating.add(new Pair<>(new Vector2I(x,y), map[x][y]));
Vector2I firstTile = null;
Expand All @@ -155,16 +162,23 @@ public void run() {
int y2 = pos.getY()+y1, x2 = pos.getX()+x1;
Tile t = map[x2][y2];
int stack = 1;
while (t.getId() == id1 && stack < 5){
do {
if(t.isFinished()) break;
x2+=x1;
y2+=y1;
t = map[x2][y2];
stack++;
}
}while (t.getId() == id1 && stack < 5);
if(stack == 5){
x2 -= x1;
y2 -= y1;
finished = true;
lastTile = new Vector2I(x2, y2);
if(id1 == 1){
player1Wins++;
}else if(id1 == 2){
player2Wins++;
}
break;
}
}catch (IndexOutOfBoundsException e){
Expand All @@ -187,6 +201,8 @@ public void run() {

line.put("start", firstTile.toJSON());
line.put("end", lastTile.toJSON());


LineType lineType = LineType.DIAGONAL;
if(firstTile.getY() == lastTile.getY()){
lineType = LineType.HORIZONTAL;
Expand All @@ -206,6 +222,9 @@ public void run() {

player1.out.println(new DataPacket(Type.LINE, line, "").toJSON());
player2.out.println(new DataPacket(Type.LINE, line, "").toJSON());

player1.out.println(new DataPacket(Type.NAME, new JSONObject().put("name", "You have: "+player1Wins+" wins. Player 2 have: "+player2Wins+" wins"), "").toJSON());
player2.out.println(new DataPacket(Type.NAME, new JSONObject().put("name", "You have: "+player2Wins+" wins. Player 1 have: "+player1Wins+" wins"), "").toJSON());
currentPlating = new ArrayList<>();
continue;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/me/alien/lufar/chack/util/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public enum Type {
INFO,
ERROR,
CLICK,
LINE
LINE,
NAME
}

0 comments on commit a1573f0

Please sign in to comment.