基于Java的图形化中国象棋游戏+PPT文档

下载地址

介绍

(具有网络通信功能)

使用Java语言实现的经典中国象棋程序, 带有演讲PPT文档

功能

  • 登录功能
  • 登录与游戏界面之间添加闪屏界面(类似启动页图片)
  • 游戏准备
  • 投降认输
  • 悔棋
  • 聊天沟通
  • 计时
  • 退出等

实现说明与代码

账户密码等是采用文本形式存储,使用认证方法进行统一认证,若是需要采用MySQL等数据库进行用户存储,只需稍微调整认证逻辑即可。
聊天功能的实现是使用Socket进行通信。

主要代码具体代码详见

package com.tarena.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashSet;
import com.tarena.dao.UserDAOForGetUser;
import com.tarena.model.ChessMap;
import com.tarena.model.Request;
import com.tarena.model.Respone;
import com.tarena.model.User;

public class ServerMain {
	private ServerSocket ss=null;
	
	private Socket s=null;
	private ArrayListqipu;
	private HashSet allSockets;
	private HashSet allThread;
	private ArrayListThreads;
	private boolean flag=false;
	
	public ServerMain(){
		try {
			qipu=new ArrayList();
			ss=new ServerSocket(8888);
			allSockets=new HashSet();
			allThread=new HashSet();
			Threads=new ArrayList();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

	
	
	
	
	public void startServer(){
		try {
			while (true) {
				s = ss.accept();
				allSockets.add(s);
				ServerThread t=new ServerThread(s);
				allThread.add(t);
				Threads.add(t);
				t.start();
				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void init(){
		startServer();
		
	}
	
	class ServerThread extends Thread{
		private Socket s;
		private ObjectInputStream ois;
		private ObjectOutputStream oos;
		boolean isok=true;
		public ServerThread(Socket s){
			this.s=s;
			try {
				ois=new ObjectInputStream(s.getInputStream());
				oos=new ObjectOutputStream(s.getOutputStream());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		public void run(){
			
			while(isok){
				Request req;
				if(allSockets.size()==0){
					flag=false;
				}
				try {
					req = (Request)ois.readObject();
					switch(req.getType()){
					case Request.LOGIN_REQUEST:login(req);break;
					case Request.READY_REQUEST:ready(req);break;
					case Request.START_REQUEST:start(req);break;
					case Request.END_REQUEST:;break;
					case Request.MOVE_REQUEST:move(req);break;
					case Request.CHART_REQUEST:chart(req);break;
					}	
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

		private void start(Request req) {
		if(flag==false){
			flag=true;
			String str=(String)req.getData("start");
			Respone res=new Respone(Respone.START_RESPONE);
			res.addData("start", str);
			sendMapToAllClient(res);
		}
			
		}

		private void login(Request req) {
				UserDAOForGetUser getuser=new UserDAOForGetUser();
				String id=(String)req.getData("id");
				String passwd=(String)req.getData("passwd");
				
				User user=(User)getuser.getUser(id, passwd);
				Respone res=new Respone(Respone.LOGIN_RESPONE);
				res.addData("user",user);
				try {
					oos.writeObject(res);
					oos.flush();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
		
		}
		
	
		
		private void move(Request req){//TODO
			ChessMap map;
			
				map=(ChessMap)req.getData("move");
				if(map.getMap().length==11){
					qipu.remove(qipu.size()-1);
					qipu.remove(qipu.size()-1);
					
					map=qipu.get(qipu.size()-1);
					Respone res=new Respone(Respone.MOVE_RESPONE);
					res.addData("move", map);
					sendMapToAllClient(res);
				}else{
					qipu.add(map);
					Respone res=new Respone(Respone.MOVE_RESPONE);
					res.addData("move", map);
					sendMapToAllClient(res);
				}
				
			
			
			
			
			
			
		}
		private void chart(Request req){
			String str=(String)req.getData("chart");
			if(str.equals("@#$EXIT!@#$")){
				isok=false;
				try {
					allSockets.remove(s);
					allThread.remove(this);
					s.close();
					Respone res=new Respone(Respone.CHART_RESPONE);
					res.addData("chart", str);
					sendMessageAllClient(res);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
//			else if(str.equals("对方请求悔棋,是否同意")
//					){//@#$对手请求悔棋,是否同意
//				
//				
//				Respone res=new Respone(Respone.MOVE_RESPONE);
//				qipu.remove(qipu.size()-1);
//				qipu.remove(qipu.size()-1);
//				ChessMap map=qipu.get(qipu.size()-1);
//
//				
//				
//				
//				res.addData("move", map);
//				sendMessageAllClient(res);
//			}
			else{
			Respone res=new Respone(Respone.CHART_RESPONE);
			res.addData("chart", str);
			sendMessageAllClient(res);	
			}
		}
		
	}
	public void sendMessageAllClient(Respone res){
		
		for(ServerThread t:allThread){
			try {
				t.oos.writeObject(res);	
				t.oos.flush();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public void sendMapToAllClient(Respone res){
		for(ServerThread t:allThread){
			try {
				t.oos.writeObject(res);	
				t.oos.flush();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	private void ready(Request req){
		if(allSockets.size()==2){
			Respone res=new Respone(Respone.READY_RESPONE);
			res.addData("ready",true);
			try {
					Threads.get(0).oos.writeObject(res);
					Threads.get(0).oos.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}
			

	}
	
	public static void main(String[] args) {
		new ServerMain().init();
	}
	
}

展示

基于Java的图形化中国象棋游戏+PPT文档_第1张图片
基于Java的图形化中国象棋游戏+PPT文档_第2张图片

基于Java的图形化中国象棋游戏+PPT文档_第3张图片
基于Java的图形化中国象棋游戏+PPT文档_第4张图片
基于Java的图形化中国象棋游戏+PPT文档_第5张图片

你可能感兴趣的:(Java,JavaGUI)