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

📄 fruitmachine.java

📁 J2ME MIDP_Example_Applications
💻 JAVA
字号:
// Copyright 2002 Nokia Corporation. // // THIS SOURCE CODE IS PROVIDED 'AS IS', WITH NO WARRANTIES WHATSOEVER, // EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY, FITNESS // FOR ANY PARTICULAR PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE // OR TRADE PRACTICE, RELATING TO THE SOURCE CODE OR ANY WARRANTY OTHERWISE // ARISING OUT OF ANY PROPOSAL, SPECIFICATION, OR SAMPLE AND WITH NO // OBLIGATION OF NOKIA TO PROVIDE THE LICENSEE WITH ANY MAINTENANCE OR // SUPPORT. FURTHERMORE, NOKIA MAKES NO WARRANTY THAT EXERCISE OF THE // RIGHTS GRANTED HEREUNDER DOES NOT INFRINGE OR MAY NOT CAUSE INFRINGEMENT // OF ANY PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OWNED OR CONTROLLED // BY THIRD PARTIES // // Furthermore, information provided in this source code is preliminary, // and may be changed substantially prior to final release. Nokia Corporation // retains the right to make changes to this source code at // any time, without notice. This source code is provided for informational // purposes only. // // Nokia and Nokia Connecting People are registered trademarks of Nokia// Corporation.// Java and all Java-based marks are trademarks or registered trademarks of// Sun Microsystems, Inc.// Other product and company names mentioned herein may be trademarks or// trade names of their respective owners.// // A non-exclusive, non-transferable, worldwide, limited license is hereby // granted to the Licensee to download, print, reproduce and modify the // source code. The licensee has the right to market, sell, distribute and // make available the source code in original or modified form only when // incorporated into the programs developed by the Licensee. No other // license, express or implied, by estoppel or otherwise, to any other // intellectual property rights is granted herein.package example.fruitmachine;import java.util.*;class FruitMachine {    static class InvalidPlayException        extends Exception    {        InvalidPlayException(String message)        {            super(message);        }    }    static class SpinResult    {        private int wheel[] = new int[3];        private boolean win;        private int winAmount;                SpinResult(int[] wheel, boolean win, int winAmount)        {            this.wheel[0] = wheel[0];            this.wheel[1] = wheel[1];            this.wheel[2] = wheel[2];            this.win = win;            this.winAmount = winAmount;        }                int getWheel(int i)        {            return wheel[i];        }                boolean isWin()        {            return win;        }                int getWinAmount()        {            return winAmount;        }    }    private static final int BAR = 0;    private static final int BELL = 1;    private static final int ORANGE = 2;    private static final int LEMON = 3;    private static final int PLUM = 4;    private static final int CHERRY = 5;    private static String names[] =    {        "bar", "bell", "orange", "lemon", "plum", "cherry"    };    private static final int wheelValues[] =    {        BAR, ORANGE, LEMON, PLUM, CHERRY,        BELL, ORANGE, LEMON, PLUM, CHERRY,        BELL, ORANGE, LEMON, PLUM, CHERRY,        BELL, ORANGE, LEMON, PLUM, CHERRY    };        private int[] lastWheel = new int[3];    private boolean[] lastHold = new boolean[3];    private int lastBet = 0;    private boolean lastWin = false;    private Random random = new Random();    private static final int HOUSE_LIMIT = 100;    // These next values were found by experiment to allow the smartest    // player to win back on average 96% of what they bet :-)    private static final int THREE_BAR_WIN = 30;    private static final int THREE_BELL_WIN = 10;    private static final int THREE_OTHER_WIN = 5;    private User user;   // so we can charge and credit their account    FruitMachine(User user)    {        this.user = user;    }    // There's a subtle constraint here: in the first spin, we will never    // allow a hold, since we start with no holds and lastBet=0. They must    // increase the bet to at least 1, and if they increase the bet they    // can't add a hold. Therefore, we don't have to initialize the    // lastWheel values before the first spin - they can never be used.    // This method is synchronized to protect us from strange effects if    // a hacker tries to run several clients with the same session ID    synchronized SpinResult spin(int bet,                                 boolean hold0,                                 boolean hold1,                                 boolean hold2)        throws InvalidPlayException    {        // you can't have a negative or zero bet        if (bet <= 0)        {            throw new InvalidPlayException(                "You must bet at least 1 credit");        }        // you can't bet more than HOUSE_LIMIT        if (bet > HOUSE_LIMIT)        {            throw new InvalidPlayException(                "House limit is " + HOUSE_LIMIT + " credits");        }        // if you add a hold, you can't increase your bet (the client        // should not allow this, but we don't trust the client :-)        if (((hold0 && !lastHold[0]) ||             (hold1 && !lastHold[1]) ||             (hold2 && !lastHold[2])) && (bet > lastBet))        {            throw new InvalidPlayException(                "If you hold more wheels, you can't increase your bet");        }        // you can't hold any part of a winning set of wheels (the client        // should not allow this, but we don't trust the client :-)        if (lastWin && (hold0 || hold1 || hold2))        {            throw new InvalidPlayException(                "You can't hold from a win");        }        //*** START REALLY-SHOULD-BE-TRANSACTION        // charge their account for the bet; complain if there wasn't        // enough credit        if (!user.deductCredit(bet))        {            throw new InvalidPlayException(                "Not enough credit for this bet");        }        lastBet = bet;        lastHold[0] = hold0;        lastHold[1] = hold1;        lastHold[2] = hold2;        for (int i = 0; i < lastHold.length; ++i)        {            if (!lastHold[i])            {                lastWheel[i] = random.nextInt(wheelValues.length);            }        }        // check for a win        lastWin = false;        int winAmount = 0;        if ((wheelValues[lastWheel[0]] == BAR) &&            (wheelValues[lastWheel[1]] == BAR) &&            (wheelValues[lastWheel[2]] == BAR))        {            lastWin = true;            winAmount = THREE_BAR_WIN * lastBet;        }        else if ((wheelValues[lastWheel[0]] == BELL) &&                 (wheelValues[lastWheel[1]] == BELL) &&                 (wheelValues[lastWheel[2]] == BELL))        {            lastWin = true;            winAmount = THREE_BELL_WIN * lastBet;        }        else if ((wheelValues[lastWheel[0]] == wheelValues[lastWheel[1]]) &&                 (wheelValues[lastWheel[1]] == wheelValues[lastWheel[2]]))        {            lastWin = true;            winAmount = THREE_OTHER_WIN * lastBet;        }        user.addCredit(winAmount);        //*** END REALLY-SHOULD-BE-TRANSACTION        return new SpinResult(lastWheel, lastWin, winAmount);    }}

⌨️ 快捷键说明

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