java实现马踏棋盘游戏

用java实现马踏棋盘游戏算法,供大家参考,具体内容如下

在4399小游戏中有这样一个游戏

java实现马踏棋盘游戏_第1张图片

这是代码实现

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 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 next(Point curpoint){
        ArrayList ps = new ArrayList();
        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)=0&&(p1.y = curpoint.y + 2)=0&&(p1.y = curpoint.y + 1) ps){
        ps.sort(new Comparator() {
            @Override
            public int compare(Point o1, Point o2) {
                int count1 = next(o1).size();
                int count2 = next(o2).size();
                if(count1 
 

然后照着步骤一步一步下就可以了

java实现马踏棋盘游戏_第2张图片

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(java实现马踏棋盘游戏)