Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 26.60 KB | None | 0 0
  1. //ผู้จัดทำ
  2. //1:  นาย     ชยุติ์พง    ปฏิเวธธรรม      5510405465
  3. //2:  นาย     ชาลี        รักตะวัต        5510405503
  4.  
  5.  
  6.  
  7. import java.awt.BorderLayout;
  8. import java.awt.Color;
  9. import java.awt.Font;
  10. import java.awt.GridLayout;
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13. import java.io.DataInputStream;
  14. import java.io.DataOutputStream;
  15. import java.io.IOException;
  16. import java.net.Socket;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19.  
  20.  
  21. import javax.swing.JButton;
  22. import javax.swing.JFrame;
  23. import javax.swing.JLabel;
  24. import javax.swing.JOptionPane;
  25. import javax.swing.JPanel;
  26. import javax.swing.JTextField;
  27.  
  28.  
  29.  
  30.  
  31. public class Game extends JFrame implements  ActionListener {
  32.  
  33.     private JTextField hostTextField,portTextField,nameTextField;
  34.     private JLabel hostLabel,portLabel,name;
  35.    
  36.     private JButton joinButton;
  37.     private int JOIN_WEIGHT = 200;
  38.     private int JOIN_HEIGHT = 220;
  39.     private int GAME_WEIGHT = 1000;
  40.     private int GAME_HEIGHT = 500;
  41.     private boolean PanelVisible = true;
  42.     private JFrame player;
  43.  
  44.     ArrayList<String> NumBer;
  45.    
  46.     private int PlayerNumber = 1;
  47.     private String FoeName;
  48.     private JLabel MyMove;
  49.     private JLabel FoeMove;
  50.     private int count = 0;
  51.     private int Foe_count = 0;
  52.    
  53.     private Socket socket ;
  54.     private DataInputStream fromserver ;
  55.     private DataOutputStream toserver;
  56.    
  57.  
  58.     private boolean GameEnd = false;
  59.    
  60.  
  61.    
  62.    
  63.  
  64.     public Game(){
  65.         CreateJoinPanel();
  66.     }
  67.  
  68.  
  69.     public static void main(String[] args) {
  70.         new Game();
  71.  
  72.     }
  73.    
  74.     public void CreateJoinPanel(){
  75.         add(joinPanel());
  76.         setTitle("เรียงตัวเลข");
  77.         setSize(JOIN_WEIGHT, JOIN_HEIGHT);
  78.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  79.         setLocationRelativeTo(null);
  80.         setResizable(true);
  81.         setVisible(true);
  82. }
  83.  
  84. private JPanel joinPanel(){
  85.         JPanel panel = new JPanel();
  86.         panel.setLayout(new GridLayout(0,1));
  87.         hostLabel = new JLabel("Host IP: ");
  88.         hostTextField = new JTextField(10);
  89.         hostTextField.setText("localhost");
  90.         hostTextField.setEditable(true);
  91.         panel.add(hostLabel);
  92.         panel.add(hostTextField);
  93.  
  94.         portLabel = new JLabel("Port: ");
  95.         portTextField = new JTextField(5);
  96.         portTextField.setText("80");
  97.         portTextField.setEditable(true);
  98. //        panel.add(portLabel);
  99. //        panel.add(portTextField);
  100.  
  101.         name = new JLabel("Nickname: ");
  102.         nameTextField = new JTextField(7);
  103.         nameTextField.setText("Anonymous");
  104.         panel.add(name);
  105.         panel.add(nameTextField);
  106.  
  107.  
  108.         joinButton = new JButton("Join");
  109.         joinButton.addActionListener(new ActionListener() {
  110.             @Override
  111.             public void actionPerformed(ActionEvent e) {
  112.                 connectToServer();
  113.                 waiting();
  114.                 recieveBoard();
  115.                 player = new playersPanel();
  116.                 setVisible(false);
  117.            
  118.             }});
  119.  
  120.         panel.add(joinButton);
  121.  
  122.         return panel;
  123.  
  124.  
  125. }
  126.  
  127.  
  128.  
  129. class playersPanel extends JFrame{
  130.    
  131.     creategamepanel Mypanel= new creategamepanel();
  132.     creategamepanel Foepanel = new creategamepanel();
  133.    
  134.     public playersPanel(){
  135.         CreateGamePanel();
  136.         Runnable rmove = new recieveMove();
  137.         Thread trmove = new Thread(rmove);
  138.         trmove.start();
  139.    
  140.        
  141.     }
  142.    
  143.    
  144.     public void CreateGamePanel(){
  145.         JPanel allpanel = new JPanel(new BorderLayout());
  146.         JPanel infoPanel = new JPanel(new GridLayout(2,2,30,0));
  147.         String Sinfo = "  Host Ip  =  " + hostTextField.getText()+"    Port =  " + portTextField.getText();
  148.         JLabel Sinfolabel = new JLabel(Sinfo);
  149.         MyMove = new JLabel(Integer.toString(count));
  150.         FoeMove = new JLabel(Integer.toString(Foe_count));
  151.        
  152.        
  153.         JLabel MyNamelabel;
  154.         JLabel FoeNamelabel;
  155.        
  156.         JPanel gamePanel = new JPanel(new GridLayout(1,2,30,20));
  157.         if (PlayerNumber == 1 ){
  158.             Mypanel.BoardNumber = 1 ;
  159.             JPanel panel = new JPanel(new GridLayout(1,2));
  160.             JPanel panel2 = new JPanel(new GridLayout(1,2));
  161.             MyNamelabel = new JLabel("  player 1  " + nameTextField.getText());
  162.             FoeNamelabel = new JLabel("player 2  " + FoeName);
  163.             MyNamelabel.setFont(new Font("Dialog", 1, 15));
  164.             FoeNamelabel.setFont(new Font("Dialog", 1, 15));
  165.             panel.add(MyNamelabel);
  166.             panel.add(MyMove);
  167.             panel2.add(FoeNamelabel);
  168.             panel2.add(FoeMove);
  169.             gamePanel.add(Mypanel);
  170.             gamePanel.add(Foepanel);
  171.             infoPanel.add(panel);
  172.             infoPanel.add(panel2);
  173.  
  174.            
  175.         }else if (PlayerNumber == 2 ){
  176.             Mypanel.BoardNumber = 2 ;
  177.             JPanel panel = new JPanel(new GridLayout(1,2));
  178.             JPanel panel2 = new JPanel(new GridLayout(1,2));
  179.             MyNamelabel = new JLabel("player 2  " + nameTextField.getText());
  180.             FoeNamelabel = new JLabel("  player 1  " + FoeName);
  181.             MyNamelabel.setFont(new Font("Dialog", 1, 15));
  182.             FoeNamelabel.setFont(new Font("Dialog", 1, 15));
  183.             panel.add(MyNamelabel);
  184.             panel.add(MyMove);
  185.             panel2.add(FoeNamelabel);
  186.             panel2.add(FoeMove);
  187.             gamePanel.add(Foepanel);
  188.             gamePanel.add(Mypanel);
  189.             infoPanel.add(panel2);
  190.             infoPanel.add(panel);
  191.  
  192.            
  193.         }
  194.        
  195.        
  196.         infoPanel.add(Sinfolabel);
  197.         allpanel.add(infoPanel,BorderLayout.NORTH);
  198.         allpanel.add(gamePanel,BorderLayout.CENTER);
  199.  
  200.  
  201.         add(allpanel);
  202.         setTitle("เรียงตัวเลข");
  203.         setSize(GAME_WEIGHT,GAME_HEIGHT);
  204.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  205.         setLocationRelativeTo(null);
  206.         setResizable(true);
  207.         setVisible(PanelVisible);
  208.     }
  209.  
  210.    
  211. class creategamepanel extends JPanel implements Cloneable{
  212.     private JButton Button1 = new JButton();
  213.     private JButton Button2 = new JButton();
  214.     private JButton Button3 = new JButton();
  215.     private JButton Button4 = new JButton();
  216.     private JButton Button5 = new JButton();
  217.     private JButton Button6 = new JButton();
  218.     private JButton Button7 = new JButton();
  219.     private JButton Button8 = new JButton();
  220.     private JButton Button9 = new JButton();
  221.     private JButton Button10 = new JButton();
  222.     private JButton Button11 = new JButton();
  223.     private JButton Button12 = new JButton();
  224.     private JButton Button13 = new JButton();
  225.     private JButton Button14 = new JButton();
  226.     private JButton Button15 = new JButton();
  227.     private JButton Button16 = new JButton();
  228.     ArrayList<JButton> buttonList = new ArrayList<JButton>();
  229.    
  230.    
  231.     private int BoardNumber = 0;
  232.    
  233.     public creategamepanel(){
  234.         this.setLayout(new BorderLayout());
  235.         this.add(gamePanel(),BorderLayout.CENTER);
  236.        
  237.     }
  238.    
  239.  
  240.     public JPanel gamePanel(){
  241.         JPanel playerPanel = new JPanel(new GridLayout(4, 4));
  242.         Button1.setFont(new Font("Dialog", 1, 64));
  243.         Button2.setFont(new Font("Dialog", 1, 64));
  244.         Button3.setFont(new Font("Dialog", 1, 64));
  245.         Button4.setFont(new Font("Dialog", 1, 64));
  246.         Button5.setFont(new Font("Dialog", 1, 64));
  247.         Button6.setFont(new Font("Dialog", 1, 64));
  248.         Button7.setFont(new Font("Dialog", 1, 64));
  249.         Button8.setFont(new Font("Dialog", 1, 64));
  250.         Button9.setFont(new Font("Dialog", 1, 64));
  251.         Button10.setFont(new Font("Dialog", 1, 64));
  252.         Button11.setFont(new Font("Dialog", 1, 64));
  253.         Button12.setFont(new Font("Dialog", 1, 64));
  254.         Button13.setFont(new Font("Dialog", 1, 64));
  255.         Button14.setFont(new Font("Dialog", 1, 64));
  256.         Button15.setFont(new Font("Dialog", 1, 64));
  257.         Button16.setFont(new Font("Dialog", 1, 64));
  258.         buttonList.add(Button1);
  259.         buttonList.add(Button2);
  260.         buttonList.add(Button3);
  261.         buttonList.add(Button4);
  262.         buttonList.add(Button5);
  263.         buttonList.add(Button6);
  264.         buttonList.add(Button7);
  265.         buttonList.add(Button8);
  266.         buttonList.add(Button9);
  267.         buttonList.add(Button10);
  268.         buttonList.add(Button11);
  269.         buttonList.add(Button12);
  270.         buttonList.add(Button13);
  271.         buttonList.add(Button14);
  272.         buttonList.add(Button15);
  273.         buttonList.add(Button16);
  274.        
  275.        
  276.        
  277.         Button1.addActionListener(new ActionListener() {
  278.             @Override
  279.             public void actionPerformed(ActionEvent e) {
  280.                 if (PlayerNumber == BoardNumber){
  281.                    
  282.                 if (Button2.getText() == null){
  283.                     Button2.setText(Button1.getText());
  284.                     Button1.setText(null);
  285.                     Button1.setBackground(Color.BLACK);
  286.                     Button2.setBackground(Color.YELLOW);
  287.                     count_move();
  288.                     sentMove(1,2);
  289.                    
  290.                 }
  291.                 else if (Button5.getText() == null){
  292.                     Button5.setText(Button1.getText());
  293.                     Button1.setText(null);
  294.                     Button1.setBackground(Color.BLACK);
  295.                     Button5.setBackground(Color.YELLOW);
  296.                     count_move();
  297.                     sentMove(1,5);
  298.                 }
  299.                 check();
  300.             }
  301.         }});
  302.  
  303.         Button2.addActionListener(new ActionListener() {
  304.             @Override
  305.             public void actionPerformed(ActionEvent e) {
  306.                 if (PlayerNumber == BoardNumber){
  307.                    
  308.                 if (Button3.getText() == null){
  309.                     Button3.setText(Button2.getText());
  310.                     Button2.setText(null);
  311.                     Button2.setBackground(Color.BLACK);
  312.                     Button3.setBackground(Color.YELLOW);
  313.                     sentMove(2,3);
  314.                     count_move();
  315.                    
  316.                 }
  317.                 else if (Button1.getText() == null){
  318.                     Button1.setText(Button2.getText());
  319.                     Button2.setText(null);
  320.                     Button2.setBackground(Color.BLACK);
  321.                     Button1.setBackground(Color.YELLOW);
  322.                     count_move();
  323.                     sentMove(2,1);
  324.                 }
  325.                 else if (Button6.getText() == null){
  326.                     Button6.setText(Button2.getText());
  327.                     Button2.setText(null);
  328.                     Button2.setBackground(Color.BLACK);
  329.                     Button6.setBackground(Color.YELLOW);
  330.                     count_move();
  331.                     sentMove(2,6);
  332.                 }
  333.                 check();
  334.             }}
  335.         });
  336.  
  337.         Button3.addActionListener(new ActionListener() {
  338.             @Override
  339.             public void actionPerformed(ActionEvent e) {
  340.                 if (PlayerNumber == BoardNumber){
  341.                    
  342.                
  343.                 if (Button2.getText() == null){
  344.                     Button2.setText(Button3.getText());
  345.                     Button3.setText(null);
  346.                     Button3.setBackground(Color.BLACK);
  347.                     Button2.setBackground(Color.YELLOW);
  348.                     count_move();
  349.                     sentMove(3,2);
  350.                 }
  351.                 else if (Button4.getText() == null){
  352.                     Button4.setText(Button3.getText());
  353.                     Button3.setText(null);
  354.                     Button3.setBackground(Color.BLACK);
  355.                     Button4.setBackground(Color.YELLOW);
  356.                     count_move();
  357.                     sentMove(3,4);
  358.                 }
  359.                 else if (Button7.getText() == null){
  360.                     Button7.setText(Button3.getText());
  361.                     Button3.setText(null);
  362.                     Button3.setBackground(Color.BLACK);
  363.                     Button7.setBackground(Color.YELLOW);
  364.                     count_move();
  365.                     sentMove(3,7);
  366.                 }
  367.                 check();
  368.             }}
  369.         });
  370.  
  371.         Button4.addActionListener(new ActionListener() {
  372.             @Override
  373.             public void actionPerformed(ActionEvent e) {
  374.                 if (PlayerNumber == BoardNumber){
  375.                 if (Button3.getText() == null){
  376.                     Button3.setText(Button4.getText());
  377.                     Button4.setText(null);
  378.                     Button4.setBackground(Color.BLACK);
  379.                     Button3.setBackground(Color.YELLOW);
  380.                     count_move();
  381.                     sentMove(4,3);
  382.                 }
  383.                 else if (Button8.getText() == null){
  384.                     Button8.setText(Button4.getText());
  385.                     Button4.setText(null);
  386.                     Button4.setBackground(Color.BLACK);
  387.                     Button8.setBackground(Color.YELLOW);
  388.                     count_move();
  389.                     sentMove(4,8);
  390.                 }
  391.                 check();
  392.             }}
  393.         });
  394.  
  395.         Button5.addActionListener(new ActionListener() {
  396.             @Override
  397.             public void actionPerformed(ActionEvent e) {
  398.                 if (PlayerNumber == BoardNumber){
  399.                 if (Button1.getText() == null){
  400.                     Button1.setText(Button5.getText());
  401.                     Button5.setText(null);
  402.                     Button5.setBackground(Color.BLACK);
  403.                     Button1.setBackground(Color.YELLOW);
  404.                     count_move();
  405.                     sentMove(5,1);
  406.                 }
  407.                 else if (Button6.getText() == null){
  408.                     Button6.setText(Button5.getText());
  409.                     Button5.setText(null);
  410.                     Button5.setBackground(Color.BLACK);
  411.                     Button6.setBackground(Color.YELLOW);
  412.                     count_move();
  413.                     sentMove(5,6);
  414.                 }
  415.                 else if (Button9.getText() == null){
  416.                     Button9.setText(Button5.getText());
  417.                     Button5.setText(null);
  418.                     Button5.setBackground(Color.BLACK);
  419.                     Button9.setBackground(Color.YELLOW);
  420.                     count_move();
  421.                     sentMove(5,9);
  422.                 }
  423.                 check();
  424.             }}
  425.         });
  426.  
  427.         Button6.addActionListener(new ActionListener() {
  428.             @Override
  429.             public void actionPerformed(ActionEvent e) {
  430.                 if (PlayerNumber == BoardNumber){
  431.                 if (Button2.getText() == null){
  432.                     Button2.setText(Button6.getText());
  433.                     Button6.setText(null);
  434.                     Button6.setBackground(Color.BLACK);
  435.                     Button2.setBackground(Color.YELLOW);
  436.                     count_move();
  437.                     sentMove(6,2);
  438.                 }
  439.                 else if (Button5.getText() == null){
  440.                     Button5.setText(Button6.getText());
  441.                     Button6.setText(null);
  442.                     Button6.setBackground(Color.BLACK);
  443.                     Button5.setBackground(Color.YELLOW);
  444.                     count_move();
  445.                     sentMove(6,5);
  446.                 }
  447.                 else if (Button7.getText() == null){
  448.                     Button7.setText(Button6.getText());
  449.                     Button6.setText(null);
  450.                     Button6.setBackground(Color.BLACK);
  451.                     Button7.setBackground(Color.YELLOW);
  452.                     count_move();
  453.                     sentMove(6,7);
  454.                 }
  455.                 else if (Button10.getText() == null){
  456.                     Button10.setText(Button6.getText());
  457.                     Button6.setText(null);
  458.                     Button6.setBackground(Color.BLACK);
  459.                     Button10.setBackground(Color.YELLOW);
  460.                     count_move();
  461.                     sentMove(6,10);
  462.                 }
  463.                 check();
  464.             }}
  465.         });
  466.  
  467.         Button7.addActionListener(new ActionListener() {
  468.             @Override
  469.             public void actionPerformed(ActionEvent e) {
  470.                 if (PlayerNumber == BoardNumber){
  471.                 if (Button3.getText() == null){
  472.                     Button3.setText(Button7.getText());
  473.                     Button7.setText(null);
  474.                     Button7.setBackground(Color.BLACK);
  475.                     Button3.setBackground(Color.YELLOW);
  476.                     count_move();
  477.                     sentMove(7,3);
  478.                 }
  479.                 else if (Button6.getText() == null){
  480.                     Button6.setText(Button7.getText());
  481.                     Button7.setText(null);
  482.                     Button7.setBackground(Color.BLACK);
  483.                     Button6.setBackground(Color.YELLOW);
  484.                     count_move();
  485.                     sentMove(7,6);
  486.                 }
  487.                 else if (Button8.getText() == null){
  488.                     Button8.setText(Button7.getText());
  489.                     Button7.setText(null);
  490.                     Button7.setBackground(Color.BLACK);
  491.                     Button8.setBackground(Color.YELLOW);
  492.                     count_move();
  493.                     sentMove(7,8);
  494.                 }
  495.                 else if (Button11.getText() == null){
  496.                     Button11.setText(Button7.getText());
  497.                     Button7.setText(null);
  498.                     Button7.setBackground(Color.BLACK);
  499.                     Button11.setBackground(Color.YELLOW);
  500.                     count_move();
  501.                     sentMove(7,11);
  502.                 }
  503.                 check();
  504.             }}
  505.         });
  506.  
  507.         Button8.addActionListener(new ActionListener() {
  508.             @Override
  509.             public void actionPerformed(ActionEvent e) {
  510.                 if (PlayerNumber == BoardNumber){
  511.                 if (Button4.getText() == null){
  512.                     Button4.setText(Button8.getText());
  513.                     Button8.setText(null);
  514.                     Button8.setBackground(Color.BLACK);
  515.                     Button4.setBackground(Color.YELLOW);
  516.                     count_move();
  517.                     sentMove(8,4);
  518.                 }
  519.                 else if (Button7.getText() == null){
  520.                     Button7.setText(Button8.getText());
  521.                     Button8.setText(null);
  522.                     Button8.setBackground(Color.BLACK);
  523.                     Button7.setBackground(Color.YELLOW);
  524.                     count_move();
  525.                     sentMove(8,7);
  526.                 }
  527.                 else if (Button12.getText() == null){
  528.                     Button12.setText(Button8.getText());
  529.                     Button8.setText(null);
  530.                     Button8.setBackground(Color.BLACK);
  531.                     Button12.setBackground(Color.YELLOW);
  532.                     count_move();
  533.                     sentMove(8,12);
  534.                 }
  535.                 check();
  536.             }}
  537.         });
  538.  
  539.         Button9.addActionListener(new ActionListener() {
  540.             @Override
  541.             public void actionPerformed(ActionEvent e) {
  542.                 if (PlayerNumber == BoardNumber){
  543.                 if (Button5.getText() == null){
  544.                     Button5.setText(Button9.getText());
  545.                     Button9.setText(null);
  546.                     Button9.setBackground(Color.BLACK);
  547.                     Button5.setBackground(Color.YELLOW);
  548.                     count_move();
  549.                     sentMove(9,5);
  550.                 }
  551.                 else if (Button10.getText() == null){
  552.                     Button10.setText(Button9.getText());
  553.                     Button9.setText(null);
  554.                     Button9.setBackground(Color.BLACK);
  555.                     Button10.setBackground(Color.YELLOW);
  556.                     count_move();
  557.                     sentMove(9,10);
  558.                 }
  559.                 else if (Button13.getText() == null){
  560.                     Button13.setText(Button9.getText());
  561.                     Button9.setText(null);
  562.                     Button9.setBackground(Color.BLACK);
  563.                     Button13.setBackground(Color.YELLOW);
  564.                     count_move();
  565.                     sentMove(9,13);
  566.                 }
  567.                 check();
  568.             }}
  569.         });
  570.         Button10.addActionListener(new ActionListener() {
  571.             @Override
  572.             public void actionPerformed(ActionEvent e) {
  573.                 if (PlayerNumber == BoardNumber){
  574.                 if (Button6.getText() == null){
  575.                     Button6.setText(Button10.getText());
  576.                     Button10.setText(null);
  577.                     Button10.setBackground(Color.BLACK);
  578.                     Button6.setBackground(Color.YELLOW);
  579.                     count_move();
  580.                     sentMove(10,6);
  581.                 }
  582.                 else if (Button9.getText() == null){
  583.                     Button9.setText(Button10.getText());
  584.                     Button10.setText(null);
  585.                     Button10.setBackground(Color.BLACK);
  586.                     Button9.setBackground(Color.YELLOW);
  587.                     count_move();
  588.                     sentMove(10,9);
  589.                 }
  590.                 else if (Button11.getText() == null){
  591.                     Button11.setText(Button10.getText());
  592.                     Button10.setText(null);
  593.                     Button10.setBackground(Color.BLACK);
  594.                     Button11.setBackground(Color.YELLOW);
  595.                     count_move();
  596.                     sentMove(10,11);
  597.                 }
  598.                 else if (Button14.getText() == null){
  599.                     Button14.setText(Button10.getText());
  600.                     Button10.setText(null);
  601.                     Button10.setBackground(Color.BLACK);
  602.                     Button14.setBackground(Color.YELLOW);
  603.                     count_move();
  604.                     sentMove(10,14);
  605.                 }
  606.                 check();
  607.             }}
  608.         });
  609.         Button11.addActionListener(new ActionListener() {
  610.             @Override
  611.             public void actionPerformed(ActionEvent e) {
  612.                 if (PlayerNumber == BoardNumber){
  613.                 if (Button7.getText() == null){
  614.                     Button7.setText(Button11.getText());
  615.                     Button11.setText(null);
  616.                     Button11.setBackground(Color.BLACK);
  617.                     Button7.setBackground(Color.YELLOW);
  618.                     count_move();
  619.                     sentMove(11,7);
  620.                 }
  621.                 else if (Button10.getText() == null){
  622.                     Button10.setText(Button11.getText());
  623.                     Button11.setText(null);
  624.                     Button11.setBackground(Color.BLACK);
  625.                     Button10.setBackground(Color.YELLOW);
  626.                     count_move();
  627.                     sentMove(11,10);
  628.                 }
  629.                 else if (Button12.getText() == null){
  630.                     Button12.setText(Button11.getText());
  631.                     Button11.setText(null);
  632.                     Button11.setBackground(Color.BLACK);
  633.                     Button12.setBackground(Color.YELLOW);
  634.                     count_move();
  635.                     sentMove(11,12);
  636.                 }
  637.                 else if (Button15.getText() == null){
  638.                     Button15.setText(Button11.getText());
  639.                     Button11.setText(null);
  640.                     Button11.setBackground(Color.BLACK);
  641.                     Button15.setBackground(Color.YELLOW);
  642.                     count_move();
  643.                     sentMove(11,15);
  644.                 }
  645.                 check();
  646.             }}
  647.         });
  648.         Button12.addActionListener(new ActionListener() {
  649.             @Override
  650.             public void actionPerformed(ActionEvent e) {
  651.                 if (PlayerNumber == BoardNumber){
  652.                 if (Button8.getText() == null){
  653.                     Button8.setText(Button12.getText());
  654.                     Button12.setText(null);
  655.                     Button12.setBackground(Color.BLACK);
  656.                     Button8.setBackground(Color.YELLOW);
  657.                     count_move();
  658.                     sentMove(12,8);
  659.                 }
  660.                 else if (Button11.getText() == null){
  661.                     Button11.setText(Button12.getText());
  662.                     Button12.setText(null);
  663.                     Button12.setBackground(Color.BLACK);
  664.                     Button11.setBackground(Color.YELLOW);
  665.                     count_move();
  666.                     sentMove(12,11);
  667.                 }
  668.                 else if (Button16.getText() == null){
  669.                     Button16.setText(Button12.getText());
  670.                     Button12.setText(null);
  671.                     Button12.setBackground(Color.BLACK);
  672.                     Button16.setBackground(Color.YELLOW);
  673.                     count_move();
  674.                     sentMove(12,16);
  675.                 }
  676.                 check();
  677.             }}
  678.         });
  679.         Button13.addActionListener(new ActionListener() {
  680.             @Override
  681.             public void actionPerformed(ActionEvent e) {
  682.                 if (PlayerNumber == BoardNumber){
  683.                 if (Button9.getText() == null){
  684.                     Button9.setText(Button13.getText());
  685.                     Button13.setText(null);
  686.                     Button13.setBackground(Color.BLACK);
  687.                     Button9.setBackground(Color.YELLOW);
  688.                     count_move();
  689.                     sentMove(13,9);
  690.                 }
  691.                 else if (Button14.getText() == null){
  692.                     Button14.setText(Button13.getText());
  693.                     Button13.setText(null);
  694.                     Button13.setBackground(Color.BLACK);
  695.                     Button14.setBackground(Color.YELLOW);
  696.                     count_move();
  697.                     sentMove(13,14);
  698.                 }
  699.                 check();
  700.             }}
  701.         });
  702.         Button14.addActionListener(new ActionListener() {
  703.             @Override
  704.             public void actionPerformed(ActionEvent e) {
  705.                 if (PlayerNumber == BoardNumber){
  706.                 if (Button10.getText() == null){
  707.                     Button10.setText(Button14.getText());
  708.                     Button14.setText(null);
  709.                     Button14.setBackground(Color.BLACK);
  710.                     Button10.setBackground(Color.YELLOW);
  711.                     count_move();
  712.                     sentMove(14,10);
  713.                 }
  714.                 else if (Button13.getText() == null){
  715.                     Button13.setText(Button14.getText());
  716.                     Button14.setText(null);
  717.                     Button14.setBackground(Color.BLACK);
  718.                     Button13.setBackground(Color.YELLOW);
  719.                     count_move();
  720.                     sentMove(14,13);
  721.                 }
  722.                 else if (Button15.getText() == null){
  723.                     Button15.setText(Button14.getText());
  724.                     Button14.setText(null);
  725.                     Button14.setBackground(Color.BLACK);
  726.                     Button15.setBackground(Color.YELLOW);
  727.                     count_move();
  728.                     sentMove(14,15);
  729.                 }
  730.                 check();
  731.             }}
  732.         });
  733.         Button15.addActionListener(new ActionListener() {
  734.             @Override
  735.             public void actionPerformed(ActionEvent e) {
  736.                 if (PlayerNumber == BoardNumber){
  737.                 if (Button11.getText() == null){
  738.                     Button11.setText(Button15.getText());
  739.                     Button15.setText(null);
  740.                     Button15.setBackground(Color.BLACK);
  741.                     Button11.setBackground(Color.YELLOW);
  742.                     count_move();
  743.                     sentMove(15,11);
  744.                 }
  745.                 else if (Button14.getText() == null){
  746.                     Button14.setText(Button15.getText());
  747.                     Button15.setText(null);
  748.                     Button15.setBackground(Color.BLACK);
  749.                     Button14.setBackground(Color.YELLOW);
  750.                     count_move();
  751.                     sentMove(15,14);
  752.                 }
  753.                 else if (Button16.getText() == null){
  754.                     Button16.setText(Button15.getText());
  755.                     Button15.setText(null);
  756.                     Button15.setBackground(Color.BLACK);
  757.                     Button16.setBackground(Color.YELLOW);
  758.                     count_move();
  759.                     sentMove(15,16);
  760.                 }
  761.                 check();
  762.             }}
  763.         });
  764.         Button16.addActionListener(new ActionListener() {
  765.             @Override
  766.             public void actionPerformed(ActionEvent e) {
  767.                 if (PlayerNumber == BoardNumber){
  768.                 if (Button12.getText() == null){
  769.                     Button12.setText(Button16.getText());
  770.                     Button16.setText(null);
  771.                     Button16.setBackground(Color.BLACK);
  772.                     Button12.setBackground(Color.YELLOW);
  773.                     count_move();
  774.                     sentMove(16,12);
  775.                 }
  776.                 else if (Button15.getText() == null){
  777.                     Button15.setText(Button16.getText());
  778.                     Button16.setText(null);
  779.                     Button16.setBackground(Color.BLACK);
  780.                     Button15.setBackground(Color.YELLOW);
  781.                     count_move();
  782.                     sentMove(16,15);
  783.                 }
  784.                 check();
  785.             }}
  786.         });
  787.         for(int i = 0;i < NumBer.size(); i++){
  788.             if (NumBer.get(i).equals("0")){
  789.                 buttonList.get(i).setText(null);
  790.                 playerPanel.add(buttonList.get(i));
  791.                 buttonList.get(i).setBackground(Color.BLACK);
  792.                
  793.             }else{
  794.                
  795.  
  796.                
  797.             buttonList.get(i).setText(NumBer.get(i));
  798.             playerPanel.add(buttonList.get(i));
  799.             buttonList.get(i).setBackground(Color.YELLOW);
  800.         }
  801.            
  802.         }
  803.        
  804.  
  805.  
  806.         return playerPanel;
  807.        
  808.        
  809.     }
  810.    
  811.     public void check(){
  812.         if (Integer.parseInt(Button1.getText()) == 1 && Integer.parseInt(Button2.getText()) == 2 && Integer.parseInt(Button3.getText()) == 3 &&
  813.                 Integer.parseInt(Button4.getText()) == 4 && Integer.parseInt(Button5.getText()) == 5 && Integer.parseInt(Button6.getText()) == 6 &&
  814.                 Integer.parseInt(Button7.getText()) == 7 && Integer.parseInt(Button8.getText()) == 8 && Integer.parseInt(Button9.getText()) == 9 &&
  815.                 Integer.parseInt(Button10.getText()) == 10 && Integer.parseInt(Button11.getText()) == 11 && Integer.parseInt(Button12.getText()) == 12 &&
  816.                 Integer.parseInt(Button13.getText()) == 13 && Integer.parseInt(Button14.getText()) == 14 && Integer.parseInt(Button15.getText()) == 15 ){
  817.             JOptionPane.showMessageDialog(null, "You WiN!!!");
  818.             try {
  819.                 toserver = new DataOutputStream(socket.getOutputStream());
  820.                 toserver.writeInt(11111);
  821.             } catch (IOException e) {
  822.                 // TODO Auto-generated catch block
  823.                 e.printStackTrace();
  824.             }
  825.             player.setVisible(false);
  826.         }
  827.    
  828.  
  829.     }
  830.    
  831.     public void sentMove(int Sbutton ,int Cbutton){
  832.         try {
  833.             toserver = new DataOutputStream(socket.getOutputStream());
  834.             toserver.writeInt(Sbutton);
  835.             toserver.writeInt(Cbutton);
  836.         } catch (Exception e1) {
  837.  
  838.         }
  839.     }
  840.     }
  841.  
  842. public class recieveMove implements Runnable{
  843.    
  844.    
  845.     public recieveMove(){
  846.        
  847.     }
  848.     @Override
  849.     public void run(){
  850.         try {
  851.             DataInputStream fromserver = new DataInputStream(socket.getInputStream());
  852.             while (GameEnd == false){
  853.                 int b = fromserver.readInt();
  854.                 if (b == 11111){
  855.                     GameEnd = true;
  856.                     JOptionPane.showMessageDialog(null, "You Lose!!!");
  857.                     fromserver.close();
  858.                     toserver.close();
  859.                     socket.close();
  860.                     System.exit(0);
  861.                    
  862.                    
  863.                 }else{
  864.                     int i = fromserver.readInt();
  865.                     move(b , i);
  866.                 }
  867.                 count_foe_move();
  868.  
  869.             }
  870.         } catch (IOException e) {
  871.            
  872.             e.printStackTrace();
  873.         }
  874.     }
  875.    
  876.     public void move(int Sbutton ,int Cbutton){
  877.         Foepanel.buttonList.get(Cbutton-1).setText(Foepanel.buttonList.get(Sbutton-1).getText());
  878.         Foepanel.buttonList.get(Sbutton-1).setText(null);
  879.         Foepanel.buttonList.get(Sbutton-1).setBackground(Color.BLACK);
  880.         Foepanel.buttonList.get(Cbutton-1).setBackground(Color.YELLOW);
  881.        
  882.         repaint();
  883.     }
  884.    
  885. }
  886. }
  887.  
  888.    
  889.     private void connectToServer() {
  890.         try {
  891.             socket = new Socket(hostTextField.getText(), Integer.parseInt(portTextField.getText()));
  892.             fromserver = new DataInputStream(socket.getInputStream());
  893.             toserver = new DataOutputStream(socket.getOutputStream());
  894.            
  895. //          sent player Name
  896.             toserver.writeUTF(nameTextField.getText());
  897.            
  898. //          reciece player Number
  899.             PlayerNumber = fromserver.readInt();
  900.             JOptionPane.showMessageDialog(null, "You join as player"+PlayerNumber );
  901.  
  902.         }
  903.      
  904.         catch (Exception ex) {
  905.                 JOptionPane.showMessageDialog(null ,"Server is offline");
  906.                 System.exit(0);
  907.             }
  908.         }
  909.    
  910.     private void waiting(){
  911.  
  912.             try {
  913.                 if (fromserver.readBoolean() == true){
  914. //                  reciece foeName
  915.                     FoeName = fromserver.readUTF();
  916.                 }
  917.                
  918.             } catch (IOException e1) {
  919.                 JOptionPane.showMessageDialog(null, "waiting for player2" );
  920.  
  921.             }
  922.        
  923.     }
  924.    
  925.     private void recieveBoard(){
  926.         NumBer = new ArrayList<String>();
  927.        
  928.         try{
  929.             fromserver = new DataInputStream(socket.getInputStream());
  930.            
  931. //          int k = fromserver.readInt();
  932.             for (int i = 0 ; i < 16; i++){
  933.                 NumBer.add(fromserver.readUTF());
  934.  
  935.             }
  936.  
  937.         }catch (Exception ex){
  938.             JOptionPane.showMessageDialog(null, "Recieve Board ERROR" );
  939.             System.exit(0);
  940.         }
  941.     }
  942.    
  943.     public void count_move(){
  944.         count++;
  945.         MyMove.setText(Integer.toString(count));
  946. }
  947.  
  948.     public void count_foe_move(){
  949.         Foe_count++;
  950.         FoeMove.setText(Integer.toString(Foe_count));
  951. }
  952.    
  953.     @Override
  954.     public void actionPerformed(ActionEvent arg0) {
  955.         // TODO Auto-generated method stub
  956.  
  957.     }
  958.    
  959.  
  960. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement