문제
단일 클라이언트용 가위바위보 프로그램에서 네트워크 서버 부분의 문제입니다 이를 해결하는 다음 프로그램에 대해 빈칸을 채우세요
코드 빈칸 채우기
import javax.swing.*;
import java.awt.event.*;
import rpsGame.RockPaperScissorsNetwork;
public class RockPaperScissorsServer
{
public static void main( String[] args ) {
final String imagePath = "C:\\Users\\user\\Downloads\\JAVA-main\\src\\rpsGame\\image\\";
RockPaperScissorsNetwork panel =
new RockPaperScissorsNetwork( imagePath );
JFrame frame = new JFrame( "가위바위보(서버)" );
frame.getContentPane().add( panel );
frame.addWindowListener( new () {
public void ( WindowEvent event ) {
panel.close();
}
} );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
frame.setVisible( true );
panel.connectAsServer();
}
}
package rpsGame;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import network.Network;
public class RockPaperScissorsNetwork RockPaperScissorsPanel
{
private Network network;
private int counterpart;
private final String header = "[RPSN] ";
private final String disconnectMessage = " -1";
public RockPaperScissorsNetwork( String imgPath ) {
super( imgPath );
super.timer.stop();
super.show( QUESTIONMARK, QUESTIONMARK );
super.strPlayers.setText( "<html><body style='text-align:center;'>상대편이 준비중입니다 기다려주세요<br>[나]</body></html>" );
network =
new Network();
}
public RockPaperScissorsNetwork( String imgPath, String serverIP ) {
this( imgPath );
network.connectAsClient( serverIP, this );
network.write( header + " " + super.PAPER );
}
public void connectAsServer() {
network.connectAsServer( this );
}
@Override
public void actionPerformed( ActionEvent event ) {
int player = super.select( event );
super.show( player, counterpart );
network.write( header + " " + player );
}
@Override
public void () {
for ( String message = null; ( message =
network.read() ) != null; ) {
if ( !message.contains( header ) )
continue;
else if ( message.contains( disconnectMessage ) ) {
close();
super.strPlayers.setText( "<html><body style='text-align:center;'>상대편이 종료하였습니다<br>[나]</body></html>" );
super.show( QUESTIONMARK, QUESTIONMARK );
break;
}
else {
super.strPlayers.setText( "<html><body style='text-align:center;'>[너]<br>[나]</body></html>" );
super.ready();
counterpart = Integer.parseInt( message.replace( header, "" ).trim() );
}
}
}
public void close() {
network.write( header + " " + disconnectMessage );
network.close();
}
}
package network;
import java.io.*;
import java.net.*;
public class Network
{
public static final int serverPort = 7700;
protected serverSocket;
protected socket;
private in;
private out;
private waitForCounterpart;
public Network() {
serverSocket = null;
socket = null;
in = null;
out = null;
waitForCounterpart = null;
}
public void connectAsServer( obj ) {
{
serverSocket = new ( serverPort );
socket =
serverSocket.();
connectInOut( obj );
} ( Exception e ) {
e.printStackTrace();
}
}
public void connectAsClient( String serverIP, obj ) {
{
socket =
new ( serverIP, serverPort );
connectInOut( obj );
} ( Exception e ) {
e.printStackTrace();
}
}
public void connectInOut( obj ) {
{
in = new ( new ( socket.() ));
out = new ( socket.(), true );
waitForCounterpart = new ( obj );
waitForCounterpart.();
} ( Exception e ) {
e.printStackTrace();
}
}
public String read() {
{
if ( this.isConnecting() == true )
return
in.readLine();
} ( Exception e ) {
e.printStackTrace();
}
return null;
}
public void write( String data ) {
{
if ( this.isConnecting() == true )
out.println( data );
} ( Exception e ) {
e.printStackTrace();
}
}
public boolean isConnecting() {
if ( ( socket != null )
&& ( in != null )
&& ( out != null )
&& ( waitForCounterpart != null )
&& ( waitForCounterpart.getState() != .State.TERMINATED ) )
return true;
else
return false;
}
public void close() {
{
if ( waitForCounterpart != null ) {
waitForCounterpart.();
waitForCounterpart = null;
}
if ( in != null ) {
in.close();
in=null;
}
if ( out != null ) {
out.flush();
out.close();
out = null;
}
if ( socket != null ) {
socket.close();
socket = null;
}
if ( serverSocket != null ) {
serverSocket.close();
serverSocket = null;
}
} ( Exception e ) {
e.printStackTrace();
}
}
}
package rpsGame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RockPaperScissorsPanel JPanel ActionListener
{
protected final int SCISSORS = 0, ROCK = 1, PAPER = 2, QUESTIONMARK = 3;
protected final int BASE = 0, WIN = 1;
protected JLabel imgPlayerA, imgPlayerB, strPlayers;
protected JButton[] button;
protected ImageIcon[][] image;
protected Timer timer;
public RockPaperScissorsPanel( final String imgPath ) {
image = new ImageIcon[4][2];
image[SCISSORS ][BASE] = new ImageIcon( new ImageIcon( imgPath + "scissors.gif" ).getImage().getScaledInstance( 200, 200, Image.SCALE_SMOOTH ) );
image[SCISSORS ][WIN] = new ImageIcon( new ImageIcon( imgPath + "scissors_win.gif" ).getImage().getScaledInstance( 200, 200, Image.SCALE_SMOOTH ) );
image[ROCK ][BASE] = new ImageIcon( new ImageIcon( imgPath + "rock.gif" ).getImage().getScaledInstance( 200, 200, Image.SCALE_SMOOTH ) );
image[ROCK ][WIN] = new ImageIcon( new ImageIcon( imgPath + "rock_win.gif" ).getImage().getScaledInstance( 200, 200, Image.SCALE_SMOOTH ) );
image[PAPER ][BASE] = new ImageIcon( new ImageIcon( imgPath + "paper.gif" ).getImage().getScaledInstance( 200, 200, Image.SCALE_SMOOTH ) );
image[PAPER ][WIN] = new ImageIcon( new ImageIcon( imgPath + "paper_win.gif" ).getImage().getScaledInstance( 200, 200, Image.SCALE_SMOOTH ) );
image[QUESTIONMARK][BASE] = new ImageIcon( new ImageIcon( imgPath + "questionmark.gif" ).getImage().getScaledInstance( 200, 200, Image.SCALE_SMOOTH ) );
image[QUESTIONMARK][WIN] = new ImageIcon( new ImageIcon( imgPath + "questionmark.gif" ).getImage().getScaledInstance( 200, 200, Image.SCALE_SMOOTH ) );
this.imgPlayerA = new JLabel( image[QUESTIONMARK][BASE] );
this.imgPlayerB = new JLabel( image[QUESTIONMARK][BASE] );
this.strPlayers = new JLabel( "<html><body>[너]<br>[나]</body></html>", JLabel.CENTER );
this.add( this.imgPlayerB );
this.add( this.strPlayers );
this.add( this.imgPlayerA );
button = new JButton[3];
button[SCISSORS] = new JButton( new ImageIcon( image[SCISSORS][BASE].getImage().getScaledInstance( 30, 30, Image.SCALE_SMOOTH ) ) );
button[ROCK ] = new JButton( new ImageIcon( image[ROCK ][BASE].getImage().getScaledInstance( 30, 30, Image.SCALE_SMOOTH ) ) );
button[PAPER ] = new JButton( new ImageIcon( image[PAPER ][BASE].getImage().getScaledInstance( 30, 30, Image.SCALE_SMOOTH ) ) );
button[SCISSORS].addActionListener( this );
button[ROCK ].addActionListener( this );
button[PAPER ].addActionListener( this );
this.add( new JLabel(" 가위 바위 보를 선택하세요 ") );
this.add( button[SCISSORS] );
this.add( button[ROCK] );
this.add( button[PAPER] );
this.setBackground( Color.cyan );
this.setPreferredSize( new Dimension(220, 530) );
this.timer = new Timer( 1000, new ActionListener() {
@Override
public void actionPerformed( ActionEvent event ) {
ready();
timer.stop();
}
});
}
public void ready() {
imgPlayerA.setIcon( image[QUESTIONMARK][BASE] );
imgPlayerB.setIcon( image[QUESTIONMARK][BASE] );
button[0].setEnabled( true );
button[1].setEnabled( true );
button[2].setEnabled( true );
}
@Override
public void actionPerformed( ActionEvent event ) {
int playerA = select( event );
int playerB = (int)( Math.random() * 3 );
show( playerA, playerB );
timer.();
}
public void show( int playerA, int playerB ) {
int playerA_winOrLose = BASE, playerB_winOrLose = BASE;
if ( ( ( playerA == SCISSORS ) && ( playerB == PAPER ) )
|| ( ( playerA == ROCK ) && ( playerB == SCISSORS ) )
|| ( ( playerA == PAPER ) && ( playerB == ROCK ) ) ) {
playerA_winOrLose = WIN;
}
else if ( ( ( playerA == PAPER ) && ( playerB == SCISSORS ) )
|| ( ( playerA == SCISSORS ) && ( playerB == ROCK ) )
|| ( ( playerA == ROCK ) && ( playerB == PAPER ) ) ) {
playerB_winOrLose = WIN;
}
imgPlayerA.setIcon( image[ playerA ][ playerA_winOrLose ] );
imgPlayerB.setIcon( image[ playerB ][ playerB_winOrLose ] );
button[0].setEnabled( false );
button[1].setEnabled( false );
button[2].setEnabled( false );
}
public int select( ActionEvent event ) {
if( event.getSource() == button[SCISSORS] )
return SCISSORS;
else if ( event.getSource() == button[ROCK] )
return ROCK;
else
return PAPER;
}
}