- package tic.tac.toe;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.*;
- import java.net.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.*;
- public class TicTacToe_Server extends javax.swing.JFrame {
- ServerSocket ss ;
- Socket s ;
- BufferedReader in ;
- PrintWriter out ;
- boolean stop = true ; //用來安排是否可按按鈕
- int i = 0 ;//時間
- Timer t = new Timer(800,new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e) {
- jLabel2.setText(Integer.toString(i++));
- }
- });
- public TicTacToe_Server() {
- initComponents();
- }
- public void game() {
- String mess ;
- try{
- while((mess = in.readLine()) != null){ //互相傳達消息 來驅動工作
- //假設對方贏或輸 會傳送消息給對方 來進行同步
- if(mess.equals("You Lose")){
- Clear() ;
- JOptionPane.showMessageDialog(null, "You Lose" );
- continue ;
- }
- if(mess.equals("You Win")){
- Clear() ;
- JOptionPane.showMessageDialog(null, "You Win" );
- continue ;
- }
- //用來判斷按了什麼按鈕
- if(mess.equals("1")){
- jButton1.setText("X");
- jButton1.setEnabled(false);
- }else if(mess.equals("2")){
- jButton2.setText("X");
- jButton2.setEnabled(false);
- }else if(mess.equals("3")){
- jButton3.setText("X");
- jButton3.setEnabled(false);
- }else if(mess.equals("4")){
- jButton4.setText("X");
- jButton4.setEnabled(false);
- }else if(mess.equals("5")){
- jButton5.setText("X");
- jButton5.setEnabled(false);
- }else if(mess.equals("6")){
- jButton6.setText("X");
- jButton6.setEnabled(false);
- }else if(mess.equals("7")){
- jButton7.setText("X");
- jButton7.setEnabled(false);
- }else if(mess.equals("8")){
- jButton8.setText("X");
- jButton8.setEnabled(false);
- }else if(mess.equals("9")){
- jButton9.setText("X");
- jButton9.setEnabled(false);
- }
- stop = false ;
- jLabel3.setText("您先 ");//收到訊息已經代表你可以按按鈕
- win() ;
- lose();
- isFull();
- }
- }catch(IOException e){
- JOptionPane.showMessageDialog(null, "對方已退出");
- try {
- s.close();
- System.exit(0);
- } catch (IOException ex) {
- JOptionPane.showMessageDialog(null, "關閉錯誤....");
- System.exit(-1);
- }
- }
- }
- public void Clear(){//清空重來
- i = 0;//時間從0開始
- jButton1.setText("");jButton4.setText("");jButton7.setText("");
- jButton2.setText("");jButton5.setText("");jButton8.setText("");
- jButton3.setText("");jButton6.setText("");jButton9.setText("");
- jButton1.setEnabled(true); jButton2.setEnabled(true); jButton3.setEnabled(true);
- jButton4.setEnabled(true); jButton5.setEnabled(true); jButton6.setEnabled(true);
- jButton7.setEnabled(true); jButton8.setEnabled(true); jButton9.setEnabled(true);
- // 產生隨機數 來安排誰先攻
- int num = (int)(Math.random()*2+1) ;
- if(num == 1){
- stop = false ;
- jLabel3.setText("您先");
- out.println("stop");
- }else{
- stop = true ;
- jLabel3.setText("對方先");
- out.println();
- }
- }
- public void win(){//贏比賽條件 Buttin顯示值為O
- if((jButton1.getText().equals("O") && jButton2.getText() == "O" && jButton3.getText() == "O")||
- (jButton4.getText().equals("O") && jButton5.getText() == "O" && jButton6.getText() == "O")||
- (jButton7.getText().equals("O") && jButton8.getText() == "O" && jButton9.getText() == "O")||
- (jButton1.getText().equals("O") && jButton4.getText() == "O" && jButton7.getText() == "O")||
- (jButton2.getText().equals("O") && jButton5.getText() == "O" && jButton8.getText() == "O")||
- (jButton3.getText().equals("O") && jButton6.getText() == "O" && jButton9.getText() == "O") ){
- out.println("You Lose");
- Clear();
- JOptionPane.showMessageDialog(null, "You Win" );
- }
- }
- public void isFull(){//檢查是否滿
- if(!jButton1.getText().isEmpty() && !jButton2.getText().isEmpty() && !jButton3.getText().isEmpty()
- && !jButton4.getText().isEmpty() && !jButton5.getText().isEmpty() && !jButton6.getText().isEmpty()
- && !jButton7.getText().isEmpty() && !jButton8.getText().isEmpty() && !jButton9.getText().isEmpty() ){
- Clear() ; out.println("You ");
- JOptionPane.showMessageDialog(null, "平手" );
- }
- }
- public void lose(){//輸比賽條件 Buttin顯示值為X
- if((jButton1.getText().equals("X") && jButton2.getText() == "X" && jButton3.getText() == "X")||
- (jButton4.getText().equals("X") && jButton5.getText() == "X" && jButton6.getText() == "X")||
- (jButton7.getText().equals("X") && jButton8.getText() == "X" && jButton9.getText() == "X")||
- (jButton1.getText().equals("X") && jButton4.getText() == "X" && jButton7.getText() == "X")||
- (jButton2.getText().equals("X") && jButton5.getText() == "X" && jButton8.getText() == "X")||
- (jButton3.getText().equals("X") && jButton6.getText() == "X" && jButton9.getText() == "X") ){
- out.println("You Win");
- Clear() ;
- JOptionPane.showMessageDialog(null, "You Lose" );
- }
- }
- public void Start(){
- new Thread(new Runnable(){
- @Override
- public void run() {
- try {
- ss = new ServerSocket(20097) ;
- JOptionPane.showMessageDialog(null, "建立成功....");
- s = ss.accept();
- in = new BufferedReader(new InputStreamReader(s.getInputStream()));
- out = new PrintWriter(s.getOutputStream() , true);
- JOptionPane.showMessageDialog(null, "連線成功....");
- jButton10.setEnabled(false);//設定不可頂選
- // 產生隨機數 來安排誰先攻
- int num = (int)(Math.random()*2+1) ;
- if(num == 1){
- stop = false ;
- jLabel3.setText("您先");
- out.println("stop");
- }else{
- stop = true ;
- jLabel3.setText("對方先");
- out.println();
- }
- t.start();
- game();
- } catch (IOException ex) {
- JOptionPane.showMessageDialog(null, "建立失敗....");
- jButton10.setEnabled(true);//設定不可頂選
- }
- }
- }).start();
- }
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- //建構式窗的區塊
- private void initComponents() {
- jPanel1 = new javax.swing.JPanel();
- jButton1 = new javax.swing.JButton();
- jButton2 = new javax.swing.JButton();
- jButton3 = new javax.swing.JButton();
- jButton4 = new javax.swing.JButton();
- jButton5 = new javax.swing.JButton();
- jButton6 = new javax.swing.JButton();
- jButton7 = new javax.swing.JButton();
- jButton8 = new javax.swing.JButton();
- jButton9 = new javax.swing.JButton();
- jButton10 = new javax.swing.JButton();
- jButton11 = new javax.swing.JButton();
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- jLabel3 = new javax.swing.JLabel();
- jLabel4 = new javax.swing.JLabel();
- jLabel5 = new javax.swing.JLabel();
- jLabel6 = new javax.swing.JLabel();
- jLabel7 = new javax.swing.JLabel();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setTitle("井字遊戲");
- setResizable(false);
- jPanel1.setPreferredSize(new java.awt.Dimension(430, 430));
- jPanel1.setLayout(new java.awt.GridLayout(3, 3));
- jButton1.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
- jButton1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- jPanel1.add(jButton1);
- jButton2.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
- jButton2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
- jButton2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton2ActionPerformed(evt);
- }
- });
- jPanel1.add(jButton2);
- jButton3.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
- jButton3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
- jButton3.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton3ActionPerformed(evt);
- }
- });
- jPanel1.add(jButton3);
- jButton4.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
- jButton4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
- jButton4.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton4ActionPerformed(evt);
- }
- });
- jPanel1.add(jButton4);
- jButton5.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
- jButton5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
- jButton5.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton5ActionPerformed(evt);
- }
- });
- jPanel1.add(jButton5);
- jButton6.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
- jButton6.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
- jButton6.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton6ActionPerformed(evt);
- }
- });
- jPanel1.add(jButton6);
- jButton7.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
- jButton7.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
- jButton7.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton7ActionPerformed(evt);
- }
- });
- jPanel1.add(jButton7);
- jButton8.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
- jButton8.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
- jButton8.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton8ActionPerformed(evt);
- }
- });
- jPanel1.add(jButton8);
- jButton9.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
- jButton9.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
- jButton9.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton9ActionPerformed(evt);
- }
- });
- jPanel1.add(jButton9);
- jButton10.setText("連線");
- jButton10.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton10ActionPerformed(evt);
- }
- });
- jButton11.setText("離開");
- jButton11.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton11ActionPerformed(evt);
- }
- });
- jLabel1.setFont(new java.awt.Font("微軟正黑體", 1, 18)); // NOI18N
- jLabel1.setText("Time : ");
- jLabel2.setFont(new java.awt.Font("微軟正黑體", 1, 18)); // NOI18N
- jLabel2.setText("00");
- jLabel3.setText("XXXX");
- jLabel4.setFont(new java.awt.Font("微軟正黑體", 1, 24)); // NOI18N
- jLabel4.setText("井");
- jLabel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
- jLabel5.setFont(new java.awt.Font("微軟正黑體", 1, 24)); // NOI18N
- jLabel5.setText("字");
- jLabel5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
- jLabel6.setFont(new java.awt.Font("微軟正黑體", 1, 24)); // NOI18N
- jLabel6.setText("遊");
- jLabel6.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
- jLabel7.setFont(new java.awt.Font("微軟正黑體", 1, 24)); // NOI18N
- jLabel7.setText("戲");
- jLabel7.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(31, 31, 31)
- .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(35, 35, 35)
- .addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(73, 73, 73)
- .addComponent(jLabel1)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jLabel2))
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(layout.createSequentialGroup()
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel4)
- .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)))
- .addComponent(jLabel5)
- .addComponent(jLabel7))
- .addGap(18, 18, 18))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addGap(44, 44, 44)
- .addComponent(jLabel4)
- .addGap(37, 37, 37)
- .addComponent(jLabel5)
- .addGap(38, 38, 38)
- .addComponent(jLabel6)
- .addGap(41, 41, 41)
- .addComponent(jLabel7)))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jButton10)
- .addComponent(jButton11)
- .addComponent(jLabel1)
- .addComponent(jLabel2)
- .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addContainerGap(13, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
- Start() ;
- }
- private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {
- System.exit(0);
- }
- private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
- if(stop != true){
- jButton5.setText("O");
- out.println("5");
- jButton5.setEnabled(false);
- stop = true ;
- jLabel3.setText("對方先");
- }
- }
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- if(stop != true){
- jButton1.setText("O");
- out.println("1");
- jButton1.setEnabled(false);
- stop = true ;
- jLabel3.setText("對方先");
- }
- }
- private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
- if(stop != true){
- jButton2.setText("O");
- out.println("2");
- jButton2.setEnabled(false);
- stop = true ;
- jLabel3.setText("對方先");
- }
- }
- private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
- if(stop != true){
- jButton3.setText("O");
- out.println("3");
- jButton3.setEnabled(false);
- stop = true ;
- jLabel3.setText("對方先");
- }
- }
- private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
- if(stop != true){
- jButton7.setText("O");
- out.println("7");
- jButton7.setEnabled(false);
- stop = true ;
- jLabel3.setText("對方先");
- }
- }
- private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
- if(stop != true){
- jButton4.setText("O");
- out.println("4");
- jButton4.setEnabled(false);
- stop = true ;
- jLabel3.setText("對方先");
- }
- }
- private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
- if(stop != true){
- jButton6.setText("O");
- out.println("6");
- jButton6.setEnabled(false);
- stop = true ;
- jLabel3.setText("對方先");
- }
- }
- private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
- if(stop != true){
- jButton8.setText("O");
- out.println("8");
- jButton8.setEnabled(false);
- stop = true ;
- jLabel3.setText("對方先");
- }
- }
- private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
- if(stop != true){
- jButton9.setText("O");
- out.println("9");
- jButton9.setEnabled(false);
- stop = true ;
- jLabel3.setText("對方先");
- }
- }
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(TicTacToe_Server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(TicTacToe_Server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(TicTacToe_Server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(TicTacToe_Server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new TicTacToe_Server().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton jButton1;
- private javax.swing.JButton jButton10;
- private javax.swing.JButton jButton11;
- private javax.swing.JButton jButton2;
- private javax.swing.JButton jButton3;
- private javax.swing.JButton jButton4;
- private javax.swing.JButton jButton5;
- private javax.swing.JButton jButton6;
- private javax.swing.JButton jButton7;
- private javax.swing.JButton jButton8;
- private javax.swing.JButton jButton9;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel jLabel3;
- private javax.swing.JLabel jLabel4;
- private javax.swing.JLabel jLabel5;
- private javax.swing.JLabel jLabel6;
- private javax.swing.JLabel jLabel7;
- private javax.swing.JPanel jPanel1;
- // End of variables declaration
- }
clinet :
- package tic.tac;
- 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 javax.swing.JOptionPane;
- import javax.swing.Timer;
- public class toe_Clinet extends javax.swing.JFrame {
- Socket s ;
- BufferedReader in ;
- PrintWriter out ;
- boolean stop = true;//用來安排是否可按按鈕
- int i = 0 ;//時間
- Timer t = new Timer(800,new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e) {
- jLabel2.setText(Integer.toString(i++));
- }
- });
- public toe_Clinet() {
- initComponents();
- }
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() { //建構式窗區塊
- jPanel1 = new javax.swing.JPanel();
- jButton1 = new javax.swing.JButton();
- jButton2 = new javax.swing.JButton();
- jButton3 = new javax.swing.JButton();
- jButton4 = new javax.swing.JButton();
- jButton5 = new javax.swing.JButton();
- jButton6 = new javax.swing.JButton();
- jButton7 = new javax.swing.JButton();
- jButton8 = new javax.swing.JButton();
- jButton9 = new javax.swing.JButton();
- jButton10 = new javax.swing.JButton();
- jButton11 = new javax.swing.JButton();
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- jLabel3 = new javax.swing.JLabel();
- jLabel4 = new javax.swing.JLabel();
- jLabel5 = new javax.swing.JLabel();
- jLabel6 = new javax.swing.JLabel();
- jLabel7 = new javax.swing.JLabel();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setTitle("井字遊戲");
- setResizable(false);
- jPanel1.setPreferredSize(new java.awt.Dimension(430, 430));
- jPanel1.setLayout(new java.awt.GridLayout(3, 3));
- jButton1.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
- jButton1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- jPanel1.add(jButton1);
- jButton2.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
- jButton2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
- jButton2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton2ActionPerformed(evt);
- }
- });
- jPanel1.add(jButton2);
- jButton3.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
- jButton3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
- jButton3.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton3ActionPerformed(evt);
- }
- });
- jPanel1.add(jButton3);
- jButton4.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
- jButton4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
- jButton4.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton4ActionPerformed(evt);
- }
- });
- jPanel1.add(jButton4);
- jButton5.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
- jButton5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
- jButton5.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton5ActionPerformed(evt);
- }
- });
- jPanel1.add(jButton5);
- jButton6.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
- jButton6.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
- jButton6.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton6ActionPerformed(evt);
- }
- });
- jPanel1.add(jButton6);
- jButton7.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
- jButton7.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
- jButton7.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton7ActionPerformed(evt);
- }
- });
- jPanel1.add(jButton7);
- jButton8.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
- jButton8.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
- jButton8.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton8ActionPerformed(evt);
- }
- });
- jPanel1.add(jButton8);
- jButton9.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N
- jButton9.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
- jButton9.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton9ActionPerformed(evt);
- }
- });
- jPanel1.add(jButton9);
- jButton10.setText("連線");
- jButton10.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton10ActionPerformed(evt);
- }
- });
- jButton11.setText("離開");
- jButton11.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton11ActionPerformed(evt);
- }
- });
- jLabel1.setFont(new java.awt.Font("微軟正黑體", 1, 18)); // NOI18N
- jLabel1.setText("Time : ");
- jLabel2.setFont(new java.awt.Font("微軟正黑體", 1, 18)); // NOI18N
- jLabel2.setText("00");
- jLabel3.setText("XXXX");
- jLabel4.setFont(new java.awt.Font("微軟正黑體", 1, 24)); // NOI18N
- jLabel4.setText("井");
- jLabel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
- jLabel5.setFont(new java.awt.Font("微軟正黑體", 1, 24)); // NOI18N
- jLabel5.setText("字");
- jLabel5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
- jLabel6.setFont(new java.awt.Font("微軟正黑體", 1, 24)); // NOI18N
- jLabel6.setText("遊");
- jLabel6.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
- jLabel7.setFont(new java.awt.Font("微軟正黑體", 1, 24)); // NOI18N
- jLabel7.setText("戲");
- jLabel7.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(31, 31, 31)
- .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(35, 35, 35)
- .addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(73, 73, 73)
- .addComponent(jLabel1)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jLabel2))
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel4)
- .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)))
- .addComponent(jLabel5)
- .addComponent(jLabel7)
- .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addGap(71, 71, 71)
- .addComponent(jLabel4)
- .addGap(37, 37, 37)
- .addComponent(jLabel5)
- .addGap(38, 38, 38)
- .addComponent(jLabel6)
- .addGap(41, 41, 41)
- .addComponent(jLabel7)))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jButton10)
- .addComponent(jButton11)
- .addComponent(jLabel1)
- .addComponent(jLabel2)
- .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- public void game() {
- String mess ;
- try{
- while((mess = in.readLine()) != null){
- //互相傳達消息 來驅動工作
- //假設對方贏或輸 會傳送消息給對方 來進行同步
- if(mess.equals("stop")){
- stop = true ;
- jLabel3.setText("對方先 ");
- continue ;
- }
- if(mess.equals("You Lose")){
- JOptionPane.showMessageDialog(null, "You Lose" );
- Clear() ;
- continue ;
- }
- if(mess.equals("You Win")){
- JOptionPane.showMessageDialog(null, "You Win" );
- Clear() ;
- continue ;
- }
- //用來判斷按了什麼按鈕
- if(mess.equals("1")){
- jButton1.setText("X");
- jButton1.setEnabled(false);
- }else if(mess.equals("2")){
- jButton2.setText("X");
- jButton2.setEnabled(false);
- }else if(mess.equals("3")){
- jButton3.setText("X");
- jButton3.setEnabled(false);
- }else if(mess.equals("4")){
- jButton4.setText("X");
- jButton4.setEnabled(false);
- }else if(mess.equals("5")){
- jButton5.setText("X");
- jButton5.setEnabled(false);
- }else if(mess.equals("6")){
- jButton6.setText("X");
- jButton6.setEnabled(false);
- }else if(mess.equals("7")){
- jButton7.setText("X");
- jButton7.setEnabled(false);
- }else if(mess.equals("8")){
- jButton8.setText("X");
- jButton8.setEnabled(false);
- }else if(mess.equals("9")){
- jButton9.setText("X");
- jButton9.setEnabled(false);
- }
- stop = false ;
- jLabel3.setText("您先 ");//收到訊息已經代表你可以按按鈕
- win() ;
- lose();
- isFull();
- }
- }catch(IOException e){
- JOptionPane.showMessageDialog(null, "對方已退出");
- try {
- s.close();
- System.exit(-1);
- } catch (IOException ex) {
- JOptionPane.showMessageDialog(null, "關閉錯誤....");
- System.exit(-1);
- }
- }
- }
- public void win(){//贏比賽條件 Buttin顯示值為O
- if((jButton1.getText().equals("X") && jButton2.getText() == "X" && jButton3.getText() == "X")||
- (jButton4.getText().equals("X") && jButton5.getText() == "X" && jButton6.getText() == "X")||
- (jButton7.getText().equals("X") && jButton8.getText() == "X" && jButton9.getText() == "X")||
- (jButton1.getText().equals("X") && jButton4.getText() == "X" && jButton7.getText() == "X")||
- (jButton2.getText().equals("X") && jButton5.getText() == "X" && jButton8.getText() == "X")||
- (jButton3.getText().equals("X") && jButton6.getText() == "X" && jButton9.getText() == "X") ){
- out.println("You Lose");
- Clear() ;
- JOptionPane.showMessageDialog(null, "You Win" );
- }
- }
- public void Clear(){//清空重來
- i = 0;
- jButton1.setText("");jButton4.setText("");jButton7.setText("");
- jButton2.setText("");jButton5.setText("");jButton8.setText("");
- jButton3.setText("");jButton6.setText("");jButton9.setText("");
- jButton1.setEnabled(true); jButton2.setEnabled(true); jButton3.setEnabled(true);
- jButton4.setEnabled(true); jButton5.setEnabled(true); jButton6.setEnabled(true);
- jButton7.setEnabled(true); jButton8.setEnabled(true); jButton9.setEnabled(true);
- }
- public void lose(){//輸比賽條件 Buttin顯示值為X
- if((jButton1.getText().equals("O") && jButton2.getText() == "O" && jButton3.getText() == "O")||
- (jButton4.getText().equals("O") && jButton5.getText() == "O" && jButton6.getText() == "O")||
- (jButton7.getText().equals("O") && jButton8.getText() == "O" && jButton9.getText() == "O")||
- (jButton1.getText().equals("O") && jButton4.getText() == "O" && jButton7.getText() == "O")||
- (jButton2.getText().equals("O") && jButton5.getText() == "O" && jButton8.getText() == "O")||
- (jButton3.getText().equals("O") && jButton6.getText() == "O" && jButton9.getText() == "O") ){
- out.println("You Win");
- Clear() ;
- JOptionPane.showMessageDialog(null, "You Lose" );
- }
- }
- public void Start(){
- new Thread(new Runnable(){
- @Override
- public void run() {
- try {
- s = new Socket("127.0.0.1" , 20097) ;
- in = new BufferedReader(new InputStreamReader(s.getInputStream()));
- out = new PrintWriter(s.getOutputStream(), true);
- JOptionPane.showMessageDialog(null, "連線成功....");
- jButton10.setEnabled(false);//設定不可頂選
- t.start();
- game();
- } catch (IOException ex) {
- JOptionPane.showMessageDialog(null, "沒連接到伺服器 再試一次.... \n");
- jButton10.setEnabled(true);//設定不可頂選
- }
- }
- }).start();
- }
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- if(stop != true){
- jButton1.setText("O");
- out.println("1");
- jButton1.setEnabled(false);
- stop = true ;
- jLabel3.setText("對方先");
- }
- }
- private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
- if(stop != true){
- jButton2.setText("O");
- out.println("2");
- jButton2.setEnabled(false);
- stop = true ;
- jLabel3.setText("對方先");
- }
- }
- private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
- if(stop != true){
- jButton3.setText("O");
- out.println("3");
- jButton3.setEnabled(false);
- stop = true ;
- jLabel3.setText("對方先");
- }
- }
- public void isFull(){//檢查是否滿
- if(!jButton1.getText().isEmpty() && !jButton2.getText().isEmpty() && !jButton3.getText().isEmpty()
- && !jButton4.getText().isEmpty() && !jButton5.getText().isEmpty() && !jButton6.getText().isEmpty()
- && !jButton7.getText().isEmpty() && !jButton8.getText().isEmpty() && !jButton9.getText().isEmpty() ){
- Clear() ;
- out.println("888");
- JOptionPane.showMessageDialog(null, "平手" );
- }
- }
- private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
- if(stop != true){
- jButton4.setText("O");
- out.println("4");
- jButton4.setEnabled(false);
- stop = true ;
- jLabel3.setText("對方先");
- }
- }
- private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
- if(stop != true){
- jButton5.setText("O");
- out.println("5");
- jButton5.setEnabled(false);
- stop = true ;
- jLabel3.setText("對方先");
- }
- }
- private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
- if(stop != true){
- jButton6.setText("O");
- out.println("6");
- jButton6.setEnabled(false);
- stop = true ;
- jLabel3.setText("對方先");
- }
- }
- private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
- if(stop != true){
- jButton7.setText("O");
- out.println("7");
- jButton7.setEnabled(false);
- stop = true ;
- jLabel3.setText("對方先");
- }
- }
- private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
- if(stop != true){
- jButton8.setText("O");
- out.println("8");
- jButton8.setEnabled(false);
- stop = true ;
- jLabel3.setText("對方先");
- }
- }
- private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
- if(stop != true){
- jButton9.setText("O");
- out.println("9");
- jButton9.setEnabled(false);
- stop = true ;
- jLabel3.setText("對方先");
- }
- }
- private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
- Start() ;
- }
- private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {
- System.exit(0);
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(toe_Clinet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(toe_Clinet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(toe_Clinet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(toe_Clinet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new toe_Clinet().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton jButton1;
- private javax.swing.JButton jButton10;
- private javax.swing.JButton jButton11;
- private javax.swing.JButton jButton2;
- private javax.swing.JButton jButton3;
- private javax.swing.JButton jButton4;
- private javax.swing.JButton jButton5;
- private javax.swing.JButton jButton6;
- private javax.swing.JButton jButton7;
- private javax.swing.JButton jButton8;
- private javax.swing.JButton jButton9;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel jLabel3;
- private javax.swing.JLabel jLabel4;
- private javax.swing.JLabel jLabel5;
- private javax.swing.JLabel jLabel6;
- private javax.swing.JLabel jLabel7;
- private javax.swing.JPanel jPanel1;
- // End of variables declaration
- }
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。