狼与狮子

    有一天,狼从羊群中抢走一只羊,正叼着往回走,碰上了狮子. 狮子立刻从狼口里夺走了羊. 狼远远地站着,自言自语地说:“你抢我的东西太不应了.”那狮子笑着说:“那么,这东西是朋友就应当赠送给你的吗?”

以下是Java代码的实现: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class AnimalGame extends JFrame implements ActionListener { JButton[][] buttons; Animal[][] animals; AnimalColor turn; boolean gameOver; public AnimalGame() { setTitle("Animal Game"); setSize(800, 800); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); buttons = new JButton[4][4]; animals = new Animal[4][4]; turn = AnimalColor.RED; gameOver = false; JPanel panel = new JPanel(new GridLayout(4, 4)); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { buttons[i][j] = new JButton(); buttons[i][j].setPreferredSize(new Dimension(200, 200)); buttons[i][j].addActionListener(this); panel.add(buttons[i][j]); } } getContentPane().add(panel); setupGame(); setVisible(true); } public void setupGame() { // Setup red animals Animal[] redAnimals = {Animal.ELEPHANT, Animal.LION, Animal.TIGER, Animal.LEOPARD, Animal.WOLF, Animal.DOG, Animal.CAT, Animal.MOUSE}; shuffleArray(redAnimals); for (int i = 0; i < 8; i++) { animals[i / 4][i % 4] = new Animal(redAnimals[i], AnimalColor.RED); } // Setup blue animals Animal[] blueAnimals = {Animal.ELEPHANT, Animal.LION, Animal.TIGER, Animal.LEOPARD, Animal.WOLF, Animal.DOG, Animal.CAT, Animal.MOUSE}; shuffleArray(blueAnimals); for (int i = 8; i < 16; i++) { animals[i / 4][i % 4] = new Animal(blueAnimals[i - 8], AnimalColor.BLUE); } updateButtons(); } public void actionPerformed(ActionEvent e) { if (gameOver) { int choice = JOptionPane.showConfirmDialog(null, "Game over! Play again?", "Game over", JOptionPane.YES_NO_OPTION); if (choice == JOptionPane.YES_OPTION) { resetGame(); } else { System.exit(0); } } else { JButton button = (JButton)e.getSource(); int row = -1, col = -1; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (button == buttons[i][j]) { row = i; col = j; break; } } } if (animals[row][col] == null || animals[row][col].getColor() != turn) { return; } boolean validMove = false; if (row > 0 && (animals[row - 1][col] == null || animals[row - 1][col].getColor() != turn)) { validMove = true; } if (row < 3 && (animals[row + 1][col] == null || animals[row + 1][col].getColor() != turn)) { validMove = true; } if (col > 0 && (animals[row][col - 1] == null || animals[row][col - 1].getColor() != turn)) { validMove = true; } if (col < 3 && (animals[row][col + 1] == null || animals[row][col + 1].getColor() != turn)) { validMove = true; } if (!validMove) { return; } int choice = JOptionPane.showOptionDialog(null, "Select a target", "Move", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null); if (choice == -1) { return; } int targetRow = choice / 4, targetCol = choice % 4; if (animals[targetRow][targetCol] != null && animals[targetRow][targetCol].getColor() == turn) { return; } if (animals[row][col].compareRank(animals[targetRow][targetCol]) >= 0) { if (animals[targetRow][targetCol] != null && animals[targetRow][targetCol].getType() == AnimalType.MOUSE) { gameOver = true; } animals[targetRow][targetCol] = animals[row][col]; animals[row][col] = null; turn = turn == AnimalColor.RED ? AnimalColor.BLUE : AnimalColor.RED; updateButtons(); } } } public void updateButtons() { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (animals[i][j] == null) { buttons[i][j].setIcon(null); } else { ImageIcon icon = new ImageIcon(animals[i][j].getImageFile()); buttons[i][j].setIcon(icon); } } } } public void resetGame() { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { animals[i][j] = null; } } turn = AnimalColor.RED; gameOver = false; setupGame(); } public static void shuffleArray(Object[] array) { for (int i = 0; i < array.length; i++) { int j = (int)(Math.random() * (array.length - i)) + i; Object temp = array[i]; array[i] = array[j]; array[j] = temp; } } public static void main(String[] args) { AnimalGame game = new AnimalGame(); } } enum AnimalColor { RED, BLUE } enum AnimalType { ELEPHANT("elephant", 8), LION("lion", 7), TIGER("tiger", 6), LEOPARD("leopard", 5), WOLF("wolf", 4), DOG("dog", 3), CAT("cat", 2), MOUSE("mouse", 1); private String imageFile; private int rank; AnimalType(String imageFile, int rank) { this.imageFile = imageFile; this.rank = rank; } public String getImageFile() { return imageFile; } public int getRank() { return rank; } } class Animal { private AnimalType type; private AnimalColor color; public Animal(AnimalType type, AnimalColor color) { this.type = type; this.color = color; } public AnimalType getType() { return type; } public AnimalColor getColor() { return color; } public int compareRank(Animal other) { return Integer.compare(this.type.getRank(), other.type.getRank()); } public String getImageFile() { return "images/" + color.toString().toLowerCase() + "_" + type.getImageFile() + ".png"; } } ``` 这个游戏使用了Java的GUI库Swing来创建界面。在游戏开始时,我们初始化一个4x4的网格,并在其中随机分配红色和蓝色的动物。每次玩家点击一个动物时,程序会弹出一个对话框,让玩家选择一个目标格子进行移动。如果目标格子中有动物,则会比较两个动物的大小,如果当前动物比目标动物大,则目标动物被吃掉。当任意一方的老鼠被吃掉时,游戏结束并提示玩家是否重新开始游戏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值