JAVA 小程序 赛马
- 格式:doc
- 大小:40.00 KB
- 文档页数:2
Java实现马踏棋盘算法本⽂实例为⼤家分享了Java实现马踏棋盘的具体代码,供⼤家参考,具体内容如下马在某个点最多可能有8种⾛法,⽤递归和回溯实现。
注:代码中,查找下⼀个可⾛坐标是从右下第⼀个开始的,也就是图中的4。
可以通过修改a,b...h的值来改变顺序。
代码:/*** 马踏棋盘算法* 递归和回溯**/public class HorseStep {public static int X = 8;public static int Y = 8;public static int returnCount = 0;/*** 棋盘*/public static int chess[][] = new int[X][Y];/*** 找到基于(x,y)位置的下⼀个可⾛位置* @param x* @param y* @param count* @return*/public static int nextxy(XY xy,int count){final int a=0,b=1,c=2,d=3,e=4,f=5,g=6,h=7;int x = xy.getX();int y = xy.getY();int returnInt = 0;switch (count) {// 从以x,y为轴⼼的右下开始case a:if( x+2<=X-1 && y+1<=Y-1 && chess[y+1][x+2]==0){x +=2;y +=1;returnInt = 1;}break;case b:if( x+1<=X-1 && y+2<=Y-1 && chess[y+2][x+1]==0){x +=1;y +=2;returnInt = 1;}break;case c:if( x-1>=0 && y+2<=Y-1 && chess[y+2][x-1]==0){ x -=1;y +=2;returnInt = 1;}break;case d:if( x-2>=0 && y+1<=Y-1 && chess[y+1][x-2]==0){ x -=2;y +=1;returnInt = 1;}break;case e:if( x-2>=0 && y-1>=0 && chess[y-1][x-2]==0){x -=2;y -=1;returnInt = 1;}break;case f:if( x-1>=0 && y-2>=0 && chess[y-2][x-1]==0){x -=1;y -=2;returnInt = 1;}break;case g:if( x+1<=X-1 && y-2>=0 && chess[y-2][x+1]==0){ x +=1;y -=2;returnInt = 1;}break;case h:if( x+2<=X-1 && y-1>=0 && chess[y-1][x+2]==0){ x +=2;y -=1;returnInt = 1;}break;default:break;}if(returnInt == 1){xy.setX(x);xy.setY(y);return 1;}return 0;}/*** 打印棋盘*/public static void print(){for(int i=0;i<X;i++){for(int j=0;j<Y;j++){if(chess[i][j]<10)System.out.print(chess[i][j]+" ");elseSystem.out.print(chess[i][j]+" ");}System.out.println();}}/*** 深度优先遍历棋盘* @param x* @param y* @param tag* @return* (x,y)为位置坐标* tag是标记变量,每⾛⼀步 tag+1。
package Test1;import java.util.Scanner;public class LOLTest<AnyType> {/*** @param args*/Node firstNode=new Node(0);Node secondNode=new Node(0);int sizeA=1;int sizeB=1;public int getSizeA() {return sizeA;}public int getSizeB() {return sizeB;}public static void main(String[] args) {// TODO Auto-generated method stubScanner reader = new Scanner(System.in);int T = reader.nextInt();for (int i = 0; i < T; i++) {int m = reader.nextInt();LOLTest test = new LOLTest();double[] a = new double[m];double[] b = new double[m];for (int j = 0; j < m; j++) {a[j] = reader.nextDouble();}for (int j = 0; j < m; j++) {b[j] = reader.nextDouble();}test.sort(a);test.sort(b);test.create1(test.firstNode, a);test.create2(test.secondNode, b);int sum = test.abcd();if (sum >= 0) {System.out.println("Xiaoming will get " + sum + "QB");} else {System.out.println("Xiaotao will get " + Math.abs(sum) + "QB");}}}// 计算sum并返回public <AnyType extends Comparable<? super AnyType>> int abcd() {int sum = 0;while (firstNode.next!=null) {Node p=firstNode.next;Node q=secondNode.next;if (p.data > q.data) {sum=sum+200;remove1(1);remove2(1);}if (p.data == q.data) {Node c=getNode(getSizeA(),firstNode);Node d=getNode(getSizeB(),secondNode);if(c.data<=d.data&&c.data!=p.data){remove1(1);remove2(getSizeB()-1);sum=sum-200;}else if(c.data>d.data){remove1(getSizeA()-1);remove2(getSizeB()-1);sum=sum+200;}else if(c.data==d.data&&c.data==p.data){remove1(getSizeA()-1);remove2(getSizeA()-1);}}if (p.data < q.data) {sum=sum-200;remove1(1);remove2(getSizeB()-1);}}return sum;}//获得idx位置的节点public Node getNode(int idx,Node node){Node p=node;for(int i=0;i<idx-1;i++){p=p.next;}return p;}// 删除单链表中的元素public void remove1(int idx){Node p=firstNode;for(int i=0;i<idx-1;i++){p=p.next;}Node node=p.next.next;p.next=node;sizeA--;}public void remove2(int idx){Node p=secondNode;for(int i=0;i<idx-1;i++){p=p.next;}Node node=p.next.next;p.next=node;sizeB--;}// 对数组进行构造单链表public void create1(Node node, double[] a) { Node p = node;for (int i = 0; i < a.length; i++) {p.next = new Node(a[i]);p = p.next;sizeA++;}}public void create2(Node node, double[] a) { Node p = node;for (int i = 0; i < a.length; i++) {p.next = new Node(a[i]);p = p.next;sizeB++;}}// 对输入的数组进行排序public void sort(double[] a) {for (int i = 0; i < a.length; i++) {for (int j = i; j < a.length - 1; j++) {if (a[j + 1] < a[i]) {double c = a[j + 1];a[j + 1] = a[i];a[i] = c;}}}}// 节点类private static class Node {double data;Node next;Node() {}Node(double date) {this.data = date;this.next = null;}public Node(double d, Node n) {this.data = d;this.next = n;}}}。
赛马⽹ACM试题(原杭电ojACM)java版答案(1000,10001,1002)赛马⽹ACM试题(原杭电OJ ACM试题)答案(java版)突然⼿痒,来做⼀下acm试题练练⼿,由于最近在学java,顺便练⼀下java编程。
但是对于ACM训练,c会更好,因为c的时间效率更⾼⼀些,这⽅⾯⽐java有优势。
其实调调⼩程序就像品茶⼀样也挺有意思的(怎么闻到⼀股屌丝⽓息)。
最近也在找⼯作阶段,对于新兴的在线⽐赛,在线程序测试略有感触,这是⼀个⼤趋势,也是互联⽹公司招聘的⼀个优势吧,不过诸多问题还有待改善,这⾥不详述。
对于计算机专业出⾝,编程是基础,想要进阶,就先积累点滴吧。
注意:提交的java代码的类名都必须为Main第1000题:A+B ProblemProblem DescriptionCalculateA + B.InputEach line will contain two integersA andB. Process to end of file.OutputFor each case, outputA +B in one line.Sample Input1 1Sample Output2题⽬解析:要求每⾏输⼊两个数,计算两数的和并输出,这⾥是要循环的输⼊,程序⾃动输出!代码:import java.util.Scanner;public class Main{public static void main(String[] args){Scanner in = new Scanner(System.in);while(in.hasNextInt()){int a = in.nextInt();int b = in.nextInt();System.out.println(a+b);}}}第1001题:Sum ProblemProblem DescriptionHey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.InputThe input will consist of a series of integers n, one integer per line.OutputFor each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer. Sample Input1100Sample Output15050题⽬解析:输⼊⼀个数n,计算1+2+3+...+n的值。
值得苦练的55个java小项目在这个数字化时代,掌握一门编程语言对于个人发展和职业规划至关重要。
而在众多编程语言中,Java是一门广泛使用的编程语言,尤其在企业级应用开发中应用广泛。
为了帮助初学者提升自己的Java编程能力,以下是值得苦练的55个Java小项目。
1.计算器:创建一个简单的图形用户界面(GUI)来模拟一个计算器,并实现基本的加、减、乘、除功能。
2.猜数游戏:编写一个程序,在一定范围内生成一个随机数,然后用户通过输入猜测数字,程序逐步给出用户的猜测是否正确。
3.五子棋游戏:实现一个供两人玩的五子棋游戏,包括游戏规则、棋盘、计分等功能。
4.银行管理系统:设计和实现一个基本的银行管理系统,包括账户创建、存款、取款、转账等功能。
5.图书管理系统:创建一个图书管理系统,包括图书的添加、删除、借出和归还等功能。
6.学生成绩管理系统:创建一个学生成绩管理系统,包括学生信息录入、查询、修改和删除等功能。
7.聊天室应用:使用Socket编程创建一个简单的聊天室应用,允许多个用户之间进行实时通信。
8.网页爬虫:编写一个程序以网络爬虫的方式访问网页并提取所需的信息。
9.文件压缩器:创建一个程序,能够将多个文件压缩为一个压缩文件,并能够解压缩已压缩的文件。
10.文件比较器:编写一个程序,用于比较两个文件的内容,以确定它们是否相同或相似。
11.数据库连接应用:使用Java JDBC连接到数据库,并实现增加、删除和修改数据的功能。
12.日历应用:开发一个简单的日历应用,可以显示当前日期、添加事件和提醒等功能。
13.地址簿应用:创建一个能够存储联系人信息的地址簿应用,包括添加、查找、编辑和删除联系人等功能。
14.天气应用:使用天气API获取实时天气信息,并显示在一个简单的用户界面中。
15.网络速度测试应用:开发一个简单的应用程序,用于测试用户的网络速度。
16.电子邮件客户端:编写一个程序,用于发送和接收电子邮件。
骑马游戏编程实现骑马游戏是一种受欢迎的游戏类型,玩家可以在虚拟世界中体验骑马的乐趣。
本文将介绍骑马游戏编程的实现方法,帮助读者了解其中的技术原理和步骤。
一、游戏引擎选择在开始编写骑马游戏前,我们首先需要选择一个适合的游戏引擎作为开发工具。
常见的游戏引擎有Unity、Unreal Engine等,它们提供了丰富的功能和插件,可以加速开发过程。
二、创建游戏场景在选择好游戏引擎后,我们可以开始创建游戏场景。
骑马游戏通常需要包含马匹、地形、障碍物等元素。
通过游戏引擎提供的工具,我们可以轻松地导入模型、设置材质,并对场景进行设计和编辑。
三、马匹动画控制为了实现逼真的骑马体验,我们需要对马匹的动画进行控制。
通过游戏引擎提供的动画系统,可以为马匹创建奔跑、跳跃、停止等动作,并设置过渡效果,以使动画过渡自然流畅。
四、玩家控制与交互玩家操控角色骑马进行游戏,我们需要实现玩家的控制与交互功能。
通过编程,可以让玩家通过键盘、手柄或触摸屏来控制马匹的移动、跳跃等操作。
同时,还可以实现与场景中其他元素的交互,比如收集物品、攻击怪物等。
五、景观渲染与光照效果为了提升游戏的视觉效果,我们需要对景观进行渲染,并添加适当的光照效果。
通过游戏引擎提供的渲染器和光源组件,可以实现绚丽的天空、真实的光照和阴影效果,增强游戏的沉浸感。
六、音效与背景音乐音效和背景音乐是游戏中不可或缺的一部分,为游戏增添了氛围和乐趣。
我们可以使用游戏引擎提供的音频组件,导入音效和音乐文件,并通过编程控制其播放和停止。
七、游戏逻辑与算法骑马游戏中的各种逻辑和算法决定了游戏的玩法和规则。
比如,我们可以实现一个追踪算法,让马匹自动追踪特定目标;或者设计一个随机生成算法,生成不同的地形和障碍物。
通过编程,我们可以实现这些复杂的逻辑和算法,让游戏更加有趣。
八、优化与测试编写完骑马游戏后,我们需要进行优化和测试,以确保游戏的稳定性和流畅性。
通过优化代码、调整参数和测试性能,可以提升游戏的帧率、减少卡顿现象,并解决潜在的bug和问题。
赛马游戏源代码:(截图)package com.guigu.king.core;public interface HandleListener{/*** 完成比赛*/public void gameHourse(HourseEvent e);/*** 比赛结束(线程结局)*/public void gameOver(HourseEvent e); }package com.guigu.king.core;public class HourseEvent{private Object source=null;private boolean error=false;private Exception exception=null;public HourseEvent(Object source){this.source=source;}public Object getSource() {return source;}public boolean isError() {return error;}public void setError(boolean error) {this.error = error;}public Exception getException() {return exception;}public void setException(Exception exception) { this.exception = exception;}}package com.guigu.king.core;import java.awt.Image;import java.awt.Toolkit;import java.util.Random;/*** 赛马类* @author Administrator*/public class HourseRace implements Runnable{public HandleListener handle=null;public void addHandleListener(HandleListener handle){this.handle=handle;}private String name="";//赛马名称private Image img=null;//赛马图片private int length=0;//赛马的步长(每跑一步的距离)private int[] rate=null;//赛马的步频(每一次奔跑的时间间隔)public HourseRace(String name,String imgPath,int length,int[] rate) {=name;this.img=Toolkit.getDefaultToolkit().getImage(imgPath);this.length=length;this.rate=rate;}private int x=0;//赛马当前赛跑的位置private int range=0;//赛跑的距离private int stop=0;//赛跑的距离public void setRaceData(int range,int stop){this.x=0;this.range=range;this.stop=stop;}public void run(){HourseEvent event=new HourseEvent(this);try{boolean b=true;Random r=new Random();while(true){int n=r.nextInt(rate.length);if (b && this.x>=this.range-116){b=false;if (handle!=null) handle.gameHourse(event);}this.x=this.x+this.length;Thread.sleep(rate[n]);if (x>=this.stop){break;}}}catch(Exception e){event.setError(true);event.setException(e);}this.x=0;if (handle!=null) handle.gameOver(event);}public String getName() {return name;}public Image getImg() {return img;}public int getX() {return x;}}package com.guigu.king.core;/*** 赛场类* @author Administrator*/public class Match{private String name="";//赛场的名称private int range=0;//赛跑的距离private int stop=0;//停跑的距离private HourseRace[] track=null;//赛道数量private int HourseRaceCount=0;//赛马数量public Match(String name,int range,int stop,int count) {=name;this.range=range;this.stop=stop;this.track=new HourseRace[count];}public void addHourseRace(HourseRace hr){if (hr!=null){hr.setRaceData(range, stop);if (this.HourseRaceCount<this.track.length){this.track[this.HourseRaceCount]=hr;this.HourseRaceCount++;}}}public int getMatchTrack(){return this.track.length;}public HourseRace getHourseRace(int index){return this.track[index];}public String getName() {return name;}public int getRange() {return range;public int getStop() {return stop;}}package com.guigu.king.exec;public class CMain{/*** @param args*/public static void main(String[] args){MainFrame mf=new MainFrame();mf.setVisible(true);}}package com.guigu.king.exec;import java.awt.*;import javax.swing.*;import com.guigu.king.core.*;public class GraphicPanel extends JPanel{/****/private static final long serialVersionUID = 2441325657329104186L;private Match match=null;public GraphicPanel(Match match){this.match=match;this.setBackground(Color.WHITE);}//绘图函数:函数重写,该函数的调用,是由系统调用。
JAVA课程设计题目Java课程设计题目一、课程设计总体要求:本次课程设计是对前面学过的所有面向对象的编程思想以及编程方法的一个总结、回顾和实践,因此,开始设计前学生一定要先回顾以前所学的内容,明确本次作业设计所要用到的技术点并到网上搜索以及查阅相关的书籍来搜集资料。
通过编写一个基于JA V A的应用系统综合实例,来掌握Java语言编程技巧。
二、参考题目及要求:1、编写一个记事本程序要求:用图形用户界面实现。
能实现编辑、保存、另存为、查找替换等功能。
提示:使用文件输入输出流。
2、模拟龟兔赛跑要求:用图形用户界面实现。
能设置比赛时间,马匹的数量等等。
在任意时间段内马匹的速度是随机的。
开始比赛之后以动画显示赛马过程。
提示:使用多线程3、学生信息管理系统要求:使用图形用户界面用数据库建立1或2个学生信息表。
(不限使用哪种数据库)能连接数据库并实现查询、增、删、改等功能。
4、图书信息管理系统要求:使用图形用户界面用数据库建立1或2个图书信息表。
(不限使用哪种数据库)能连接数据库并实现查询、增、删、改等功能。
5、聊天小程序要求:使用图形用户界面。
能实现一个聊天室中多人聊天。
可以两人私聊。
提示:使用socket通信6、ATM柜员机模拟程序要求:使用图形用户界面。
当输入给定的卡号和密码(初始卡号和密码为123456)时,系统能登录ATM柜员机系统,用户可以按照以下规则进行:1、查询余额:初始余额为10000元2、A TM取款:每次取款金额为100的倍数,总额不超过5000元,支取金额不允许透支。
3、A TM存款:不能出现负存款。
4、修改密码:新密码长度不小于6位,不允许出现6位完全相同的情况,只有旧密码正确,新密码符合要求,且两次输入相同的情况下才可以成功修改密码。
7、超市收银系统要求:使用图形用户界面。
由收银员输入顾客的会员卡卡号(若有卡)、所购商品的货号等。
从数据库中取出有关价格信息,再把这些信息返回给收银台。
java实现马踏棋盘游戏⽤java实现马踏棋盘游戏算法,供⼤家参考,具体内容如下在4399⼩游戏中有这样⼀个游戏这是代码实现package com.HorseChess;import java.awt.*;import java.util.ArrayList;import parator;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;}}//判断当前棋⼦下⼀个可以⾛的所有位置数组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;}//使⽤贪⼼算法提⾼算法运⾏速度public static void sort(ArrayList<Point> ps){ps.sort(new Comparator<Point>() {@Overridepublic 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;}}});}}然后照着步骤⼀步⼀步下就可以了以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
1、Horse
public class Horse extends JLabel implements Runnable {
public Thread horseThread; public String horseName;
public int horseMoveFrom;//起点public int horseMoveTo;//终点
public int horseY;//垂直位置public Dimension horseSize;//马的大小
public Horse(String horseName ,Dimension horseSize ,int horseMoveFrom ,int horseMoveTo ,int horseY) {
this.horseName = horseName; this.horseSize = horseSize;
this.horseMoveFrom = horseMoveFrom; this.horseMoveTo = horseMoveTo;
this.horseY = horseY; this.setText(horseName);
this.setHorizontalTextPosition(CENTER);
this.setVerticalTextPosition(TOP);
this.setSize(horseSize);
this.setIcon(new ImageIcon(getClass().getResource("imgs/RaceHorse/horse.gif")));
this.horseGoto(this.horseMoveFrom, this.horseY);
}
public void createThread() {
this.horseThread = new Thread(this); this.horseThread.start();
}
public void horseGoto(int x, int y) { this.setLocation(x, y); }
public void run() {
int xPosition = horseMoveFrom; //this.horseThread.suspend();
synchronized (this) {
try {
wait();
} catch (InterruptedException e) { e.printStackTrace(); }
}
while(true) {
xPosition += 2;
try{
long randomMoveTime = (long)(Math.random()*40);
this.horseThread.sleep(randomMoveTime);
this.horseGoto(xPosition, horseY);
}catch(Exception ex) { System.out.println("Error!"); }
if(xPosition>=horseMoveTo) {
xPosition = horseMoveFrom;
this.horseGoto(xPosition, horseY);
synchronized (this) {
try {
this.wait();
} catch (InterruptedException e) { e.printStackTrace(); }
}
}else if(xPosition>=(horseMoveTo-this.horseSize.width)&&
xPosition<(horseMoveTo-this.horseSize.width)+2)
{//报告到达终点 //endSet(this);}
}
}
public synchronized void sleepUp(){ this.notify(); }
}
2、RaceHorse
public class RaceHorse extends JFrame implements ActionListener{
private JPanel contentPane; //界面变量
JPanel horsesP, command;//定义两个面板,马所在的和命令面板
JPanel endLine;
//全局变量
Horse horse[] = new Horse[4];
Horse winner[] = new Horse[4];
int nowWinNum = 0;//本次已到终点的马数
int heaveRaceedNum = 0;//已赛马的次数
boolean startFlag = false;//运行中的标志为true时表示已开始
int horseRestorationNum = 4;//马匹复位的个数
public RaceHorse() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 518, 416);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("\u5F00\u59CB\u8D5B\u9A6C");
btnNewButton.setBounds(224, 354, 95, 25);
contentPane.add(btnNewButton);
init();
btnNewButton.addActionListener(this);
}
public void init(){
final int endInt = 600;
horsesP = new JPanel(null); horsesP.setBounds(10, 10, 700, 350); //马匹面板horsesP.setMaximumSize(new Dimension(10,10));
horsesP.setBackground(Color.white); endLine = new JPanel();
endLine.setBounds(endInt,0,2, 280); endLine.setBackground(Color.red);
horsesP.add(endLine);
for(int i=0;i<4;i++) {//初始化马匹
horse[i] = new Horse((i+1)+"号马",new Dimension(116,80),30,endInt,80*i);
horsesP.add(horse[i]);
horse[i].createThread();
}
getContentPane().add(horsesP);
}
public static void main(String[] args) {
RaceHorse frame = new RaceHorse();
}
public void actionPerformed(ActionEvent e) {
for(int i=0; i<4; i++) {
//horse[i].horseThread.resume();//horse[i].notifyAll();
horse[i].sleepUp();
}
}
}。