summaryrefslogtreecommitdiff
path: root/engine/Engine.java
blob: 8a022dc7c2d0cf7ed6bac28214a39a59a4a6f86e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package mahjong.engine;

import mahjong.aux.Card;
import mahjong.aux.set.Meld;
import mahjong.aux.set.Set;
import mahjong.player.GenericPlayer;
import mahjong.player.PlayerActionServer;
import mahjong.tools.Deck;

import java.util.Random;

/**
 * Created by joe on 12/2/14.
 */
public class Engine {
    PlayerActionServer[] players=new PlayerActionServer[4];
    Set deck;

    public Engine(PlayerActionServer player1,PlayerActionServer player2,PlayerActionServer player3,PlayerActionServer player4)
    {
        players[0]=player1;
        players[1]=player2;
        players[2]=player3;
        players[3]=player4;
    }

    public void ShufflePlayer()
    {
        Random rand=new Random();

        for (int i=0;i<5;++i) {
            int x = rand.nextInt(4);
            int y = (x+rand.nextInt(3)+1)%4;
            PlayerActionServer tmp;
            tmp=players[x];
            players[x]=players[y];
            players[y]=tmp;
        }
    }

    public void start()
    {
        ShufflePlayer();
        for (int i=0;i<4;++i)
            System.out.println(players[i].getName());
        deck = Deck.generateDeck(true);
    }

    public boolean syncPlayer(int id)
    {
        return (players[id].stateAlert(new Action(Action.Type.System, Action.Place.Info, Action.Act.Info,"Heartbeat")).getMessage().compareTo("ACK")==0);
    }

    public static void playerPutAsideCards(GenericPlayer player, Card card, Meld meld)
    {
        boolean mark=true;
        for (Card asCard:meld.getRep())
            if (card.getId() == asCard.getId() && mark)
                mark = true;
            else
                player.hand.remove(asCard);
    }

    public void mainLoop()
    {
        /*start();
        boolean hu=false;
        int pos=0;
        while (!hu && deck.getSize()>0)
        {
            Card card=deck.draw();
            PlayerActionServer player=players[pos];

            // Hu
            if (player.hand.huAble(card)) {
                player.confirmHu(card, 0);
                hu=true;
                System.out.println(player.getName().concat(" Hule"));
            }
            // Gang
            if (player.hand.gangAble(card))
            {
                Aside aside = player.confirmJiaAnGang(card, 0);
                if (aside.getRep()[0].getStdId()==card.getStdId())
                {
                    // An
                    for (Aside exAside:player.hand.asides)
                        if (exAside.getRep()[0].getStdId() == card.getStdId()) {
                            aside.setFrom(exAside.getFrom());
                            player.hand.asides.remove(exAside);
                            break;
                        }
                    player.hand.asides.add(aside);
                }
                else {
                    // Jia
                    playerPutAsideCards(player,card,aside);
                    // @TODO Remove consider card no
                }
            }

            //

            pos=(pos+1)%4;
        }*/
    }
}