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

📄 chessmodel.java

📁 五子棋游戏源代码,体现了基本的编程思路.
💻 JAVA
字号:
package chess;/** * <p>Title:5chess </p> * <p>Description: </p> * <p>Copyright: seaboy.ping@263.net Copyright (c) 2002</p> * <p>Company: </p> * @author chenxiaoping * @version 1.0 */import javax.swing.JOptionPane;import javax.swing.JPanel;public class ChessModel {private int width,height,degree;private int x=0,y=0;private int[][] arrMap,arrMapShow;private boolean isOdd,isExist;private ChessFrame cf;    public ChessModel() {    }    public ChessModel(int degree)    {        this.isOdd=true;        //this.isExist=false;        if(degree == 1)        {            PanelInit(20, 15, degree);        }        if(degree == 2)        {            PanelInit(30, 20, degree);        }        if(degree == 3)        {            PanelInit(40, 30, degree);        }    }    private void PanelInit(int width, int height, int degree)    {        this.width = width;        this.height = height;        this.degree = degree;        //arrMap = new int[width+1][height+1];        arrMapShow = new int[width+1][height+1];        for(int i = 0; i <= width; i++)        {            for(int j = 0; j <= height; j++)            {                arrMapShow[i][j] = -5;                //arrMap[i][j] = 0;            }        }    }    public boolean getisOdd(){        return this.isOdd;    }    public void setisOdd(boolean isodd){       if(isodd)          this.isOdd=true;       else          this.isOdd=false;    }    public boolean getisExist(){        return this.isExist;    }    public int getWidth(){        return this.width;    }    public int getHeight(){        return this.height;    }    public int getDegree(){        return this.degree;    }    public int[][] getarrMapShow()    {        return arrMapShow;    }    private boolean badxy(int x, int y)    {        if(x >= width+20 || x < 0)            return true;        return y >= height+20 || y < 0;    }    public boolean chessExist(int i,int j){       if(this.arrMapShow[i][j]==1 || this.arrMapShow[i][j]==2)           return true;       return false;    }/*    public void flash(int x,int y){       if(badxy(x,y))          return;       if(chessExist(x,y))          return;       this.arrMapShow[x][y]=-1;    }*/    public void readyplay(int x,int y){       if(badxy(x,y))          return;       if (chessExist(x,y))          return;       this.arrMapShow[x][y]=3;    }    public void play(int x,int y){       if(badxy(x,y))          return;       if(chessExist(x,y))       {          this.isExist=true;          return;       }       else          this.isExist=false;       if(getisOdd()){          setisOdd(false);          this.arrMapShow[x][y]=1;       }       else{          setisOdd(true);          this.arrMapShow[x][y]=2;       }    }   //计算机走棋   /*     说明:用穷举法判断每一个坐标点的四个方向的的最大棋子数,最后     得出棋子数最大值的坐标,下子   */    public void computerdo(int width,int height){        int max_black,max_red,max_temp,max=0;        //int x=0,y=0;        setisOdd(true);        System.out.println("computer is doing ...");        for(int i = 0; i <= width; i++)        {            for(int j = 0; j <= height; j++)            {                if(!chessExist(i,j))                {  //算法判断是否下子                   max_red=checkMax(i,j,2);//判断红子的最大值                   max_black=checkMax(i,j,1);//判断黑子的最大值                   //max_temp=max_red>=max_black?max_red:max_black;                   max_temp=Math.max(max_red,max_black);                   if(max_temp>max)                   {                      max=max_temp;                      this.x=i;                      this.y=j;                   }                }            }        }        setX(this.x);        setY(this.y);        this.arrMapShow[this.x][this.y]=2;    }  //记录计算机下子的坐标    public void setX(int x){        this.x=x;    }    public void setY(int y){        this.y=y;    }  //获得计算机下子的坐标    public int getX(){        return this.x;    }    public int getY(){        return this.y;    }    //计算某一点的四个方向上棋子最多值    public int checkMax(int x, int y,int black_or_red){          int num=0,max_num,max_temp=0;          int x_temp=x,y_temp=y;          int x_temp1=x_temp,y_temp1=y_temp;        //judge right          for(int i=1;i<5;i++){              x_temp1+=1;              if(x_temp1>this.width)                  break;              if(this.arrMapShow[x_temp1][y_temp1]==black_or_red)                  num++;              else                  break;          }        //judge left          x_temp1=x_temp;          for(int i=1;i<5;i++){              x_temp1-=1;              if(x_temp1<0)                  break;              if(this.arrMapShow[x_temp1][y_temp1]==black_or_red)                  num++;              else                  break;          }          if(num<5)             max_temp=num;        //judge up          x_temp1=x_temp;          y_temp1=y_temp;          num=0;          for(int i=1;i<5;i++){              y_temp1-=1;              if(y_temp1<0)                  break;              if(this.arrMapShow[x_temp1][y_temp1]==black_or_red)                  num++;              else                  break;          }        //judge down          y_temp1=y_temp;          for(int i=1;i<5;i++){              y_temp1+=1;              if(y_temp1>this.height)                  break;              if(this.arrMapShow[x_temp1][y_temp1]==black_or_red)                  num++;              else                  break;          }          if(num>max_temp&&num<5)             max_temp=num;        //judge left_up          x_temp1=x_temp;          y_temp1=y_temp;          num=0;          for(int i=1;i<5;i++){              x_temp1-=1;              y_temp1-=1;              if(y_temp1<0 || x_temp1<0)                  break;              if(this.arrMapShow[x_temp1][y_temp1]==black_or_red)                  num++;              else                  break;          }        //judge right_down          x_temp1=x_temp;          y_temp1=y_temp;          for(int i=1;i<5;i++){              x_temp1+=1;              y_temp1+=1;              if(y_temp1>this.height || x_temp1>this.width)                  break;              if(this.arrMapShow[x_temp1][y_temp1]==black_or_red)                  num++;              else                  break;          }          if(num>max_temp&&num<5)               max_temp=num;        //judge right_up          x_temp1=x_temp;          y_temp1=y_temp;          num=0;          for(int i=1;i<5;i++){              x_temp1+=1;              y_temp1-=1;              if(y_temp1<0 || x_temp1>this.width)                  break;              if(this.arrMapShow[x_temp1][y_temp1]==black_or_red)                  num++;              else                  break;          }        //judge left_down          x_temp1=x_temp;          y_temp1=y_temp;          for(int i=1;i<5;i++){              x_temp1-=1;              y_temp1+=1;              if(y_temp1>this.height || x_temp1<0)                  break;              if(this.arrMapShow[x_temp1][y_temp1]==black_or_red)                  num++;              else                  break;          }          if(num>max_temp&&num<5)                 max_temp=num;          max_num=max_temp;          return max_num;    }    public boolean judgeSuccess(int x,int y,boolean isodd){          int num=1;          int arrvalue;          int x_temp=x,y_temp=y;          if(isodd)              arrvalue=2;          else              arrvalue=1;          int x_temp1=x_temp,y_temp1=y_temp;        //judge right          for(int i=1;i<6;i++){              x_temp1+=1;              if(x_temp1>this.width)                  break;              if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)                  num++;              else                  break;          }        //judge left          x_temp1=x_temp;          for(int i=1;i<6;i++){              x_temp1-=1;              if(x_temp1<0)                  break;              if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)                  num++;              else                  break;          }          if(num==5)               return true;        //judge up          x_temp1=x_temp;          y_temp1=y_temp;          num=1;          for(int i=1;i<6;i++){              y_temp1-=1;              if(y_temp1<0)                  break;              if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)                  num++;              else                  break;          }        //judge down          y_temp1=y_temp;          for(int i=1;i<6;i++){              y_temp1+=1;              if(y_temp1>this.height)                  break;              if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)                  num++;              else                  break;          }          if(num==5)               return true;        //judge left_up          x_temp1=x_temp;          y_temp1=y_temp;          num=1;          for(int i=1;i<6;i++){              x_temp1-=1;              y_temp1-=1;              if(y_temp1<0 || x_temp1<0)                  break;              if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)                  num++;              else                  break;          }        //judge right_down          x_temp1=x_temp;          y_temp1=y_temp;          for(int i=1;i<6;i++){              x_temp1+=1;              y_temp1+=1;              if(y_temp1>this.height || x_temp1>this.width)                  break;              if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)                  num++;              else                  break;          }          if(num==5)               return true;        //judge right_up          x_temp1=x_temp;          y_temp1=y_temp;          num=1;          for(int i=1;i<6;i++){              x_temp1+=1;              y_temp1-=1;              if(y_temp1<0 || x_temp1>this.width)                  break;              if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)                  num++;              else                  break;          }        //judge left_down          x_temp1=x_temp;          y_temp1=y_temp;          for(int i=1;i<6;i++){              x_temp1-=1;              y_temp1+=1;              if(y_temp1>this.height || x_temp1<0)                  break;              if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)                  num++;              else                  break;          }          if(num==5)               return true;       return false;    }    public void showSuccess(JPanel jp){          JOptionPane.showMessageDialog(jp,"Congratulation!You win the game!","win",JOptionPane.INFORMATION_MESSAGE);    }    public void showDefeat(JPanel jp){          JOptionPane.showMessageDialog(jp,"You lost,try again!","lost",JOptionPane.INFORMATION_MESSAGE);    }}

⌨️ 快捷键说明

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