Goziqi AIアルゴリズムも典型的なゲームAIアルゴリズムです。一部のチェスAIアルゴリズムは、参照として使用できます。以下はJava実装コードです。
チェスボード抽象インターフェイス
java.util.listをインポートします。パブリックインターフェイスICHESSBOARD {//ボードの最大水平座標を取得しますpublic int getMaxx(); //最大垂直座標public int getMaxy(); //現在のすべての空白のポイントを取得すると、これらのポイントはチェスのパブリックリスト<point> getFreePoints()のみを再生できます。 }チェスクラスの実装
//チェスクラスポイント{//これはパフォーマンスであり、public int xに設定されています。公開y; public int getx(){return x; } public point setx(int x){this.x = x;これを返します。 } public int gety(){return y; } public Point sety(int y){this.y = y;これを返します。 } public point(int x、int y){this.x = x; this.y = y; } @Override public int hashcode(){return x + y; } @Override public boolean equals(object obj){if(this == obj)return true;ポイントother =(point)obj; if(x!= other.x)falseを返します。 if(y!= other.y)falseを返します。 trueを返します。 }}プレーヤーの抽象インターフェイス
java.util.listをインポートします。 public Interface iPlayer {//次のステップは、対戦相手が既にpublic void run(list <point> enemypoints、point point)を設定しているチェスボードセットを渡すことです。 public boolean haswin(); public void setChessBoard(ICHESSBOARDチェスボード); public List <point> getMypoints(); }プレーヤーの基本抽象クラス
java.util.arraylistをインポートします。 java.util.listをインポートします。パブリックアブストラクトクラスベースプレイヤーはiplayerを実装します{//保護されたリスト<point> mypoints = new arraylist <point>(200); //チェスボード保護されたアイクセスボードチェスボード。 //ボードの最大水平および垂直座標は、MAXXで保護されています。保護されたint maxy; //すべての空白チェスピース保護されたリスト<point> allfreepoints; @Override public final list <point> getMypoints(){return mypoints; } @Override public void setChessboard(iChessboardチェスボード){this.chessboard = chessboard; allfreepoints = chessboard.getFreePoints(); maxx = chessboard.getmaxx(); maxy = chessboard.getmaxy(); mypoints.clear(); } private final Point temp = new Point(0、0); // public final boolean haswin(){if(mypoints.size()<5){return false; } point point = mypoints.get(mypoints.size() - 1); int count = 1; int x = point.getx()、y = point.gety(); // horizontal- temp.setx(x).sety(y); while(mypoints.contains(temp.setx(temp.getx() - 1))&& temp.getx()> = 0 && count <5){count ++; } if(count> = 5){return true; } temp.setx(x).sety(y); while(mypoints.contains(temp.setx(temp.getx() - 1))&& temp.getx()> = 0 && count <5){count ++; } if(count> = 5){return true; } temp.setx(x).sety(y); while(mypoints.contains(temp.setx(temp.getx()+1))&& temp.getx()<maxx && count <5){count ++; } if(count> = 5){return true; } //垂直| count = 1; temp.setx(x).sety(y); while(mypoints.contains(temp.sety(temp.gety() - 1))&& temp.gety()> = 0){count ++; } if(count> = 5){return true; } temp.setx(x).sety(y); while(mypoints.contains(temp.sety(temp.gety() - 1))&& temp.gety()> = 0){count ++; } if(count> = 5){return true; } temp.setx(x).sety(y); while(mypoints.contains(temp.sety(temp.gety()+1))&& temp.gety()<maxy && count <5){count ++; } if(count> = 5){return true; } //フォワードオブリック/count = 1; temp.setx(x).sety(y); while(mypoints.contains(temp.setx(temp.getx() - 1).sety(temp.gety()+1)&& temp.getx()> = 0 && temp.gety()<maxy){count ++; } if(count> = 5){return true; } temp.setx(x).sety(y); while(mypoints.contains(temp.setx(temp.getx()+1).sety(temp.gety() - 1)&& temp.getx()<maxx && temp.gety()> = 0 && count <6){count ++; } if(count> = 5){return true; } // backslash/count = 1; temp.setx(x).sety(y); while(mypoints.contains(temp.setx(temp.getx() - 1).sety(temp.gety() - 1)&& temp.getx()> = 0 && temp.gety()> = 0){count ++; } if(count> = 5){return true; } temp.setx(x).sety(y); while(mypoints.contains(temp.setx(temp.getx()+1).sety(temp.gety()+1))&& temp.getx()<maxx && temp.gety()<maxy && count <5){count ++; } if(count> = 5){return true; } falseを返します。 }}コンピューターAI実装
java.util.arraylistをインポートします。 java.util.collectionsをインポートします。 java.util.hashmapをインポートします。 java.util.listをインポートします。 java.util.mapをインポートします。 //アルゴリズムのコアクラス、アルゴリズムの主なアイデアは3つのステップに分割されます。 //最初のステップ:両当事者の現在の状況に応じて、自分自身と相手のスコアを循環的に仮定し、この作品がもたらす可能性のある状況の変更を判断します。敵と私たちの側を含む、ハーフライブ4)などと呼んでください。 //ステップ3:ユーザーが指定したルールに従って前の結果を並べ替え、サブ主題と防御的な形状を選択します(攻撃的および防御的な形状のルールがあります)パブリッククラスBasecomputeraiはベースプレイヤーを拡張します{// 4つの方向、水平 - 、垂直| 、フォワードスラント /、バックスラント /プライベート静的最終int heng = 0;プライベート静的最終int Zhong = 1;プライベート静的最終int zheng_xie = 2;プライベート静的最終int fan_xie = 3; //フロントツーバックプライベート静的ファイナルブールフォワード= true; private static final boolean backward = false; //分析結果を表示します。現在のポイントが2つのエンドパス(Alive)であろうと1つのパス(half_alive)のみであろうと、ブロックされたチェスピース分析プロセスが自動的にブロックされます。プライベート静的final int half_alive = 0; // private static final int dead = -1; //範囲を計算すると、範囲が大きすぎるとパフォーマンスの問題がありますプライベートクラスのカルカッタランゲ{int xstart、ystart、xStop、ystop; Private Calcuterange(int xStart、int ystart、int xStop、int ystop){this.xstart = xstart; this.ystart = ystart; this.xstop = xstop; this.ystop = ystop; }} //コンピューターの計算範囲を制限します。チェスボードの計算パフォーマンス全体があまりにも低すぎる場合、現在使用されているすべてのチェスピースの境界値に基づいて形成されています。現在、1つのprivate static final int range_step = 1です。 Calcuterange currentrange = new Calcuterange(0、0、0、0); private void initrange(list <point> couters、list <point>人間){currentrange.xstart = humans(0).getx() - range_step; currentrange.ystart = humans.get(0).gety() - range_step; currentrange.xstop = humans.get(0).getx()+range_step; currentrange.ystop = humans.get(0).gety()+range_step; for(ポイントポイント:人間){if(point.getx() - range_step <currentrange.xstart){currentrange.xstart = point.getx() - range_step; } else if(point.getx()+range_step> currentrange.xstop){currentrange.xstop = point.getx()+rience_step; } if(point.gety() - range_step <currentrange.ystart){currentrange.ystart = point.gety() - range_step; } else if(point.gety()+range_step> currentrange.ystop){currentrange.ystop = point.gety()+range_step; }} for(point point:couters){if(point.getx() - range_step <currentrange.xstart){currentrange.xstart = point.getx() - range_step; } else if(point.getx()+range_step> currentrange.xstop){currentrange.xstop = point.getx()+rience_step; } if(point.gety() - range_step <currentrange.ystart){currentrange.ystart = point.gety() - range_step; } else if(point.gety()+range_step> currentrange.ystop){currentrange.ystop = point.gety()+range_step; }} //範囲が拡張され、チェスボードを超えている場合、チェスボードcurrentrange.xstart = currentrange.xstart <0?0:currentrange.xstartに等しくなります。 currentrange.ystart = currentrange.ystart <0?0:currentrange.ystart; currentrange.ystart = currentrange.ystart <0?0:currentrange.ystart; currentrange.xstop = currentrange.xstop> = maxx?maxx-1:currentrange.xstop; currentrange.ystop = currentrange.ystop> = maxy?maxy-1:currentrange.ystop; } //入力方法の現在の形式を分析します。分析は、合計3つのステップに分割されます。 3番目のステップは、サブクラスによって困難なため制御できます。プライベートポイントdoanalysis(list <point> couters、list <point>人間){if(human.size()== 1){//最初のステップはgetFirstpoint(人間)を返します。 } //計算範囲の初期化(comuters、人間)を初期化します。 //以前の結果をクリアします。 //分析を開始し、すべての空白ポイントをスキャンし、最初の分析結果ポイントBestPoint = Dofirstanalysis(Comuters、人間)を形成します。 if(bestpoint!= null){//system.out.println("この作品は最も重要であり、この作品のみを再生できます」); BestPointを返します。 } //最初の結果を分析して、ベストポイントベストポイント= docomputersencondanalysis(ComputerFirstreSults、ComputerSencodResults)を見つけます。 if(bestpoint!= null){//system.out.println("i'm win win、私はこの作品を再生します "); BestPointを返します。 } computerFirstreSults.clear(); System.gc(); //最初の結果を分析して、敵のベストポイントの最良のポイントを見つけます= dohumansencondanalysis(HumanfirstreSults、hunsensencodResults); if(bestpoint!= null){//system.out.println("この作品を再生しない場合、負けます "); BestPointを返します。 } humanfirstresults.clear(); System.gc(); //最終キルポイントは見つかりませんでした、3番目の結果分析はdothirdanalysis()を返しました。 } //チェスピースの最初のステップは複雑な計算を必要とせず、チェスピースの人間の最初のステップのx値に基づいて完成します。プライベートポイントGetFirstPoint(リスト<Point>人間){Point Point = Humans.get(0); if(point.getx()== 0 || point.gety()== 0 || point.getx()== maxx && point.gety()== maxy)return new point(maxx/2、maxy/2); else {return new point(point.getx()-1、point.gety()); }} // private int debugx、debugy; // debugの使用//分析を開始し、すべてのブランクポイントをスキャンし、プライベートポイントdofirstanalysis(list <point> couters、list <point> humans){int size = allfreepoints.size();ポイントコンピュータポイント= null;ポイントHumanPoint = null; int x、y; FirstAnalysis Result firstAnalysis result; for(int i = 0; i <size; i ++){computerpoint = allfreepoints.get(i); //最初にxとyの座標をクリックします。これは、分析プロセス中に元のオブジェクトが変更されるため、x = computerpoint.getx(); y = computerpoint.gety(); if(x <currentrange.xstart || x> currentrange.xstop || y <currentrange.ystart || y> currentrange.ystop){contion; } // if(x == debugx && y == debugy){// system.out.println( "ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss System.out.println("ssssssssssssssssssssssssssssssssssssssssssss){ // } //Try to set a chess piece at this position and analyze the states that we can only form in the "horizontal" direction, such as live 4, live 3, semi-living 4, live 2, etc. firstAnalysisResult = tryandCountresult(comuters、人間、コンピューターポイント、ヘン); ComputerPoint.SetX(x).SETY(y); //次の分析のポイントの元の値を返信します(firstAnalysisResult!= null){//戻り結果なし、この方向に5つのチェスピースに到達することは不可能です。 if(firstanalysissresult.count.count == 5)// 5に等しいことは、この時点でチェスピースを再生でき、5に接続できることを意味します。勝った場合、それを分析しません。 ComputerPointを返します。 //最初の分析結果addtofirstanalysisResult(firstAnalysisResult、ComputerFirstreSults)を記録します。 } //上記の手順を「垂直方向」で繰り返しますfirstAnalysisResult = tryandCountresult(comuters、人間、コンピューターポイント、Zhong); ComputerPoint.setx(x).sety(y); if(firstAnalysisResult!= null){//チェスを死なせて、playしないでくださいaddtofirstanalysis result(firstAnalysisResult、computerfirstreSults); } //フォワードオブリックfirstAnalysisResult = tryandCountresult(comuters、人間、コンピュータポイント、Zheng_xie); ComputerPoint.setx(x).sety(y); if(firstAnalysisResult!= null){//死んだチェス、if(firstAnalysisResult.count == 5)を再生しないでください。コンピュータポイントを返します。 addtofirstanalysis result(firstAnalysisResult、computerfirstreSults); } // backside firstAnalysisResult = tryandCountresult(comuters、人間、Computerpoint、fan_xie); ComputerPoint.setx(x).sety(y); if(firstAnalysisResult!= null){//死んだチェス、if(firstAnalysisResult.count == 5)を再生しないでください。コンピュータポイントを返します。 addtofirstanalysis result(firstAnalysisResult、computerfirstreSults); } //敵のライブ3、セミライブ4など、「水平」方向に敵のこの作品の状態を分析します。 ComputerPoint.setx(x).sety(y); if(firstAnalysisResult!= null){// die chess、play(firstanalysissresult.count.count == 5)humanpoint = computerpoint; addtofirstanalysis result(firstAnalysisResult、humanfirstresults); } // "long-pernpendicular" firstAnalysisResult = tryandCountresult(人間、comuters、Computerpoint、Zhong); ComputerPoint.setx(x).sety(y); if(firstAnalysisResult!= null){// die chess、play(firstanalysissresult.count.count == 5)Humanpoint = computerpoint; addtofirstanalysis result(firstAnalysisResult、humanfirstresults); } // "forward-slant" firstAnalysisResult = tryandCountresult(人間、comuters、computerpoint、zheng_xie); ComputerPoint.setx(x).sety(y); if(firstAnalysisResult!= null){// die chess、play(firstanalysissresult.count.count == 5)Humanpoint = computerpoint; addtofirstanalysis result(firstAnalysisResult、humanfirstresults); } // "Backslash" firstAnalysisResult = tryandCountresult(人間、comuters、Computerpoint、fan_xie); ComputerPoint.setx(x).sety(y); if(firstAnalysisResult!= null){// die chess、play(firstanalysissresult.count.count == 5)Humanpoint = computerpoint; addtofirstanalysis result(firstAnalysisResult、humanfirstresults); }} //勝ちのピースがない場合、最初の分析では最初の分析の結果を返す必要はありません。 } // 2番目の分析で、最初の形成の結果を分析すると、最初の分析結果は最大4つのFirstAnalysis Resultオブジェクト(4つの敵と互い)を生成します。 // 4つのオブジェクトは、SencondanalysisResultオブジェクト、プライベートポイントdocomputersencondanalysis(Map <Point、list <firstanalysisresresult >> firstreSults、list <sencondanalysisResult> sencodResults){list <firstanalysisresult> list = null; sencondanalysis result sr = null; for(ポイントP:firstresults.keyset()){sr = new sencondanalysisResult(p); list = firstresults.get(p); for(firstAnalysisResult result:list){if(result.count == 4){if(result.alivestate == avering){//前のフィルタリングの後、双方は究極の殺害を除外しました。 4つある場合は、このステップを踏み、次のステップに勝ちます。 result.point; //最終的なキルがある場合、最初のラウンドが戻ってきました。このラウンドの4はすでに良い作品であり、直接戻り、分析しなくなりました} else {sr.halfalive4 ++; computer4halfalives.add(sr); }} else if(result.count == 3){if(result.alivestate == averain){sr.alive3 ++; if(sr.alive3 == 1){computer3alives.add(sr); } else {computerDouble3Alives.add(sr); }} else {sr.halfalive3 ++; computer3halfalives.add(sr); }} else {// semi-live 2は最初の段階で除外されており、sr.alive2 ++を処理しなくなりました。 if(sr.alive2 == 1){computer2Alives.add(sr); } else {computerDouble2Alives.add(sr); }}}} sencodresults.add(sr); } //ライブ4は見つかりませんでした。 nullを返します。 } //この方法は基本的に上記と同じですが、パフォーマンスのために、それは少ない判断であり、人間とコンピューターのプライベートポイントドーマンセンセンセンナリシス(Map <Point、list <firstAnalysisResult >> firstreSults、list <sencondanalysult> sencodResults){firstAnalysult> list = null; sencondanalysis result sr = null; for(ポイントP:firstresults.keyset()){sr = new sencondanalysisResult(p); list = firstresults.get(p); for(firstAnalysisResult result:list){if(result.count == 4){if(result.Alivestate == live){human4alives.add(sr); } else {sr.halfalive4 ++; human4halfalives.add(sr); }} else if(result.count == 3){if(result.alivestate == averain){sr.alive3 ++; if(sr.alive3 == 1){human3alives.add(sr); } else {humandouble3alives.add(sr); }} else {sr.halfalive3 ++; human3halfalives.add(sr); }} else {sr.alive2 ++; if(sr.alive2 == 1){human2alives.add(sr); } else {humandouble2alives.add(sr); }}} sencodresults.add(sr); } //ライブ4はnullのリターンが見つかりませんでした。 } private void sleep(int minisecond){try {thread.sleep(minisecond); } catch(arturtedexception e){}} // 3番目の分析では、どちらの側もライブ4を作成できず、ダブルライブ3ピースを見つけられない場合、3つのライブ4を見つけます。 } system.gc();睡眠(300); collections.sort(computersencodResults); System.gc(); //私は4を生きようとしていますが、4人以上の生活をしていないので、MostBest = GetBestPoint(Human4Alives、ComputersEncodResults)をブロックすることができます。 if(mostbest!= null)mostbestを返します。 collections.sort(hunsensencodresults); System.gc(); mostbest = getBestPoint(); if(mostbest!= null)mostbestを返します。 //最初のものを取り出します。 } //サブクラスはこの方法を実装し、防御指向または攻撃の保護されたポイントgetBestpoint()を達成するための順序を変更します{// 4を生きようとしているので、4つ以上の生活をしていないので、ポイントのみをブロックすることができないので、mostbestpoint(computerdouble3alive、computerdouble3alive、humansencodoults); if(mostbest!= null)mostbestを返します。 mostbest = getBestPoint(Computer3Alives、hunsensencodResults); if(mostbest!= null)mostbestを返します。 mostbest = getBestPoint(humandouble3Alives、ComputerSencodResults); if(mostbest!= null)mostbestを返します。 mostbest = getBestPoint(Human3Alives、ComputerSencodResults); if(mostbest!= null)mostbestを返します。 mostbest = getBestPoint(ComputerDouble2Alives、hunsensencodResults); if(mostbest!= null)mostbestを返します。 mostbest = getBestPoint(computer2Alives、hunsensencodresults); if(mostbest!= null)mostbestを返します。 mostbest = getBestPoint(computer3halfalives、hunsensencodresults); if(mostbest!= null)mostbestを返します。 mostbest = getBestPoint(human4halfalives、computersencodResults); if(mostbest!= null)mostbestを返します。 mostbest = getBestPoint(humandouble2Alives、ComputerSencodResults); if(mostbest!= null)mostbestを返します。 mostbest = getBestPoint(Human2Alives、ComputerSencodResults); if(mostbest!= null)mostbestを返します。 mostbest = getBestPoint(Human3Halfalives、ComputerSencodResults); MostBestを返します。 } // 3番目の分析の最後のステップでは、2番目の結果がソートされました。ここでは、フロントで保護されたポイントGetBestPointから最高のチェスピースを選択できます(リスト<<sencondanalysisresult> mybest、list <sencondanalysisresult> yoursEncodResults){if(!mybest.isempty()){youbest.if(){){) yoursencodResults){if(mybest.contains(your)){return your.point; }} mybest.get(0).pointを返します。 } else {return mybest.get(0).point; }} nullを返します。 } //最初の分析結果プライベート最終マップ<ポイント、リスト<firstAnalysisResult >> ComputerFirstResults = new Hashmap <Point、List <FirstAnalysisResult >>();プライベート最終マップ<ポイント、リスト<firstanalysisresult >> humanfirstreSults = new Hashmap <Point、list <firstAnalysisResult >>(); // 2番目の合計結果保護最終リスト<SencondanalysisResult> ComputerSencodResults = new ArrayList <SencondanalysisResult>();保護された最終リスト<SencondanalysisResult> humnansencodResults = new ArrayList <SencondanalysisResult>();保護された最終リスト<SencondanalysisResult>(); // 2番目の結果は、コンピューターで保護された最終リスト<sencondanalysisResult> computer4halfalives = new arrayList <sencondanalysisResult>(2);です。保護された最終リスト<SencondanalysisResult> ComputerDouble3Alives = new ArrayList <SencondanalysisResult>(4);保護された最終リスト<SencondanalysisResult> Computer3Alives = new ArrayList <SencondanalysisResult>(5);保護された最終リスト<SencondanalysisResult> Computer3Alives = new ArrayList <SencondanalysisResult>(5);保護された最終リスト<SencondanalysisResult> ComputerDouble2Alives = new ArrayList <SencondanalysisResult>();保護された最終リスト<SencondanalysisResult> Computer2Alives = new ArrayList <SencondanalysisResult>();保護された最終リスト<SencondanalysisResult> Computer3Halfalives = new ArrayList <SencondanalysisResult>(); // 2番目の結果は、ヒトで保護された最終リスト<SencondanalysisResult> Human4Alives = new ArrayList <SencondanalysisResult>(2)です。保護された最終リスト<SencondanalysisResult> human4halfalives = new ArrayList <sencondanalysisresult>(5);保護された最終リスト<SencondanalysisResult> humandouble3Alives = new ArrayList <SencondanalysisResult>(2);保護された最終リスト<SencondanalysisResult> Human3Alives = new ArrayList <SencondanalysisResult>(10);保護された最終リスト<SencondanalysisResult> humandouble2Alives = new ArrayList <sencondanalysisResult>(3);保護された最終リスト<SencondanalysisResult> Human2Alives = new ArrayList <SencondanalysisResult>();保護された最終リスト<SencondanalysisResult> Human3Halfalives = new ArrayList <SencondanalysisResult>(); //最初の分析の前に前の記事をクリアする分析の結果private void intyanalysisResults(){ComputerFirstreSults.Clear(); HumanFirstreSults.Clear(); // 2番目の合計結果computersencodresults.clear(); hansencodresults.clear(); // 2番目の結果computer4halfalives.clear(); ComputerDouble3Alives.clear(); Computer3Alives.Clear(); ComputerDouble2Alives.clear(); Computer2Alives.Clear(); computer3halfalives.clear(); Computer3Alives.Clear(); ComputerDouble2Alives.clear(); Computer2Alives.Clear(); computer3halfalives.clear(); // 2番目の結果はhuman4alives.clear();です。 human4halfalives.clear(); humandouble3alives.clear(); human3alives.clear(); humandouble2alives.clear(); Human2Alives.Clear(); human3halfalives.clear(); System.gc(); } //最初の分析結果に追加プライベートvoid addtofirstanalysis result(firstAnalysisResult result、map <point、list <firstanalysisresresult >> dest){if(dest.containskey(result.point)){dest.get(result.point).add(result)(result); } else {list <firstanalysisresult> list = new arrayList <firstAnalysisResult>(1); list.add(result); dest.put(result.point、list); }} //最初の分析結果クラスのプライベートクラスfirstAnalysisResult {//連続カウント; //ポイントポイント; //方向int方向; // int alivestate; Private firstAnalysisResult(int count、point point、int direction){this(count、point、direction、live); } private firstAnalysisResult(int count、point point、int direction、int alivestate){this.count = count; this.point = point; this.direction = direction; this.alivestate = alivestate; } private firstAnalysisResult init(ポイントポイント、int方向、int alivestate){this.count = 1; this.point = point; this.direction = direction; this.alivestate = alivestate;これを返します。 } private firstAnalysisResult cloneme(){新しいfirstAnalysisResult(count、point、direction、alivestate); }} // secondAnalysisResultは同等の<sencondanalysisresult> {int avelad4 = 0; // Living 3 Int Alive3 = 0; //セカンドライブ4、シールの一方の端int halhalive4 = 0; //セカンドライブ3、シールの一方の端int halfalive3 = 0; //生きている2量int live2 = 0; //ポイントポイント; @override public int hashcode(){final int prime = 31; int result = 1; result = prime * result +((point == null)?0:point.hashcode());返品結果; } @Override public boolean equals(object obj){sencondanalysis result other =(sencondanalysisresult)obj; if(point == null){if(other.point!= null)false; } else if(!point.equals(other.point))return false; trueを返します。 } private sencondanalysisResult(ポイントポイント){this.point = point; } // 3番目の分析では、2番目の分析の結果がソートされます。これは、ソートコールバックfunction @override public int compareto(sencondanalysis result aonther){return comparetowresult(this、another); }} // return -1が最初のパラメーターである場合、1は2番目のパラメーターであり、0は元の順序ではプライベートintです。 } if(oneresult.alive4 <atering.alive4){return 1; } if(oneresult.alive4> another.alive4){return -1; } if(oneresult.alive3> another.alive3){return -1; } if(oneresult.alive3 <atering.alive3){return 1; } if(oneresult.alive2> another.alive2){return -1; } if(oneresult.alive2 <atering.alive2){return 1; } if(oneresult.alive3> another.alive3){return -1; } if(oneresult.alive3> another.alive3){return 1; } return 0; } //一時的なオブジェクトを使用して、最初の分析中に分析結果を一時的に保存します。結果を取得するために、複数の(除外)結果がある場合、クロネメ法が呼び出されます。それ以外の場合、この結果は、私的な最終的なfirstAnalysisResult far = new firstAnalysisResult(1、null、heng)を破棄されます。 //分析サブが現在の位置の隣にある場合、特定の方向にいくつの潜水艦が形成されますか。パラメーター:現在の側に配置されたすべてのポイント、想定する必要があるポイント、プライベートであると判断する必要がある方向TryandCountresult(list <point> mypoints、list <point> emypoints、point point、int方向){int x = point.getx(); int y = point.gety(); FirstAnalysisResult fr = null; int maxcountonthisdirection = maxcountonthisdirection(point、enemypoints、direction、1); if(maxcountonthisdirection <5){//無意味なチェスのピース返品null; //この方向には5つの空のスペースがあり、すでに設定したチェスピースは除外されています} else(maxcountontonthisdirection == 5) } else {//両端はfr = far.init(point、direction、live)です。 } // countPoint(mypoints、enemypoints、point.setx(x).sety(y)、fr、方向、フォワード)を計算します。 countPoint(mypoints、enemypoints、point.setx(x).sety(y)、fr、方向、後方); if(fr.count <= 1 ||(fr.count == 2 && fr.alivestate == half_alive)){// realive1、semi-active2および次の結果は、return nullを放棄します。 } // return return return fr.cloneme(); } //チェスのピースが壁から出てきますプライベートブーリアンisoutsidefwall(ポイントポイント、int方向){if(direction == heng){return point.getx()<0 || point.getx()> = maxx; //最大xとyの値は壁の外側にあるので、等記号を使用します} else if(direction == zhong){return point.gety()<0 || point.gety()> = maxy; } else {//ここに問題があるかもしれませんreturn point.getx()<0 || point.gety()<0 || point.gety()<0 || point.gety()<0 || point.gety()<0 || point.getx()> = maxx || point.gety()> = maxy; }} private point pointTonext(ポイントポイント、int方向、ブールフォワード){switch(方向){case heng:if(forward)point.x ++; else point.x--;壊す;ケースZhong:if(forward)point.y ++; else point.y--;壊す; case zheng_xie:if(forward){point.x ++; point.y--; } else {point.x--; point.y ++; } 壊す; case fan_xie:if(forward){point.x ++; point.y ++; } else {point.x--; point.y--; } 壊す; } return Point; } //特定の方向(8つのうちの1つ)で再生できるピースの数。この方法は、最初の分析のプライベートボイドカウントポイント(リスト<point> mypoints、list <point> enemypoints、firstanalysisResult fr、int direction、boolean forward)のコアメソッドです。 if(mypoints.contains(pointtonext(point、direction、forward))){fr.count ++; if(mypoints.contains(pointtonext(point、direction、forward))){fr.count ++; if(mypoints.contains(pointtonext(point、direction、forward))){fr.count ++; } else if(enemypoints.contains(point)|| isoutsideofwall(point、direction)){fr.alivestate = half_alive; }} else if(enemypoints.contains(point)|| isoutsideofwall(point、direction)){fr.alivestate = half_alive; }} else if(enemypoints.contains(point、direction)){fr.alivestate = half_alive; }} else if(enemypoints.contains(point)|| isoutsideofwall(point、direction)){fr.alivestate = half_alive; }} else if(enemypoints.contains(point)|| isoutsideofwall(point、direction)){fr.alivestate = half_alive; }} //特定の方向に5つのピースを手に入れることができますか?private int maxcountonthisdirection(ポイントポイント、リスト<point> enemypoints、int direction、int count){int x = point.getx()、y = point.gety(); switch(direction){// Horizontal case Heng:while(!enemypoints.contains(point.setx(point.getx() - 1))&& point.getx()> = 0 && count <6){count ++; } point.setx(x); while(!enemypoints.contains(point.setx(point.getx()+1))&& point.getx()<maxx && count <6){count ++; } 壊す; //垂直ケースZhong:while(!enemypoints.contains(point.sety(point.gety() - 1))&& point.gety()> = 0){count ++; } point.sety(y); while(!enemypoints.contains(point.sety(point.gety()+1))&& point.get.gety()<maxy && count <6){count ++; } 壊す; //フォワードオブリック/ケースzheng_xie:while(!enemypoints.contains(point.setx(point.getx() - 1).sety(point.gety()+1))&& point.getx()> = 0 && point.get()<maxy){count ++; } point.setx(x).sety(y); while(!enemypoints.contains(point.setx(point.getx()+1).sety(point.gety() - 1)&& point.getx()<maxx && point.get.gety()> = 0 && count <6){count ++; } 壊す; // backslash/case fan_xie:while(!enemypoints.contains(point.setx(point.getx() - 1).sety(point.gety() - 1)&& point.getx()> = 0 && point.get()> = 0){count ++; } point.setx(x).sety(y); while(!enemypoints.contains(point.setx(point.getx()+1).sety(point.gety()+1))&& point.getx()<maxx && point.gety()<maxy && count <6){count ++; } 壊す; } return count; } //チェスのピースを再生し、外部インターフェース@Override public void run(list <point> humans、point p){//人間の最後のステップを削除してくださいallfreepoints.remove(humans.size() - 1)); //コンピューターの1つのステップは、ポイント= doanalysis(私のポイント、人間)を作成できます。 //コンピューターのチェス片を削除してくださいallfreepoints.remove(result); //コンピューターチェスのピースに追加して、mypoints.add(result)を再生します。 }}人間のプレイヤーを実装するのは非常に簡単です
java.util.listをインポートします。パブリッククラスHumanPlayerは、baseplayerを拡張します{@Override public void run(list <point> enemypoints、point p){getMypoints()。add(p); allfreepoints.remove(p); }}概要:Javaで記述されていますが、アルゴリズムは抽象化されており、さまざまなプラットフォームの実装に簡単に変更できます。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。