溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用java實(shí)現(xiàn)馬踏棋盤游戲

發(fā)布時間:2022-02-14 14:34:57 來源:億速云 閱讀:126 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下如何使用java實(shí)現(xiàn)馬踏棋盤游戲,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

具體內(nèi)容如下

在4399小游戲中有這樣一個游戲

如何使用java實(shí)現(xiàn)馬踏棋盤游戲

這是代碼實(shí)現(xiàn)

package com.HorseChess;

import java.awt.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;

public class HorseChess {
    private static int X;
    private static int Y;
    private static boolean visited[];
    private static boolean finished;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入行:");
        X = sc.nextInt();
        System.out.println("請輸入列:");
        Y = sc.nextInt();
        System.out.println("請輸入棋子所在行:");
        int row = sc.nextInt();
        System.out.println("請輸入棋子所在列:");
        int column = sc.nextInt();
        int [][] chessboard = new int[X][Y];
        visited = new boolean[X*Y];
        traverchess(chessboard,row-1,column-1,1);
        for(int[] rows : chessboard){
            for (int step : rows){
                System.out.print(step + "\t");
            }
            System.out.println();
        }
    }

    public static void traverchess(int[][] chessboard,int row,int column,int step){
        chessboard[row][column] = step;
        visited[row * X+column] = true;
        ArrayList<Point> ps = next(new Point(column,row));
        sort(ps);
        while (!ps.isEmpty()){
            Point p = ps.remove(0);
            if(!visited[p.y*X+p.x]){
                traverchess(chessboard,p.y,p.x,step+1);
            }
        }

        if(step<X*Y&&!finished){
            chessboard[row][column] = 0;
            visited[row * X + column] = false;
        }
        else {
            finished = true;
        }
    }
    //判斷當(dāng)前棋子下一個可以走的所有位置數(shù)組
    public static ArrayList<Point> next(Point curpoint){
        ArrayList<Point> ps = new ArrayList<Point>();
        Point p1 = new Point();
        if((p1.x = curpoint.x - 2)>=0&&(p1.y = curpoint.y - 1)>=0){
            ps.add(new Point(p1));
        }
        if((p1.x = curpoint.x - 1)>=0&&(p1.y = curpoint.y - 2)>=0){
            ps.add(new Point(p1));
        }
        if((p1.x = curpoint.x + 1)< X && (p1.y = curpoint.y - 2)>=0){
            ps.add(new Point(p1));
        }
        if((p1.x = curpoint.x + 2)< X && (p1.y = curpoint.y - 1)>=0){
            ps.add(new Point(p1));
        }
        if((p1.x = curpoint.x + 2)<X&&(p1.y = curpoint.y + 1)<Y){
            ps.add(new Point(p1));
        }
        if((p1.x = curpoint.x + 1)<X&&(p1.y = curpoint.y + 2)<Y){
            ps.add(new Point(p1));
        }
        if((p1.x = curpoint.x - 1)>=0&&(p1.y = curpoint.y + 2)<Y){
            ps.add(new Point(p1));
        }
        if((p1.x = curpoint.x - 2)>=0&&(p1.y = curpoint.y + 1)<Y){
            ps.add(new Point(p1));
        }
        return ps;
    }

    //使用貪心算法提高算法運(yùn)行速度
    public static void sort(ArrayList<Point> ps){
        ps.sort(new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                int count1 = next(o1).size();
                int count2 = next(o2).size();
                if(count1<count2){
                    return  -1;
                }else if (count1 == count2){
                    return 0;
                }
                else {
                    return  1;
                }
            }
        });
    }
}

然后照著步驟一步一步下就可以了

如何使用java實(shí)現(xiàn)馬踏棋盤游戲

以上是“如何使用java實(shí)現(xiàn)馬踏棋盤游戲”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI