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

📄 ticgame.java

📁 TicTacToe是一个人机对战的棋类游戏
💻 JAVA
字号:
import java.awt.*;

public class TicGame
{ public TicGame(TicBoard b,TicTacToe TTT)
  { reset();board=b;Tic=TTT;}
  public void reset()
  { circle=(1<<0)|(1<<1)|(1<<2);
    cross=(1<<6)|(1<<7)|(1<<8);
    end=false;
  }
  public boolean ended() {return end;}
  public void draw()
  { int o=circle,x=cross;
    int i=0,c=6;
    while(c>0)
    { if((o&1)!=0)
      {  board.drawPiece(PLAYER_O,i);
         c--;
      }
      else if((x&1)!=0)
      {  board.drawPiece(PLAYER_X,i);
         c--;
      }
      i++;o>>=1;x>>=1;
    }
  }
  public int move(int source,int target,boolean player)
  {  if(source<0||source>8||
        target<0||target>8||
        end||
        ((1<<target)&cross)!=0||
        ((1<<target)&circle)!=0||
        !isaround(source,target))
     return NOMOVE;
     if(player==PLAYER_X) cross=cross^(1<<source)^(1<<target);
     else circle=circle^(1<<source)^(1<<target);
     return status();
  }
  public int status()
  { if(isWin(circle))
    { end=true;return WIN_O;}
    if(isWin(cross))
    { end=true;return WIN_X;}
    return PLAY;
  }
  public static final int NOMOVE=-1;
  public static final int PLAY=0;
  public static final int WIN_O=1;
  public static final int WIN_X=2;
  public static final boolean PLAYER_X=true;
  public static final boolean PLAYER_O=false;

  public static boolean isWin(int pos)
  { if((pos&win[0])==win[0]||
       (pos&win[1])==win[1])
           return true;
    else return false; 
  }
  public static int[] win=
  { (1<<0)|(1<<4)|(1<<8),
    (1<<2)|(1<<4)|(1<<6)
  };
  protected static int[] moves={4,0,2,6,8,1,3,5,7};
  public int generateMove()
  { 

    int o_count=3,o=circle,s=0;
    if(end) return NOMOVE;
    while(o_count>0)
    { if((o&1)!=0)
      { o_count--;
        for(int m=0;m<9;m++)
        { if(isaround(s,m)&&((circle|cross)&(1<<m))==0)
           { if(isWin(circle^(1<<s)^(1<<m)))
             { Tic.setMove(s,m);return PLAY;}
             int x_count=3,x=cross,j=0;
             while(x_count>0)
             { if((x&1)!=0)
               { x_count--;
                 if(isaround(j,m)&&isWin(cross^(1<<j)^(1<<m)))
                 { Tic.setMove(s,m);return PLAY;}
               }
              x>>=1;j++;
             }
           }
        }
      }
     o>>=1;s++;
    }
   int c=3;
   s=0;o=circle;
   while(c>0)
   { if((o&1)!=0)
     { c--;
       for(int i=0;i<9;i++)
       { int move=moves[i];
         if(isaround(s,move)&&((circle|cross)&(1<<move))==0)
            { Tic.setMove(s,move);return PLAY;}
       }
     }
     o>>=1;s++;
   }
  return PLAY;
  }
  private boolean isaround(int source,int target)
  {  switch(source)
     {
       case 0: if(target==1||target==3||target==4) return true;
               else return false;
       case 1: if(target==0||target==2||target==4) return true;
               else return false;
       case 2: if(target==1||target==4||target==5) return true;
               else return false;
       case 3: if(target==0||target==4||target==6) return true;
               else return false;
       case 4: if(target==0||target==1||target==2||target==3||
                  target==5||target==6||target==7||target==8) return true;
               else return false;
       case 5: if(target==2||target==4||target==8) return true;
               else return false;
       case 6: if(target==3||target==4||target==7) return true;
               else return false;
       case 7: if(target==4||target==6||target==8) return true;
               else return false;
       case 8: if(target==4||target==5||target==7) return true;
               else return false;
     }
     return false;
  }
  public int getCircleValue() { return circle;}
  public int getCrossValue() { return cross;}
  protected TicBoard board;
  protected TicTacToe Tic;
  protected int circle,cross;
  private boolean end;
}

⌨️ 快捷键说明

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