Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ผู้จัดทำ
- //1: นาย ชยุติ์พง ปฏิเวธธรรม 5510405465
- //2: นาย ชาลี รักตะวัต 5510405503
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.net.Socket;
- import java.util.ArrayList;
- import java.util.Arrays;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- public class Game extends JFrame implements ActionListener {
- private JTextField hostTextField,portTextField,nameTextField;
- private JLabel hostLabel,portLabel,name;
- private JButton joinButton;
- private int JOIN_WEIGHT = 200;
- private int JOIN_HEIGHT = 220;
- private int GAME_WEIGHT = 1000;
- private int GAME_HEIGHT = 500;
- private boolean PanelVisible = true;
- private JFrame player;
- ArrayList<String> NumBer;
- private int PlayerNumber = 1;
- private String FoeName;
- private JLabel MyMove;
- private JLabel FoeMove;
- private int count = 0;
- private int Foe_count = 0;
- private Socket socket ;
- private DataInputStream fromserver ;
- private DataOutputStream toserver;
- private boolean GameEnd = false;
- public Game(){
- CreateJoinPanel();
- }
- public static void main(String[] args) {
- new Game();
- }
- public void CreateJoinPanel(){
- add(joinPanel());
- setTitle("เรียงตัวเลข");
- setSize(JOIN_WEIGHT, JOIN_HEIGHT);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setLocationRelativeTo(null);
- setResizable(true);
- setVisible(true);
- }
- private JPanel joinPanel(){
- JPanel panel = new JPanel();
- panel.setLayout(new GridLayout(0,1));
- hostLabel = new JLabel("Host IP: ");
- hostTextField = new JTextField(10);
- hostTextField.setText("localhost");
- hostTextField.setEditable(true);
- panel.add(hostLabel);
- panel.add(hostTextField);
- portLabel = new JLabel("Port: ");
- portTextField = new JTextField(5);
- portTextField.setText("80");
- portTextField.setEditable(true);
- // panel.add(portLabel);
- // panel.add(portTextField);
- name = new JLabel("Nickname: ");
- nameTextField = new JTextField(7);
- nameTextField.setText("Anonymous");
- panel.add(name);
- panel.add(nameTextField);
- joinButton = new JButton("Join");
- joinButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- connectToServer();
- waiting();
- recieveBoard();
- player = new playersPanel();
- setVisible(false);
- }});
- panel.add(joinButton);
- return panel;
- }
- class playersPanel extends JFrame{
- creategamepanel Mypanel= new creategamepanel();
- creategamepanel Foepanel = new creategamepanel();
- public playersPanel(){
- CreateGamePanel();
- Runnable rmove = new recieveMove();
- Thread trmove = new Thread(rmove);
- trmove.start();
- }
- public void CreateGamePanel(){
- JPanel allpanel = new JPanel(new BorderLayout());
- JPanel infoPanel = new JPanel(new GridLayout(2,2,30,0));
- String Sinfo = " Host Ip = " + hostTextField.getText()+" Port = " + portTextField.getText();
- JLabel Sinfolabel = new JLabel(Sinfo);
- MyMove = new JLabel(Integer.toString(count));
- FoeMove = new JLabel(Integer.toString(Foe_count));
- JLabel MyNamelabel;
- JLabel FoeNamelabel;
- JPanel gamePanel = new JPanel(new GridLayout(1,2,30,20));
- if (PlayerNumber == 1 ){
- Mypanel.BoardNumber = 1 ;
- JPanel panel = new JPanel(new GridLayout(1,2));
- JPanel panel2 = new JPanel(new GridLayout(1,2));
- MyNamelabel = new JLabel(" player 1 " + nameTextField.getText());
- FoeNamelabel = new JLabel("player 2 " + FoeName);
- MyNamelabel.setFont(new Font("Dialog", 1, 15));
- FoeNamelabel.setFont(new Font("Dialog", 1, 15));
- panel.add(MyNamelabel);
- panel.add(MyMove);
- panel2.add(FoeNamelabel);
- panel2.add(FoeMove);
- gamePanel.add(Mypanel);
- gamePanel.add(Foepanel);
- infoPanel.add(panel);
- infoPanel.add(panel2);
- }else if (PlayerNumber == 2 ){
- Mypanel.BoardNumber = 2 ;
- JPanel panel = new JPanel(new GridLayout(1,2));
- JPanel panel2 = new JPanel(new GridLayout(1,2));
- MyNamelabel = new JLabel("player 2 " + nameTextField.getText());
- FoeNamelabel = new JLabel(" player 1 " + FoeName);
- MyNamelabel.setFont(new Font("Dialog", 1, 15));
- FoeNamelabel.setFont(new Font("Dialog", 1, 15));
- panel.add(MyNamelabel);
- panel.add(MyMove);
- panel2.add(FoeNamelabel);
- panel2.add(FoeMove);
- gamePanel.add(Foepanel);
- gamePanel.add(Mypanel);
- infoPanel.add(panel2);
- infoPanel.add(panel);
- }
- infoPanel.add(Sinfolabel);
- allpanel.add(infoPanel,BorderLayout.NORTH);
- allpanel.add(gamePanel,BorderLayout.CENTER);
- add(allpanel);
- setTitle("เรียงตัวเลข");
- setSize(GAME_WEIGHT,GAME_HEIGHT);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setLocationRelativeTo(null);
- setResizable(true);
- setVisible(PanelVisible);
- }
- class creategamepanel extends JPanel implements Cloneable{
- private JButton Button1 = new JButton();
- private JButton Button2 = new JButton();
- private JButton Button3 = new JButton();
- private JButton Button4 = new JButton();
- private JButton Button5 = new JButton();
- private JButton Button6 = new JButton();
- private JButton Button7 = new JButton();
- private JButton Button8 = new JButton();
- private JButton Button9 = new JButton();
- private JButton Button10 = new JButton();
- private JButton Button11 = new JButton();
- private JButton Button12 = new JButton();
- private JButton Button13 = new JButton();
- private JButton Button14 = new JButton();
- private JButton Button15 = new JButton();
- private JButton Button16 = new JButton();
- ArrayList<JButton> buttonList = new ArrayList<JButton>();
- private int BoardNumber = 0;
- public creategamepanel(){
- this.setLayout(new BorderLayout());
- this.add(gamePanel(),BorderLayout.CENTER);
- }
- public JPanel gamePanel(){
- JPanel playerPanel = new JPanel(new GridLayout(4, 4));
- Button1.setFont(new Font("Dialog", 1, 64));
- Button2.setFont(new Font("Dialog", 1, 64));
- Button3.setFont(new Font("Dialog", 1, 64));
- Button4.setFont(new Font("Dialog", 1, 64));
- Button5.setFont(new Font("Dialog", 1, 64));
- Button6.setFont(new Font("Dialog", 1, 64));
- Button7.setFont(new Font("Dialog", 1, 64));
- Button8.setFont(new Font("Dialog", 1, 64));
- Button9.setFont(new Font("Dialog", 1, 64));
- Button10.setFont(new Font("Dialog", 1, 64));
- Button11.setFont(new Font("Dialog", 1, 64));
- Button12.setFont(new Font("Dialog", 1, 64));
- Button13.setFont(new Font("Dialog", 1, 64));
- Button14.setFont(new Font("Dialog", 1, 64));
- Button15.setFont(new Font("Dialog", 1, 64));
- Button16.setFont(new Font("Dialog", 1, 64));
- buttonList.add(Button1);
- buttonList.add(Button2);
- buttonList.add(Button3);
- buttonList.add(Button4);
- buttonList.add(Button5);
- buttonList.add(Button6);
- buttonList.add(Button7);
- buttonList.add(Button8);
- buttonList.add(Button9);
- buttonList.add(Button10);
- buttonList.add(Button11);
- buttonList.add(Button12);
- buttonList.add(Button13);
- buttonList.add(Button14);
- buttonList.add(Button15);
- buttonList.add(Button16);
- Button1.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (PlayerNumber == BoardNumber){
- if (Button2.getText() == null){
- Button2.setText(Button1.getText());
- Button1.setText(null);
- Button1.setBackground(Color.BLACK);
- Button2.setBackground(Color.YELLOW);
- count_move();
- sentMove(1,2);
- }
- else if (Button5.getText() == null){
- Button5.setText(Button1.getText());
- Button1.setText(null);
- Button1.setBackground(Color.BLACK);
- Button5.setBackground(Color.YELLOW);
- count_move();
- sentMove(1,5);
- }
- check();
- }
- }});
- Button2.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (PlayerNumber == BoardNumber){
- if (Button3.getText() == null){
- Button3.setText(Button2.getText());
- Button2.setText(null);
- Button2.setBackground(Color.BLACK);
- Button3.setBackground(Color.YELLOW);
- sentMove(2,3);
- count_move();
- }
- else if (Button1.getText() == null){
- Button1.setText(Button2.getText());
- Button2.setText(null);
- Button2.setBackground(Color.BLACK);
- Button1.setBackground(Color.YELLOW);
- count_move();
- sentMove(2,1);
- }
- else if (Button6.getText() == null){
- Button6.setText(Button2.getText());
- Button2.setText(null);
- Button2.setBackground(Color.BLACK);
- Button6.setBackground(Color.YELLOW);
- count_move();
- sentMove(2,6);
- }
- check();
- }}
- });
- Button3.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (PlayerNumber == BoardNumber){
- if (Button2.getText() == null){
- Button2.setText(Button3.getText());
- Button3.setText(null);
- Button3.setBackground(Color.BLACK);
- Button2.setBackground(Color.YELLOW);
- count_move();
- sentMove(3,2);
- }
- else if (Button4.getText() == null){
- Button4.setText(Button3.getText());
- Button3.setText(null);
- Button3.setBackground(Color.BLACK);
- Button4.setBackground(Color.YELLOW);
- count_move();
- sentMove(3,4);
- }
- else if (Button7.getText() == null){
- Button7.setText(Button3.getText());
- Button3.setText(null);
- Button3.setBackground(Color.BLACK);
- Button7.setBackground(Color.YELLOW);
- count_move();
- sentMove(3,7);
- }
- check();
- }}
- });
- Button4.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (PlayerNumber == BoardNumber){
- if (Button3.getText() == null){
- Button3.setText(Button4.getText());
- Button4.setText(null);
- Button4.setBackground(Color.BLACK);
- Button3.setBackground(Color.YELLOW);
- count_move();
- sentMove(4,3);
- }
- else if (Button8.getText() == null){
- Button8.setText(Button4.getText());
- Button4.setText(null);
- Button4.setBackground(Color.BLACK);
- Button8.setBackground(Color.YELLOW);
- count_move();
- sentMove(4,8);
- }
- check();
- }}
- });
- Button5.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (PlayerNumber == BoardNumber){
- if (Button1.getText() == null){
- Button1.setText(Button5.getText());
- Button5.setText(null);
- Button5.setBackground(Color.BLACK);
- Button1.setBackground(Color.YELLOW);
- count_move();
- sentMove(5,1);
- }
- else if (Button6.getText() == null){
- Button6.setText(Button5.getText());
- Button5.setText(null);
- Button5.setBackground(Color.BLACK);
- Button6.setBackground(Color.YELLOW);
- count_move();
- sentMove(5,6);
- }
- else if (Button9.getText() == null){
- Button9.setText(Button5.getText());
- Button5.setText(null);
- Button5.setBackground(Color.BLACK);
- Button9.setBackground(Color.YELLOW);
- count_move();
- sentMove(5,9);
- }
- check();
- }}
- });
- Button6.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (PlayerNumber == BoardNumber){
- if (Button2.getText() == null){
- Button2.setText(Button6.getText());
- Button6.setText(null);
- Button6.setBackground(Color.BLACK);
- Button2.setBackground(Color.YELLOW);
- count_move();
- sentMove(6,2);
- }
- else if (Button5.getText() == null){
- Button5.setText(Button6.getText());
- Button6.setText(null);
- Button6.setBackground(Color.BLACK);
- Button5.setBackground(Color.YELLOW);
- count_move();
- sentMove(6,5);
- }
- else if (Button7.getText() == null){
- Button7.setText(Button6.getText());
- Button6.setText(null);
- Button6.setBackground(Color.BLACK);
- Button7.setBackground(Color.YELLOW);
- count_move();
- sentMove(6,7);
- }
- else if (Button10.getText() == null){
- Button10.setText(Button6.getText());
- Button6.setText(null);
- Button6.setBackground(Color.BLACK);
- Button10.setBackground(Color.YELLOW);
- count_move();
- sentMove(6,10);
- }
- check();
- }}
- });
- Button7.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (PlayerNumber == BoardNumber){
- if (Button3.getText() == null){
- Button3.setText(Button7.getText());
- Button7.setText(null);
- Button7.setBackground(Color.BLACK);
- Button3.setBackground(Color.YELLOW);
- count_move();
- sentMove(7,3);
- }
- else if (Button6.getText() == null){
- Button6.setText(Button7.getText());
- Button7.setText(null);
- Button7.setBackground(Color.BLACK);
- Button6.setBackground(Color.YELLOW);
- count_move();
- sentMove(7,6);
- }
- else if (Button8.getText() == null){
- Button8.setText(Button7.getText());
- Button7.setText(null);
- Button7.setBackground(Color.BLACK);
- Button8.setBackground(Color.YELLOW);
- count_move();
- sentMove(7,8);
- }
- else if (Button11.getText() == null){
- Button11.setText(Button7.getText());
- Button7.setText(null);
- Button7.setBackground(Color.BLACK);
- Button11.setBackground(Color.YELLOW);
- count_move();
- sentMove(7,11);
- }
- check();
- }}
- });
- Button8.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (PlayerNumber == BoardNumber){
- if (Button4.getText() == null){
- Button4.setText(Button8.getText());
- Button8.setText(null);
- Button8.setBackground(Color.BLACK);
- Button4.setBackground(Color.YELLOW);
- count_move();
- sentMove(8,4);
- }
- else if (Button7.getText() == null){
- Button7.setText(Button8.getText());
- Button8.setText(null);
- Button8.setBackground(Color.BLACK);
- Button7.setBackground(Color.YELLOW);
- count_move();
- sentMove(8,7);
- }
- else if (Button12.getText() == null){
- Button12.setText(Button8.getText());
- Button8.setText(null);
- Button8.setBackground(Color.BLACK);
- Button12.setBackground(Color.YELLOW);
- count_move();
- sentMove(8,12);
- }
- check();
- }}
- });
- Button9.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (PlayerNumber == BoardNumber){
- if (Button5.getText() == null){
- Button5.setText(Button9.getText());
- Button9.setText(null);
- Button9.setBackground(Color.BLACK);
- Button5.setBackground(Color.YELLOW);
- count_move();
- sentMove(9,5);
- }
- else if (Button10.getText() == null){
- Button10.setText(Button9.getText());
- Button9.setText(null);
- Button9.setBackground(Color.BLACK);
- Button10.setBackground(Color.YELLOW);
- count_move();
- sentMove(9,10);
- }
- else if (Button13.getText() == null){
- Button13.setText(Button9.getText());
- Button9.setText(null);
- Button9.setBackground(Color.BLACK);
- Button13.setBackground(Color.YELLOW);
- count_move();
- sentMove(9,13);
- }
- check();
- }}
- });
- Button10.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (PlayerNumber == BoardNumber){
- if (Button6.getText() == null){
- Button6.setText(Button10.getText());
- Button10.setText(null);
- Button10.setBackground(Color.BLACK);
- Button6.setBackground(Color.YELLOW);
- count_move();
- sentMove(10,6);
- }
- else if (Button9.getText() == null){
- Button9.setText(Button10.getText());
- Button10.setText(null);
- Button10.setBackground(Color.BLACK);
- Button9.setBackground(Color.YELLOW);
- count_move();
- sentMove(10,9);
- }
- else if (Button11.getText() == null){
- Button11.setText(Button10.getText());
- Button10.setText(null);
- Button10.setBackground(Color.BLACK);
- Button11.setBackground(Color.YELLOW);
- count_move();
- sentMove(10,11);
- }
- else if (Button14.getText() == null){
- Button14.setText(Button10.getText());
- Button10.setText(null);
- Button10.setBackground(Color.BLACK);
- Button14.setBackground(Color.YELLOW);
- count_move();
- sentMove(10,14);
- }
- check();
- }}
- });
- Button11.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (PlayerNumber == BoardNumber){
- if (Button7.getText() == null){
- Button7.setText(Button11.getText());
- Button11.setText(null);
- Button11.setBackground(Color.BLACK);
- Button7.setBackground(Color.YELLOW);
- count_move();
- sentMove(11,7);
- }
- else if (Button10.getText() == null){
- Button10.setText(Button11.getText());
- Button11.setText(null);
- Button11.setBackground(Color.BLACK);
- Button10.setBackground(Color.YELLOW);
- count_move();
- sentMove(11,10);
- }
- else if (Button12.getText() == null){
- Button12.setText(Button11.getText());
- Button11.setText(null);
- Button11.setBackground(Color.BLACK);
- Button12.setBackground(Color.YELLOW);
- count_move();
- sentMove(11,12);
- }
- else if (Button15.getText() == null){
- Button15.setText(Button11.getText());
- Button11.setText(null);
- Button11.setBackground(Color.BLACK);
- Button15.setBackground(Color.YELLOW);
- count_move();
- sentMove(11,15);
- }
- check();
- }}
- });
- Button12.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (PlayerNumber == BoardNumber){
- if (Button8.getText() == null){
- Button8.setText(Button12.getText());
- Button12.setText(null);
- Button12.setBackground(Color.BLACK);
- Button8.setBackground(Color.YELLOW);
- count_move();
- sentMove(12,8);
- }
- else if (Button11.getText() == null){
- Button11.setText(Button12.getText());
- Button12.setText(null);
- Button12.setBackground(Color.BLACK);
- Button11.setBackground(Color.YELLOW);
- count_move();
- sentMove(12,11);
- }
- else if (Button16.getText() == null){
- Button16.setText(Button12.getText());
- Button12.setText(null);
- Button12.setBackground(Color.BLACK);
- Button16.setBackground(Color.YELLOW);
- count_move();
- sentMove(12,16);
- }
- check();
- }}
- });
- Button13.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (PlayerNumber == BoardNumber){
- if (Button9.getText() == null){
- Button9.setText(Button13.getText());
- Button13.setText(null);
- Button13.setBackground(Color.BLACK);
- Button9.setBackground(Color.YELLOW);
- count_move();
- sentMove(13,9);
- }
- else if (Button14.getText() == null){
- Button14.setText(Button13.getText());
- Button13.setText(null);
- Button13.setBackground(Color.BLACK);
- Button14.setBackground(Color.YELLOW);
- count_move();
- sentMove(13,14);
- }
- check();
- }}
- });
- Button14.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (PlayerNumber == BoardNumber){
- if (Button10.getText() == null){
- Button10.setText(Button14.getText());
- Button14.setText(null);
- Button14.setBackground(Color.BLACK);
- Button10.setBackground(Color.YELLOW);
- count_move();
- sentMove(14,10);
- }
- else if (Button13.getText() == null){
- Button13.setText(Button14.getText());
- Button14.setText(null);
- Button14.setBackground(Color.BLACK);
- Button13.setBackground(Color.YELLOW);
- count_move();
- sentMove(14,13);
- }
- else if (Button15.getText() == null){
- Button15.setText(Button14.getText());
- Button14.setText(null);
- Button14.setBackground(Color.BLACK);
- Button15.setBackground(Color.YELLOW);
- count_move();
- sentMove(14,15);
- }
- check();
- }}
- });
- Button15.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (PlayerNumber == BoardNumber){
- if (Button11.getText() == null){
- Button11.setText(Button15.getText());
- Button15.setText(null);
- Button15.setBackground(Color.BLACK);
- Button11.setBackground(Color.YELLOW);
- count_move();
- sentMove(15,11);
- }
- else if (Button14.getText() == null){
- Button14.setText(Button15.getText());
- Button15.setText(null);
- Button15.setBackground(Color.BLACK);
- Button14.setBackground(Color.YELLOW);
- count_move();
- sentMove(15,14);
- }
- else if (Button16.getText() == null){
- Button16.setText(Button15.getText());
- Button15.setText(null);
- Button15.setBackground(Color.BLACK);
- Button16.setBackground(Color.YELLOW);
- count_move();
- sentMove(15,16);
- }
- check();
- }}
- });
- Button16.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (PlayerNumber == BoardNumber){
- if (Button12.getText() == null){
- Button12.setText(Button16.getText());
- Button16.setText(null);
- Button16.setBackground(Color.BLACK);
- Button12.setBackground(Color.YELLOW);
- count_move();
- sentMove(16,12);
- }
- else if (Button15.getText() == null){
- Button15.setText(Button16.getText());
- Button16.setText(null);
- Button16.setBackground(Color.BLACK);
- Button15.setBackground(Color.YELLOW);
- count_move();
- sentMove(16,15);
- }
- check();
- }}
- });
- for(int i = 0;i < NumBer.size(); i++){
- if (NumBer.get(i).equals("0")){
- buttonList.get(i).setText(null);
- playerPanel.add(buttonList.get(i));
- buttonList.get(i).setBackground(Color.BLACK);
- }else{
- buttonList.get(i).setText(NumBer.get(i));
- playerPanel.add(buttonList.get(i));
- buttonList.get(i).setBackground(Color.YELLOW);
- }
- }
- return playerPanel;
- }
- public void check(){
- if (Integer.parseInt(Button1.getText()) == 1 && Integer.parseInt(Button2.getText()) == 2 && Integer.parseInt(Button3.getText()) == 3 &&
- Integer.parseInt(Button4.getText()) == 4 && Integer.parseInt(Button5.getText()) == 5 && Integer.parseInt(Button6.getText()) == 6 &&
- Integer.parseInt(Button7.getText()) == 7 && Integer.parseInt(Button8.getText()) == 8 && Integer.parseInt(Button9.getText()) == 9 &&
- Integer.parseInt(Button10.getText()) == 10 && Integer.parseInt(Button11.getText()) == 11 && Integer.parseInt(Button12.getText()) == 12 &&
- Integer.parseInt(Button13.getText()) == 13 && Integer.parseInt(Button14.getText()) == 14 && Integer.parseInt(Button15.getText()) == 15 ){
- JOptionPane.showMessageDialog(null, "You WiN!!!");
- try {
- toserver = new DataOutputStream(socket.getOutputStream());
- toserver.writeInt(11111);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- player.setVisible(false);
- }
- }
- public void sentMove(int Sbutton ,int Cbutton){
- try {
- toserver = new DataOutputStream(socket.getOutputStream());
- toserver.writeInt(Sbutton);
- toserver.writeInt(Cbutton);
- } catch (Exception e1) {
- }
- }
- }
- public class recieveMove implements Runnable{
- public recieveMove(){
- }
- @Override
- public void run(){
- try {
- DataInputStream fromserver = new DataInputStream(socket.getInputStream());
- while (GameEnd == false){
- int b = fromserver.readInt();
- if (b == 11111){
- GameEnd = true;
- JOptionPane.showMessageDialog(null, "You Lose!!!");
- fromserver.close();
- toserver.close();
- socket.close();
- System.exit(0);
- }else{
- int i = fromserver.readInt();
- move(b , i);
- }
- count_foe_move();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void move(int Sbutton ,int Cbutton){
- Foepanel.buttonList.get(Cbutton-1).setText(Foepanel.buttonList.get(Sbutton-1).getText());
- Foepanel.buttonList.get(Sbutton-1).setText(null);
- Foepanel.buttonList.get(Sbutton-1).setBackground(Color.BLACK);
- Foepanel.buttonList.get(Cbutton-1).setBackground(Color.YELLOW);
- repaint();
- }
- }
- }
- private void connectToServer() {
- try {
- socket = new Socket(hostTextField.getText(), Integer.parseInt(portTextField.getText()));
- fromserver = new DataInputStream(socket.getInputStream());
- toserver = new DataOutputStream(socket.getOutputStream());
- // sent player Name
- toserver.writeUTF(nameTextField.getText());
- // reciece player Number
- PlayerNumber = fromserver.readInt();
- JOptionPane.showMessageDialog(null, "You join as player"+PlayerNumber );
- }
- catch (Exception ex) {
- JOptionPane.showMessageDialog(null ,"Server is offline");
- System.exit(0);
- }
- }
- private void waiting(){
- try {
- if (fromserver.readBoolean() == true){
- // reciece foeName
- FoeName = fromserver.readUTF();
- }
- } catch (IOException e1) {
- JOptionPane.showMessageDialog(null, "waiting for player2" );
- }
- }
- private void recieveBoard(){
- NumBer = new ArrayList<String>();
- try{
- fromserver = new DataInputStream(socket.getInputStream());
- // int k = fromserver.readInt();
- for (int i = 0 ; i < 16; i++){
- NumBer.add(fromserver.readUTF());
- }
- }catch (Exception ex){
- JOptionPane.showMessageDialog(null, "Recieve Board ERROR" );
- System.exit(0);
- }
- }
- public void count_move(){
- count++;
- MyMove.setText(Integer.toString(count));
- }
- public void count_foe_move(){
- Foe_count++;
- FoeMove.setText(Integer.toString(Foe_count));
- }
- @Override
- public void actionPerformed(ActionEvent arg0) {
- // TODO Auto-generated method stub
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement