小女子跪求用JAVA编一个坦克,要有严密的算法,最后要和30多个坦克一起打,不能输啊,输了就挂科了,大家好心人啊!!!!!!!!!!!!!!!!

解决方案 »

  1.   

    很复杂的问题哦,只可惜我是个java初学者,帮不了你什么忙,不过祝福你,希望你能加油!
      

  2.   


    JAVA编一个坦克出来了  搞JAVA的都可以进部队当官啦
         呵呵  去csdn下载区去搜搜吧   很多的
      

  3.   

    public class 坦克{
        private String name ="超华丽坦克";
        private int life = 0;
        public void setName(String name){
           this.name=name;
       }
       public String getName(){
           return this.name;
      }  public boolean fight(){
          this.life = 100;
          return true;
      }
    }豪华版坦克,怎么打生命都是100,30个别的坦克,算什么,3万个都打得赢!!!!just joke.up.
      

  4.   

    尚学堂有,到电驴里搜下JAVA就有了,在稍微改下代码就能满足你的要求了
      

  5.   

    http://www.verycd.com/topics/93279/
    马士兵的视频 里面有3个版本的坦克大战
      

  6.   

    楼主你最好去国外的网站下载一个,每年sun都有一个这样的比赛
      

  7.   

    /**
     * 代表方向的枚举类
     * 共九个方向: L-Left LU-LeftUp U-Up RU-RightUp
     *           R-Right RD-RightDown D-Down LD-LeftDown
     *           STOP-Stop
     * 这个类可以用于给Tank或Missile指定方向
     * @author mashibing
     *
     */
    public enum Dir {
    L, LU, U, RU, R, RD, D, LD, STOP
    }import java.awt.Color;
    import java.awt.Graphics;/**
     * 代表爆炸的类
     * 使用大小不一的圆进行模拟
     * @author mashibing
     *
     */
    public class Explode {
    int x, y; private int[] diameters = { 4, 7, 12, 18, 26, 32, 49, 30, 14, 6 }; private boolean live = true; private TankClient tc; int step = 0;

    /**
     * 根据位置产生新的爆炸
     * @param x 爆炸点的x坐标
     * @param y 爆炸点的y坐标
     * @param tc 爆炸所产生的场所
     * @see TankClient
     */
    public Explode(int x, int y, TankClient tc) {
    this.x = x;
    this.y = y;
    this.tc = tc;
    }

    /**
     * 画出爆炸当前的圆
     * @param g 画笔
     * @see java.awt.Graphics
     */
    public void draw(Graphics g) {
    if (!live) {
    tc.explodes.remove(this);
    return;
    } Color c = g.getColor();
    g.setColor(Color.ORANGE);
    g.fillOval(x, y, diameters[step], diameters[step]);
    g.setColor(c); step++;
    if (step == diameters.length) {
    live = false;
    }
    }
    }import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    import java.util.List;/**
     * 代表子弹的类
     * @author mashibing
     *
     */
    public class Missile {
    /**
     * 子弹x方向的速度
     */
    public static final int XSPEED = 10;
    /**
     * 子弹y方向的速度
     */
    public static final int YSPEED = 10;
    /**
     * 子弹的宽度
     */
    public static final int WIDTH = 10;
    /**
     * 子弹的高度
     */
    public static final int HEIGHT = 10; private static int ID = 1; TankClient tc; int tankId; int id; int x, y; Dir dir = Dir.R; boolean live = true; boolean good;

    /**
     * 根据位置等属性构造子弹
     * @param tankId 所属坦克的id号(用于网络版)
     * @param x 子弹产生的x坐标
     * @param y 子弹产生的y坐标
     * @param good 子弹的立场是好还是坏
     * @param dir 子弹的方向
     * @see Dir
     */

    public Missile(int tankId, int x, int y, boolean good, Dir dir) {
    this.tankId = tankId;
    this.x = x;
    this.y = y;
    this.good = good;
    this.dir = dir;
    this.id = ID++;
    }

    /**
     * 根据位置和TankClient构建子弹
     * @param tankId
     * @param x
     * @param y
     * @param good
     * @param dir
     * @param tc 子弹构建的场所
     * @see TankClient
     */
    public Missile(int tankId, int x, int y, boolean good, Dir dir,
    TankClient tc) {
    this(tankId, x, y, good, dir);
    this.tc = tc;
    }

    /**
     * 画出子弹
     * @param g 画笔
     */
    public void draw(Graphics g) {
    if (!live) {
    tc.missiles.remove(this);
    return;
    } Color c = g.getColor();
    g.setColor(Color.BLACK);
    g.fillOval(x, y, WIDTH, HEIGHT);
    g.setColor(c); move();
    } private void move() {
    switch (dir) {
    case L:
    x -= XSPEED;
    break;
    case LU:
    x -= XSPEED;
    y -= YSPEED;
    break;
    case U:
    y -= YSPEED;
    break;
    case RU:
    x += XSPEED;
    y -= YSPEED;
    break;
    case R:
    x += XSPEED;
    break;
    case RD:
    x += XSPEED;
    y += YSPEED;
    break;
    case D:
    y += YSPEED;
    break;
    case LD:
    x -= XSPEED;
    y += YSPEED;
    break;
    case STOP:
    break;
    } if (x < 0 || y < 0 || x > TankClient.GAME_WIDTH
    || y > TankClient.GAME_HEIGHT) {
    live = false;
    }
    }

    /**
     * 取得子弹的外切方形
     * @return 子弹的外切Rectangle
     */
    public Rectangle getRect() {
    return new Rectangle(x, y, WIDTH, HEIGHT);
    }

    /**
     * 检测子弹是否撞到坦克
     * @param t 被检测的坦克
     * @return 如果撞到返回true,否则返回false
     */
    public boolean hitTank(Tank t) {
    if (this.live && t.isLive() && this.good != t.good
    && this.getRect().intersects(t.getRect())) {
    this.live = false;
    t.setLive(false);
    tc.explodes.add(new Explode(x, y, tc));
    return true;
    }
    return false;
    }

    /**
     * 检测是否撞到一系列坦克中的一个
     * @param tanks 被检测的坦克序列
     * @return 如果撞到其中一个,返回true,否则返回false
     */
    public boolean hitTanks(List<Tank> tanks) {
    for (int i = 0; i < tanks.size(); i++) {
    if (this.hitTank(tanks.get(i))) {
    return true;
    }
    }
    return false;
    }
    }
    [code=Java]import java.io.ByteArrayOutputStream;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetSocketAddress;
    import java.net.SocketException;/**
     * 代表子弹消失的消息类
     * @author mashibing
     *
     */
    public class MissileDeadMsg implements Msg {
    int msgType = Msg.MISSILE_DEAD_MSG; TankClient tc; int tankId; int id;

    /**
     * 根据坦克的id和子弹本身的id构建消息
     * @param tankId 坦克id
     * @param id 子弹本身的id
     */
    public MissileDeadMsg(int tankId, int id) {
    this.tankId = tankId;
    this.id = id;
    }

    /**
     * 根据消息产生的场所构建新的消息
     * @param tc
     */
    public MissileDeadMsg(TankClient tc) {
    this.tc = tc;
    }

    /**
     * 分析接收到的消息数据
     * @param dis 接收到的消息数据的输入流
     */
    public void parse(DataInputStream dis) {
    try {
    int tankId = dis.readInt();
    int id = dis.readInt(); // System.out.println("id:" + id + "-x:" + x + "-y:" + y + "-dir:" +
    // dir + "-good:" + good);
    for (int i = 0; i < tc.missiles.size(); i++) {
    Missile m = tc.missiles.get(i);
    if (m.tankId == tankId && m.id == id) {
    m.live = false;
    tc.explodes.add(new Explode(m.x, m.y, tc));
    break;
    }
    } } catch (IOException e) {
    e.printStackTrace();
    } }

    /**
     * 发送相关的消息
     * @param ds 通过该socket发送数据
     * @param IP 数据的目标IP
     * @param udpPort 数据的目标端口
     */
    public void send(DatagramSocket ds, String IP, int udpPort) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    try {
    dos.writeInt(msgType);
    dos.writeInt(tankId);
    dos.writeInt(id);
    } catch (IOException e) {
    e.printStackTrace();
    }
    byte[] buf = baos.toByteArray();
    try {
    DatagramPacket dp = new DatagramPacket(buf, buf.length,
    new InetSocketAddress(IP, udpPort));
    ds.send(dp);
    } catch (SocketException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } }}
    [/code]
      

  8.   

    import java.io.ByteArrayOutputStream;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetSocketAddress;
    import java.net.SocketException;/**
     * 代表子弹产生的消息类
     * @author mashibing
     *
     */
    public class MissileNewMsg implements Msg {
    int msgType = Msg.MISSILE_NEW_MSG; TankClient tc; Missile m;

    /**
     * 根据子弹信息构建新的消息类
     * @param m 产生消息的子弹
     */
    public MissileNewMsg(Missile m) {
    this.m = m;
    }

    /**
     * 根据消息产生的场所构建新的消息
     * @param tc
     */
    public MissileNewMsg(TankClient tc) {
    this.tc = tc;
    }

    /**
     * 发送相关的消息
     * @param ds 通过该socket发送数据
     * @param IP 数据的目标IP
     * @param udpPort 数据的目标端口
     */
    public void send(DatagramSocket ds, String IP, int udpPort) { ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    try {
    dos.writeInt(msgType);
    dos.writeInt(m.tankId);
    dos.writeInt(m.id);
    dos.writeInt(m.x);
    dos.writeInt(m.y);
    dos.writeInt(m.dir.ordinal());
    dos.writeBoolean(m.good);
    } catch (IOException e) {
    e.printStackTrace();
    }
    byte[] buf = baos.toByteArray();
    try {
    DatagramPacket dp = new DatagramPacket(buf, buf.length,
    new InetSocketAddress(IP, udpPort));
    ds.send(dp);
    } catch (SocketException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    /**
     * 分析接收到的消息数据
     * @param dis 接收到的消息数据的输入流
     */
    public void parse(DataInputStream dis) {
    try {
    int tankId = dis.readInt();
    if (tankId == tc.myTank.id) {
    return;
    }
    int id = dis.readInt();
    int x = dis.readInt();
    int y = dis.readInt();
    Dir dir = Dir.values()[dis.readInt()];
    boolean good = dis.readBoolean(); // System.out.println("id:" + id + "-x:" + x + "-y:" + y + "-dir:" +
    // dir + "-good:" + good);
    Missile m = new Missile(tankId, x, y, good, dir, tc);
    m.id = id;
    tc.missiles.add(m);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }}
    import java.io.DataInputStream;
    import java.net.DatagramSocket;
    /**
     * 代表网络协议的数据接口
     * @author mashibing
     *
     */
    public interface Msg {
    /**
     * 坦克产生的消息
     */
    public static final int TANK_NEW_MSG = 1;

    /**
     * 坦克移动的消息
     */
    public static final int TANK_MOVE_MSG = 2;

    /**
     * 子弹产生的消息
     */
    public static final int MISSILE_NEW_MSG = 3;

    /**
     * 坦克死掉的消息
     */
    public static final int TANK_DEAD_MSG = 4;

    /**
     * 子弹死掉的消息
     */
    public static final int MISSILE_DEAD_MSG = 5;

    /**
     * 发送数据
     * @param ds
     * @param IP
     * @param udpPort
     */
    public void send(DatagramSocket ds, String IP, int udpPort);

    /**
     * 接收并处理数据
     * @param dis
     */
    public void parse(DataInputStream dis);
    }
    import java.io.ByteArrayInputStream;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.Socket;
    import java.net.SocketException;
    import java.net.UnknownHostException;/**
     * 代表网络连接的客户端类
     * @author mashibing
     *
     */
    public class NetClient {
    TankClient tc; private int udpPort; String IP; // server IP DatagramSocket ds = null;

    /**
     * 根据场所构建网络客户端
     * @param tc 游戏场所
     */
    public NetClient(TankClient tc) {
    this.tc = tc; }

    /**
     * 连接服务器
     * @param IP 服务器IP
     * @param port 服务器端口
     */
    public void connect(String IP, int port) { this.IP = IP; try {
    ds = new DatagramSocket(udpPort);
    } catch (SocketException e) {
    e.printStackTrace();
    } Socket s = null;
    try {
    s = new Socket(IP, port);
    DataOutputStream dos = new DataOutputStream(s.getOutputStream());
    dos.writeInt(udpPort);
    DataInputStream dis = new DataInputStream(s.getInputStream());
    int id = dis.readInt();
    tc.myTank.id = id; if (id % 2 == 0)
    tc.myTank.good = false;
    else
    tc.myTank.good = true; System.out.println("Connected to server! and server give me a ID:"
    + id);
    } catch (UnknownHostException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (s != null) {
    try {
    s.close();
    s = null;
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    } TankNewMsg msg = new TankNewMsg(tc.myTank);
    send(msg); new Thread(new UDPRecvThread()).start();
    }

    /**
     * 发送消息
     * @param msg 待发送的消息
     */
    public void send(Msg msg) {
    msg.send(ds, IP, TankServer.UDP_PORT);
    } private class UDPRecvThread implements Runnable { byte[] buf = new byte[1024]; public void run() { while (ds != null) {
    DatagramPacket dp = new DatagramPacket(buf, buf.length);
    try {
    ds.receive(dp);
    parse(dp);
    System.out.println("a packet received from server!");
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    } private void parse(DatagramPacket dp) {
    ByteArrayInputStream bais = new ByteArrayInputStream(buf, 0, dp
    .getLength());
    DataInputStream dis = new DataInputStream(bais);
    int msgType = 0;
    try {
    msgType = dis.readInt();
    } catch (IOException e) {
    e.printStackTrace();
    }
    Msg msg = null;
    switch (msgType) {
    case Msg.TANK_NEW_MSG:
    msg = new TankNewMsg(NetClient.this.tc);
    msg.parse(dis);
    break;
    case Msg.TANK_MOVE_MSG:
    msg = new TankMoveMsg(NetClient.this.tc);
    msg.parse(dis);
    break;
    case Msg.MISSILE_NEW_MSG:
    msg = new MissileNewMsg(NetClient.this.tc);
    msg.parse(dis);
    break;
    case Msg.TANK_DEAD_MSG:
    msg = new TankDeadMsg(NetClient.this.tc);
    msg.parse(dis);
    break;
    case Msg.MISSILE_DEAD_MSG:
    msg = new MissileDeadMsg(NetClient.this.tc);
    msg.parse(dis);
    break;
    } } }

    /**
     * 取得UDP端口(客户端接收数据用)
     * @return
     */
    public int getUdpPort() {
    return udpPort;
    }

    /**
     * 设定UDP端口(客户端接收数据用)
     * @param udpPort
     */
    public void setUdpPort(int udpPort) {
    this.udpPort = udpPort;
    }
    }
      

  9.   

    尚学堂科技_坦克大战网络版视频教程   点击下载里面有,你看看http://www.bjsxt.com/load.htm
      

  10.   

    你参考参考一下尚学堂科技_坦克大战网络版视频教程 
    http://www.bjsxt.com/load.htm
      

  11.   

    最后打30个还不能输,玩java的超级坦克也用挂么?
      

  12.   

    <html>
    <body><hr><body>
    <html>
      

  13.   

    <html> 
    <body> 
     
    <hr> 
     
    </body> 
    </html>