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

📄 tictactoe.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/************************************************************************
 *
 * $Id: TicTacToe.java,v 1.10 2002/04/12 22:20:06 akhil Exp $
 *
 * Copyright (c) 2001 Sun Microsystems, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *       Sun Microsystems, Inc. for Project JXTA."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
 *    must not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact Project JXTA at http://www.jxta.org.
 *
 * 5. Products derived from this software may not be called "JXTA",
 *    nor may "JXTA" appear in their name, without prior written
 *    permission of Sun.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL SUN MICROSYSTEMS OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of Project JXTA.  For more
 * information on Project JXTA, please see
 * <http://www.jxta.org/>.
 *
 * This license is based on the BSD license adopted by the Apache
 * Foundation.
 **********************************************************************/

/*
 * @(#)TicTacToe.java	1.4 98/06/29
 *
 * Copyright (c) 1997, 1998 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license
 * to use, modify and redistribute this software in source and binary
 * code form, provided that i) this copyright notice and license
 * appear on all copies of the software; and ii) Licensee does not
 * utilize the software in a manner which is disparaging to Sun.
 *
 * This software is provided "AS IS," without a warranty of any
 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
 * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line
 * control of aircraft, air traffic, aircraft navigation or aircraft
 * communications; or in the design, construction, operation or
 * maintenance of any nuclear facility. Licensee represents and
 * warrants that it will not use or redistribute the Software for such
 * purposes.
 */

package net.jxta.midp.demo.tictactoe;

import java.io.IOException;
import java.util.Random;

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;

import net.jxta.j2me.PeerNetwork;
import net.jxta.j2me.Message;
import net.jxta.j2me.Element;

/**
 * A TicTacToe MIDlet. A very simple, and mostly brain-dead
 * implementation of your favorite game! <p>
 *
 * In this game a position is represented by a bitmask. A bit is set
 * if a position is ocupied. There are 9 squares so there are 1<<9
 * possible positions for each side. An array of 1<<9 booleans is
 * created, it marks all the winning positions.
 *
 * @version 	1.2, 13 Oct 1995
 * @author Arthur van Hoff
 * @modified 04/23/96 Jim Hagen : winning sounds
 * @modified 02/10/98 Mike McCloskey : added destroy()
 * @modified 11/07/01 Akhil Arora : converted to a MIDlet
 */

