java编写火柴游戏

java编写火柴游戏,第1张

画面上按左边COMPTER FIRST那个按钮,就是电脑先拿,

如果右边的TAKE按钮,就是人先拿。

import java.awt.Color

import java.awt.Component

import java.awt.Dimension

import java.awt.Graphics

import java.awt.Graphics2D

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import java.awt.image.BufferedImage

import java.util.Stack

import java.util.Timer

import java.util.TimerTask

import javax.swing.JButton

import javax.swing.JComboBox

import javax.swing.JFrame

import javax.swing.JLabel

import javax.swing.JOptionPane

import javax.swing.JPanel

public class Test {

public static void main(String[] args) {

new Test()

}

private static final int MIN_CNT = 20

private static final int MAX_CNT = 50

private static final int MAX_TAKE = 3

private static final int DELAY = 1000

private JFrame mainFrame

private MyPaintPanel[] paints

private JButton userBtn

private JComboBox userNum

private JButton compBtn

private JLabel compTxt

private Timer timer

private Test() {

mainFrame = new JFrame()

mainFrame.setResizable(false)

mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

JPanel mainPanel = new JPanel()

mainPanel.setLayout(null)

mainPanel.setPreferredSize(new Dimension(600, 400))

mainFrame.add(mainPanel)

JLabel lab11 = new JLabel("COMPUTER")

lab11.setHorizontalAlignment(JLabel.CENTER)

lab11.setBounds(0, 10, 200, 20)

mainPanel.add(lab11)

JButton btn11 = new JButton("RESET")

btn11.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

init()

}

})

btn11.setBounds(250, 10, 100, 20)

mainPanel.add(btn11)

JLabel lab12 = new JLabel("USER")

lab12.setHorizontalAlignment(JLabel.CENTER)

lab12.setBounds(400, 10, 200, 20)

mainPanel.add(lab12)

compBtn = new JButton("COMPTER FIRST")

compBtn.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

compTake()

}

})

compBtn.setBounds(10, 40, 180, 20)

mainPanel.add(compBtn)

compTxt = new JLabel()

compTxt.setBounds(10, 70, 400, 20)

mainPanel.add(compTxt)

userBtn = new JButton("TAKE")

userBtn.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

userTake()

}

})

userBtn.setBounds(400, 40, 90, 20)

mainPanel.add(userBtn)

userNum = new JComboBox()

for (int i = 0i <MAX_TAKEi++) {

userNum.addItem(i + 1)

}

userNum.setBounds(495, 40, 90, 20)

mainPanel.add(userNum)

paints = new MyPaintPanel[3]

for (int i = 0i <3i++) {

paints[i] = new MyPaintPanel()

paints[i].setBounds(5 + 200 * i, 110, 190, 285)

mainPanel.add(paints[i])

}

mainFrame.pack()

mainFrame.setVisible(true)

init()

}

private void init() {

if (timer != null) {

timer.cancel()

}

timer = new Timer()

compTxt.setText("")

compBtn.setEnabled(true)

userBtn.setEnabled(true)

paints[0].reset()

paints[1].reset()

paints[2].reset()

int num = MIN_CNT + (int) ((MAX_CNT + 1 - MIN_CNT) * Math.random())

for (int i = 0i <numi++) {

paints[1].add(i + 1)

}

}

private void userTake() {

compBtn.setEnabled(false)

int takeCnt = userNum.getSelectedIndex() + 1

int rel = paints[1].getCnt()

if (takeCnt >rel) {

JOptionPane.showMessageDialog(mainFrame, "There is only " + rel + " matches.")

return

}

for (int i = 0i <takeCnti++) {

int index = paints[1].remove()

paints[2].add(index)

}

if (takeCnt == rel) {

JOptionPane.showMessageDialog(mainFrame, "You are the winner.")

init()

} else {

compTake()

}

}

private void compTake() {

compBtn.setEnabled(false)

userBtn.setEnabled(false)

MyTask task = new MyTask()

timer.schedule(task, DELAY)

}

private class MyTask extends TimerTask {

@Override

public void run() {

int rel = paints[1].getCnt()

int takeCnt = 0

if (rel <= MAX_TAKE) {

takeCnt = rel

} else if (rel % (MAX_TAKE + 1) >0) {

takeCnt = rel % (MAX_TAKE + 1)

} else {

takeCnt = 1 + (int) (MAX_TAKE * Math.random())

}

for (int i = 0i <takeCnti++) {

int index = paints[1].remove()

paints[0].add(index)

}

compTxt.setText("Computer takes " + takeCnt + " matches this time.")

if (takeCnt == rel) {

JOptionPane.showMessageDialog(mainFrame, "Computer is the winner.")

init()

}

userBtn.setEnabled(true)

}

}

@SuppressWarnings("serial")

