本文实例为大家分享了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.net.Socket; import java.util.Scanner; import org.apache.log4j.Logger; import com.sun.security.ntlm.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 = socket.getOutputStream(); byte[] by = choice.getBytes(); outputStream.write(by, 0, by.length); outputStream.flush(); // socket.shutdownOutput(); } printwriter = new PrintWriter(outputStream); bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); System.out.println(bufferedReader.readLine()); switch (choice) { case "0": System.exit(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(); System.exit(0); } printwriter.println(choiceCards); printwriter.flush(); String str = bufferedReader.readLine(); Logger logger = Logger.getLogger(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) { System.err.println("您的输入不合法,请重新输入"); e.printStackTrace(); } } String checkstr = "Name="+name+":Age="+age; //将字符串传入网络流后对服务器的文件进行判断 printwriter.println(checkstr); printwriter.flush(); } }
Server端(多线程)
package SocketS; import java.io.IOException; import java.net.ServerSocket; import java.net.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(); 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) { socket = serverSocket.accept(); new Thread(new Server(socket)).start(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
ServerDao
package SocketS; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; import Cards.Cards; import Cards.CardsDao; import User.UsersDao; /** * 服务的操作 * * @author Administrator * */ public class ServerDao { UsersDao ud = new UsersDao(); File file = new File("D:\\Users.txt"); /** * 服务器接收Client发来的注册信息。并且将用户信息保存到文件中 * 暗 * @param socket * @return * @throws IOException * @throws ClassNotFoundException */ public boolean isRegistCheck(Scanner sc) throws IOException, ClassNotFoundException { String userStr = getObjectFromSocket(sc); // 将读取的User对象写入文件中 PrintWriter printWriter = new PrintWriter(new FileOutputStream(file,true)); printWriter.println(userStr); printWriter.flush(); return true; } /** * 检查登录信息是否正确 ,未完成!!! * * @param socket * @return 返回登录信息是否正确 * @throws IOException * @throws ClassNotFoundException */ public boolean isLoginCheck(Scanner sc) throws IOException, ClassNotFoundException { String userStr = getObjectFromSocket(sc); BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); String str = bufferedReader.readLine(); while (str != null) { if (str.contains(userStr)) { return true; } str = bufferedReader.readLine(); } return false; } /** * 获取客户端提供的Users对象 * @param socket 传入的对应客户端网络套接字 * @param reader 已经包装的BufferedReader 类获取了网络输入流 * @return 返回Users对象 * @throws IOException * @throws ClassNotFoundException */ private String getObjectFromSocket(Scanner sc) throws IOException, ClassNotFoundException { // 读取网络流,获取客户端提供的Users对象 String str = null; System.out.println(sc.hasNextLine()); while (sc.hasNextLine()) { str = sc.nextLine(); return str; } return str; } private String getCards(String choiceCards, PrintWriter printWriter){ CardsDao cardsDao = new CardsDao(); if ("1".equals(choiceCards)) { Cards card = cardsDao.once(); return card.getName(); }else if ("2".equals(choiceCards)) {//10连 Cards[] cardsArr = cardsDao.tenTimes(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < cardsArr.length; i++) { sb.append(cardsArr[i].getName()+" "); } return sb.toString(); } return null; } /** * 服务器接收用户传来的操作信息提供菜单 * * @param socket * @throws IOException * @throws ClassNotFoundException */ public void Menu(Socket socket) throws IOException, ClassNotFoundException { // 先处理玩家发来的注册登录选项 InputStream in = socket.getInputStream(); Scanner sc = new Scanner(in); byte[] by = new byte[10]; in.read(by, 0, by.length); String str = new String(by); String str1 = "1"; String str2 = "2"; System.out.println(str); str =str.trim(); PrintWriter printWriter = new PrintWriter(socket.getOutputStream()); boolean flag = true; if (str1.equals(str)) { // 登录 printWriter.println("请输入登录信息"); printWriter.flush(); flag= isLoginCheck(sc); } else if (str2.equals(str)) {// printWriter.println("请输入注册信息"); printWriter.flush(); isRegistCheck(sc); flag= isLoginCheck(sc); } if (flag) { System.out.println("欢迎登录"); }else { System.out.println("请您注册后再登录"); System.exit(0); } while (true) { //获取玩家的选择抽卡方式 by = new byte[10]; in.read(by, 0, by.length); String choiceCards = new String(by); choiceCards = choiceCards.trim(); //调取抽卡游戏: String cardsStr = getCards(choiceCards, printWriter); printWriter.println(cardsStr); printWriter.flush(); } } }
抽卡的具体实现(比较简单)
package Cards; import java.util.Random; /** * 抽卡类 * @author Administrator * */ public class CardsDao { public Cards once(){ double d = Math.random(); if (d>0 && d<=0.01) { return new NvWa(); }else if (d>0.01 && d<=0.1) { return new Crafty(); }else if(d>0.1){ Random random = new Random(); switch (random.nextInt(3)) { case 0: return new HeavenlyHound(); case 1: return new BlackFengHuang(); case 2: return new NineFox(); default: break; } } return null; } public Cards[] tenTimes(){ Cards[] cardsArr = new Cards[10]; for (int i = 0; i < cardsArr.length; i++) { Cards card = once(); cardsArr[i] = card; } return cardsArr; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。
- 本文固定链接: https://zxbcw.cn/post/200696/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)