欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JAVASE系统实现抽卡功能

程序员文章站 2022-03-10 09:10:42
本文实例为大家分享了javase系统实现抽卡功能的具体代码,供大家参考,具体内容如下先看下文件结构使用到的知识点:看下client类的实现:package sockets;import java.io...

本文实例为大家分享了javase系统实现抽卡功能的具体代码,供大家参考,具体内容如下

先看下文件结构

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.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 抽卡