第十一次实验课代码

添加C08~C011
This commit is contained in:
2025-11-20 17:01:56 +08:00
parent 1c869f816f
commit 9930f2421a
67 changed files with 1137 additions and 7 deletions

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@@ -0,0 +1,24 @@
import java.util.*;
public class Josephus {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<Integer> arr = new ArrayList<>();
for(int i=0;i<n;++i)
arr.add(i+1);
int cnt = 0;
int i=2;
while(arr.size()>2) {
Iterator<Integer> it = arr.iterator();
while(it.hasNext()) {
it.next();
cnt++;
if(cnt==3) {
it.remove();
cnt = 0;
}
}
System.out.println(arr);
}
}
}

View File

@@ -0,0 +1,17 @@
package game;
import java.awt.*;
public enum Direction {
UP(new Point(0,-1)),
DOWN(new Point(0,1)),
LEFT(new Point(-1,0)),
RIGHT(new Point(1,0));
private Point offset;
Direction(Point offset) {
this.offset = offset;
}
public static Point getOffset(Direction dir) {
return dir.offset;
}
}

View File

@@ -0,0 +1,7 @@
package game;
import java.awt.*;
public interface Drawable {
void draw(Graphics g);
}

View File

@@ -0,0 +1,38 @@
package game;
import java.awt.*;
import java.util.Random;
public class Food extends GameElement{
private static Random randeom = new Random();
public Food(int width,int height) {
super(0,0);
randomPosition(width,height);
}
/**
* 随机设置位置坐标
* @param width 可用宽度范围
* @param height 可用高度范围
*/
public void randomPosition(int width, int height) {
//计算一排或一列最多的格子数
int maxX = width/SIZE;
int maxY = height/SIZE;
//将位置坐标映射到像素点
int x = randeom.nextInt(maxX)*SIZE;
int y = randeom.nextInt(maxY)*SIZE;
//设置坐标
position.setLocation(x,y);
}
@Override
public void draw(Graphics g) {
//红色填充,黑色边框
g.setColor(Color.RED);
g.fillOval(position.x,position.y,SIZE,SIZE);
g.setColor(Color.BLACK);
g.drawOval(position.x,position.y,SIZE,SIZE);
}
}

View File

@@ -0,0 +1,15 @@
package game;
import java.awt.*;
public abstract class GameElement {
protected Point position;
public static final int SIZE = 20;
protected GameElement(int x,int y) {
position = new Point(x,y);
}
public Point getPosition() {
return position;
}
public abstract void draw(Graphics g);
}

View File

@@ -0,0 +1,5 @@
package game;
public interface Movable {
void move();
}

View File

@@ -0,0 +1,34 @@
package game;
import java.awt.*;
public class SnakeNode extends GameElement implements Movable{
private Direction direction;
public SnakeNode(int x, int y, Direction direction) {
super(x, y);
this.direction = direction;
}
public void setDirection(Direction direction) {
this.direction = direction;
}
public Direction getDirection() {
return direction;
}
@Override
public void draw(Graphics g) {
//绿色填充,黑色边框
g.setColor(Color.GREEN);
g.fillRect(position.x, position.y, SIZE, SIZE);
// 绘制边框,让蛇身更清晰
g.setColor(Color.BLACK);
g.drawRect(position.x, position.y, SIZE, SIZE);
}
@Override
public void move() {
Point offset = Direction.getOffset(direction);
position.x += offset.x*SIZE;
position.y += offset.y*SIZE;
}
}

View File

@@ -0,0 +1,138 @@
package view;
import game.Direction;
import game.Food;
import game.GameElement;
import game.SnakeNode;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.*;
public class GamePanel extends JPanel {
private static final int WIDTH = 600;
private static final int HEIGHT = 600;
private boolean isRunning;
private LinkedList<SnakeNode> snakeBody = new LinkedList<>();
private final Food food;
private int score = 0;
public GamePanel() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.WHITE); // 背景色设为白色
isRunning = true;
snakeBody.add(new SnakeNode(100, 100, Direction.RIGHT));
snakeBody.add(new SnakeNode(80, 100, Direction.RIGHT));
snakeBody.add(new SnakeNode(60, 100, Direction.RIGHT));
food = new Food(WIDTH, HEIGHT);
// 键盘监听
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
Direction currentDir = snakeBody.getFirst().getDirection();
switch(e.getKeyCode()){
case KeyEvent.VK_UP: {
if (currentDir != Direction.DOWN)
snakeBody.getFirst().setDirection(Direction.UP);
break;
}
case KeyEvent.VK_DOWN: {
if (currentDir != Direction.UP)
snakeBody.getFirst().setDirection(Direction.DOWN);
break;
}
case KeyEvent.VK_LEFT: {
if (currentDir != Direction.RIGHT)
snakeBody.getFirst().setDirection(Direction.LEFT);
break;
}
case KeyEvent.VK_RIGHT: {
if (currentDir != Direction.LEFT)
snakeBody.getFirst().setDirection(Direction.RIGHT);
break;
}
default:
System.out.println("位置案件");
}
}
});
setFocusable(true); // 确保面板能接收键盘事件
// 游戏循环
new Thread(() -> {
while (isRunning) {
try {
Thread.sleep(50);
moveSnake(); // 移动蛇
checkCollision(); // 检查碰撞
repaint(); // 调用paintComponent重绘
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
private void moveSnake() {
//获取蛇头
SnakeNode head = snakeBody.getFirst();
Point offset = Direction.getOffset(head.getDirection());
//(x',y')=(x,y)+offset*SIZE
int xNew= head.getPosition().x+offset.x*GameElement.SIZE;
int yNew= head.getPosition().y+offset.y*GameElement.SIZE;
//在新位置添加蛇头
snakeBody.addFirst(new SnakeNode(xNew, yNew, head.getDirection()));
//判断是否吃到食物
if(head.getPosition().equals(food.getPosition())){
//吃到食物
food.randomPosition(WIDTH, HEIGHT);
score++;
}
//删除蛇尾
else {
snakeBody.removeLast();
}
}
private void checkCollision() {
SnakeNode head = snakeBody.getFirst();
Point headPos = head.getPosition();
//撞墙
if(headPos.x<0||headPos.x>WIDTH||headPos.y<0||headPos.y>HEIGHT)
gameOver();
//撞自己
for(int i=1;i<snakeBody.size();i++){
if(snakeBody.get(i).getPosition().equals(headPos)){
gameOver();
break;
}
}
}
private void gameOver() {
isRunning = false;
JOptionPane.showMessageDialog(this, "游戏结束!得分:"+score);
System.exit(0);
}
// 核心改进重写paintComponent而非paint利用Swing双缓冲
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); // 先绘制背景(双缓冲关键步骤)
// 绘制蛇
for (SnakeNode node : snakeBody) {
node.draw(g);
}
// 绘制食物
food.draw(g);
}
}

View File

@@ -0,0 +1,18 @@
package view;
import javax.swing.*;
public class SnakeGame extends JFrame {
public SnakeGame() {
setTitle("贪吃蛇");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new GamePanel()); // 添加游戏面板
pack(); // 自动调整窗口大小以适应面板
setLocationRelativeTo(null); // 窗口居中
setVisible(true);
}
public static void main(String[] args) {
new SnakeGame();
}
}