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

📄 whist.java

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

package com.symbian.devnet.whist.awt;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.net.datagram.*;
import com.symbian.devnet.quartz.awt.*;

/** 
 * This class controls the initialisation of the game and the respective
 * views.
 * @author Symbian Devnet
 */
public class Whist extends QFrame
{
	/** The variable used to determine when UDP datagrams are required. */
	private static final int UDP = 0;
	/** The variable used to determine when WDPSMS datagrams are required. */
	private static final int WDPSMS = 1;
	/** A <code>MenuItem</code> to allow the user to set us a game using WDPSMS */
	public final static MenuItem mWDPSMS = new MenuItem("WDPSMS");
	/** A <code>MenuItem</code> to allow the user to set us a game in demo mode */
	public final static MenuItem mDemo = new MenuItem("Demo");
	/** A <code>MenuItem</code> to allow the user to view the current scores */
	public final static MenuItem score = new MenuItem("Score");
	/** A <code>MenuItem</code> to allow the user to end the game */
	public final static MenuItem end = new MenuItem("End Game");
	/** A <code>String</code> representation of the IP address of this device */ 
	public String myUDPAddress;
	/** A <code>String</code> representation of the SMS address of this device */ 
	public String mySMSAddress = "+44123456789";
	/** The port which the listener is listening on */
	public String port;
	/** The addresses of the three players joining the game */
	public static String[] addresses = new String[3];
	/** 
	 * A reference determining if this is demo is running on one machine only
	 * (for demo mode only) 
	 */
	public static boolean singleMachine = true;
	/** A representation of the sending protocol in use */
	private int protocol;
	/** A temporary UDP address used to determine the IP number of the machine. */	
	private final static String DUMMY_ADDRESS_UDP = "udp://127.1.2.3:9999";
	/** A temporary WDPSMS address used to determine the IP number of the machine. */
	private final static String DUMMY_ADDRESS_WDPSMS = "wdpsms://+123456789:9123"; 
	/** An instance of a DatagramService. */
	private DatagramService dummy;
	/** The Base View for SMS */
	private SMSBaseView smsView;
	/** The Base View for Demo mode */
	private DemoBaseView demoView; 
	/** The Waiting View */
	private WaitingView wait;
	/** An instance of a GUInterface */
	private GUInterface gui;
	
	/** 
	 * Constructor which displays the <code>SplashScreen</code>, adds the 
	 * relevant menu items (and adds ActionListeners to them), starts the game,
	 * and then undisplays <code>SplashScreen</code>.
	 */
	public Whist()
	{
		super(4);
		SplashScreen splosh = new SplashScreen(this);	
		splosh.display();		
		
		createAboutDialog(new String[] {"Whist v1.1", "By Symbian DevNet", "14/12/2000", "Copyright Symbian DevNet"});
		port = "1010";
		
		gui = new GUInterface(getCardAt(2), this);
		protocol = UDP;
		myUDPAddress = getMyAddress();		

		smsView = new SMSBaseView(getCardAt(0), this);
		demoView = new DemoBaseView(getCardAt(1), this);		
		wait = new WaitingView(getCardAt(3));

		editMenu.add(mWDPSMS);
		editMenu.add(mDemo);
		setVisible(true);
		
		gui.runGame();
	
		mWDPSMS.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent ae)
			{
				showSMSBaseView();
			}
		});
		
		mDemo.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent ae)
			{
				showDemoBaseView();
			}
		});
		
	
		splosh.unDisplay();
		
	}
	
	/** 
	 * Displays the SMS Base View, ensuring that all previous details which
	 * may be present have been removed.
	 */
	public void showSMSBaseView()
	{
		displayCardAt(0);
		editMenu.removeAll();
		menuBar.add(editMenu);
		editMenu.add(mWDPSMS);
		editMenu.add(mDemo);
	}
	
	/**
	 * Displays the Demo Base View, ensuring that all previous details which
	 * may be present have been removed.
	 */
	public void showDemoBaseView()
	{
		displayCardAt(1);
		editMenu.removeAll();
		menuBar.add(editMenu);
		editMenu.add(mWDPSMS);
		editMenu.add(mDemo);
	}
	
	/** 
	 * Displays the GUInterface view.  This is where most of the game action 
	 * is displayed.
	 */
	public void showGUI()
	{
		displayCardAt(2);
		editMenu.removeAll();
		appMenu.add(score);
		appMenu.add(end);
	}
		
	/**
	 * Utility to get address of this device
	 * @return String containing address of this node
	 */	
	private String getMyAddress()
	{
		Address nodeAddress = null;		
		if (protocol == UDP)
		{		
			try
			{			
				nodeAddress = DatagramService.parseAddress(DUMMY_ADDRESS_UDP);				
			}
			catch(Exception ex)
			{
				System.out.println("Server address could not be parsed.");
				System.exit(0);
			}				
			port = gui.getPort();
		}
		
		else // protocol == WDPSMS
		{		
			try
			{			
				nodeAddress = DatagramService.parseAddress(DUMMY_ADDRESS_WDPSMS);			
			}
			catch(Exception ex)
			{
				System.out.println("Server address could not be parsed.");
				System.exit(0);
			}
		}
		try
		{
			Thread.sleep(10000);
			dummy = DatagramService.getService(nodeAddress);
		}
		catch (Exception e)
		{
			System.out.println("Could not get service: " + e.getMessage());
			System.exit(0);
		}
		try
		{	
			nodeAddress = dummy.getAddress();		
		}
		catch (Exception e)
		{
			System.out.println("Address could not be parsed: " + e.getMessage());
			System.exit(0);
		}
		
		int noChars;
		if (protocol == UDP)
			noChars = 6;
		else
			noChars = 9;
			
		char[] add = nodeAddress.toString().toCharArray();
		char[] tele = new char[(add.length - noChars - 5)];
		int count = 0;
		int i = noChars;
		while (add[i] !=':') //(i < add.length)
			tele[(count++)] = add[(i++)];
		System.out.println("port no set to: " + port);	
	
		String t = new String(tele);
		try
		{
			dummy.close();
		}
		catch (IOException ioe)
		{
			System.out.println("Could not close service: " + ioe.getMessage());
			System.exit(0);
		}	
		return t;
	}
	
	/** 
	 * Sets up the Game to work with SMS instead of UDP.
	 */
	public void setUpSMS()
	{
		gui.setUpSMS();
	}
	
	/**
	 * Displays the <code>WaitingView</code> and starts the game.
	 */
	public void startGame(String[] names, String[] addresses)
	{
		displayCardAt(3);
		editMenu.removeAll();
		appMenu.add(score);
		gui.startGame(names, addresses);
	}
	
	/** 
	 * Main method which makess the <code>Thread</code> sleep for
	 * 15000ms to give the emulator time to load when run from
	 * a .exe file directly and not started from the AppLauncher.
	 */
	public static void main(String args[]) 
	{
		if (args.length > 0)
		{
			String s = args[args.length-1];					
			if (s.equals("wait"))
			{
				try
				{
					Thread.sleep(15000);
				}	
				catch (InterruptedException ie)
				{}
			}	
			if (args.length == 4)
			{
				singleMachine = false;
				for (int i = 0; i < 3; i++)
					addresses[i] = args[i];			
			}				
		}
		new Whist();
	}
}

⌨️ 快捷键说明

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