您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關利用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; } }
以上就是利用JAVASE系統怎么實現一個抽卡功能,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。