您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)利用JAVASE系統(tǒng)怎么實(shí)現(xiàn)一個(gè)抽卡功能,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
先看下文件結(jié)構(gòu)
使用到的知識點(diǎn):
看下Client類的實(shí)現(xiàn):
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; /** * 客戶端調(diào)用登錄/注冊 后綁定用戶操作 * * @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" + "請輸入正確的數(shù),輸入0退出系統(tǒng)"); choice = sc.next(); System.out.println(choice); // 先傳入玩家的操作選項(xiàng) 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) { //登錄完成! //獲取服務(wù)器傳來的消息! 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); } } /** * 客戶端用戶注冊//注冊,并將對象通過對象寫出到網(wǎng)絡(luò)流中 * * @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; //將字符串傳入網(wǎng)絡(luò)流后對服務(wù)器的文件進(jìn)行判斷 printwriter.println(checkstr); printwriter.flush(); } }
Server端(多線程)
package SocketS; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; /** * 服務(wù)器 需要完成登錄校驗(yàn) ,注冊校驗(yàn) 以及游戲抽卡 * @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; /** * 服務(wù)的操作 * * @author Administrator * */ public class ServerDao { UsersDao ud = new UsersDao(); File file = new File("D:\\Users.txt"); /** * 服務(wù)器接收Client發(fā)來的注冊信息。并且將用戶信息保存到文件中 * 暗 * @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 傳入的對應(yīng)客戶端網(wǎng)絡(luò)套接字 * @param reader 已經(jīng)包裝的BufferedReader 類獲取了網(wǎng)絡(luò)輸入流 * @return 返回Users對象 * @throws IOException * @throws ClassNotFoundException */ private String getObjectFromSocket(Scanner sc) throws IOException, ClassNotFoundException { // 讀取網(wǎng)絡(luò)流,獲取客戶端提供的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; } /** * 服務(wù)器接收用戶傳來的操作信息提供菜單 * * @param socket * @throws IOException * @throws ClassNotFoundException */ public void Menu(Socket socket) throws IOException, ClassNotFoundException { // 先處理玩家發(fā)來的注冊登錄選項(xiàng) 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(); //調(diào)取抽卡游戲: String cardsStr = getCards(choiceCards, printWriter); printWriter.println(cardsStr); printWriter.flush(); } } }
抽卡的具體實(shí)現(xiàn)(比較簡單)
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系統(tǒng)怎么實(shí)現(xiàn)一個(gè)抽卡功能,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。