⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fiftactoe.java

📁 五子棋代码
💻 JAVA
字号:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet; 
import java.awt.Graphics; 
import javax.swing.*;
import javax.swing.border.LineBorder;

public class FIFTacToe extends JApplet{
       private char Whoseturn = 'X';
       private Cell[][] cells = new Cell[15][15];
       private JLabel jlbStatus = new JLabel("'X's turn to play.");

       public FIFTacToe(){
              JPanel p = new JPanel(new GridLayout(15,15,0,0));
              for(int i=0;i<15;i++)
                 for(int j=0;j<15;j++)
                    p.add(cells[i][j] = new Cell());

              p.setBorder(new LineBorder(Color.red,1));
              jlbStatus.setBorder(new LineBorder(Color.yellow,1));
              this.getContentPane().add(p,BorderLayout.CENTER);
              this.getContentPane().add(jlbStatus,BorderLayout.SOUTH);
              }

       public boolean isFull(){
              for(int i=0;i<15;i++)
                 for(int j=0;j<15;j++)
                    if(cells[i][j].getToken() == ' ')
                    return false;
                    
              return true;
               }

       public boolean isWon(char token){
              for(int i=0;i<15;i++)
                 for(int j=0;j<11;j++)
                    if((cells[i][j].getToken()==token)
                       &&(cells[i][j+1].getToken()==token)
                       &&(cells[i][j+2].getToken()==token)
                       &&(cells[i][j+3].getToken()==token)
                       &&(cells[i][j+4].getToken()==token)){
                    return true;
                    }

              for(int j=0;j<15;j++)
                 for(int i=0;i<11;i++)
                    if((cells[i][j].getToken()==token)
                       &&(cells[i+1][j].getToken()==token)
                       &&(cells[i+2][j].getToken()==token)
                       &&(cells[i+3][j].getToken()==token)
                       &&(cells[i+4][j].getToken()==token)){
                    return true;
                    }

              for(int i=0;i<11;i++)
                 for(int j=0;j<11;j++)
                    if((cells[i][j].getToken()==token)
                       &&(cells[i+1][j+1].getToken()==token)
                       &&(cells[i+2][j+2].getToken()==token)
                       &&(cells[i+3][j+3].getToken()==token)
                       &&(cells[i+4][j+4].getToken()==token)){
                    return true;
                    }

              for(int i=0;i<11;i++)
                 for(int j=14;j>3;j--)
                    if((cells[i][j].getToken()==token)
                       &&(cells[i+1][j-1].getToken()==token)
                       &&(cells[i+2][j-2].getToken()==token)
                       &&(cells[i+3][j-3].getToken()==token)
                       &&(cells[i+4][j-4].getToken()==token)){
                    return true;
                    }
                    return false;
             }

       public class Cell extends JPanel implements MouseListener{
              private char token = ' ';
              public Cell(){
                     setBorder(new LineBorder(Color.black,1));
                     addMouseListener(this);
                     }

              public char getToken(){
                     return token;
                     }

              public void setToken(char c){
                     token = c;
                     repaint();
                     }

              protected void paintComponent(Graphics g){
                     super.paintComponent(g);
                     if(token == 'X'){
                     	g.setColor(Color.red);
                        g.drawLine(10,10,getWidth()-10,getHeight()-10);
                        g.drawLine(getWidth()-10,10,10,getHeight()-10);
                        }
                     else if(token == 'O'){
                     	  g.setColor(Color.blue);        
                          g.fillOval(10,10,getWidth()-20,getHeight()-20);
                          //g.drawLine(10,10,getWidth()-10,getHeight()-10);
                          //g.drawLine(getWidth()-10,10,10,getHeight()-10);
                          }
                     }

             public void mouseClicked(MouseEvent e){
                    if((token == ' ')&& (Whoseturn != ' ')){
                       setToken(Whoseturn);
                    if(isWon(Whoseturn)){
                       jlbStatus.setText(Whoseturn + "Won! The game is over.");
                       Whoseturn = ' ';
                       }
                    else if(isFull()){
                           jlbStatus.setText("Draw! The game is over.");
                           Whoseturn = ' ';
                           }
                         else{
                             Whoseturn = (Whoseturn == 'X')?'O':'X';
                             jlbStatus.setText(Whoseturn + "'s turn.");
                             }
                      }
                    }

             public void mousePressed(MouseEvent e){
                    }

             public void mouseReleased(MouseEvent e){
                    }

             public void mouseEntered(MouseEvent e){
                    }

             public void mouseExited(MouseEvent e){
                    }
            }
           }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -