当前位置:文档之家› 人机猜拳JAVA

人机猜拳JAVA

Java 程序设计人机猜拳小游戏实验报告专业:计算机科学与技术专业班级:学号:姓名:指导老师:摘要面向对象方法是一种把面向对象的思想应用于软件开发过程中,指导开发活动的系统方法,它吸取了结构化程序设计的精华,以更接近人们通常思维的方式来处理问题,是一种全新的软件开发技术。

作为一种新的的编程语言,Java很大程度推广了面向对象的思想。

随着计算机技术的发展,Java的强大展现出它不可替代的作用。

Java的功能不仅体现在它网络方面的应用,对于应用程序设计、游戏设计等Java都能满足需求。

本文针对Java 的具体应用,设计了一个小游戏。

根据游戏的规则,用户将可以和计算机进行简单对战。

程序实现了友好的用户交互界面,让用户能很好地对游戏进行操作。

关键字:JAVA GUI 游戏ABSTRACTThe Object-Oriented Method is a method which apllies the Object-Oriented Design to the process of the software exploiture and directs the ploy of development. It extracts the prime of the Structured Design for Computer Programmers, deals matters with a folksy fashion, which is considered to be a grand new software-develop technique.As a new programming language, Java spread the Object-Oriented Design in a certain degree. With the development of the technique of computer, the insurmountable function of JA V A is exhibited for its’ mightines s. Not only applies in network, JA V A also can fulfill the requirement of the Application-Design and Game-Design.In this paper, to use the idiographic application of JA V A , I design a game called . Following the regular of the game, user can play a simple revalry with computer. This programme actualizes a folksy GUI mode, which will make user master game easily. Keywords: JA V A GUI Game一、背景与意义游戏是全球最大的娱乐经济项目之一,已经被命名为“第九艺术”。

而由“游戏”派生出来的职业,除了软件开发外,游戏包装宣传推广、攻略指导、网络游戏管理员和职业游戏玩家等,已成为了一系列专门的岗位。

面对游戏产业如此巨大的市场空间,国内各大城市纷纷加大了这方面的人才培养,中国的游戏人才培训已随着游戏行业的总体发展开始逐步迈入了正轨。

虽然说中国的游戏行业开始逐步的迈入了正轨,但是现今,游戏人才出现严重的瓶颈时期,高校的扩招、金融危机的侵袭直接影响着我们最关心的就业问题,但是另一方面游戏人才的缺乏又直接制约着行业的发展,这就更加严格了对社会培训机构的稳定性和正规性的要求。

在我国,游戏人才培养体系还未健全,部分开设了相关专业的高校,游由于理论和实践的脱离无法完全满足网游公司对人才的需求。

所以,就中国目前的市场分析来看,加强游戏产业必须在多渠道人才培养体系上加快突破,尽快建立起多渠道人才培养体系,来满足社会对游戏人才的需求。

实验目的本课程设计所要求达到的目的:通过Java程序设计的一些重要理论基础来实现“人机猜拳小游戏”的功能。

通过利用所学到的计算科学的理论知识,提高解决实际问题的能力,增强运算、编程和使用技术资料的技能,通过实际问题的分析设计、编程和调试,掌握应用软件的分析方法和工程设计方法,能正确阐述设计和实验结果,逐步建立正确的全局观念。

二、实验设计与要求问题定义:创建小游戏,实现电脑与玩家之间互动猜拳这主要功能,并能显示设置的图片,增加美观。

研究意义:通过本次程序设计,我掌握了Java语言的基本运用,提高了理论结合实际能力。

设计要求:1)界面的左侧是电脑或者玩家的名字,让界面看起来一目了然。

2)系统界面的中间是电脑或者玩家出拳的图片。

图片可以生动形象得表示出电脑或者玩家出的拳,显示的图片在对话框中也相得益彰。

在图片的下方会显示,人机猜拳以后的输赢。

3)界面的右侧是三个剪刀石头布的按钮。

玩家通过按钮出拳,电脑随机出拳,猜拳的输赢结果在下方显示。

三、系统的主要功能1.可以通过按钮的方式,与电脑进行猜拳游戏,电脑随机出拳;2.可以显示出拳图片。

四、实验工具Windows 7;jdk1.6.0_21;eclipse五、项目实现首先建立一个Test类,如下:import java.awt.Color;import java.awt.Image;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.AbstractButton;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;public class Test extends JFrame {JLabel jl,jl1;final JLabel jl2,jl3;JLabel jl4;public String person;public String m;int j;int i;final String[] arr = new String[3];public void Win(String person) {arr[0] = "石头";arr[1] = "剪刀";arr[2] = "布";person = arr[j];i = (int) (Math.random() * 3);String c = arr[i];//判断输赢if (c.equals(person)) {jl4.setText("平手");} else if (c.equals("剪刀") && person.equals("石头") || c.equals("石头")&& person.equals("布") || c.equals("布") && person.equals("剪刀")) {jl4.setText("你赢了");} else {jl4.setText("你输了");}}public Test() {JFrame jf = new JFrame();this.setSize(400, 300);this.setTitle("猜拳游戏");this.setLayout(null);jl = new JLabel("电脑出的是:");jl.setBounds(50, 50, 100, 50);this.add(jl);jl1 = new JLabel("你出的是:");jl1.setBounds(50, 120, 100, 50);this.add(jl1);// 电脑的出的拳标签jl2 = new JLabel();jl2.setBounds(150, 50, 100, 50);this.add(jl2);//我出的拳的标签jl3 = new JLabel();jl3.setBounds(150, 120, 100, 60);this.add(jl3);jl4 = new JLabel("");jl4.setBounds(150, 200, 100, 50);this.add(jl4);// 按钮final JButton btn = new JButton("石头");btn.setBounds(280, 50, 60, 40);this.add(btn);final JButton btn1 = new JButton("剪刀");btn1.setBounds(280, 105, 60, 40);this.add(btn1);final JButton btn2 = new JButton("布");btn2.setBounds(280, 160, 60, 40);this.add(btn2);//按钮事件监听btn.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) { //如果你鼠标点击的是石头按钮j = 0;String b = btn.getActionCommand();jl3.setText(b);jl3.setIcon(newImageIcon(MyImg.class.getResource(j+".jpg")));Win(person);Test.this.jl2.setIcon(newImageIcon(MyImg.class.getResource (i+".jpg")));Test.this.jl2.setText(arr[i]);System.out.println(arr[i]);}});btn1.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) { j = 1;String b1 = btn1.getActionCommand();jl3.setText(b1);jl3.setIcon(newImageIcon(MyImg.class.getResource(j+".jpg")));Win(person);Test.this.jl2.setIcon(newImageIcon(MyImg.class.getResource (i+".jpg")));Test.this.jl2.setText(arr[i]);System.out.println(arr[i]);// System.out.println(b);}});btn2.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) { j = 2;String b2 = btn2.getActionCommand();jl3.setText(b2);jl3.setIcon(newImageIcon(MyImg.class.getResource(j+".jpg")));Win(person);Test.this.jl2.setText(arr[i]);System.out.println(arr[i]);Test.this.jl2.setIcon(newImageIcon(MyImg.class.getResource (i+".jpg")));// System.out.println(b);}});this.setVisible(true);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setLocationRelativeTo(null);this.setResizable(false);}public static void main(String[] args) {Test t = new Test();}}再建立一个名为MyImg的类,把下面几张图片放到该类下图片名称自左向右依次为布、石头、剪刀。

相关主题