package myPack;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import myPack.HandCard.HANDCARDTYPE;
public class Game extends OperateCard{
/**
*
*/
private static final long serialVersionUID = 1L;
public enum COMMNUNICATIONFLAG {
// 枚举列表必须写在最前面,否则编译出错
fapai(0), tongzhi(1);
private final int value;
public int getValue()
{
return this.value;
}
COMMNUNICATIONFLAG(int value)
{
this.value = value;
}
}
static List<Player> players = new ArrayList<Player>();
private List<Thread> threadList = new ArrayList<Thread>();
static HandCard curCards = null;
private int winnerIndex;
private int LimitedPlayerNum = 3;
static int curPlayerIndex = 0;
static int readyPlayerNum = 0;//准备的人数
static int notSetCardNum = 0;
private ServerSocket server = null;
private int port = 10086;
private boolean openServer = true;
public Game()
{
DataInputStream dis = null;
DataOutputStream dos = null;
try {
server = new ServerSocket(port);
while(openServer)
{
Socket sk = server.accept();
dis = new DataInputStream(sk.getInputStream());
dos = new DataOutputStream(sk.getOutputStream());
String playerName = null;
while(true)
{
dos.writeUTF("请输入用户名:");
playerName = dis.readUTF();
if(playerName.trim() == "")
{
dos.writeUTF("auto:您好,玩家的昵称不能为空,请重新取一个用户名");
}
else if(this.isPlayerExist(playerName))
{
dos.writeUTF("auto:您好,"+ playerName +"已经被使用,请重新取一个用户名");
}
else
{
break;
}
}
Player player = new Player(sk ,playerName);
Thread thread= new Thread(player);
players.add(player);
threadList.add(thread);
if(Game.players.size() == 3)
{
this.divideCard();
dos.writeUTF("hi,"+ playerName +" already for 3 player,Are you ready? please input 'ready' to start game,it's your turn!");
/*
* 输入格式规范
* 不出牌:#或者以#开始
* 出牌:根据下标来出牌
*/
for(Thread td:threadList)
td.start();
// try
// {
// Thread.sleep(1000);
// }catch(InterruptedException ie)
// {
// ie.printStackTrace();
// }
//告知谁先出牌
// Game.curPlayerIndex = (int)Math.rint(Math.random()*(Game.players.size()-1));
// this.tellWhoSet(curPlayerIndex);
}
else
{
dos.writeUTF("hi," + playerName +" wait for 3 player start,Are you ready? please input 'ready' to start game,it's your turn!");
}
}
}
catch(IOException ioe)
{
//System.out.println(ioe.getMessage());
ioe.printStackTrace();
}
catch(SecurityException se)
{
//System.out.println(se.getMessage());
se.printStackTrace();
}
catch(Exception ex)
{
//System.out.println(ex.getMessage());
ex.printStackTrace();
}
finally
{
try {
dis.close();
dos.close();
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public boolean isPlayerExist(String playerName)
{
for(Player player:players)
{
if(player.getPlayerName().equalsIgnoreCase(playerName))
return true;
}
return false;
}
public static void addReadyPlayer()
{
Game.readyPlayerNum ++;
if(Game.readyPlayerNum == 4)
{
Game.readyPlayerNum = 0;
}
}
public void addPlayer(Player player)
{
if(null == Game.players)
Game.players = new ArrayList<Player>();
Game.players.add(player);
}
public void removePlayer(Player player)
{
if(null == Game.players)
{
return ;
}
Game.players.remove(player);
}
public void removePlayerAll()
{
if(null == Game.players)
{
return ;
}
Game.players.clear();
}
public void start()
{
//玩家到达规定人数开始游戏
if(Game.players.size() != this.LimitedPlayerNum)
{
return ;
}
//洗牌
this.wash();
//发牌
this.sendCards();
//对玩家的牌进行排序
for(Player player:players)
player.sort();
}
public void run()
{
int random = (int)Math.rint(Math.random()*(Game.players.size()-1));
Game.players.get(random).selectCard();
Game.outCard(random);
System.out.println(Game.players.get(random).getPlayerName()+"出牌");
Game.players.get(random).removeHandCard();
if(Game.judgeOver(random))
{
return ;
}
int num = 0;
while(true)
{
System.out.println("---------curCard---------");
Game.curCards.show();
System.out.println("---------curCard---------");
random = (random+1)%Game.players.size();
if(num == 2)
{
Game.outCard(random);
System.out.println(Game.players.get(random).getPlayerName()+"出牌");
Game.players.get(random).removeHandCard();
if(Game.judgeOver(random))
{
return ;
}
num = 0;
}
else
{
if(Game.eatCard(random))
{
System.out.println(Game.players.get(random).getPlayerName()+"吃");
Game.players.get(random).removeHandCard();
if(Game.judgeOver(random))
{
return ;
}
num = 0;
}
else
{
System.out.println(Game.players.get(random).getPlayerName()+"不吃");
num++;
}
}
}
}
public static void outCard(int playerIndex)
{
int flag = 0;
do{
flag = Game.players.get(playerIndex).selectCard();
if(flag == 1)
{
Game.curCards = Game.players.get(playerIndex).getHandCard();
}
else if(flag == -1)
{
Game.players.get(playerIndex).send("auto:输入数据不合规则");
}
}while(flag == -1||flag == 0);
return ;
}
/*
* return
* true:可以吃
* false:不吃或吃不了
*/
public static boolean eatCard(int playerIndex)
{
int flag = 0;
do{
flag = Game.players.get(playerIndex).selectCard();
if(flag == 0)
{
return false;
}
else if(flag == 1)
{
//判断是否吃的下
if(Game.eat(Game.players.get(playerIndex).getHandCard()))
{
Game.curCards = Game.players.get(playerIndex).getHandCard();
return true;
}
else
{
//System.out.println(Game.players.get(playerIndex).getPlayerName()+"吃不下");
Game.players.get(playerIndex).send("auto:您出的牌吃不下,请重出或者'#'不出");
flag = -1;
}
}
else
{
Game.players.get(playerIndex).send("auto:输入数据不合规则");
}
}while(flag == -1);
return false;
}
public static boolean eat(HandCard handCard)
{
if(handCard.getCardType() == HANDCARDTYPE.huojian.getValue())
return true;
else if(handCard.getCardType() == HANDCARDTYPE.zhadan.getValue())
{
if(curCards.getCardType() == handCard.getCardType())
{
return handCard.getValue()>Game.curCards.getValue()? (true):(false);
}
else
return true;
}
else if((handCard.getCardType() == Game.curCards.getCardType()) && (handCard.getCardLen() == Game.curCards.getCardLen()))
{
return handCard.getValue()>Game.curCards.getValue()? (true):(false);
}
return false;
}
public static boolean judgeOver(int playerIndex)
{
if(Game.players.get(playerIndex).getCardLen()==0)
{
//this.winnerIndex = playerIndex;
return true;