public final class TicTacToe extends MIDlet 
    implements CommandListener, Runnable {

    private static final int ALERT_TIMEOUT = 5;
    private static final String PIPE_NAME = "JxtaTicTacToe";
    private static final String TITLE_NAME = "JxtaTicTacToe";
    private static final String ELEMENT_NAME = "JxtaTicTacToe";

    private static final String CMD_EXIT = "Exit";
    private static final String CMD_RESET = "Reset Game";
    private static final String CMD_SETTINGS = "Settings";
    private static final String CMD_CONNECT = "Connect";

    private static final int STATE_NOT_CONNECTED = 0;
    private static final int STATE_WAITING_FOR_OPPONENT = 1;
    private static final int STATE_PLAYING = 2;
    private static final int STATE_OBSERVING = 3;
    private static final int STATE_GAME_OVER = 4;
    private static final int STATE_WIN = 5;
    private static final int STATE_LOSE = 6;
    private static final int STATE_STALEMATE = 7;
    private static final int STATE_INVALID = 8;

    /**
     * The Display object for the MIDlet.
     */
    private static Display display = null;

    private Config config = null;
    private byte[] state = new byte[0];
    private PeerNetwork peerNet = null;

    /**
     * The Board canvas.
     */
    private BoardCanvas canvas = null;

    /**
     * The Message displayed on the canvas.
     */
    String message = null;

    /**
     * Computer's current position. 
     */
    private int peer = 0;

    /**
     * Your current position. 
     */
    private int you = 0;

    /**
     * The squares in order of importance...
     */
    private static final int moves[] = {4, 0, 2, 6, 8, 1, 3, 5, 7};

    /**
     * The winning positions.
     */
    private static boolean won[] = new boolean[1 << 9];
    private static final int DONE = (1 << 9) - 1;
    private static final int OK = 0;
    private static final int WIN = 1;
    private static final int LOSE = 2;
    private static final int STALEMATE = 3;


    /** 
     * For debuging and quantifying
     */
    private static final boolean DEBUG = true;
    private static final boolean QUANTIFY = true;

    private boolean stopPolling = false;
    private int gameState = STATE_NOT_CONNECTED;
    private String opponent = null;

    /**
     * Mark all positions with these bits set as winning.
     */
    private static void isWon(int pos) {
	for (int i = 0 ; i < DONE ; i++) {
	    if ((i & pos) == pos) {
		won[i] = true;
	    }
	}
    }

    /**
     * Initialize all winning positions.
     */
    static {
	isWon((1 << 0) | (1 << 1) | (1 << 2));
	isWon((1 << 3) | (1 << 4) | (1 << 5));
	isWon((1 << 6) | (1 << 7) | (1 << 8));
	isWon((1 << 0) | (1 << 3) | (1 << 6));
	isWon((1 << 1) | (1 << 4) | (1 << 7));
	isWon((1 << 2) | (1 << 5) | (1 << 8));
	isWon((1 << 0) | (1 << 4) | (1 << 8));
	isWon((1 << 2) | (1 << 4) | (1 << 6));
    }
    private static final String PIPE_ID = "urn:jxta:uuid-" + 
        "59616261646162614E50472050325033" + 
        "AFAFAFAFAFAFAFAFAFAFAFAFAFAFAFA104";


    public TicTacToe() {
    }

    public void startApp() {
        display = Display.getDisplay(this);

	if (canvas == null) {
	    canvas = new BoardCanvas(this);
	    canvas.addCommand(new Command(CMD_EXIT, Command.STOP, 1));
	    canvas.addCommand(new Command(CMD_CONNECT, Command.SCREEN, 2));
	    canvas.addCommand(new Command(CMD_RESET, Command.SCREEN, 3));
	    canvas.addCommand(new Command(CMD_SETTINGS, Command.SCREEN, 4));
	    canvas.setCommandListener(this);
	}

	if (config == null) {
	    config = new Config(this, display, canvas);
	}
	
	// startup in the config screen if the identity is not configured
	if ("".equals(config.getIdentity())) {
	    display.setCurrent(config);
	} else {
	    display.setCurrent(canvas);
	}
        stopPolling = false;
    }
    
    public void pauseApp() {
        stopPolling = true;
    }

    public void destroyApp(boolean unconditional) {
        stopPolling = true;
    }

    public void commandAction(Command c, Displayable displayable) {
        if (c.getCommandType() == Command.STOP) {
            destroyApp(true);
            notifyDestroyed();
            return;
        } 

        Displayable next = canvas;
        String label = c.getLabel();
        if (label.equals(CMD_SETTINGS)) {
            next = config;
        } else if (label.equals(CMD_RESET)) {
            canvas.reset();
        } else if (label.equals(CMD_CONNECT)) {
            connect();
        } 

        display.setCurrent(next);
    }


    /**
     * User move.
     * @return true if legal
     */
    private boolean yourMove(int m) {
	if ((m < 0) || (m > 8)) {
	    return false;
	}
	if (((you | peer) & (1 << m)) != 0) {
	    return false;
	}
	you |= 1 << m;
	return true;
    }

    /**
     * Figure what the status of the game is.
     */
    private int status() {
	if (won[peer]) {
	    return WIN;
	}
	if (won[you]) {
	    return LOSE;
	}
	if ((you | peer) == DONE) {
	    return STALEMATE;
	}
	return OK;
    }

    /**
     * Who goes first in the next game?
     */
    private boolean first = true;

    /**
     * Whose move is it?
     */
    private boolean myMove = false;

    private static class BoardCanvas extends Canvas
    {
        /**
         * The image for peer.
         */
        Image notImage;
        
        /**
         * The image for you.
         */
        Image crossImage;

        TicTacToe midlet;
        boolean alerted = false;
        int width;
        int fullheight;
        int height;
        int lineHeight;

        BoardCanvas(TicTacToe midlet) {
            this.midlet = midlet;

            width = getWidth();
            fullheight = getHeight();

            lineHeight = Font.getDefaultFont().getHeight();
            // keep space for the status line
            height = fullheight - lineHeight - 2;

            try {
                notImage = Image.createImage("/not.png");
                crossImage = Image.createImage("/cross.png");

⌨️ 快捷键说明

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