JAVA的剪刀石头布游戏
摘要
面向对象方法是一种把面向对象的思想应用于软件开发过程中,指导开发活动的系统方法,它吸取了结构化程序设计的精华,以更接近人们通常思维的方式来处理问题,是一种全新的软件开发技术。
作为一种新的的编程语言,Java很大程度推广了面向对象的思想。随着计算机技术的发展,Java的强大展现出它不可替代的作用。Java的功能不仅体现在它网络方面的应用,对于应用程序设计、游戏设计等Java都能满足需求。
本文针对Java 的具体应用,设计了一个小游戏。根据游戏的规则,用户将可以和计算机进行简单对战。程序实现了友好的用户交互界面,让用户能很好地对游戏进行操作。
关键字: JAVA GUI 游戏
ABSTRACT
The 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 JAVA is exhibited for its’ mightiness. Not only applies in network, JAVA also can fulfill the requirement of the Application-Design and Game-Design.
In this paper, to use the idiographic application of JAVA , 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: JAVA GUI Game
目 录
1剪刀石头布游戏设计思路阐述 ............................................................................................ 3 2程序概要设计 ............................................................................................................................ 3
2.1功能需求分析 ................................................................................................................ 3
2.2性能需求分析 ................................................................................................................ 3
2.3程序框图 ......................................................................................................................... 4
2.4 JAVA类及自定义类相互继承的层次关系 ............................................................ 4
2.4.1 Java类及自定义类的说明 .................................................................................... 4
2.4.2类中成员及作用 ....................................................................................................... 5 String data;//接收数据 ..................................................................................................... 5 Socket client = null;//定义套接字 ................................................................................ 5 BufferedReader inputs = null;// 读取数据 ............................................................... 5 BufferedReader key = null;// 缓冲数据 .................................................................... 5 PrintStream outputs = null;//输出数据 ...................................................................... 5
2.5 程序运行效果及存在的问题 ................................................................................... 5
2.5.1运行效果图 ................................................................................................................ 5
2.5.2存在的问题 ................................................................................................................ 6 3程序详细设计 ............................................................................................................................ 6
3.1类获包的加载 ................................................................................................................ 6 import java.io.*; .................................................................................................................. 6 import java.net.*; ............................................................................................................... 6
3.2自定义类创建服务器端 .............................................................................................. 6
3.3创建数据处理类 ........................................................................................................... 7 4测试运行 ..................................................................................................................................... 8 5源代码清单 ................................................................................................................................ 9 6总结 ............................................................................................................................................ 11
基于JAVA的剪刀石头布游戏设计
1剪刀石头布游戏设计思路阐述
在设计游戏时,我们要充分考虑到剪刀石头布游戏的特性,以及多种技术的实现:
⑴构造创建服务器端和客户端
⑵创建输入输出流
⑶编写服务器端处理数据的线程方法
⑷绘制游戏界面
⑸绘制界面上的提示
采用此方法设计使程序简洁明了,使绘制游戏界面时简单化,更加易于游戏的实现,程序也更易于读懂。
如java中得到输入输出流的方法:
//调用客户端的getInputStream()方法
Inputs=newBufferedReader(new InputStreamReader(client.getInputStream())); 通过调用此方法获取从服务器得到的数据
调用客户端的getOutputStream()方法,通过此方法把客户端的数据传输给服务器。 2程序概要设计
2.1功能需求分析
一个剪刀石头布游戏,根据一般的常识,首先要有两个人,两个人同时出,然后,根据双方的出拳,决定谁是赢者,虽然想起来这个游戏很简单,但如何实现 ,还是有一定难度的。实现这个时针时涉及到几个技术问题:
1. 双方如何出拳
2. 如何同时出拳
3. 如何判断哪方赢了
4. 如何显示结果
剪刀石头布游戏,都是通过两个人同时出拳,然后根据双方出的拳,判定哪方是赢家。
2.2性能需求分析
准确性:在实现此的过程中,我们要考虑到,双方是否能同时地出拳,还要就是如何去判断,准确地知道哪方是赢家.
简洁性:在实现此的过程中,我们要考虑到,怎样使游戏界面更简单,让人更容易明白它怎样去玩。
2.3程序框图
游戏程序框图如图1所示:
图1 游戏程序框图
2.4 JAVA类及自定义类相互继承的层次关系
2.4.1 Java类及自定义类的说明
自定义类:
类名:SocketServer
作用:服务器类
继承的父类:Thread类
实现的接口:无
类名:Service
作用: 服务器类,它是判定胜负的类
继承的父类:Thread类
类中成员: String data;
BufferedReader inputs;
PrintStream outputs;
Socket client;
实现的接口:无
类名:Client
作用:客户端类
类中的成员: getInputStream();
getOutputStream();
InputStreamReader ();
readLine();
实现的接口:无
自定义类中子类和其父类的继承关系如图2如所示:
图2 子类及其父类继承关系图
2.4.2类中成员及作用
Service类成员:
String data; //接收数据
BufferedReader inputs;// 读取数据
PrintStream outputs;//输出数据
Socket client;//定义套接字
Client类成员:
String data;//接收数据
Socket client = null;//定义套接字
BufferedReader inputs = null;// 读取数据
BufferedReader key = null;// 缓冲数据
PrintStream outputs = null;//输出数据
2.5 程序运行效果及存在的问题
2.5.1运行效果图
剪刀石头布游戏效果如图3所示:
图3时钟效果图
2.5.2存在的问题
此游戏界面简洁明了,但还是有许多问题的,比如说这个界面不是GUI的,所以操作性不强,也不美观,而且处理数据的random函数也有问题,它可能出现相同的结果,不能成为真正意义上的随机,所以这个小游戏还是有许多问题的,我会进一步的改进它。
3程序详细设计
3.1类获包的加载
加载剪刀石头布游戏设计中用到的类和包,用于运行主程序:
import java.io.*;
import java.net.*;
3.2自定义类创建服务器端
使用自定义类ServerSocket继承父类Thread使用run等方法,构造函数创建一个服务器端
程序代码如下: 【5】。
public class SocketServer extends Thread {
ServerSocket server;
public SocketServer() {
try {
server = new ServerSocket(6000);
}
catch(IOException e) {
System.exit(0);
}
this.start();
}
public void run() {
try {
while(true) {
Socket client = server.accept();
Service ss = new Service(client);
}
}
catch(IOException e) {
System.exit(1);
}
}
public static void main(String[] args) {
new SocketServer();
}
}
3.3创建数据处理类
创建一个用于数据处理类使服务器能准确处理数据,获取数据,并使用方法run()实现数据处理。实现代码如下:
//创建数据储存变量
inputs = new BufferedReader(new InputStreamReader(client.getInputStream())); outputs = new PrintStream(client.getOutputStream());
//创建一个Run方法
public void run() {
}
3.4创建客户端类
创建一个用于客户端接收玩家输入的数据的类,程序实现代码如下:
//接收客户端的数据
public class Client {
public static void main(String[] args) {
String data;
Socket client = null;
BufferedReader inputs = null;
BufferedReader key = null;
PrintStream outputs = null;
try {
client = new Socket("localhost",6000);
inputs = new BufferedReader(new InputStreamReader(client.getInputStream()));
outputs = new PrintStream(client.getOutputStream());
key = new BufferedReader(new InputStreamReader(System.in));
}
catch(IOException e) {
System.out.println("CAN NOT CONN WITH SERVER");
}
try {
while(true) {
System.out.println("----------------------------");
System.out.print("请出拳:石头/剪刀/布 ");
data = key.readLine();
outputs.println(data);
System.out.println("ECHO: " + inputs.readLine());
}
}
catch(IOException e) {
System.out.println("ERROR IO");
}
try {
client.close();
}
catch(IOException e) {
System.out.println("can not close it");
}
}
}
4测试运行
程序编写好后,用JCreator软件运行,检测程序设计结果,执行目标程序后得到如图4所示的窗体:
图4 程序测试图
经检测程序运行正常。
5源代码清单
import java.io.*;
import java.net.*;
public class SocketServer extends Thread {
ServerSocket server;
public SocketServer() {
try {
server = new ServerSocket(6000);
}
catch(IOException e) {
System.exit(0);
}
this.start();
}
public void run() {
try {
while(true) {
Socket client = server.accept();
Service ss = new Service(client);
}
}
catch(IOException e) {
System.exit(1);
}
}
public static void main(String[] args) {
new SocketServer();
}
}
public class Service extends Thread {
String data;
BufferedReader inputs;
PrintStream outputs;
Socket client;
public Service(Socket socket) {
client = socket;
try {
inputs = new BufferedReader(new InputStreamReader(client.getInputStream()));
outputs = new PrintStream(client.getOutputStream());
}
catch (IOException e) {
e.printStackTrace();
}
this.start();
}
public void run() {
try {
while (true) {
data = inputs.readLine();
int res = (int) (Math.random() * 3); if (data == null)
break;
else {
if (res == 0 && "石头".equals(data)) { data = "电脑也是石头,打平了!!";
} else if (res == 0 && "剪刀".equals(data)) { data = "电脑是石头,电脑赢了!!";
} else if (res == 0 && "布".equals(data)) { data = "电脑是石头,恭喜,你赢了!!";
} else if (res == 1 && "石头".equals(data)) { data = "电脑是剪刀,恭喜,你赢了!!";
} else if (res == 1 && "剪刀".equals(data)) { data = "电脑也是剪刀,打平了!!";
} else if (res == 1 && "布".equals(data)) { data = "电脑是剪刀,电脑赢了!!";
} else if (res == 2 && "石头".equals(data)) { data = "电脑是布,电脑赢了!!";
} else if (res == 2 && "剪刀".equals(data)) { data = "电脑是布,恭喜,你赢了!!";
} else if (res == 2 && "布".equals(data)) { data = "电脑也是布,打平了!!";
} else {
data = "电脑不认识你出的拳"; }
outputs.println(data);
}
}
} catch (IOException e) {
e.printStackTrace();
}
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Client {
public static void main(String[] args) {
String data;
Socket client = null;
BufferedReader inputs = null;
BufferedReader key = null;
PrintStream outputs = null;
try {
client = new Socket("localhost",6000);
inputs = new
InputStreamReader(client.getInputStream()));
BufferedReader(new
outputs = new PrintStream(client.getOutputStream());
key = new BufferedReader(new InputStreamReader(System.in));
}
catch(IOException e) {
System.out.println("CAN NOT CONN WITH SERVER");
}
try {
while(true) {
System.out.println("----------------------------");
System.out.print("请出拳:石头/剪刀/布 ");
data = key.readLine();
outputs.println(data);
System.out.println("ECHO: " + inputs.readLine());
}
}
catch(IOException e) {
System.out.println("ERROR IO");
}
try {
client.close();
}
catch(IOException e) {
System.out.println("can not close it");
}
}
}
6总结
通过此次的课程设计,让我了解和学会了许多Java的基础知识。从课程设计中我学会了Java中类的作用及其方法的调用,知道了类的父类和子类的的继承关系及自定义类的作用,还使我了解到服务器和客户端的特点和作用,我需要学习的东西还很多。 通过此次的课程设计,让我了解到自己以往学习中的不足之处,认识到自己还有哪一些知识没有掌握,让我有了学习的重点和方向,更有利于今后的学习。在实践中发现问题,分析问题,解决问题,在每一个问题中,吸取知识和经验,最终掌握知识和技巧。通过课程剪刀石头布游戏,让我体会到了付出劳动才能有所收获的喜悦感,让我明白了只有真正通过实践才能更好的融汇和掌握所学的理论知识。这次课程设计,让我今后有了更好的学习方向,给了我更多的学习动力,对我的帮助很大。
第 11 页 共 12 页
参考文献
JAVA开发实战宝典--------编著:李钟蔚 JAVA面向对象编程--------编著:孙卫琴
第 12 页 共 12 页 出版社:清华大学出版社 出版社:电子工业出版社