Skip to content

Commit

Permalink
communication via Cell
Browse files Browse the repository at this point in the history
  • Loading branch information
akorobeinikov committed Oct 20, 2020
1 parent 9121352 commit b7fe6f3
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 67 deletions.
28 changes: 15 additions & 13 deletions src/main/java/client/mvc/Model/ModelClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package client.mvc.Model;

import client.mvc.View.IObserver;
import resources.Cell;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -61,14 +63,12 @@ public void run() {
int op = dis.readInt();
if(op == 1)
{
int height = dis.readInt();
int width = dis.readInt();
field = new int[height][width];
for(int i = 0; i < height; ++i) {
for(int j = 0; j < width; ++j) {
field[i][j] = dis.readInt();
}
}
op = dis.readInt();
int x = dis.readInt();
int y = dis.readInt();
String state = dis.readUTF();
System.out.printf("Received: x=%d; y=%d state = %s\n", x, y, state);
point = new Cell(x, y, state);
refresh();
}
if(op == -1)
Expand All @@ -89,21 +89,23 @@ public void run() {
refresh();
}

int[][] field;
Cell point;

public int[][] getField(){
public Cell getPoint(){
System.out.println(cs);
if(cs == null) return null;

return field;
return point;
}

public void setText(String mes)
public void setCell(Cell point)
{
if(cs == null) return;
try {
dos.writeInt(1);
dos.writeUTF(mes);
dos.writeInt(point.x);
dos.writeInt(point.y);
dos.writeUTF(point.state);
dos.flush();
} catch (IOException ex) {
Logger.getLogger(ModelClient.class.getName()).log(Level.SEVERE, null, ex);
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/client/mvc/View/ViewClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import client.mvc.Model.BModelClient;
import client.mvc.Model.ModelClient;
import resources.Cell;

import javax.swing.*;
import java.awt.*;
Expand Down Expand Up @@ -59,13 +60,10 @@ private void initComponents() {

@Override
public void refresh() {
int[][] field = m.getField();
if (field == null)
Cell point = m.getPoint();
if (point == null)
return;
for(int i = 0; i < field.length; ++i) {
for(int j = 0; j < field[0].length; ++j) {
viewField[i][j].setText(String.valueOf(field[i][j]));
}
}
System.out.printf("new point = %s", point.state);
viewField[point.x][point.y].setIcon(point.getIcon());
}
}
35 changes: 14 additions & 21 deletions src/main/java/resources/Cell.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,31 @@

public class Cell {

HashMap<String, ImageIcon> icons;
int x, y;
String current;

public Cell (int _x, int _y) {
x = _x;
y = _y;
static HashMap<String, ImageIcon> icons;
static {
icons = new HashMap<>();
icons.put("empty", new ImageIcon(new ImageIcon("green.jpg").getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT)));
icons.put("eat", new ImageIcon(new ImageIcon("apple.jpg").getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT)));
icons.put("eat", new ImageIcon(new ImageIcon("apple2.jpg").getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT)));
icons.put("snake", new ImageIcon(new ImageIcon("snake.jpg").getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT)));
current = "empty";
}
public ImageIcon getCurrent() {
return icons.get(current);
}
public int x, y;
public String state;

public void setState(String state) {
current = state;
public Cell (int _x, int _y, String _state) {
x = _x;
y = _y;

state = _state;
}

public boolean isEmpty() {
if (current == "empty")
if (state == "empty") {
return true;
}
return false;
}

public int getX() {
return x;
}

public int getY() {
return y;
public ImageIcon getIcon() {
return icons.get(state);
}
}
11 changes: 6 additions & 5 deletions src/main/java/server/mvp/Model/ModelServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ class ModelServer implements IModelServer{
ArrayList<IPresenter> list_players = new ArrayList<>();

public ModelServer() {
buffer = new Cell(0, 0);
buffer = new Cell(0, 0, "empty");
for(int i = 0; i < height; ++i) {
for(int j = 0; j < width; ++j) {
game_field[i][j] = new Cell(i, j);
game_field[i][j] = new Cell(i, j, "empty");
}
}
}

public void setCell(Cell new_c) {
game_field[new_c.getX()][new_c.getY()] = new_c;
game_field[new_c.x][new_c.y] = new_c;
buffer = new_c;
refresh();
}
Expand All @@ -41,16 +41,16 @@ public void generateNewItem() {
for(int j = 0; j < width; j++){
if(game_field[i][j].isEmpty()) {
if (point == 0) {
game_field[i][j].setState("eat");
game_field[i][j].state = "eat";
buffer = game_field[i][j];
refresh();
return;
}else {
point--;
}
}
}
}
refresh();
}
void refresh()
{
Expand All @@ -60,6 +60,7 @@ void refresh()
}

public Cell getBuffer() {
System.out.printf("buffer x = %d, buffer y = %d, state = %s\n", buffer.x, buffer.y, buffer.state);
return buffer;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/server/mvp/Presenter/Presenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public void run() {
code = v.getOp();
if(code == 1)
{
observed_model.setField(v.getField());
observed_model.setCell(v.getCell());
}
if(code == 2)
{
v.setOp(1);
v.setField(observed_model.getField());
v.setCell(observed_model.getBuffer());
}
if(code == -1)
{
Expand All @@ -50,7 +50,7 @@ public void run() {
@Override
public void update() {
v.setOp(1);
v.setField(observed_model.getField());
v.setCell(observed_model.getBuffer());
}

}
7 changes: 5 additions & 2 deletions src/main/java/server/mvp/View/IViewServer.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package server.mvp.View;


import resources.Cell;

public interface IViewServer {
int getOp();
void setField(int[][] gameField);
int[][] getField();
void setCell(final Cell point);
Cell getCell();
void setOp(int op);
}
27 changes: 11 additions & 16 deletions src/main/java/server/mvp/View/ViewServer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package server.mvp.View;

import resources.Cell;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -33,19 +35,16 @@ public int getOp() {
return -1;
}

public void setField(final int[][] gameField) {
public void setCell(final Cell point) {
new Thread()
{
@Override
public void run() {
try {
dos.writeInt(gameField.length);
dos.writeInt(gameField[0].length);
for(int i = 0; i < gameField.length; i++) {
for(int j = 0; j < gameField[i].length; j++) {
dos.writeInt(gameField[i][j]);
}
}
System.out.printf("Send: x=%d, y=%d, state= %s", point.x, point.y, point.state);
dos.writeInt(point.x);
dos.writeInt(point.y);
dos.writeUTF(point.state);
dos.flush();
} catch (IOException ex) {
Logger.getLogger(ViewServer.class.getName()).log(Level.SEVERE, null, ex);
Expand All @@ -57,14 +56,10 @@ public void run() {

public Cell getCell() {
try {
int height = dis.readInt();
int width = dis.readInt();
int[][] field = new int[height][width];
for(int i = 0; i < height; ++i) {
for(int j = 0; j < width; ++j) {
field[i][j] = dis.readInt();
}
}
int x = dis.readInt();
int y = dis.readInt();
String state = dis.readUTF();
Cell cell = new Cell(x, y, state);
return cell;
} catch (IOException ex) {
Logger.getLogger(ViewServer.class.getName()).log(Level.SEVERE, null, ex);
Expand Down

0 comments on commit b7fe6f3

Please sign in to comment.