jspublic class DataPoke {
String pokeFace;//牌面
String pokepoint;//点数
String pokeColor;//花色
String pokeImgPath;//扑克图片对应地址
boolean useFlag = false;//是否使用标记false未使用,true使用中
public DataPoke() {
}
public void setuserFlag(boolean flag){
this.useFlag = flag;
}
public boolean getuserFlag(){
return this.useFlag;
}
public String getPoint(){
return this.pokepoint;
}
public String getColor(){
return this.pokeColor;
}
public String getImgPath(){
return this.pokeImgPath;
}
public String getpokeFace(){
return this.pokeFace;
}
}
///////////////////////////////////////////////////////////////////////
import java.util.*;
public class pokeBean {
private String [] color = {"black","red","flower","sequa"};
private String pokeImgurl = "/m/g/bj/images/";
private LinkedList pokeList = new LinkedList();//poke链表列表
DataPoke[] dataPoke = new DataPoke[52];
public pokeBean() {
}
public void initPokeList(){
for(int i=0; i<=51; i++)
{
dataPoke[i] = new DataPoke();//初始化52个datapoke类变量存储
}
int over = 10;
int count=0;
for(int i=1; i<=13; i++){
for(int j=1; j<=4; j++){
if(i<10)
dataPoke[count].pokepoint = "" + i;//点数1-10
else
dataPoke[count].pokepoint = "" + over; //点数11-13
dataPoke[count].pokeFace = ""+i;//牌面
dataPoke[count].pokeColor = color[j-1];//牌色1-4: 黑 红 梅 方
dataPoke[count].pokeImgPath = pokeImgurl+Integer.toString(i)+"/"+Integer.toString(j)+".png";
dataPoke[count].setuserFlag(false);
pokeList.add(count,dataPoke[count]);//将用内部类DataPoke初始化好的52张牌放置于LinkedList中
//System.out.println("after insert poke --------- = "+count);
count=count+1;
}
}
}
//初始化完毕后,进行洗牌//这里暂且不用此功能
public void washPokeList(){
int temp1 = 0;
int temp2 = 0;
LinkedList pokeListtemp = new LinkedList();
for(int i=0; i<this.pokeList.size(); i++){
temp1 = (int)(Math.random()*100)%51;//取1-52之间的随机数
while(temp2 == temp1)
temp1 = (int)(Math.random()*100)%51;//如果出现重复的随机数,重新取
pokeListtemp.add(i,this.pokeList.get(temp1));//依次将乱序的pokeData对象插入
temp2 = temp1;
}
setuserLinklist(pokeListtemp);
}
public LinkedList getuserLinklist(){
return this.pokeList;
}
public void setuserLinklist(LinkedList pokeListtemp){
this.pokeList=pokeListtemp;
}
public void printBack(){
LinkedList temp = getuserLinklist();
for(int i=0; i<temp.size(); i++){
DataPoke tempDP = (DataPoke)temp.get(i);
System.out.println("********************"+i+"*********************");
System.out.println("------------牌面 "+tempDP.pokeFace);
System.out.println("------------点数 "+tempDP.pokepoint);
System.out.println("------------花色 "+tempDP.pokeColor);
System.out.println("------------地址 "+tempDP.pokeImgPath);
System.out.println("------------是否使用中 "+tempDP.useFlag);
}
}
}
/////////////////////////////////////////////////////////////////////////////
public class packageDb {
private String userName;//用户名
private String userPswd;//密码
private long numuserguid;//用户GUID
private long numsex;//用户性别
private long numage;//用户年龄
private long nummoney;//用户点数
private long numwincount;//用户胜利局数
private long numlostcount;//用户失败局数
public packageDb() {
}
//////////////////////////////////////////numwincount
public long getNumwincount() {
return this.numwincount;
}
public void setNumwincount(long numwincount) {
this.numwincount = numwincount;
}
//////////////////////////////////////////numlostcount
public long getNumlostcount() {
return this.numlostcount;
}
public void setNumlostcount(long numlostcount) {
this.numlostcount = numlostcount;
}
//////////////////////////////////////////numuserguid
public long getNumuserguid() {
return this.numuserguid;
}
public void setNumuserguid(long numuserguid) {
this.numuserguid = numuserguid;
}
///////////////////////////////////////////numsex
public long getNumsex() {
return this.numsex;
}
public void setNumsex(long numsex) {
this.numsex = numsex;
}
////////////////////////////////////////////numage
public long getNumage() {
return this.numage;
}
public void setNumage(long numage) {
this.numage = numage;
}
////////////////////////////////////////////nummoney
public long getNummoney() {
return this.nummoney;
}
public void setNummoney(long nummoney) {
this.nummoney = nummoney;
}
////////////////////////////////////////////userName
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
////////////////////////////////////////////取出用户基本信息,需先setUserGuid()
public void getUserInfo()throws Exception{
String sql = "select * from USER where NUMUSERGUID = '"+numuserguid+"'";
try{
DBOB DBwork = new DBOB();
DBwork.execute(sql);
if(DBwork.result.next())
{
setUserName(DBwork.result.getString("VC2USERNAME"));
setNummoney(DBwork.result.getLong("NUMMONEY"));
setNumage(DBwork.result.getLong("NUMAGE"));
setNumsex(DBwork.result.getLong("NUMSEX"));
setNumlostcount(DBwork.result.getLong("NUMLOSTCOUNT"));
setNumwincount(DBwork.result.getLong("NUMWINCOUNT"));
}
DBwork.releaseResource();
}catch(Exception e){
System.out.println("getUserInfo取出用户基本信息出错"+e);
}
}
/////////////////////////////////////////////检查新用户注册名是否占用
public boolean checkUserInfo(String userNameNew)throws Exception{
String sql = "select * from USER where VC2USERNAME = '"+userNameNew+"'";
//System.out.println("-------------before-sql : "+sql);
boolean temp = true;
try{
DBOB DBwork = new DBOB();
DBwork.execute(sql);
//System.out.println("-------------after-sql : "+sql);
if(DBwork.result.next())
{
temp = true;
}else{
temp = false;
}
DBwork.releaseResource();
}catch(Exception e){
System.out.println("checkUserInfo 检查新用户注册名是否占用出错"+e);
}
return temp;//用户名已经被占用,返回true,否则返回false;
}
/////////////////////////////////////////////插入新用户注册信息
public void insertUserNew(String userNamenew, String password, long sex, long age)throws Exception{
String sql = "insert into USER(NUMUSERGUID,VC2USERNAME,VC2PASSWORD,NUMSEX,NUMAGE) values (";
sql = sql + "MOBJSEQ.NEXTVAL, '";
sql = sql + userNamenew + "', '";
sql = sql + password + "', ";
sql = sql + sex + ", ";
sql = sql + age + ")";
//System.out.println("-------------before-sql : "+sql);
try{
DBOB DBwork = new DBOB();
DBwork.execute(sql);
//System.out.println("------------after--sql : "+sql);
DBwork.releaseResource();
}catch(Exception e){
System.out.println("insertUserNew 插入新用户注册信息出错"+e);
}
}
/////////////////////////////////////////////根据用户名得到用户guid
public long getUserGuid(String un)throws Exception{
String sql = "select NUMUSERGUID from USER where VC2USERNAME = '"+un+"'";//
long temp = 0;
try{
DBOB DBwork = new DBOB();
DBwork.execute(sql);
if(DBwork.result.next())
{
temp = DBwork.result.getLong("NUMUSERGUID");
}
DBwork.releaseResource();
}catch(Exception e){
System.out.println("getUserGuid 根据用户名得到用户guid出错"+e);
}
return temp;
}
/////////////////////////////////////////////检查老用户登陆
public boolean checkUserOld(String userNameOld, String password)throws Exception{
String sql = "select * from USER where VC2USERNAME='"+userNameOld+"' and VC2PASSWORD='"+password+"'";
boolean temp = true;
try{
DBOB DBwork = new DBOB();
DBwork.execute(sql);
if(DBwork.result.next())
{
temp = true;
}else{
temp = false;
}
DBwork.releaseResource();
}catch(Exception e){
System.out.println("checkUserOld 检查老用户登陆出错"+e);
}
return temp;//老用户的登陆用户名和密码正确无误,返回true,否则返回false;
}
/////////////////////////////////////////////更新用户数据
public void updateUser(long userGuid, long userMoney, long winCount, long lostCount)throws Exception{
String sql = "update USER set NUMMONEY="+userMoney+",NUMWINCOUNT="+winCount+",NUMLOSTCOUNT="+lostCount+"where NUMUSERGUID="+userGuid;
try{
DBOB DBwork = new DBOB();
DBwork.execute(sql);
DBwork.releaseResource();
}catch(Exception e){
System.out.println("updateUser 更新用户数据出错"+e);
}
}
}
///////////////////////////////////////////////////////
import javax.servlet.http.*;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;
import java.util.*;//随机数需要使用
import java.lang.*;
public class gamePai{
private HttpSession paiSession;
private pokeBean userPokeBean;
public gamePai(){
}
//取点数user
public int getPointuser(PageContext pagecontext){
HttpSession session = pagecontext.getSession();
LinkedList pokeLuser = (LinkedList) session.getAttribute("pokeuser");
int userValue = 0;
//用户点数计算
for(int i=0; i<pokeLuser.size(); i++){
userValue = userValue + Integer.parseInt(((DataPoke)pokeLuser.get(i)).getPoint());
}
return userValue;
}
//取点数pc
public int getPointpc(PageContext pagecontext){
HttpSession session = pagecontext.getSession();
LinkedList pokeLpc = (LinkedList) session.getAttribute("pokepc");
int pcValue = 0;
//电脑点数计算
for(int i=0; i<pokeLpc.size(); i++){
pcValue = pcValue + Integer.parseInt(((DataPoke)pokeLpc.get(i)).getPoint());
}
return pcValue;
}
//判断是否还有牌//不用作了,因为加了判断标志位
public boolean getPaiStatus(PageContext pagecontext)
{
HttpSession session = pagecontext.getSession();
LinkedList pokeLall = (LinkedList)session.getAttribute("poketotal");
if(pokeLall.size()<=4)//小于四张牌,就要重新洗牌了
return true;
return false;
}
//继续起牌,玩家调用,每次起牌之后,需要jsp叶面进行判断是否爆牌,是否跳转等
public void getNextPaiuser(PageContext pageContext){
HttpSession session = pageContext.getSession();
LinkedList pokeLall = (LinkedList)session.getAttribute("poketotal");
LinkedList pokeLuser = (LinkedList)session.getAttribute("pokeuser");
int temp = (int)(Math.random()*100)%51;//取1-52之间的随机数
while( ((DataPoke)pokeLall.get(temp)).getuserFlag() ){
temp = (int) (Math.random() * 100) % 51;
}
pokeLuser.add(pokeLall.get(temp));
((DataPoke)pokeLall.get(temp)).setuserFlag(true);
session.setAttribute("pokeuser",pokeLuser);//插入用户得到的牌
session.setAttribute("poketotal",pokeLall);//更新总牌
}
//判断电脑的分数是否低于16点,低于16点,就返回真,需要电脑自动加牌
//这里应该有jsp叶面调用
public boolean judgeAddPc16(PageContext pageContext){
HttpSession session = pageContext.getSession();
LinkedList pokeLpc = (LinkedList)session.getAttribute("pokepc");
int pcValue = 0;
//用户点数计算
for(int i=0; i<pokeLpc.size(); i++){
pcValue = pcValue + Integer.parseInt(((DataPoke)pokeLpc.get(i)).getPoint());
}
if( pcValue<=16 && pcValue <= 21 )//满足小于16,然后加了牌之后满足小于21,就再加
return true;
return false;
}
//判断电脑的分数是否高于17点,低于21点,就返回真,需要电脑自动加牌
//这里应该有jsp叶面调用
public boolean judgeAddPc21(PageContext pageContext){
HttpSession session = pageContext.getSession();
LinkedList pokeLpc = (LinkedList)session.getAttribute("pokepc");
int pcValue = 0;
//用户点数计算
for(int i=0; i<pokeLpc.size(); i++){
pcValue = pcValue + Integer.parseInt(((DataPoke)pokeLpc.get(i)).getPoint());
}
if( pcValue >= 17 && pcValue<=21 )
return true;
return false;
}
//电脑要牌
public void getNextPaipc(PageContext pageContext){
HttpSession session = pageContext.getSession();
LinkedList pokeLall = (LinkedList)session.getAttribute("poketotal");
LinkedList pokeLpc = (LinkedList)session.getAttribute("pokepc");
int temp = (int)(Math.random()*100)%51;//取1-52之间的随机数
while( ((DataPoke)pokeLall.get(temp)).getuserFlag() ){
temp = (int) (Math.random() * 100) % 51;
}
pokeLpc.add(pokeLall.get(temp));
((DataPoke)pokeLall.get(temp)).setuserFlag(true);
session.setAttribute("pokepc",pokeLpc);//插入用户得到的牌
session.setAttribute("poketotal",pokeLall);//更新总牌
}
//jsp叶面调用 判断user是否爆牌,true,爆牌,在叶面进行跳转到爆牌叶面
public boolean judgeBusrtuser(PageContext pageContext){
HttpSession session = pageContext.getSession();
LinkedList pokeLuser = (LinkedList)session.getAttribute("pokeuser");
int userValue = 0;
for(int i=0; i<pokeLuser.size(); i++){
userValue = userValue + Integer.parseInt(((DataPoke)pokeLuser.get(i)).getPoint());
if(userValue > 21)
return true;
}
return false;
}
//判断pc是否爆牌,true,爆牌
public boolean judgeBusrtpc(PageContext pageContext){
HttpSession session = pageContext.getSession();
LinkedList pokeLpc = (LinkedList)session.getAttribute("pokepc");
int pcValue = 0;
for(int i=0; i<pokeLpc.size(); i++){
pcValue = pcValue + Integer.parseInt(((DataPoke)pokeLpc.get(i)).getPoint());
if(pcValue > 21)
return true;
}
return false;
}
//取得扑克牌列表user
public ArrayList getPaiListUser(PageContext pageContext){
ArrayList templistuser = new ArrayList();
HttpSession session = pageContext.getSession();
LinkedList pokeLuser = (LinkedList)session.getAttribute("pokeuser");
for(int i=0; i<pokeLuser.size(); i++){
templistuser.add( ((DataPoke)pokeLuser.get(i)).getImgPath() );
}
return templistuser;
}
//取得扑克牌列表pc
public ArrayList getPaiListPc(PageContext pageContext){
ArrayList templistpc = new ArrayList();
HttpSession session = pageContext.getSession();
LinkedList pokeLpc = (LinkedList)session.getAttribute("pokepc");
for(int i=0; i<pokeLpc.size(); i++){
templistpc.add( ((DataPoke)pokeLpc.get(i)).getImgPath() );
}
return templistpc;
}
//开牌描述封装
public String getKaiPaiDesc(int resultPai){
if(resultPai==2){return "玩家胜!";}
if(resultPai==3){return "庄家胜!";}
if(resultPai==4){return "庄家 BlackJack, 庄家胜!";}
if(resultPai==5){return "玩家 BlackJack, 玩家胜!";}
if(resultPai==6){return "玩家爆牌!庄家胜!";}
if(resultPai==7){return "庄家爆牌!玩家胜!";}
return "";
}
//开牌
public int kaiPai(PageContext pagecontext){
HttpSession session = pagecontext.getSession();
LinkedList pokeLuser = (LinkedList) session.getAttribute("pokeuser");
LinkedList pokeLpc = (LinkedList) session.getAttribute("pokepc");
int userValue = 0;
int pcValue = 0;
//用户点数计算
for(int i=0; i<pokeLuser.size(); i++){
//DataPoke pokeuser = (DataPoke)pokeLuser.get(i);
userValue = userValue + Integer.parseInt(((DataPoke)pokeLuser.get(i)).getPoint());
}
//电脑点数计算
for(int i=0; i<pokeLpc.size(); i++){
//DataPoke pokepc = (DataPoke)pokeLpc.get(i);
pcValue = pcValue + Integer.parseInt(((DataPoke)pokeLpc.get(i)).getPoint());
}
//电脑blackJack,电脑胜利
if(pokeLpc.size()==2){
//DataPoke pokepcF = (DataPoke)pokeLpc.getFirst();
//DataPoke pokepcL = (DataPoke)pokeLpc.getLast();
if(((DataPoke)pokeLpc.getFirst()).getpokeFace().equals("1")&&((DataPoke)pokeLpc.getLast()).getpokeFace().equals("11")||
((DataPoke)pokeLpc.getFirst()).getpokeFace().equals("1")&&((DataPoke)pokeLpc.getLast()).getpokeFace().equals("11")){
return 4;//电脑blackJack,电脑胜利
}
}
//玩家blackJack,玩家胜利,分数加倍
if(pokeLuser.size()==2){
//DataPoke pokeuserF = (DataPoke)pokeLuser.getFirst();
//DataPoke pokeuserL = (DataPoke)pokeLuser.getLast();
if(((DataPoke)pokeLuser.getFirst()).getpokeFace().equals("1")&&((DataPoke)pokeLuser.getLast()).getpokeFace().equals("11")||
((DataPoke)pokeLuser.getLast()).getpokeFace().equals("1")&&((DataPoke)pokeLuser.getFirst()).getpokeFace().equals("11")){
return 5;//玩家blackJack,玩家胜利,分数加倍
}
}
if( userValue > pcValue )
return 2;//2为玩家胜利
if( userValue <= pcValue && pcValue <= 21)
return 3;//3为电脑胜利
if( userValue > 21 )
return 6;//玩家爆牌
if( pcValue > 21 && userValue <= 21 )
return 7;//电脑爆牌
return 0;
}
public void initPai(PageContext pagecontext)
{
userPokeBean = new pokeBean();
paiSession = pagecontext.getSession();//初始化得到session
userPokeBean.initPokeList();//初始化52扑克
LinkedList pokelistAll = userPokeBean.getuserLinklist();
LinkedList pokelistuser = new LinkedList();
LinkedList pokelistpc = new LinkedList();
//userPokeBean.washPokeList();//初始化之后洗扑克,有问题,不能洗牌
paiSession.setAttribute("poketotal",pokelistAll);//将初始化并洗好的扑克放置于该玩家session中
paiSession.setAttribute("pokeuser",pokelistuser);//初始化用户扑克数据
paiSession.setAttribute("pokepc",pokelistpc);//初始化电脑扑克数据
//userPokeBean.printBack();//打出牌看看
}
//大印出来检测一下
public void printPai()
{
userPokeBean.printBack();
}
//每次重新开局,都要将52张扑克牌的使用标志置为false,未使用
public void resetPai(PageContext pagecontext){
HttpSession session = pagecontext.getSession();
LinkedList pokeList = (LinkedList)session.getAttribute("poketotal");
for(int i=0; i<pokeList.size(); i++){
((DataPoke)pokeList.get(i)).setuserFlag(false);//所有的牌置为false,未使用
}
session.setAttribute("poketotal",pokeList);//更新总牌
}
//第一次起牌,在扑克牌中随机抽取四张,分别派给玩家和电脑
public void getFirstPaiuser(PageContext pagecontext)
{
HttpSession session = pagecontext.getSession();
//先取得用户的数据
LinkedList pokeListtempuser = (LinkedList)session.getAttribute("pokeuser");
pokeListtempuser.clear();//清空用户扑克数据列表
//LinkedList pokeListtempPC = new LinkedList();
//先从52张点数牌中随机抽取4张,分别为玩家的两张和庄家的两张
int inttemp1 = 0;
int inttemp2 = 0;
//先将总牌取出
//paiSession
LinkedList pokeListtempall1 = (LinkedList)session.getAttribute("poketotal");
//给玩家的牌
//for(int i=0; i<2; i++){//从中间取2张牌给玩家
//inttemp1 = (int)(Math.random()*100)%51;//取1-52之间的随机数
//inttemp2 = (int)(Math.random()*100)%51;
while(inttemp1==inttemp2){
inttemp1 = (int)(Math.random()*100)%51;//取1-52之间的随机数
inttemp2 = (int)(Math.random()*100)%51;
}
pokeListtempuser.add(pokeListtempall1.get(inttemp1));
pokeListtempuser.add(pokeListtempall1.get(inttemp2));
((DataPoke)pokeListtempall1.get(inttemp1)).setuserFlag(true);
((DataPoke)pokeListtempall1.get(inttemp2)).setuserFlag(true);
//while( inttemp2 == inttemp1 && (((DataPoke)pokeListtempall1.get(inttemp1)).getuserFlag()) )//如果出现重复的随机数或者该牌已经被取,重新取
// inttemp1 = (int)(Math.random()*100)%51;
//pokeListtempuser.add(pokeListtempall1.get(inttemp1));
//System.out.println("给玩家的牌的地址:"+((DataPoke)pokeListtempall1.get(inttemp1)).getImgPath());
//System.out.println("给玩家的牌的牌面点数:"+((DataPoke)pokeListtempall1.get(inttemp1)).getpokeFace());
//System.out.println("******************************************************");
//pokeListtempuser.a
//将取到的牌置为使用状态
//((DataPoke)pokeListtempall1.get(inttemp1)).setuserFlag(true);
//inttemp2 = inttemp1;
/
自己写的一个JSP小游戏21点,记录一下
80酷酷网 80kuku.com