java实现马踏棋盘游戏

网友投稿 286 2022-08-28


java实现马踏棋盘游戏

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

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

这是代码实现

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(http://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

chessboard[row][column] = 0;

visited[row * X + column] = false;

}

else {

finished = true;

}

}

//判断当前棋子下一个可以走的所有位置数组

public static ArrayList 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));

}

nlBhKd 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)

ps.add(new Point(p1));

}

if((p1.x = curpoint.x + 1)

ps.add(new Point(p1));

}

if((p1.x = curpoint.x - 1)>=0&&(p1.y = curpoint.y + 2)

ps.add(new Point(p1));

}

if((p1.x = curpoint.x - 2)>=0&&(p1.y = curpoint.y + 1)

ps.add(new Point(p1));

}

return ps;

}

//使用贪心算法提高算法运行速度

public static void sort(ArrayList 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

return -1;

}else if (count1 == count2){

return 0;

}

else {

return 1;

}

}

});

}

}

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

chessboard[row][column] = 0;

visited[row * X + column] = false;

}

else {

finished = true;

}

}

//判断当前棋子下一个可以走的所有位置数组

public static ArrayList 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));

}

nlBhKd 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)

ps.add(new Point(p1));

}

if((p1.x = curpoint.x + 1)

ps.add(new Point(p1));

}

if((p1.x = curpoint.x - 1)>=0&&(p1.y = curpoint.y + 2)

ps.add(new Point(p1));

}

if((p1.x = curpoint.x - 2)>=0&&(p1.y = curpoint.y + 1)

ps.add(new Point(p1));

}

return ps;

}

//使用贪心算法提高算法运行速度

public static void sort(ArrayList 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

return -1;

}else if (count1 == count2){

return 0;

}

else {

return 1;

}

}

});

}

}

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

ps.add(new Point(p1));

}

if((p1.x = curpoint.x + 1)

ps.add(new Point(p1));

}

if((p1.x = curpoint.x - 1)>=0&&(p1.y = curpoint.y + 2)

ps.add(new Point(p1));

}

if((p1.x = curpoint.x - 2)>=0&&(p1.y = curpoint.y + 1)

ps.add(new Point(p1));

}

return ps;

}

//使用贪心算法提高算法运行速度

public static void sort(ArrayList 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

return -1;

}else if (count1 == count2){

return 0;

}

else {

return 1;

}

}

});

}

}

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

ps.add(new Point(p1));

}

if((p1.x = curpoint.x - 1)>=0&&(p1.y = curpoint.y + 2)

ps.add(new Point(p1));

}

if((p1.x = curpoint.x - 2)>=0&&(p1.y = curpoint.y + 1)

ps.add(new Point(p1));

}

return ps;

}

//使用贪心算法提高算法运行速度

public static void sort(ArrayList 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

return -1;

}else if (count1 == count2){

return 0;

}

else {

return 1;

}

}

});

}

}

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

ps.add(new Point(p1));

}

if((p1.x = curpoint.x - 2)>=0&&(p1.y = curpoint.y + 1)

ps.add(new Point(p1));

}

return ps;

}

//使用贪心算法提高算法运行速度

public static void sort(ArrayList 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

return -1;

}else if (count1 == count2){

return 0;

}

else {

return 1;

}

}

});

}

}

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

ps.add(new Point(p1));

}

return ps;

}

//使用贪心算法提高算法运行速度

public static void sort(ArrayList 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

return -1;

}else if (count1 == count2){

return 0;

}

else {

return 1;

}

}

});

}

}

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

return -1;

}else if (count1 == count2){

return 0;

}

else {

return 1;

}

}

});

}

}

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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:python之处理selenium工具中的frame/iframe/frameset框架切换问题(selenium切换iframe窗口)
下一篇:PySnooper – 一个更好的python调试工具(pysnooper用法)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~