21xrx.com
2025-03-28 07:01:11 Friday
文章检索 我的文章 写文章
Java课程设计实验报告:动物换位
2023-06-17 09:09:19 深夜i     8     0

   return p.x == empty.x && Math.abs(p.y-empty.y) == 100 ||

       p.y == empty.y && Math.abs(p.x-empty.x) == 100;

在本次Java课程设计实验中,我们需要实现一个动物换位的小游戏。这个小游戏的规则是,有一个3*3的方格,其中有8个方格放着不同的动物,另一个方格为空。玩家需要点击周围的方格,将空格移动到想要放置动物的位置上,最终将所有的动物换到正确的位置上。

首先,我们需要定义每个动物的图片和位置。然后,我们可以使用Java的Swing库来创建一个图形界面,将所有的动物以及空格放置在这个界面上。玩家可以通过点击鼠标来移动空格,我们可以通过编写鼠标事件来实现这个功能。当玩家移动了空格,程序需要判断是否完成了游戏,如果完成了,弹出提示框告诉玩家游戏结束。

下面是一个简单的Java代码示例:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AnimalSwap extends JFrame implements MouseListener {
  // 动物图片
  private ImageIcon i0 = new ImageIcon("dog.png");
  private ImageIcon i1 = new ImageIcon("cat.png");
  private ImageIcon i2 = new ImageIcon("lion.png");
  private ImageIcon i3 = new ImageIcon("panda.png");
  private ImageIcon i4 = new ImageIcon("tiger.png");
  private ImageIcon i5 = new ImageIcon("elephant.png");
  private ImageIcon i6 = new ImageIcon("monkey.png");
  private ImageIcon i7 = new ImageIcon("rabbit.png");
 
  // 动物位置
  private Point p0 = new Point(0, 0);
  private Point p1 = new Point(100, 0);
  private Point p2 = new Point(200, 0);
  private Point p3 = new Point(0, 100);
  private Point p4 = new Point(100, 100);
  private Point p5 = new Point(200, 100);
  private Point p6 = new Point(0, 200);
  private Point p7 = new Point(100, 200);
  private Point p8 = new Point(200, 200);
  private Point empty = new Point(200, 200);
 
  // 动物数组
  private ImageIcon[] animals = i0;
  private Point[] points = p8;
 
  // 窗口构造函数
  public AnimalSwap() {
   setSize(300, 300);
   setResizable(false);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   addMouseListener(this);
   setVisible(true);
  }
 
  // 绘制动物图像
  public void paint(Graphics g) {
   for(int i=0; i
     Point p = points[i];
     animals[i].paintIcon(this, g, p.x, p.y);
   }
  }
  // 点击鼠标事件
  public void mouseClicked(MouseEvent e) {
   int x = e.getX();
   int y = e.getY();
   Point clicked = new Point(x, y);
   for(int i=0; i
     Point p = points[i];
     if(clicked.equals(p)) {
      if(isEmpty(p)) {
        // 移动空格
        empty = p;
        repaint();
        if(checkWin()) {
         JOptionPane.showMessageDialog(this, "You win!");
        }
      } else if(isAdjacent(p)) {
        // 交换两个格子
        swap(p);
        repaint();
        if(checkWin()) {
         JOptionPane.showMessageDialog(this, "You win!");
        }
      }
     }
   }
  }
  // 判断该位置是否为空
  public boolean isEmpty(Point p) {
   return p.equals(empty);
  }
  // 判断该位置是否与空格直接相邻
  public boolean isAdjacent(Point p) {
   return p.x == empty.x && Math.abs(p.y-empty.y) == 100 ||
       p.y == empty.y && Math.abs(p.x-empty.x) == 100;
  }
  // 交换两个位置
  public void swap(Point p) {
   ImageIcon temp = getAnimal(p);
   setAnimal(p, getAnimal(empty));
   setAnimal(empty, temp);
   empty = p;
  }
  // 获取某个位置上的动物
  public ImageIcon getAnimal(Point p) {
   for(int i=0; i
     if(points[i].equals(p)) {
      return animals[i];
     }
   }
   return null;
  }
  // 设置某个位置上的动物
  public void setAnimal(Point p, ImageIcon animal) {
   for(int i=0; i
     if(points[i].equals(p)) {
      animals[i] = animal;
      return;
     }
   }
  }
  // 判断是否完成游戏
  public boolean checkWin() {
   for(int i=0; i
     if(!points[i].equals(empty)) {
      if(!animals[i].getImage().equals(getAnimal(points[i]).getImage()))
        return false;
      
     }
   }
   return true;
  }
  // 鼠标事件未使用的空函数
  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}
  public void mousePressed(MouseEvent e) {}
  public void mouseReleased(MouseEvent e) {}
  // 主函数入口
  public static void main(String[] args) {
   AnimalSwap game = new AnimalSwap();
  }
}

这个小游戏不仅可以帮助我们巩固Java编程知识,还可以锻炼我们的空间思维和逻辑思维。如果有兴趣,可以将其改为更难的4*4方格,或者将动物图片改成其他主题。

  
  
下一篇: Java语言中

评论区

请求出错了