java游戏抽卡_JAVASE系统实现抽卡功能本⽂实例为⼤家分享了JAVASE系统实现抽卡功能的具体代码,供⼤家参考,具体内容如下
先看下⽂件结构
使⽤到的知识点:
看下Client类的实现:
package SocketS;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.Socket;
import java.util.Scanner;
import org.apache.log4j.Logger;
import com.lm.Client;
import User.Users;
import User.UsersDao;
/**
* 客户端调⽤登录/注册 后绑定⽤户操作
*
* @author Administrator
*
*/
public class Cilent {
public static void main(String[] args) {
try {
Socket socket = new Socket("127.0.0.1", 11536);
Menu(socket);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void Menu(Socket socket) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter printwriter = null;
OutputStream outputStream = null;
BufferedReader bufferedReader = null;
String choice;
do {
System.out.println("请您选择:1.⽼⽤户⽴即登录 2.新⽤户注册即玩\n" + "请输⼊正确的数,输⼊0退出系统"); choice = sc.next();
System.out.println(choice);
// 先传⼊玩家的操作选项
if (Integer.parseInt(choice) > 0 && Integer.parseInt(choice) < 3) {
outputStream = OutputStream();
byte[] by = Bytes();
outputStream.write(by, 0, by.length);
outputStream.flush();
// socket.shutdownOutput();
}
printwriter = new PrintWriter(outputStream);
bufferedReader = new BufferedReader(new InputStream())); System.out.adLine());
switch (choice) {
case "0":
break;
case "1":
ClientLogin(printwriter,sc);
break;
case "2":
ClientRegist(printwriter);// 注册
ClientLogin(printwriter,sc);
break;
}
} while (Integer.parseInt(choice) > 3 || Integer.parseInt(choice) < 1); while (true) {
//登录完成!
//获取服务器传来的消息!
System.out.println("请选择:1.单抽过过瘾!2.10连抽任性 0.退出"); String choiceCards = sc.next();
if ("0".equals(choiceCards)) {
socket.close();
}
printwriter.println(choiceCards);
printwriter.flush();
String str = adLine();
Logger logger = Logger(Client.class);
logger.info(str);
System.out.println(str);
}
}
/**
* 客户端⽤户注册//注册,并将对象通过对象写出到⽹络流中
*
* @param socket
* @throws IOException
private static void ClientRegist(PrintWriter printwriter) throws IOException { UsersDao uersDao = new UsersDao();
Users u = uersDao.UserRegister();
printwriter.println(u);
printwriter.flush();
// socket.shutdownOutput();
}
private static void ClientLogin(PrintWriter printwriter,Scanner sc){
String name = null;
int age = 0 ;
while (true) {
try {
System.out.println("请输⼊昵称");
name = sc.next();
System.out.println("请输⼊年龄");
age = sc.nextInt();
break;
} catch (Exception e) {
e.printStackTrace();
}
}
String checkstr = "Name="+name+":Age="+age;
//将字符串传⼊⽹络流后对服务器的⽂件进⾏判断
printwriter.println(checkstr);
printwriter.flush();
}
}
Server端(多线程)
package SocketS;
import java.io.IOException;
import java.ServerSocket;
import java.Socket;
* 服务器 需要完成登录校验 ,注册校验 以及游戏抽卡* @author Administrator
*
*/
public class Server implements Runnable { Socket socket = null;
public Server(Socket socket) {
super();
this.socket = socket;
}
@Override
public void run() {
ServerDao serverDao = new ServerDao();简单的java游戏代码
try {
serverDao.Menu(socket);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) { ServerSocket serverSocket = null;
Socket socket = null;
try {
serverSocket = new ServerSocket(11536);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
while (true) {
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论