
正文
火柴游戏java源代码 java拿火柴小游戏
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java编写火柴游戏
画面上按左边COMPTER FIRST那个按钮火柴游戏java源代码,就是电脑先拿,
如果右边火柴游戏java源代码的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 = 0; i MAX_TAKE; i++) {
userNum.addItem(i + 1);
}
userNum.setBounds(495, 40, 90, 20);
mainPanel.add(userNum);
paints = new MyPaintPanel[3];
for (int i = 0; i 3; i++) {
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 = 0; i num; i++) {
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 = 0; i takeCnt; i++) {
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 = 0; i takeCnt; i++) {
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 StackInteger 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 StackInteger();
}
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();
}
}
}
相关问答
Q1: 求一个JAVA“拿火柴小游戏”的程序 要求如下!!!求!!!!!!!!!!!!!!!!
按照题目要求,人拿完火柴后计算机自动拿火柴,判断胜利者。鼠标点击ok或者键盘按enter键即可提交人拿的火柴个数。图形界面如下,
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
public class game extends javax.swing.JFrame implements ActionListener{
private JPanel frame;
private JTextField pwd;
private JTextField jLabel1;
private JTextField jLabel2;
private JButton bntOk;
private JLabel l1;
private JLabel l2;
private JLabel l3;
int total;
public static void main(String[] args)
{
game ff=new game();
}
public game(){
initGUI();
Random r=new Random();
total=r.nextInt(100);
jLabel2.setText(String.valueOf(total));
this.setSize(500, 500);
this.setLocation(300, 400);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getRootPane().setDefaultButton(bntOk);
bntOk.addActionListener(this);
}
public void actionPerformed(java.awt.event.ActionEvent evt)
{
int a=Integer.parseInt(jLabel1.getText());
if((a3)||(a==0)){
JOptionPane.showMessageDialog(game.this,"输入错误\n请重新输入");
return;
}
total=total-a;
jLabel2.setText(String.valueOf(total));
if(total=0){
JOptionPane.showMessageDialog(game.this,"恭喜你,胜利了!");
return;
}
Random r=new Random();
int b=r.nextInt(4);
while(b==0)
b=r.nextInt(4);
pwd.setText(String.valueOf(b));
total=total-b;
jLabel2.setText(String.valueOf(total));
if(total=0){
JOptionPane.showMessageDialog(game.this,"很遗憾,你输了!");
}
}
private void initGUI(){
frame=new JPanel();
getContentPane().add(frame, BorderLayout.CENTER);
frame.setLayout(null);
bntOk =new JButton();
bntOk.setText("OK");
bntOk.setBounds(150, 200, 66, 30);
frame.add(bntOk);
jLabel2= new JTextField();
jLabel2.setBounds(150, 50, 100, 50);
frame.add(jLabel2);
l1=new JLabel();
l1.setText("火柴数量");
l1.setBounds(90, 50, 100, 50);
frame.add(l1);
jLabel1= new JTextField();
jLabel1.setText("");
jLabel1.setBounds(200,149, 77, 22);
frame.add(jLabel1);
l2=new JLabel();
l2.setText("我拿火柴");
l2.setBounds(100, 149, 77, 22);
frame.add(l2);
l3=new JLabel();
l3.setText("对方拿火柴");
l3.setBounds(100, 249, 77, 22);
frame.add(l3);
pwd = new JTextField();
pwd.setBounds(200, 249, 77, 22);
frame.add(pwd);
bntOk.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e1) {
int a=Integer.parseInt(jLabel1.getText());
if((a3)||(a==0)){
JOptionPane.showMessageDialog(game.this,"输入错误\n请重新输入");
return;
}
total=total-a;
jLabel2.setText(String.valueOf(total));
if(total=0){
jLabel2.setText("0");
JOptionPane.showMessageDialog(game.this,"恭喜你,胜利了!");
return;
}
Random r=new Random();
int b=r.nextInt(4);
while(b==0)
b=r.nextInt(4);
pwd.setText(String.valueOf(b));
total=total-b;
jLabel2.setText(String.valueOf(total));
if(total=0){
JOptionPane.showMessageDialog(game.this,"很遗憾,你输了!");
}
}
}
);
}
}
Q2: java程序编写小游戏 要求:程序随机产生20—50根火柴
为什么用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();
}
}
Q3: 编写一个人与计算机对拿火柴的游戏程序。用随机函数产生火柴数量(20-50)每次最多拿3,拿到最后一根为胜
import java.util.Scanner;
public class TestNumberGame {
private int Number;
private int n;
public TestNumberGame(){
Number=(int)(Math.random()*31)+20;
n=2;
System.out.println("一共产生"+Number+"根火柴!");
}
public void play(){
computerplay();
while(Number!=0){
int temp;
while(true){
System.out.println("输入你准备取走的火柴数1-"+n+"之间的数!");
Scanner sca=new Scanner(System.in);
temp=sca.nextInt();
if(temp0 temp=n){
break;
}
}
System.out.println("你"+temp+"根火柴!");
for(int i=0;itemp;i++){
Number-- ;
System.out.println("还剩下"+Number+"根火柴!");
}
if(Number=0){
System.out.println("玩家胜");
System.exit(0);
}
if((Number%n)==0){
computerplay();
}else{
computerplay(Number%n);
}
if(Number=0){
System.out.println("电脑胜");
System.exit(0);
}
}
}
public void computerplay(){
System.out.println("电脑取火柴!");
int Rand=(int)(Math.random()*n+1);
System.out.println("电脑取走"+Rand+"根火柴!");
for(int i=0;iRand;i++){
Number-- ;
System.out.println("还剩下"+Number+"根火柴!");
}
}
public void computerplay(int Rand){
System.out.println("电脑取火柴!");
System.out.println("电脑取走"+Rand+"根火柴!");
for(int i=0;iRand;i++){
Number-- ;
System.out.println("还剩下"+Number+"根火柴!");
}
}
public static void main(String[] args) {
TestNumberGame tb=new TestNumberGame();
tb.play();
}
}
Q4: 随机产生20-50根火柴由人和计算机轮流拿,每次拿的不能超过3根,拿到最后一根为胜,JAVA程序
package testDemo;
import java.util.Scanner;
public class testDemo {
//随机生成范围内的数
public static int setRandomNum(int minnum,int maxnum){
return (int)(Math.random()*(maxnum-minnum+1))+minnum;
}
public static void main(String[] args){
//获取生成火柴的总数
int i = setRandomNum(20,50);
System.out.println("----game started,this count is "+i+"-------");
while(i0){
System.out.println("your turn,select num(1~3) you pick");
int tmpnum = new Scanner(System.in).nextInt();
if(tmpnum3){
System.out.println("your selected num is out of range");
continue;
}
System.out.println("you pick "+tmpnum+","+"Remain num is "+(i-tmpnum));
i=i-tmpnum;
if(i==0){
System.out.println("you win");
return;
}
int tmpnum2 = i=3?i:setRandomNum(1,3);
System.out.println("computer pick "+tmpnum2+","+"Remain num is "+(i-tmpnum2));
i=i-tmpnum2;
if(i==0){
System.out.println("computer win");
}
}
}
}
Q5: 谁会用JAVA编写程序随机产生20—50根火柴
import java.util.Random;
import java.io.*;
public class MatchGame {
private Random rnd = new Random ();
private int total;
private boolean yourTurn;
private boolean youWin;
public MatchGame () {
this.total = this.rnd.nextInt(15) + 20;
}
public MatchGame (boolean youFirst) {
this();
this.yourTurn = yourTurn;
}
public void takeAway (boolean yourTurn) {
int n = 0;
if (yourTurn) {
System.out.print("Your turn! How many matches do you want to take? ");
try {
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
n = Integer.parseInt (br.readLine());
} catch (IOException ioe) {}
if (n 1 || n 3) {
System.out.println("You can only take 1~3 matches for each time.");
takeAway (yourTurn);
return;
}
} else {
n = this.rnd.nextInt(3) + 1;
System.out.println("The computer took " + n + " matches.");
}
this.total -= n;
if (this.total = 0) {
if (yourTurn)
System.out.println("You win!");
else
System.out.println("You lose!");
} else {
takeAway (!yourTurn);
}
}
public static void main (String args[]) {
new MatchGame ().takeAway (true);
}
}
关于火柴游戏java源代码和java拿火柴小游戏的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






