JAVASE系統(tǒng)實現(xiàn)抽卡功能
本文實例為大家分享了JAVASE系統(tǒng)實現(xiàn)抽卡功能的具體代碼,供大家參考,具體內(nèi)容如下
先看下文件結(jié)構(gòu)

使用到的知識點:

看下Client類的實現(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);
// 先傳入玩家的操作選項
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ù)器的文件進行判斷
printwriter.println(checkstr);
printwriter.flush();
}
}
Server端(多線程)
package SocketS;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 服務(wù)器 需要完成登錄校驗 ,注冊校驗 以及游戲抽卡
* @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 傳入的對應客戶端網(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ā)來的注冊登錄選項
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();
}
}
}
抽卡的具體實現(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;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SSM框架整合之Spring+SpringMVC+MyBatis實踐步驟
大家都知道Spring是一個輕量級的控制反轉(zhuǎn)(IoC)和面向切面(AOP)的容器框架,本文主要介紹三大框架的整合包含spring和mybatis的配置文件,還有spring-mvc的配置文件的詳細介紹,通過項目實踐步驟給大家詳細介紹,感興趣的朋友一起看看吧2021-06-06
Java實戰(zhàn)之小蜜蜂擴音器網(wǎng)上商城系統(tǒng)的實現(xiàn)
這篇文章主要介紹了如何利用Java實現(xiàn)簡單的小蜜蜂擴音器網(wǎng)上商城系統(tǒng),文中采用到的技術(shù)有JSP、Servlet?、JDBC、Ajax等,感興趣的可以動手試一試2022-03-03
Java代碼中與Lua相互調(diào)用實現(xiàn)詳解
這篇文章主要為大家介紹了Java代碼中與Lua相互調(diào)用實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
jeefast和Mybatis實現(xiàn)三級聯(lián)動的示例代碼
這篇文章主要介紹了jeefast和Mybatis實現(xiàn)三級聯(lián)動的示例代碼,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
Spring?Boot?Actuator管理日志的實現(xiàn)
本文主要介紹了Spring?Boot?Actuator管理日志的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07
如何基于mybatis框架查詢數(shù)據(jù)庫表數(shù)據(jù)并打印
這篇文章主要介紹了如何基于mybatis框架查詢數(shù)據(jù)庫表數(shù)據(jù)并打印,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-11-11

