⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gameserver.java

📁 局域网斗地主框架,好象没有card的功能不能打牌
💻 JAVA
字号:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/*
 * 创建日期 2005-7-22
 *
 * TODO 要更改此生成的文件的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */

/**
 * @author 叶少伟 TODO 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
public class GameServer implements Runnable
{
	private ServerSocket		server;
	private Socket				sSocket[]	= new Socket [3];
	private ObjectInputStream	sin[]		= new ObjectInputStream [3];
	private ObjectOutputStream	sout[]		= new ObjectOutputStream [3];
	private Player				player[]	= new Player [3];
	private JTextField			messageTextField;
	private JButton				pButton, nButton, jButton;
	private JFrame				owner;
	private boolean				closed		= false;
	private int					port;
	private PKCard []			allCards, lastCards;
	private PKCard playerCards[][] = new PKCard[3][20];
	private int[]					cardsNumber	= {17, 17, 17};

	public GameServer(JTextField text, JButton pb, JButton nb, JButton jb,
			JFrame owner, int port)
	{
		this.messageTextField = text;
		this.pButton = pb;
		this.nButton = nb;
		this.jButton = jb;
		this.owner = owner;
		this.port = port;
		new Thread(this).start();
	}

	public void initCards()
	{
		allCards = new PKCard [54];
		for(int i = 1; i <= 4; i++)
		{
			for(int j = 1; j <= 13; j++)
			{
				allCards[(i - 1) * 13 + j - 1] = new PKCard(i + "-" + j);
			}
		}
		allCards[52] = new PKCard("g1");
		allCards[53] = new PKCard("g2");
		randomCards();
	}

	public void randomCards()
	{
		PKCard temp;
		for(int i = 0; i < 54; i++)
		{
			int x = (int) (Math.random() * 54);
			int y = (int) (Math.random() * 54);
			temp = allCards[x];
			allCards[x] = allCards[y];
			allCards[y] = temp;
		}
	}

	public void dealCards()
	{
		randomCards();

		lastCards = new PKCard [3];
		for(int i = 0; i < 3; i++)
			lastCards[i] = allCards[i];
		for(int i = 0; i < 3; i ++)
		{
			for(int j = 0; j < 17; j ++)
			{
				playerCards[i][j] = allCards[i * 17 + j + 3];
			}
		}
	}

	public void startGame()
	{
		initCards();
		randomCards();
		dealCards();
		try
		{
			for(int i = 0; i < 3; i++)
			{
				sout[i].writeObject(new Message(Message.CONTROL, "start"));
			}
		}
		catch(IOException e)
		{
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
	}

	public boolean canClose()
	{
		if(player[1] == null && player[2] == null)
		{
			return true;
		}
		else
			return false;
	}

	public boolean isColsed()
	{
		return closed;
	}

	public void colse()
	{
		try
		{
			for(int i = 0; i < 3; i++)
			{
				if(sSocket[i] != null)
				{
					sout[i].writeObject(new Message(Message.CONTROL, "exit"));
					player[i] = null;
				}
			}
			this.server.close();
		}
		catch(IOException e)
		{
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		closed = true;
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see java.lang.Runnable#run()
	 */
	public void run()
	{
		try
		{
			try
			{
				// TODO 自动生成方法存根
				this.server = new ServerSocket(port);
			}
			catch(BindException e1)
			{
				// TODO 自动生成 catch 块
				e1.printStackTrace();
				JOptionPane.showMessageDialog(owner, "此端口已被其它程序使用!", "提示",
						JOptionPane.WARNING_MESSAGE);
				messageTextField.setEditable(false);
				pButton.setEnabled(true);
				nButton.setText("新建游戏");
				jButton.setEnabled(true);
				closed = true;
				return;
			}
			while(true)
			{
				Socket s = this.server.accept();
				int i = 0;
				while(i < 3)
				{
					if(this.sSocket[i] != null)
						i++;
					else
						break;
				}
				if(i == 3)
				{
					try
					{
						new ObjectInputStream(s.getInputStream()).readObject();
					}
					catch(ClassNotFoundException e2)
					{
						// TODO 自动生成 catch 块
						e2.printStackTrace();
					}
					new ObjectOutputStream(s.getOutputStream())
							.writeObject(new Message(Message.CONTROL, "服务器已满"));
					s.close();
				}
				else
				{
					this.sSocket[i] = s;
					this.sin[i] = new ObjectInputStream(this.sSocket[i]
							.getInputStream());
					this.sout[i] = new ObjectOutputStream(this.sSocket[i]
							.getOutputStream());
					new Thread(new ServerThread(i)).start();
				}
			}
		}
		catch(SocketException e)
		{
			return;
		}
		catch(IOException e)
		{
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
	}

	class ServerThread implements Runnable
	{
		private int	i;

		public void sendMessage(Message mess)
		{
			for(int j = 0; j < 3; j++)
			{
				if(sSocket[j] != null)
					try
					{
						sout[j].writeObject(mess);
					}
					catch(IOException e)
					{
						// TODO 自动生成 catch 块
						e.printStackTrace();
					}
			}
		}

		public void sendPlayers()
		{
			for(int j = 0; j < 3; j++)
			{
				sendMessage(new Message(Message.PLAYER, player[j]));
			}
			if(player[0] != null && player[1] != null && player[2] != null)
			{
				startGame();
			}
			else
			{

			}
		}

		public ServerThread(int i)
		{
			this.i = i;
		}

		public synchronized void run()
		{
			Message mess;
			Player p;
			try
			{
				mess = (Message) sin[i].readObject();
				p = (Player) mess.getContent();
				for(int j = 0; j < 3; j++)
				{
					if(player[j] != null
							&& player[j].getName()
									.equalsIgnoreCase(p.getName()))
					{
						sout[i]
								.writeObject(new Message(Message.CONTROL, "已存在"));
						sSocket[i] = null;
						return;
					}
				}
				player[i] = p;
				sendMessage(new Message(Message.STRING, player[i].getName()
						+ "加入游戏!"));
				sendPlayers();
			}
			catch(ClassNotFoundException e)
			{
				e.printStackTrace();
			}
			catch(IOException e)
			{
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}

			try
			{
				Message resend = null;
				while(true)
				{
					mess = (Message) sin[i].readObject();
					if(mess.getType() == Message.CONTROL
							&& mess.getContent().toString().equals("exit"))
					{
						try
						{
							sout[i].writeObject(new Message(Message.CONTROL,
									"okexit"));
							sin[i].close();
							sin[i] = null;
							sout[i].close();
							sout[i] = null;
							sSocket[i].close();
							sSocket[i] = null;
						}
						catch(IOException e1)
						{
							// TODO 自动生成 catch 块
							e1.printStackTrace();
						}
						sendMessage(new Message(Message.STRING, player[i]
								.getName()
								+ "离开游戏!"));
						sendMessage(new Message(Message.CONTROL, "oneclientexit"));
						player[i] = null;
						sendPlayers();
						return;
					}
					else if(mess.getType() == Message.CONTROL
							&& mess.getContent().toString().equals("okexit"))
					{
						sout[i].close();
						sout[i] = null;
						sin[i].close();
						sin[i] = null;
						sSocket[i].close();
						sSocket[i] = null;
						player[i] = null;
						return;
					}
					else if(mess.getType() == Message.CONTROL
							&& mess.getContent().toString().equals("okstart"))
					{
//						System.out.println("okstart" + i);
						sout[i].writeObject(resend = new Message(Message.PLAYERCARDS, playerCards[i]));
//						sout[i].flush();
//						System.out.println("sendok" + i);
					}
					else if(mess.getType() == Message.CONTROL
							&& mess.getContent().toString().equals("okplayercards"))
					{
//						System.out.println("okplayercards" + i);
						sout[i].writeObject(resend = new Message(Message.LASTCARDS, lastCards));
//						sout[i].flush();
//						System.out.println("sendok" + i);
					}
					else if(mess.getType() == Message.CONTROL
							&& mess.getContent().toString().equals("oklastcards"))
					{
//						System.out.println("oklastcards" + i);
						sout[i].writeObject(resend = new Message(cardsNumber));
//						sout[i].flush();
//						System.out.println("sendok" + i);
					}
					else if(mess.getType() == Message.CONTROL
							&& mess.getContent().toString().equals("resend"))
					{
						sout[i].writeObject(resend);
//						sout[i].flush();
					}
					else
					{
						sendMessage(mess);
					}
				}
			}
			catch(ClassNotFoundException e)
			{
				e.printStackTrace();
			}
			catch(IOException e)
			{
				// TODO 自动生成 catch 块
				e.printStackTrace();
				String m = e.getMessage();
				if(m.indexOf("reset") != -1)
				{
					JOptionPane.showMessageDialog(owner, "丢失与"
							+ player[i].getName() + "玩家的连接!", "提示",
							JOptionPane.WARNING_MESSAGE);
				}
			}

		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -