您好,欢迎来到年旅网。
搜索
您的当前位置:首页Java程序设计实验报告

Java程序设计实验报告

来源:年旅网
Java程序设计实验报告

班级: 100601X 学号: 34 姓名: 李树明

实验题目:猜拳小游戏

实验要求:

用java编写一个人机对战的猜拳小游戏。人选择性出拳,电脑随机出拳,判断输赢,记录输赢情况。有简单的操作界面。

实验内容:

1、问题分析过程:

游戏名称:悟空猜拳 游戏类型:益智类 平台:java 编程语言:java 操作:鼠标

游戏规则:游戏为单人模式,玩家控制己方出拳,对方为电脑随机出拳,有3种胜出模式(三局两胜制、五局三胜制和七局四胜制),双方只要先胜出2、3、4局游戏结束 程序框架设计: Menu类,Game类,Npc类,Player类 游戏界面设计: 登录界面:

游戏主界面

胜利界面

2、主要实现代码: Menu类:

package Game_guessFist;

import java.awt.Color; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Label;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane;

public class Menu extends JFrame implements ActionListener{

JMenuBar menuBar; JMenu setting;

JMenuItem item1,item2,item3;

JButton start,about,exit; Label l; int model =2; public Menu() {

//窗口

super(\"悟空猜拳\"); //设置窗口宽高以及位置 this.setSize(500,400);

this.setLocationRelativeTo(null); //不可修改

this.setResizable(false); //设置可见

this.setVisible(true); //创建布局

FlowLayout f = new FlowLayout(); f.setAlignment(FlowLayout.CENTER); f.setHgap(500); f.setVgap(40); //设置布局

this.setLayout(f); //菜单 //实例化控件

menuBar = new JMenuBar(); setting = new JMenu(\"设置\");

item1 = new JMenuItem(\"三局两胜制\"); item2 = new JMenuItem(\"五局三胜制\"); item3 = new JMenuItem(\"七局四胜制\"); //添加

item1.addActionListener(this); item2.addActionListener(this); item3.addActionListener(this); //向窗口添加元素 setting.add(item1); setting.add(item2); setting.add(item3); menuBar.add(setting);

//按钮

start = new JButton(\"开始游戏\"); about = new JButton(\"关于游戏\"); exit = new JButton(\"退出游戏\"); start.addActionListener(this);

}

about.addActionListener(this); exit.addActionListener(this); l = new Label(\" 悟空猜拳\" +

\"\");

Font ff = new Font(null,1, 20); l.setFont(ff);

l.setBackground(Color.RED); this.add(l); this.add(start); this.add(about); this.add(exit);

this.setJMenuBar(menuBar);

this.validate();

public static void main(String[] args) { }

// 按钮 @Override

public void actionPerformed(ActionEvent e) {

//菜单按钮判断

if(e.getSource()==item1){

model = 2;

}else if(e.getSource()==item2){ model = 3;

}else if(e.getSource()==item3){ model = 4; }

if(e.getSource()==start){

this.dispose(); new Game(model);

//程序入口 new Menu();

//游戏流程按钮判断

}else if(e.getSource()==about){

JOptionPane.showMessageDialog(null, \"您好,本游戏有网络工程专业李树

明制作\", \"关于\", JOptionPane.INFORMATION_MESSAGE);

}

}

}else if(e.getSource()==exit){ }

System.exit(0); this.dispose();

Game类:

package Game_guessFist;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel;

public class Game extends JFrame implements ActionListener {

Game(int model) {

// 窗口

super(\"悟空猜拳\"); this.setSize(500,400);

this.setLocationRelativeTo(null);

this.setResizable(false); this.setVisible(true);

FlowLayout ff = new FlowLayout(FlowLayout.CENTER, 400, 10); FlowLayout f1 = new FlowLayout(FlowLayout.CENTER, 250, 10); ff.setAlignment(FlowLayout.CENTER); ff.setHgap(500); ff.setVgap(30); this.setLayout(ff);

private JLabel npc_score, player_score, npc_sign, player_sign, label; private JButton btnReturn, btn_scissore, btn_stone, btn_cloth; private JPanel panel1, panel2, panel3, panel4, panel5; private Npc npc; private Player player; int model = 2;

add(panel1); add(panel2); add(panel3); add(panel4);

panel2.add(npc_sign); panel3.add(player_sign); panel4.add(player_score); panel4.add(label); panel5.add(btn_cloth); panel5.add(btn_scissore); panel5.add(btn_stone);

panel1.setLayout(f1); panel4.setLayout(f1); panel1.add(npc_score); panel1.add(btnReturn);

btn_cloth.addActionListener(this); btn_scissore.addActionListener(this); btn_stone.addActionListener(this); btnReturn.addActionListener(this);

label = new JLabel(\" \"); panel1 = new JPanel(); panel2 = new JPanel(); panel3 = new JPanel(); panel4 = new JPanel(); panel5 = new JPanel();

btn_cloth = new JButton(\"布\"); btn_scissore = new JButton(\"剪刀\"); btn_stone = new JButton(\"石头\"); btnReturn = new JButton(\"返回\");

setVisible(true);

npc_sign = new JLabel(\"出拳\"); player_sign = new JLabel(\"出拳\"); npc_score = new JLabel(\"悟空胜:0\"); player_score = new JLabel(\"八戒胜:0\");

npc = new Npc(); player = new Player();

}

add(panel5);

this.model = model;

// 按钮监听 @Override

public void actionPerformed(ActionEvent e) {

int id = 0;

if (e.getSource() == btnReturn) {

this.dispose(); new Menu();

} else { }

//判断何时结束游戏 switch (id) { case 0:

//平局不做处理 break;

//设置两个字符串对象,接受npc以及player的出拳 String player_quan = null;

String npc_quan = npc.shouFist(); //按照玩家点击的按钮,判断用户出拳

if (e.getSource() == btn_scissore) { }

//设置出拳到界面

npc_sign.setText(npc_quan); player_sign.setText(player_quan); //判断胜负

id = guess(npc_quan, player_quan);

player_quan = player.shouFist(0); player_quan = player.shouFist(1); player_quan = player.shouFist(2); } else if (e.getSource() == btn_stone) { } else if (e.getSource() == btn_cloth) {

case 1:

//玩家胜利 player.score++;

player_score.setText(\"八戒胜:\" + player.score); if (player.score >= model) {

JOptionPane.showMessageDialog(null, \"八戒获得了胜利\", \"提示\", init();

JOptionPane.INFORMATION_MESSAGE);

}

}

} break;

case 2: }

//npc胜利 npc.score++;

npc_score.setText(\"悟空胜:\" + npc.score); if (npc.score >= model) { } break;

JOptionPane.showMessageDialog(null, \"悟空取得了胜利\", \"提示\", init();

JOptionPane.INFORMATION_MESSAGE);

// 比较结果方法

public int guess(String npc, String player) { }

int id = 0;

if((npc.equals(\"剪刀\")&&player.equals(\"布\"))||(npc.equals(\"石头 }

return id;

id = 2; id = 0; id = 1;

\")&&player.equals(\"剪刀\"))||(npc.equals(\"布\")&&player.equals(\"石头\"))){

}else if(npc.equals(player)){ }else{

public void init()// 初始化数据方法 { }

npc.score = 0; player.score = 0;

npc_score.setText(\"悟空胜:0\"); player_score.setText(\"八戒胜:0\"); npc_sign.setText(\"出拳\"); player_sign.setText(\"出拳\");

Npc类:

package Game_guessFist;

import java.util.Random;

class Npc{ }

private Random r; Npc() { }

public int score; public int score1;

public String shouFist(){ }

int id = Math.abs(r.nextInt())%3; String fist = null; switch (id){ case 0:

fist = \"剪刀\"; break;

r = new Random();

case 1:

fist = \"石头\"; break;

case 2: }

return fist;

fist = \"布\"; break;

Player类:

package Game_guessFist; public class Player {

public int score;

public String shouFist(int id){ String fist = null; switch (id){ case 0: fist = \"剪刀\";

}

break; case 1: fist = \"石头\"; break; case 2: fist = \"布\"; break; }

return fist; }

3、实验心得体会:

通过这次试验,发现了学习的不足。熟悉了java程序的制作及应用,以及提高了团队合作的默契。也提高了大家学习java的兴趣。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- oldu.cn 版权所有 浙ICP备2024123271号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务