Skip to content

Commit

Permalink
✨ FEAT. 루미큐브 UI 구현
Browse files Browse the repository at this point in the history
Client, Server 소켓 클래스 추가

Related to : #2
  • Loading branch information
junhaa committed Dec 16, 2023
1 parent a3a42d4 commit 20e788c
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 24 deletions.
4 changes: 1 addition & 3 deletions src/Application.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import Controller.GameController;
import Model.GameModel;
import View.GameView;

public class Application {
public static void main(String[] args) {
GameModel model = new GameModel();
GameView view = new GameView();
GameController controller = new GameController(model, view);
GameController controller = new GameController(view);
controller.start();
}
}
34 changes: 30 additions & 4 deletions src/Controller/GameController.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
package Controller;

import Model.GameModel;
import Model.Player;
import Model.Server;
import View.GameView;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.SQLOutput;


// 서버 플레이어가 게임 진행을 관리
public class GameController {
private GameModel model;
private GameView view;

public static final int PORT = 12345; // 포트 번호

private boolean isServer = false; // 현재 서버로 운영되는 사용자인지
private Server server = null;

public static final int MAX_PLAYER_COUNT = 4; // 최대 플레이어 수 (본인 포함)
public static final int BOARD_WIDTH = 20;
public static final int BOARD_HEIGHT = 7;
Expand All @@ -20,14 +34,26 @@ public class GameController {

private Player[] players = new Player[MAX_PLAYER_COUNT];

public GameController(GameModel model, GameView view) {
this.model = model;
public GameController(GameView view) {
this.view = view;
view.setGameController(this);
}

public void start(){
view.startUI();
}


// 방 만드는 함수 - 서버
public void makeRoom() {
server = new Server(PORT);
isServer = true;
// 뷰에서 화면 전환함수
}




}


30 changes: 30 additions & 0 deletions src/Model/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package Model;

import Controller.GameController;

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

public class Client {
static void connect(String address, int port) throws UnknownHostException{
try (Socket socket = new Socket(address, port);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) {

System.out.println("서버에 연결되었습니다.");

String userInput;

while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("서버로 전송한 메시지: " + userInput);

String response = in.readLine();
System.out.println("서버로부터 받은 응답: " + response);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
32 changes: 25 additions & 7 deletions src/Model/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ public Game(){
}


// 플레이어 목록에 플레이어 추가
public boolean addPlayer(Player player){
for(int i = 0 ; i < GameController.MAX_PLAYER_COUNT ; i ++){
if(players[i] == null){
players[i] = player;
return true;
}
}
return false;
}

// 현재 플레이어 수
public int getPlayerCount(){
int curCnt = 0;
for(int i = 0 ; i < GameController.MAX_PLAYER_COUNT ; i ++){
if(players[i] != null){
curCnt ++;
}
}
return curCnt;
}

// 처음 시작할 사람 랜덤으로 결정
private int getStartPlayerIndex(){
int startIdx = new Random().nextInt(playerCount);
Expand All @@ -43,16 +65,12 @@ private int getStartPlayerIndex(){
return -1;
}



// 게임 시작 메서드
public void start(){

// 플레이어 수 정하기
playerCount = 0;
for(int i = 0 ; i < GameController.MAX_PLAYER_COUNT ; i ++){
if(players[i] != null){
playerCount ++;
}
}
playerCount = getPlayerCount();

currentPlayerIndex = getStartPlayerIndex();

Expand Down
73 changes: 73 additions & 0 deletions src/Model/Server.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package Model;

import Controller.GameController;

import java.io.*;
import java.net.*;
public class Server {


static int clientCnt = 0;
static int port;
static ClientHandler[] handlers = new ClientHandler[GameController.MAX_PLAYER_COUNT];
public Server(int port) {
this.port = port;
openServer();
}

static void openServer() {

try (ServerSocket serverSocket = new ServerSocket(port)) {
System.out.println("서버가 " + port + " 포트에서 대기 중...");

while (clientCnt < GameController.MAX_PLAYER_COUNT) {
Socket clientSocket = serverSocket.accept();
clientCnt ++;
System.out.println("클라이언트 연결됨: " + clientSocket.getInetAddress().getHostAddress());

// 클라이언트와 통신하는 스레드 생성
ClientHandler curHandler = new ClientHandler(clientSocket);
handlers[clientCnt - 1] = curHandler;
Thread clientThread = new Thread(curHandler);
clientThread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

class ClientHandler implements Runnable {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;

public ClientHandler(Socket socket) {
this.clientSocket = socket;
try {
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void run() {
String message;
try {
while ((message = in.readLine()) != null) {
System.out.println("클라이언트로부터 받은 메시지: " + message);
out.println("서버에서 응답: " + message);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
70 changes: 60 additions & 10 deletions src/View/GameView.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package View;

import Controller.GameController;

import java.awt.EventQueue;
import java.awt.FlowLayout;

Expand All @@ -10,6 +12,8 @@

import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import static Controller.GameController.MAX_PLAYER_COUNT;
import static Controller.GameController.BOARD_HEIGHT;
Expand All @@ -28,6 +32,53 @@ public class GameView {
private JPanel[][] hand = new JPanel[HAND_HEIGHT][HAND_WIDTH];
private JPanel[] playerIcon = new JPanel[MAX_PLAYER_COUNT];

private GameController gameController;


public void setGameController(GameController gameController){
this.gameController = gameController;
}

public JFrame getFrame() {
return frame;
}

public JTextField getNameTF() {
return nameTF;
}

public JTextField getAddressTF() {
return addressTF;
}

public JButton getMakeRoomButton() {
return makeRoomButton;
}

public JButton getConnectButton() {
return connectButton;
}

public JButton getQuitButton() {
return QuitButton;
}

public JButton getRunSortButton() {
return RunSortButton;
}

public JButton getGroupSortButton() {
return GroupSortButton;
}

public JButton getDrawTileButton() {
return DrawTileButton;
}

public JLabel getErrorLabel() {
return errorLabel;
}

/**
* Launch the application.
*/
Expand All @@ -46,16 +97,7 @@ public void moveTileUI(boolean startHand, boolean toHand, int startRow, int star
}

public void startUI() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GameView window = new GameView();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
frame.setVisible(true);
}

/**
Expand Down Expand Up @@ -124,6 +166,14 @@ private void initialize() {
sl_panel.putConstraint(SpringLayout.WEST, makeRoomButton, 400, SpringLayout.WEST, panel);
sl_panel.putConstraint(SpringLayout.SOUTH, makeRoomButton, 704, SpringLayout.NORTH, panel);
sl_panel.putConstraint(SpringLayout.EAST, makeRoomButton, -850, SpringLayout.EAST, panel);
makeRoomButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(gameController);
gameController.makeRoom();
}
});

panel.add(makeRoomButton);

JPanel LoginImagePanel = new JPanel();
Expand Down

0 comments on commit 20e788c

Please sign in to comment.