Created
December 11, 2014 14:22
-
-
Save mrabaev48/9287ca06937ee60e0d12 to your computer and use it in GitHub Desktop.
ClientXO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package clientx0; | |
/** | |
* | |
* @author jshvartsman | |
*/ | |
public class ClientX0 { | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
int port = 0; | |
String ipAddress = "127.0.0.1"; | |
if(args.length == 2){ | |
try{ | |
port = Integer.parseInt(args[0]); | |
}catch(Exception exc){ | |
System.out.println(exc.toString()); | |
} | |
ipAddress = args[1]; | |
} | |
new MyJFrame(port, ipAddress).setVisible(true); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package clientx0; | |
import java.awt.*; | |
import javax.swing.*; | |
import java.awt.event.*; | |
import java.io.*; | |
import java.net.*; | |
public class MyJFrame extends JFrame implements ActionListener { | |
private JButton button_array[] = new JButton[9]; | |
public final int DEFAULT_PORT = 1701; | |
protected int port; | |
protected String ipAddress; //// | |
Socket s; | |
DataInputStream in; | |
DataOutputStream out; | |
StreamListener listener; | |
JLabel statusLabel; | |
public MyJFrame(int port, String ipAddress) { //!!!!!!!!!!!!!!!! | |
super(); | |
if (port == 0) { | |
this.port = DEFAULT_PORT; | |
} else { | |
this.port = port; | |
} | |
this.ipAddress = ipAddress; | |
System.out.println("port = " + this.port); | |
System.out.println("ipAddress = " + this.ipAddress); | |
init(); | |
setLocation(400, 200); | |
setSize(600, 600); //!!!!!!!!!!!! | |
setTitle("MyJFrame"); | |
this.addWindowListener(new WindowDestroyer()); | |
Container con = getContentPane(); | |
con.setBackground(Color.WHITE); | |
con.setLayout(new BorderLayout()); | |
/**/ | |
JPanel statusPanel = new JPanel(); | |
statusPanel.setBackground(Color.white); | |
statusLabel = new JLabel( | |
"Status: CONNECTED, waiting for your opponent..."); | |
Font font = new Font("Arial", Font.BOLD, 16); | |
statusLabel.setFont(font); | |
statusPanel.add(statusLabel); | |
con.add(statusPanel, BorderLayout.SOUTH); | |
/**/ | |
JPanel panel = new JPanel(); | |
panel.setLayout(new GridLayout(3, 3, 3, 3)); | |
con.add(panel, BorderLayout.CENTER); | |
Font fontButton | |
= new Font("Arial", Font.BOLD | Font.ITALIC, 20); | |
for (int i = 0; i < button_array.length; i++) { | |
button_array[i] = new JButton(); | |
button_array[i].setActionCommand("" + i); | |
button_array[i].addActionListener(this); | |
button_array[i].setFont(fontButton); ///// | |
panel.add(button_array[i]); | |
} | |
listener = | |
new StreamListener( in, button_array, statusLabel); | |
} | |
private void init() { | |
try { | |
InetAddress addr = InetAddress.getByName(ipAddress); //!!! | |
s = new Socket(addr, this.port); //!!! | |
in = new DataInputStream(s.getInputStream()); | |
out = new DataOutputStream(s.getOutputStream()); | |
} catch (IOException exc) { | |
System.out.println(exc.toString()); | |
} catch (Exception exc) { | |
System.out.println(exc.toString()); | |
} | |
} | |
@Override | |
public void actionPerformed(ActionEvent ae) { | |
String str = ae.getActionCommand(); | |
int index = Integer.parseInt(str); | |
try{ | |
out.writeInt(index); | |
}catch(IOException exc){ | |
System.out.println(exc.toString()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package clientx0; | |
import java.awt.*; | |
import javax.swing.*; //!!!!!!!!!!!!! | |
import java.io.*; | |
public class StreamListener extends Thread { | |
DataInputStream in; | |
JButton buttons[]; //!!!!! | |
JLabel statusLabel; //!!!!! | |
private ImageIcon myIcon; | |
public StreamListener( | |
DataInputStream in, JButton[] buttons, JLabel statusLabel) { | |
this.in = in; | |
this.buttons = buttons; | |
this.statusLabel = statusLabel; | |
this.start(); | |
} | |
@Override | |
public void run() { | |
int box, i; | |
// 0 -18, 254, 255, 101, 253 | |
// 0 - 8 X | |
// 9 - This user has selected a box | |
//253 | |
// 10 - 18 O | |
// 255 User Wins | |
// 254 User Loses | |
/* | |
101 | |
"Status: Another player has connected,"+ | |
please submit your name to start the game"); | |
*/ | |
try { | |
while (true) { | |
box = in.readInt(); | |
System.out.println(box); | |
if (box == 101) { | |
statusLabel.setText("Status: Another player has connected"); | |
// + ", please submit your name to start the game"); | |
} else if (box == 9) { //The server says this is an invaliid box | |
statusLabel.setText( | |
"Status: This square is already occupied, please choose another"); | |
} else if (box == 255) {//Winner | |
for (i = 0; i < 9; i++) { | |
buttons[i].setBackground(Color.green); | |
} | |
statusLabel.setText("WIN!!!"); | |
} else if (box == 254) {//LOSER | |
for (i = 0; i < 9; i++) { | |
buttons[i].setBackground(Color.red); | |
} | |
statusLabel.setText("LOSER!"); | |
} /** | |
* ************************* | |
*/ | |
else if (box == 253) { | |
statusLabel.setText( | |
"Status: This square is already occupied, please choose another"); | |
} else if (box == 252) { | |
statusLabel.setText("Status: Waiting for opponent to move.."); | |
} else if (box == 251) { | |
statusLabel.setText("Status: Opponent moved. Your turn.."); | |
} else if (box == 250) { | |
statusLabel.setText( | |
"Status: Opponent has disconnected. Game stopped."); | |
} | |
/*else if (box == 249) { | |
statusLabel.setText( | |
"Status: Another player has connected, please submit your name to start the game"); | |
} else if (box == 248) { | |
String name = in.readUTF(); | |
statusLabel.setText("Status: Your opponent is " + name); | |
} */ | |
/** | |
* ************************* | |
*/ | |
else if (box > 9 && box < 19) { | |
//buttons[box - 10].setText("O"); | |
myIcon = new ImageIcon("cross.gif"); | |
buttons[box - 10].setIcon(myIcon); | |
} else { | |
//buttons[box].setText("X"); | |
myIcon = new ImageIcon("circle.gif"); | |
buttons[box].setIcon(myIcon); | |
} | |
} | |
} catch (IOException e) { | |
System.out.println(e.toString()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package clientx0; | |
import java.awt.event.*; | |
/** | |
* | |
* @author jshvartsman | |
*/ | |
public class WindowDestroyer extends WindowAdapter { | |
@Override | |
public void windowClosing(WindowEvent e){ | |
System.exit(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment