Package xunome.game

Source Code of xunome.game.BaseGame

package xunome.game;

/**
*
* @author Sem iNick
*/

import java.util.Random;
import java.util.Vector;
import xunome.xUnoME;
import xunome.graphics.GraphicUNO;
import xunome.graphics.GraphicPlayer;
import xunome.graphics.EndGame;

/*
* Classe que liga a parte lógica à parte gráfica e faz as ações do jogo
*/
public abstract class BaseGame {

    private xUnoME mid;
    private GraphicUNO gUNO;
    private Player[] players;
    private Table table;
    private int atualPlayer = 0, atualColor;
    private int direction = 1;
    public static final int CLOCKWISE = 1;
    public static final int ANTICLOCKWISE = -1;

    private final int colors[] = {
                                    0x35B3B3,
                                    0x00FF00,
                                    0xFF0000,
                                    0xECF00C
                                };
    private boolean chooseColor = false;
    private boolean callUno = false;
    private boolean playing = false;
    private boolean waited1 = true;
    private final int localPlayerPos;

    public BaseGame(Vector deck, int nPlayers, int pos, xUnoME mid) {

        if ((nPlayers <= 1) || (nPlayers > 4)) {

            throw new IllegalArgumentException("Number of players must be more "
                                               + "than 1 and less than 5");
        }

        if (deck.size() != 108) {

            throw new IllegalArgumentException("Deck length is invalid.");
        }

        if ((pos < 0) || (pos > nPlayers - 1)) {

            throw new IllegalArgumentException("Invalid player pos at table.");
        }

        this.mid = mid;
        localPlayerPos = pos;

        //Create deck of cards
        table = new Table(deck);

        //Create players
        players = new Player[nPlayers];
        players[pos] = new Player(pos, GraphicPlayer.DOWN, true);

        int upPlayer = -1;
        int leftPlayer = -1;
        int rightPlayer = -1;

        if (nPlayers == 2) {

            upPlayer = (pos == 0)?1:0;
        } else {

            int[][] playersP = {
                            {1, 2, 3}, //Left , up and right player for player 0
                            {2, 3, 0},
                            {3, 0, 1},
                            {0, 1, 2}
                          };

            leftPlayer = playersP[pos][0];
            upPlayer = playersP[pos][1];
            rightPlayer = playersP[pos][2];
        }

        //Create deck for each player
        for (int p = 0; p < players.length; p++) {

            if (p != pos) {

                if (p == upPlayer) {

                    players[p] = new Player(p, GraphicPlayer.UP, false);
                } else if (p == leftPlayer) {

                    players[p] = new Player(p, GraphicPlayer.LEFT, false);
                } else if (p == rightPlayer) {

                    players[p] = new Player(p, GraphicPlayer.RIGHT, false);
                }
            }
        }

        //Select a random player
        atualPlayer = new Random().nextInt(nPlayers);

        gUNO = new GraphicUNO(this);
   }

    /*
     * Método que retorna todos os player em uma Array
     */
    public final Player[] getAllPlayers() {

        return players;
    }

    /*
     * Método que informa a cor corrente no jogo em RGB
     */
    public final int getAtualColorRGB() {
       
        return colors[getAtualColor()];
    }

    /*
     * Método que retorna a cor corrente no jogo
     */
    public final int getAtualColor() {
       
        return atualColor;
    }

    /*
     * Retorna o player que atual na jogada
     */
    public final Player getAtualPlayer() {
       
        return players[atualPlayer];
    }

    /*
     * Retorna o player dada uma posição
     */
    public final Player getPlayer(int index) {
       
        return players[index];
    }


    public final Table getTable() {
       
        return table;
    }

    /*
     * Método que retorna a melhor cor para o computador jogar baseado no
     * número de carta de cada cor
     */
    public int getBestColor(Player p) {

        int yellowTot = 0;
        int blueTot = 0;
        int redTot = 0;
        int greenTot = 0;

        for (int atualCard = 0; atualCard < p.getNumberOfCards(); atualCard++) {

            switch (p.getCard(atualCard).getColor()) {

                case Card.YELLOW:

                    yellowTot++;
                    break;

                case Card.RED:

                    redTot++;
                    break;

                case Card.GREEN:

                    greenTot++;
                    break;

                case Card.BLUE:

                    blueTot++;
                    break;
            }
        }

        int bestColor = Card.YELLOW;
        int atualMaxValue = yellowTot;

        if (atualMaxValue < redTot) {

            bestColor = Card.RED;
            atualMaxValue = redTot;
        }

        if (atualMaxValue < greenTot) {

            bestColor = Card.GREEN;
            atualMaxValue = greenTot;
        }

        if (atualMaxValue < blueTot) {

            bestColor = Card.BLUE;
        }

        return bestColor;
    }

    /*
     * Método que seta a nova cor atual do jogo
     */
    public final void setColor(int nc) {
       
           atualColor = nc;
    }

    /*
     * Método que cria o deck de cartas
     */
    public static final Vector createDeck() {

        Vector dDeck = new Vector(108);
       
        Card[] nBlue ={
                                new Card(Card.BLUE, Card.NORMAL, 1),
                                new Card(Card.BLUE, Card.NORMAL, 2),
                                new Card(Card.BLUE, Card.NORMAL, 3),
                                new Card(Card.BLUE, Card.NORMAL, 4),
                                new Card(Card.BLUE, Card.NORMAL, 5),
                                new Card(Card.BLUE, Card.NORMAL, 6),
                                new Card(Card.BLUE, Card.NORMAL, 7),
                                new Card(Card.BLUE, Card.NORMAL, 8),
                                new Card(Card.BLUE, Card.NORMAL, 9),
                                new Card(Card.BLUE, Card.NORMAL, 0)
                      };

        Card[] nGreen ={
                                new Card(Card.GREEN, Card.NORMAL, 1),
                                new Card(Card.GREEN, Card.NORMAL, 2),
                                new Card(Card.GREEN, Card.NORMAL, 3),
                                new Card(Card.GREEN, Card.NORMAL, 4),
                                new Card(Card.GREEN, Card.NORMAL, 5),
                                new Card(Card.GREEN, Card.NORMAL, 6),
                                new Card(Card.GREEN, Card.NORMAL, 7),
                                new Card(Card.GREEN, Card.NORMAL, 8),
                                new Card(Card.GREEN, Card.NORMAL, 9),
                                new Card(Card.GREEN, Card.NORMAL, 0)
                        };

        Card[] nRed ={
                                new Card(Card.RED, Card.NORMAL, 1),
                                new Card(Card.RED, Card.NORMAL, 2),
                                new Card(Card.RED, Card.NORMAL, 3),
                                new Card(Card.RED, Card.NORMAL, 4),
                                new Card(Card.RED, Card.NORMAL, 5),
                                new Card(Card.RED, Card.NORMAL, 6),
                                new Card(Card.RED, Card.NORMAL, 7),
                                new Card(Card.RED, Card.NORMAL, 8),
                                new Card(Card.RED, Card.NORMAL, 9),
                                new Card(Card.RED, Card.NORMAL, 0)
                    };

        Card[] nYellow ={
                                new Card(Card.YELLOW, Card.NORMAL, 1),
                                new Card(Card.YELLOW, Card.NORMAL, 2),
                                new Card(Card.YELLOW, Card.NORMAL, 3),
                                new Card(Card.YELLOW, Card.NORMAL, 4),
                                new Card(Card.YELLOW, Card.NORMAL, 5),
                                new Card(Card.YELLOW, Card.NORMAL, 6),
                                new Card(Card.YELLOW, Card.NORMAL, 7),
                                new Card(Card.YELLOW, Card.NORMAL, 8),
                                new Card(Card.YELLOW, Card.NORMAL, 9),
                                new Card(Card.YELLOW, Card.NORMAL, 0)
                        };
            //Create blue normal cards
            for(int n = 0, number = 0; n < 19; n++) {
               
                dDeck.addElement(nBlue[number]);
                number = (number==9)?0:number+1;
            }

            //Create green normal cards
            for(int n=0, number=0; n< 19; n++) {

                dDeck.addElement(nGreen[number]);
                number = (number==9)?0:number+1;
            }

            //Create red normal cards
            for(int n=0, number=0; n< 19; n++) {
               
                dDeck.addElement(nRed[number]);
                number = (number==9)?0:number+1;
            }

            //Create yellow normal cards
            for(int n=0, number=0; n< 19; n++) {
               
                dDeck.addElement(nYellow[number]);
                number = (number==9)?0:number+1;
            }

            //Create blue get2cards
            Card get2Blue = new Card(Card.BLUE, Card.GET2, Card.NO_NUMBER);
            dDeck.addElement(get2Blue);
            dDeck.addElement(get2Blue);
           
            //Create green get2cards
            Card get2Green = new Card(Card.GREEN, Card.GET2, Card.NO_NUMBER);
            dDeck.addElement(get2Green);
            dDeck.addElement(get2Green);
           
            //Create green get2cards
            Card get2Red = new Card(Card.RED, Card.GET2, Card.NO_NUMBER);
            dDeck.addElement(get2Red);
            dDeck.addElement(get2Red);

            //Create yellow get2cards
            Card get2Yellow = new Card(Card.YELLOW, Card.GET2, Card.NO_NUMBER);
            dDeck.addElement(get2Yellow);
            dDeck.addElement(get2Yellow);

            //Create blue skip cards
            Card skipBlue = new Card(Card.BLUE, Card.SKIP, Card.NO_NUMBER);
            dDeck.addElement(skipBlue);
            dDeck.addElement(skipBlue);

            //Create green skip cards
            Card skipGreen = new Card(Card.GREEN, Card.SKIP, Card.NO_NUMBER);
            dDeck.addElement(skipGreen);
            dDeck.addElement(skipGreen);

            //Create red skip cards
            Card skipRed = new Card(Card.RED, Card.SKIP, Card.NO_NUMBER);
            dDeck.addElement(skipRed);
            dDeck.addElement(skipRed);

            //Create yellow skip cards
            Card skipYellow = new Card(Card.YELLOW, Card.SKIP, Card.NO_NUMBER);
            dDeck.addElement(skipYellow);
            dDeck.addElement(skipYellow);

            //Create blue reverse card
            Card reverseBlue = new Card(Card.BLUE, Card.REVERSE, Card.NO_NUMBER);
            dDeck.addElement(reverseBlue);
            dDeck.addElement(reverseBlue);

            //Create green reverse card
            Card reverseGreen = new Card(Card.GREEN, Card.REVERSE, Card.NO_NUMBER);
            dDeck.addElement(reverseGreen);
            dDeck.addElement(reverseGreen);

            //Create red reverse card
            Card reverseRed = new Card(Card.RED, Card.REVERSE, Card.NO_NUMBER);
            dDeck.addElement(reverseRed);
            dDeck.addElement(reverseRed);

            //Create yellow reverse card
            Card reverseYellow = new Card(Card.YELLOW, Card.REVERSE, Card.NO_NUMBER);
            dDeck.addElement(reverseYellow);
            dDeck.addElement(reverseYellow);

            //Create joker and joker4 cards
            Card joker = new Card(Card.JOKER);
            Card joker4 = new Card(Card.JOKER4);
            for(int n = 0; n < 4; n++) {
                dDeck.addElement(joker);
                dDeck.addElement(joker4);
            }

            return dDeck;
   }

   /*
    * Método que verifica qual direção e passa a vez para o próximo jogador
    */
   public final void nextPlayer() {

        atualPlayer += direction;

        if (atualPlayer == -1) {

            atualPlayer = players.length - 1;
        } else if (atualPlayer >= players.length) {

            atualPlayer = 0;
        }
    }

    /*
     * Método que muda a direção pela direção atual
     */
    public final void changeDirection() {
       
        direction = (direction == CLOCKWISE)
                    ?ANTICLOCKWISE
                    :CLOCKWISE;
    }

    public final GraphicUNO getCanvas() {
       
        return gUNO;
    }

    public final void setMainMenu() {

        mid.setMain();
    }

    /*
     * Método que verifica e faz a ação do joga baseado na regra corrente
     */
    public abstract boolean jogada(Player player, Card card) throws InvalidMove;

    public void startGame() {

        playing = true;
    }

   
    public final int getDirection() {

        return direction;
    }

    public void pauseGame() {

        playing = false;
    }

    public final xUnoME getMidlet() {

        return mid;
    }

    public void getEndForm(boolean winner) {

        mid.getLCD().vibrate(2000);
        EndGame end = new EndGame(winner);
        end.startEffect();
        mid.getLCD().setCurrent(end);

        try {

            Thread.sleep(5000L);
        } catch (InterruptedException ie) {}

        end.stopEffect();
        mid.setMain();
    }

    public final boolean getChoose() {

        return chooseColor;
    }

    public final void setChoose(boolean nC) {
       
        chooseColor = nC;
    }

    public final boolean getCallUno() {
       
        return callUno;
    }

    public final void setCallUno(boolean n) {

        callUno = n;
    }

    /*
     * Método que verifica se o jogo está rodando
     */
    public final boolean isPlaying() {

        return playing;
    }

    public final void setPlaying(boolean newStatus) {

        playing = newStatus;
    }

    /*
     * Método que adicona cartas extras a um determinado player
     */
    public final void penalize(Player p, int numberCards) {

        for(int n = 1; n <= numberCards; n++) {

            p.addCard(table.getCard(false));
        }
    }

    public final Player getNextPlayer() {

        int index = atualPlayer + direction;

        if (index == -1) {

            index = players.length - 1;
        } else if (index >= players.length) {

            index = 0;
        }

        return players[index];
    }

    public boolean isValidMove(Card moveCard) {

        Card upCard = table.getUpCard();

        if (moveCard.getType() == Card.NORMAL) {

            return (moveCard.getColor() == atualColor)
                    || (moveCard.getNumber() == upCard.getNumber());
        } else if (moveCard.getType() != Card.JOKER) {

            return (upCard.getType() == moveCard.getType())
                    || (moveCard.getColor() == atualColor);
        }
       
        return true;
    }

    public final boolean getWaited() {

        return waited1;
    }

    public final void setWaited(boolean wait) {

        waited1 = wait;
    }

    public final void distributeCards() {

        for (int p = 0; p < players.length; p++) {

            for (int c = 1; c <= 7; c++) {

                players[p].addCard(table.getCard(false));
            }
        }
    }

    public final void putFirstCard() {

        Card firstCard = table.getCard(true);
        table.add2Discarded(firstCard);
        setColor(firstCard.getColor());
    }

    public final int getAtualPlayerPos() {

        return atualPlayer;
    }

    public final void setAtualPlayer(int npos) {

        atualPlayer = npos;
    }

    public final void setGameStatus(boolean nStatus) {

        playing = nStatus;
    }

    public final Player getLocalPlayer() {

        return players[localPlayerPos];
    }

    public final int getLocalPlayerPos() {

        return localPlayerPos;
    }

    public void stopPlaying() {

        playing = false;
        table = null;
        gUNO = null;
        players = null;
    }
}
TOP

Related Classes of xunome.game.BaseGame

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.