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

📄 j_fir_client.java

📁 单机黑白棋显示
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
				
				while (jfh.u_Choice[0]==-1)
					;
					
				if (jfh.u_Choice[0]==1) {
					host = jfh.u_Host[0];
					isConnected = true;
				} else 
				if (jfh.u_Choice[0]==-2) {
					isConnected = false;
				}
				
				jfh.dispose();
			} else {
				J_FIR_TwoChoice jfs = new J_FIR_TwoChoice(PopContent,Posi,Nega);
				//jfs.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				jfs.setSize(300,200);
				jfs.setLocation(450,450);
				jfs.setVisible(true);
				while (jfs.u_Choice[0]==-1)
					;
				if (jfs.u_Choice[0]==1) {
					if (PopContent.equals("您真的要认输吗?")) {
						m_output.writeObject(WINNER_DECIDED+"Lose");
					} else
					if (PopContent.equals("您当前有打开的棋局,邀请对方接着下吗?")) {
						m_output.writeObject(CONTINUE_REQUEST+Opponent[0]);
					} else
					if (PopContent.equals("您确定要退出程序吗?")) {
						if (isConnected) m_output.writeObject(EXIT_SERVER);
						System.exit(0);
					} else
					if (PopContent.equals("对方想悔棋,您是否同意?")) {
						m_output.writeObject(REGRET_AS_YOU_LIKE);
						if (n%2==0 && ColorInHand==1) { if (n>=1) n-=2; } else
						if (n%2==0 && ColorInHand==2) n--; else
						if (n%2==1 && ColorInHand==1) { if (n>=0) n--; } else
						if (n%2==1 && ColorInHand==2) { if (n>=1) n-=2; }
						m_board.updateBoard(x,y,n);
					}
				} else {
					if (PopContent.equals("对方想悔棋,您是否同意?")) {
						m_output.writeObject(NO_REGRET);
					}
				}
				jfs.dispose();
			}
		}
		catch (Exception ee)
		{
			System.out.println("出现异常:"+ee);
			ee.printStackTrace();
		}
	}
	public void ShowInvitation(String Name,String InvitationContent) //弹邀请框
	{
		try
		{
			J_FIR_Invite jfi = new J_FIR_Invite(Name,InvitationContent);
			//jfi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			jfi.setSize(300,200);
			jfi.setLocation(500,450);
			jfi.setVisible(true);
			
			while (jfi.u_Choice[0]==-1)
				;
				
			if (jfi.u_Choice[0]==1) {
				m_output.writeObject(REQUEST_ACCEPTED+Name);
				mb_displayAppend("您接受了"+Name+"的邀请。");
				Opponent[0] = Name;
				readyToPlay = true; 
			}	else
				if (jfi.u_Choice[0]==-2) {
					m_output.writeObject(REQUEST_REJECTED);
					mb_displayAppend("您拒绝了"+Name+"的邀请。");
				}
			
			jfi.dispose();
		}
		catch (Exception ee)
		{
			System.out.println("出现异常:"+ee);
			ee.printStackTrace();
		}
	}
	
	public void m_Load(String filename) //载入文件
	{
		try
		{
			DataInputStream dfin = new DataInputStream(new FileInputStream (filename));
			boolean ReadError = false;
			int n1;
			
			n1 = dfin.readInt();
			if (n1<0 || n1>=LINE_NUM*LINE_NUM) ReadError = true;
			int[] x1 = new int[LINE_NUM*LINE_NUM];
			int[] y1 = new int[LINE_NUM*LINE_NUM];
			
			if (!ReadError) {
				for (int i=0; i<=n1; i++) {
					x1[i] = dfin.readInt();
					if (x1[i]<0 || x1[i]>=LINE_NUM) { ReadError = true; break; }
					y1[i] = dfin.readInt();
					if (y1[i]<0 || y1[i]>=LINE_NUM) { ReadError = true; break; }
				}
			}
			dfin.close();
			if (ReadError) {
				mb_displayAppend("文件读取出错。"); 
				PopContent = "该文件不是合法的棋局文件或已损坏!";
				MessageExist = true;
			} else {
				n = n1;
				for (int i=0;i<=n;i++) { x[i] = x1[i];  y[i] = y1[i]; }
				mb_displayAppend("棋局读取成功!");
				GameLoaded = true;
			}
		}
		catch (Exception ee)
		{
			mb_displayAppend("文件读取出错。"); 
			PopContent = "该文件不是合法的棋局文件或已损坏!";
			MessageExist = true;
			System.out.println("出现异常:"+ee);
			ee.printStackTrace();
		}
	}
	
	public void m_Save() //保存文件
	{
		try
		{
			String filename;
			if (!readyToPlay) {
				PopContent = new String("当前没有棋局!");
				MessageExist = true;
				return;
			} else {
				fsave.setVisible(true);
			    filename=fsave.getFile();
			}
			
			DataOutputStream dfout = new DataOutputStream(new FileOutputStream (filename));
			dfout.writeInt(n);
			for (int i=0; i<=n; i++) {
				dfout.writeInt(x[i]);
				dfout.writeInt(y[i]);
			}
			dfout.close();
			mb_displayAppend("成功保存棋局到"+filename+"。"); 
		}
		catch (Exception ee)
		{
			System.out.println("出现异常:"+ee);
			ee.printStackTrace();
		}
	}
	
	public void m_Replay() //复盘
	{
		try
		{
			if (isConnected) {
				m_output.writeObject(REPLAYING_GAME);
			}
			mb_displayAppend("您进入了复盘模式。");
			
			m_board.updateBoard(x,y,-1);
			
			int i;
			
			for (i=0; i<=buttonNum; i++) p[2].remove(b[i]);
			buttonNum = -1;
			b[0] = new JButton("到开始");
			b[1] = new JButton("上一步");
			b[2] = new JButton("下一步");
			b[3] = new JButton("到末尾");
			b[4] = new JButton("退出复盘");
			
			final JLabel ShowStep = new JLabel("第0步,共"+(n+1)+"步");
			
			p[2].add(ShowStep);
			for (i=0; i<5; i++) p[2].add(b[i]);
			
			
			final int[] CurrentStep = { -1 };
			StopReplay = false;
			
			b[0].addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent e)
					{
						CurrentStep[0] = -1;
						ShowStep.setText("第0步,共"+(n+1)+"步");
						m_board.updateBoard(x,y,CurrentStep[0]);
					}
				}
			);
			b[1].addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent e)
					{
						if (CurrentStep[0]>=0) {
							CurrentStep[0]--;
							ShowStep.setText("第"+(CurrentStep[0]+1)+"步,共"+(n+1)+"步");
							m_board.updateBoard(x,y,CurrentStep[0]);
						}
					}
				}
			);
			b[2].addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent e)
					{
						if (CurrentStep[0]<n) {
							CurrentStep[0]++;
							String str = "第"+(CurrentStep[0]+1)+"步,共"+(n+1)+"步";
							m_board.updateBoard(x,y,CurrentStep[0]);
							if (CurrentStep[0]==n) {
								if (m_board.isWinner(1)) str += "。黑方获胜!"; else
									if (m_board.isWinner(2)) str += "。白方获胜!";
							}
							ShowStep.setText(str);
						}
					}
				}
			);
			b[3].addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent e)
					{
						CurrentStep[0] = n;
						String str = "第"+(n+1)+"步,共"+(n+1)+"步";
						m_board.updateBoard(x,y,n);
						if (m_board.isWinner(1)) str += "。黑方获胜!"; else
							if (m_board.isWinner(2)) str += "。白方获胜!";
						ShowStep.setText(str);
					}
				}
			);
			b[4].addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent e)
					{
						StopReplay = true;
					}
				}
			);
									
			p[2].updateUI();
			
			while (!StopReplay)
				;
			
			p[2].remove(ShowStep);
			for (i=0; i<5; i++) 	{
				p[2].remove(b[i]);
			}
			p[2].updateUI();
			n = -1;
			m_board.updateBoard(x,y,-1);
			GameLoaded = false;
			mb_displayAppend("您退出了复盘模式。");
			if (isConnected) m_output.writeObject(EXIT_REPLAY);
		}
		catch (Exception ee)
		{
			System.out.println("出现异常:"+ee);
			ee.printStackTrace();
		}
	}
	
	public void mb_displayAppend(String s) //更新信息框
	{
		m_display.append(s+"\n");
		m_display.setCaretPosition(m_display.getText().length());
		m_enter.requestFocusInWindow();
	}
	
	public void mb_run() //主要运行过程,专为网络模式
	{
		
		
		new Pop();
		
		while (true) {
			while (true) {
				isConnected = false;
				
				while (!isConnected)
					;
				//等待连接
				m_board.setSingle(false);
				
				J_FIR_Name jfn = new J_FIR_Name();
				jfn.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				jfn.setSize(370,200);
				jfn.setLocation(420,420);
				jfn.setVisible(true);
				
				while (jfn.u_Choice[0]==-1)
					;
					
				if (jfn.u_Choice[0]==1) {
					myName = jfn.u_Name[0];
					jfn.dispose();
					break;
				} else 
				if (jfn.u_Choice[0]==-2) {
					isConnected = false;
					jfn.dispose();
				}
				
			} //以上输入用户名
					
			try
			{
				
				while (isConnected)
				{
					mb_displayAppend("尝试连接");
					Socket s = new Socket(host,port);
					String m;
					StringBuffer mBuf;
					int i;
					
					m_output = new ObjectOutputStream(s.getOutputStream());
					m_input = new ObjectInputStream(s.getInputStream());
					
					while (true) {
						m = (String) m_input.readObject();
						if (m.equals(WELCOME_INFO)) {
							mb_displayAppend("连接成功!");
							mb_displayAppend("系统消息:欢迎您来到五子棋的世界!");
							break;
						}
					}
					
					Entered[0] = false;
					Invited = false;
					
					new MessageThread();
					
					m_output.writeObject(NAME_COMMAND+myName);
					
					LookingForOnliners = true;
					
					while (isConnected) {
						while (LookingForOnliners) {
							m_output.writeObject(FIND_REQUEST);
							mb_displayAppend("正在查询在线用户...");
							
							Entered[0] = false;
							Invited = false;
		
							while (!Entered[0] && !Invited && isConnected)  
								;
							
							if (Invited) {
								mBuf = new StringBuffer(m_Message);
							
								if (mBuf.indexOf(PLAY_REQUEST)==0) {
									mBuf.delete(0,PLAY_REQUEST.length());
									m = mBuf.toString();
									ShowInvitation(m,"向您发出了下棋的邀请。");
								}  else
								if (mBuf.indexOf(CONTINUE_REQUEST)==0) {
									mBuf.delete(0,CONTINUE_REQUEST.length());
									m = mBuf.toString();
									ShowInvitation(m,"想和您接着下保存着的一盘棋");
								}
								
							} else						
							
							if (Entered[0]) {
								InvitationAccepted = false;
								
								if (GameLoaded) {
									PopContent = "您当前有打开的棋局,邀请对方接着下吗?";
									ChoiceExist = true;
								} else {
									m_output.writeObject(OPPONENT_COMMAND+Opponent[0]);
								}
								mb_displayAppend("您已向"+Opponent[0]+"发出了邀请,请等待回应。");
								RequestReplied = false;
								while (!RequestReplied)
									;
								if (InvitationAccepted) readyToPlay = true;
							}
							
							if (readyToPlay) LookingForOnliners = false;
							
						}
						
						if (readyToPlay) {
							
							if (GameLoaded) {
								mb_displayAppend("正在传送对局数据,请稍候...");
								m_output.writeObject(STEP_NUM+n);
								for (int j = 0; j<=n; j++) m_output.writeObject(STEP_CONTENT+Integer.toString(x[j]*LINE_NUM+y[j]));
								mb_displayAppend("数据传输完毕。");
								GameLoaded = false;
								

⌨️ 快捷键说明

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