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

📄 ring.java

📁 手机无线网络纸牌游戏源代码。非常适合学习使用
💻 JAVA
字号:
// Ring.java
//
// Copyright (c) 2000-2001 Symbian Ltd. All rights reserved

package com.symbian.devnet.whist.tokenRing;

import javax.net.datagram.*;

/** 
 * Used to manage the nodes in the current token ring network.
 * @author Symbian Devnet
 */ 
class Ring
{
	/** Number of nodes supported */
	private final int NO_OF_NODES = 4;
	/** Array of names for each node */
	private String[] names = new String[NO_OF_NODES];
	/** Array of addresses of each node */
	private Address[] aAddresses = new Address[NO_OF_NODES];
	
	/**
	 * Constructor with specified addresses
	 * @param n Array of names for all nodes
	 * @param add Array of addresses for all nodes
	 */
	Ring(String[] n, String[] add)
	{
		for (int i = 0; i < NO_OF_NODES; i++)
		{
			names[i] = n[i];
			try
			{
				aAddresses[i] = DatagramService.parseAddress(add[i]);
			}
			catch(Exception e)
			{
				System.out.println("Server address could not be parsed." + e.getMessage());
				System.exit(0);			 
			}
		}
	}
			
	/**
	 * Constructor taking setup data
	 * @param details Setup data
	 */		
	Ring(char[] details)
	{
		int ct = setUpAddresses(details);
		setUpNames(ct, details);
	}
	
	/**
	 * Extract addresses from setup data
	 * @param details Setup data
	 * @return Number of addresses
	 */	
	private int setUpAddresses(char[] details)
	{
		char[] c = null;
		int a = 0;
		int count = 0;
		String trimmed = null;
		for (int i = 0; i < NO_OF_NODES; i++)
		{
			int j = 0; 
			c = new char[30];
			while (details[count] != '?')			
				c[j++] = details[count++];
			trimmed = new String(c).trim();
			try
			{
				aAddresses[a] = DatagramService.parseAddress(trimmed);
			}
			catch(Exception e)
			{
				System.out.println("Server address could not be parsed." + e.getMessage());
				System.exit(0);			 
			}
			a++;
			count++; // required to skip '?'
		}
		return count;
	}
		
	/**
	 * Extract addresses from setup data
	 * @param details Setup data
	 */		
	private void setUpNames	(int count, char[] details)
	{
		int ct = count;
		int a = 0;
		char[] c = new char[10];
		String trimmed = null;
		for (int i = 0; i < NO_OF_NODES; i++)
		{
			int j = 0;
			c = new char[10];
			while (details[ct] != '?')
				c[j++] = details[ct++];
			trimmed = new String(c).trim();
			names[a++] = trimmed;
			ct++; // required to skip '?'		
		}
	}	
	
	/**
	 * Get the name of the specified node
	 * @param i Node 
	 * @return Name of node
	 */		
	public String getName(int i)
	{
		String s = new String(names[i]);
		return s;
	}
	
	/**
	 * Get an array of the names of all nodes
	 * @return Array of names of all nodes
	 */		
	public String[] getAllNames()
	{
		String[] s = new String[NO_OF_NODES];
		for (int i = 0; i < NO_OF_NODES; i++)
			s[i] = 	names[i];
		return s;
	}
	
	/**
	 * Get the address of the specified node 	
	 * @param i Node
	 * @return Address of node
	 */
	public Address getAddressAddress(int i)
	{
		Address a = aAddresses[i];
		return a;
	}
		
	/**
	 * Get an array of the addresses of all nodes
	 * @return Array of addresses of all nodes
	 */	
	public Address[] getAllAddressAddresses()
	{
		Address[] a = new Address[NO_OF_NODES];
		for (int i = 0; i < NO_OF_NODES; i++)
			a[i] = aAddresses[i];
		return a;
	}	
	
	/** 
	 * Get the name of the player corresponding to the array position passed in
	 * as a paramater.
	 * @param no	the position in the array of the player name which is to 
	 * be returned.
	 */
	public String getPlayerName(int no)
	{
		return names[no];
	}
}

⌨️ 快捷键说明

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