private class MyPaintPanel extends Component {

private BufferedImage bimg

private Stack<Integer>data

private MyPaintPanel() {

bimg = new BufferedImage(190, 285, BufferedImage.TYPE_3BYTE_BGR)

Graphics2D g2 = bimg.createGraphics()

g2.setColor(Color.WHITE)

g2.fillRect(0, 0, 190, 285)

g2.dispose()

data = new Stack<Integer>()

}

public void paint(Graphics g) {

g.drawImage(bimg, 0, 0, null)

g.dispose()

}

private void add(int num) {

Graphics2D g2 = bimg.createGraphics()

int loc = data.size()

int offX = loc % 3 * 65

int offY = loc / 3 * 15

g2.setColor(Color.YELLOW)

g2.fillRect(offX + 8, offY + 5, 50, 6)

g2.setColor(Color.PINK)

g2.fillArc(offX + 50, offY + 3, 10, 10, 0, 360)

g2.setColor(Color.BLACK)

g2.drawString(String.valueOf(num), offX, offY + g2.getFont().getSize())

data.push(num)

repaint()

g2.dispose()

}

private int remove() {

Graphics2D g2 = bimg.createGraphics()

int loc = data.size() - 1

int offX = loc % 3 * 65

int offY = loc / 3 * 15

g2.setColor(Color.WHITE)

g2.fillRect(offX, offY, 60, 15)

g2.dispose()

repaint()

return data.pop()

}

private void reset() {

data.clear()

Graphics2D g2 = bimg.createGraphics()

g2.setColor(Color.WHITE)

g2.fillRect(0, 0, 190, 285)

g2.dispose()

repaint()

}

private int getCnt() {

return data.size()

}

}

}

为什么用AWT不用 swing?

算法思想很简单

是取胜原理

用反推法:欲拿最后一根,必须留2根在那里,欲留2根,必须上一轮留2+3+1=6对方,(它拿一,你拿三,它拿二,你拿二,它拿三,你拿一。都是留2根)。再向上一轮,就是6+4=10。

取胜原理:把随机产生的火柴数,分解成:2+4的n次方+m,(m≤3),当m=0的时候,后取者胜,当m=1、2、3的时候,先取者胜。先取者取完m,留2+4的n次方给对方,对方不管取多少,你取的数和对方相加等于4,一直到最后,留2根给对方,根据规则,对方只能取一根,留一根给你取胜。

另:取完者胜(含最后一根):最后留4根给对方,不管对方取多少,你都可以一次取完。上一轮同样加4。

取胜原理:把随机产生的火柴数,分解成:4的n次方+m,(m≤3),当m=0的时候,后取者胜,当m=1、2、3的时候,先取者胜。先取者取完m,留4的n次方给对方,对方不管取多少,你取的数和对方相加等于4,一直到最后,留4根给对方。

代码调试可用

自己用GUI搭个界面 二十分钟的事

import java.util.Scanner

public class MatchGame {

private static Scanner scanner = new Scanner(System.in)

private int total

private Computer com

private static int exit = 1

public MatchGame(int from, int to, String level) {

if (from >= to) {

throw new IllegalArgumentException()

}

total = (int)( Math.random() * (to - from)) + from

com = new Computer(level)

}

public void start() {

System.out.println("0 means endGame, 4 means restartGame!")

System.out.println("The number of matches is " + total)

System.out.println("~Start~")

System.out.println("----------------------------------------")

while (true) {

int u = scanner.nextInt()

if (u >0 &&u <4) {

System.out.println("You entered " + u)

if (total - u <= 0) {

exit = 2

endGame()

}

total = total - u

System.out.println("Total : " + total)

int c = com.play(u, total)

System.out.println("Computer selected " + c + " matches~")

if (total - c <= 0) {

exit = 0

endGame()

}

total = total - c

System.out.println("Total : " + total)

}else if (u == 0) {

endGame()

}else if (u >4 || u <0) {

System.out.println("You entered Wrong number~")

} else {

restart()

}

}

}

public static void restart() {

MatchGame game

System.out

.println("Please select Computer Level: 1:HARD 2:NORMAL 3:EASY")

int level = scanner.nextInt()

if (level == 1) {

game = new MatchGame(20, 50, Computer.HARD)

} else if (level == 2) {

game = new MatchGame(20, 50, Computer.NORMAL)

} else {

game = new MatchGame(20, 50, Computer.EASY)

}

game.start()

}

public static void endGame() {

if (exit == 0) {

System.out.println("YOU WIN!!!")

} else if (exit == 2) {

System.out.println("YOU LOSE!!!")

}

System.exit(exit)

}

public static class Computer {

private static String EASY = "EASY"

private static String NORMAL = "NORMAL"

private static String HARD = "HARD"

private static String LEVEL

private int com

public Computer(String level) {

LEVEL = level

}

public int play(int user, int total) {

if (LEVEL.equals(EASY)) {

com = 1

} else if (LEVEL.equals(NORMAL)) {

com = (int) (Math.random() * 2) + 1

} else {

int i

if (total % 4 == 0) {

i = total / 4 - 1

} else {

i = total / 4

}

com = total - 4 * i - 1

if (com == 0) {

com = 4 - user

}

}

return com

}

}

public static void main(String[] args) {

MatchGame game

System.out

.println("Please select Computer Level: 1:HARD 2:NORMAL 3:EASY")

int level = scanner.nextInt()

if (level == 1) {

game = new MatchGame(20, 50, Computer.HARD)

} else if (level == 2) {

game = new MatchGame(20, 50, Computer.NORMAL)

} else {

game = new MatchGame(20, 50, Computer.EASY)

}

game.start()

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11949031.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存