当前位置:文档之家› JAVA的剪刀石头布游戏设计方案

JAVA的剪刀石头布游戏设计方案

目录1剪刀石头布游戏设计思路阐述12程序概要设计22.1功能需求分析22.2性能需求分析22.3程序框图22.4 JA V A类及自定义类相互继承的层次关系32.4.1 Java类及自定义类的说明32.4.2类中成员及作用3String data。

//接收数据4Socket client = null。

//定义套接字4BufferedReader inputs = null。

// 读取数据4BufferedReader key = null。

// 缓冲数据4PrintStream outputs = null。

//输出数据42.5 程序运行效果及存在的问题42.5.1运行效果图42.5.2存在的问题43程序详细设计43.1类获包的加载4import java.io.*。

4import .*。

53.2自定义类创建服务器端53.3创建数据处理类54测试运行65源代码清单76总结9基于JAVA的剪刀石头布游戏设计1剪刀石头布游戏设计思路阐述在设计游戏时,我们要充分考虑到剪刀石头布游戏的特性,以及多种技术的实现:⑴构造创建服务器端和客户端⑵创建输入输出流⑶编写服务器端处理数据的线程方法⑷绘制游戏界面⑸绘制界面上的提示采用此方法设计使程序简洁明了,使绘制游戏界面时简单化,更加易于游戏的实现,程序也更易于读懂。

如java中得到输入输出流的方法://调用客户端的getInputStream()方法Inputs=newBufferedReader(newInputStreamReader(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 .*。

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 .*。

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()。

相关主题