IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Développement 2D, 3D et Jeux Discussion :

Jeu de Go en Java


Sujet :

Développement 2D, 3D et Jeux

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    97
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 97
    Points : 77
    Points
    77
    Par défaut Jeu de Go en Java
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    //////////////////////////////////////////////
    //
    //   GO java applet, Ver 1.1
    //   
    //   Author: Chenyang Xu, chenyang@jhu.edu
    //      http://iacl.ece.jhu.edu/~chenyang
    //
    //////////////////////////////////////////////
     
    import java.applet.Applet;    
    import java.awt.*; 
    import java.util.Vector;
     
    public class go extends Applet {
     
      int AppletWidth, AppletHeight, 
          BoardLeft, BoardTop, BoardSize, Delta,
          mouseMoveX, mouseMoveY,
          StoneColor, START;
      boolean REMOVE;
      Vector StoneList = new Vector();
      int[][] BoardMap = new int[19][19];
      Image offscreenImg;
      Graphics offscreenG;
      Button Start = new Button("Start");
      Button Finish = new Button("Finish");
      Checkbox Remove = new Checkbox("Remove", null, false);
     
      public void init() {
     
        START = 0;
        REMOVE = false;
        add(Start);
        add(Remove);
        add(Finish);
        Remove.disable();
        Finish.disable();
     
        Dimension d = size();
        AppletHeight = d.height;
        AppletWidth = d.width;
     
        offscreenImg = createImage(AppletWidth, AppletHeight);
        offscreenG = offscreenImg.getGraphics();
     
        if (AppletHeight < AppletWidth)
            BoardSize = (int) (0.9 * AppletHeight);
        else
    	BoardSize = (int) (0.9 * AppletWidth);
     
        Delta = BoardSize/18;
        BoardLeft = (AppletWidth - BoardSize)/2;
        BoardTop  = (AppletHeight - BoardSize)/2 + Delta/2;
     
        StoneList.removeAllElements();
        mouseMoveX = -10;
        mouseMoveY = -10;
     
        StoneColor = 0;  // 0 --> black,  1 --> white
     
        // initialize the BoardMap array to zeros
        for (int i=0; i<19; i++) 
    	for (int j=0; j<19; j++)
    	     BoardMap[i][j] = 0;
     
        repaint();
      }
     
      public void paint(Graphics g) {
     
        Color BoardColor;
        int x0, y0, x1, y1, n, i, j, stone_color;
        Integer tempInt;
     
        // Draw the background of the applet
        offscreenG.setColor(Color.gray);
        offscreenG.fillRect(0, 0, AppletWidth, AppletHeight);   
     
        // Paint the GO board
        BoardColor = new Color(230,205,80); // Draw the board background
        offscreenG.setColor(BoardColor);
        offscreenG.fillRect(BoardLeft, BoardTop, BoardSize-1, BoardSize-1);   
        offscreenG.setColor(Color.black);   // Draw the board grids
        for (i=0; i<= 18; i++)  {
    	x0 = BoardLeft; y0 = i*Delta + BoardTop; 
            x1 = 18*Delta + BoardLeft; y1 = i*Delta + BoardTop;
            offscreenG.drawLine(x0, y0, x1, y1); // draw the row
    	x0 = i*Delta + BoardLeft; y0 = BoardTop; 
            x1 = i*Delta + BoardLeft; y1 = 18*Delta + BoardTop;
            offscreenG.drawLine(x0, y0, x1, y1); // draw the column
        }
        for (i=0; i<3; i++)                // Draw the board markers
    	for (j=0; j<3; j++) {
    	    x0 = (3 + i*6) * Delta + BoardLeft - 2 ;  
    	    y0 = (3 + j*6) * Delta + BoardTop - 2;
    	    offscreenG.fillRect(x0, y0, 5, 5);
            }
     
        // Paint the stone list
        for (n=0; n < StoneList.size(); n+=3) {
    	tempInt = (Integer) StoneList.elementAt(n);
            i = tempInt.intValue();
            tempInt = (Integer) StoneList.elementAt(n+1);
    	j = tempInt.intValue();
    	tempInt = (Integer) StoneList.elementAt(n+2);
            stone_color = tempInt.intValue();
     
            if (stone_color==0) {  // Draw the black stone
       	   offscreenG.setColor(Color.black);
    	   offscreenG.fillOval((int)((i-0.5)*Delta) + BoardLeft, 
    	   (int)((j-0.5)*Delta) + BoardTop, Delta, Delta);
            } else {               // Draw the white stone
      	   offscreenG.setColor(Color.white);
               offscreenG.fillOval((int)((i-0.5)*Delta) + BoardLeft + 1, 
    	     (int)((j-0.5)*Delta) + BoardTop + 1, Delta-2, Delta-2);
       	   offscreenG.setColor(Color.black);
               offscreenG.drawOval((int)((i-0.5)*Delta) + BoardLeft + 1, 
    	     (int)((j-0.5)*Delta) + BoardTop + 1, Delta-2, Delta-2);
            }
     
            // paint a marker on the current stone
            if (n == StoneList.size()-3) {
               if (stone_color==0) {  // Draw the black marker
                  offscreenG.setColor(Color.white);
    	      offscreenG.fillRect(i*Delta + BoardLeft - 1, 
    	        j*Delta + BoardTop - 1, 3, 3);
               } else {               // Draw the white marker
                  offscreenG.setColor(Color.black);
                  offscreenG.fillRect(i*Delta + BoardLeft -1, 
    	        j*Delta + BoardTop - 1, 3, 3);
               }
            }
        }
     
        // Paint the stone moving with the mouse
        if (START!=0 && mouseMoveX > BoardLeft-3 && mouseMoveY > BoardTop-3 &&
            mouseMoveX < BoardLeft+BoardSize+3 
    	&& mouseMoveY < BoardTop+BoardSize+3) {
     
    	if (!REMOVE) { // plot stone if REMOVE state is false
               if (StoneColor==0) { // Draw the black stone 
                  offscreenG.setColor(Color.black);
                  offscreenG.fillOval(mouseMoveX-Delta/2 + 1, 
    	        mouseMoveY-Delta/2 + 1, Delta-2, Delta-2); 
               } else {             // Draw the white stone
      	      offscreenG.setColor(Color.white);
                  offscreenG.fillOval(mouseMoveX-Delta/2 + 1, 
    	        mouseMoveY-Delta/2 + 1, Delta-2, Delta-2); 
                  offscreenG.setColor(Color.black);
                  offscreenG.drawOval(mouseMoveX-Delta/2, 
    	        mouseMoveY-Delta/2, Delta, Delta); 
               }
            } else { // draw the removing cross symbol
    	   x0 = mouseMoveX-Delta/2 + 2; 
    	   y0 = mouseMoveY-Delta/2 + 2;
    	   x1 = x0 + Delta - 4;
    	   y1 = y0 + Delta - 4;
               offscreenG.setColor(Color.red);
    	   offscreenG.drawLine(x0,   y0,   x1,   y1);
    	   offscreenG.drawLine(x0+1, y0,   x1,   y1-1);
    	   offscreenG.drawLine(x0,   y0+1, x1-1, y1);
    	   offscreenG.drawLine(x0,   y1,   x1,   y0);
    	   offscreenG.drawLine(x0,   y1-1, x1-1, y0);
    	   offscreenG.drawLine(x0+1, y1,   x1,   y0+1);
            }
     
        }
     
        g.drawImage(offscreenImg, 0, 0, this);
     
      }
     
      public Point stoneCoord(int x, int y) {
     
        int i, j;
        Point p = new Point(0,0);
     
        p.x = (int) (((float)(x)-BoardLeft)/Delta+0.5);
        p.y = (int) (((float)(y)-BoardTop)/Delta+0.5);
     
        return p;
      }
     
      public boolean mouseDown(Event e, int x, int y) {
     
         Point coord = stoneCoord(x,y);
         int MapVal, index, i, j;
         Integer tempInt;
         boolean found;
     
         if (START==1 && x > BoardLeft-3 && y > BoardTop-3 &&
            x < BoardLeft+BoardSize+3 && y < BoardTop+BoardSize+3) {
            // add stone while in play
            MapVal = BoardMap[coord.y][coord.x];
            if (REMOVE && MapVal != 0) { 
               // handle removing stones 
               found = false;  
    	   int n = 0;   
    	   while (!found && n < StoneList.size()) {
    	       tempInt = (Integer) StoneList.elementAt(n);
                   i = tempInt.intValue();
                   tempInt = (Integer) StoneList.elementAt(n+1);
    	       j = tempInt.intValue();
    	       if (i==coord.x && j==coord.y) 
    	          found = true;
    	       n += 3;
               }
    	   // under no circumstance, found must be true when finish
               // the above loop unless there is a bug in the program.
               // So I don't deal with exceptions here.
    	   StoneList.removeElementAt(n-3);
    	   StoneList.removeElementAt(n-3);
    	   StoneList.removeElementAt(n-3);
               BoardMap[coord.y][coord.x] = 0;
     
               repaint();
     
            } else if (!REMOVE && MapVal == 0) {
               // handle adding stones
               BoardMap[coord.y][coord.x] = 1;
               StoneList.addElement(new Integer(coord.x));
               StoneList.addElement(new Integer(coord.y));
               StoneList.addElement(new Integer(StoneColor));
               StoneColor = 1 - StoneColor;  
               repaint();
            }
         }
     
         return true;
      }
     
      public boolean mouseMove(Event e, int x, int y) {
     
         mouseMoveX = x;
         mouseMoveY = y;
         repaint();
     
         return true;
      }
     
      public boolean action(Event evt, Object arg) {
     
         if (evt.target instanceof Button) 
    	handleButton((String)arg);
         else if (evt.target instanceof Checkbox)	
    	handleCheckbox(evt);
     
         return true;		
      }
     
      void handleButton(String bname) {
     
         if (bname.equals("Start")) {
    	START = 1;
     	Start.disable();
    	Remove.enable();
    	Finish.enable();        
            // initialize the BoardMap array to zeros
            for (int i=0; i<19; i++) 
    	    for (int j=0; j<19; j++)
    	        BoardMap[i][j] = 0;
     
    	repaint();
     
         } else if (bname.equals("Finish")) {
            StoneList.removeAllElements();
            START = 0;
    	REMOVE = false;
            StoneColor = 0;
    	Start.enable();
    	Remove.setState(false);
    	Remove.disable();
    	Finish.disable();
    	repaint();
         }
         else; // handle exceptions
      }
     
      void handleCheckbox(Event evt) {
     
         Checkbox currentCheckbox = (Checkbox)evt.target;
         if (currentCheckbox.getLabel() == "Remove") {
    	REMOVE = Remove.getState();
    	repaint();
         }
     
      }
     
      public void update(Graphics g) {
         paint(g);
      }
     
    }
    [Balises code ajoutées par fearyourself, merci d'y penser à l'avenir]
    Mercenaire du code

  2. #2
    Expert éminent sénior

    Avatar de fearyourself
    Homme Profil pro
    Ingénieur Informaticien Senior
    Inscrit en
    Décembre 2005
    Messages
    5 121
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Ingénieur Informaticien Senior
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2005
    Messages : 5 121
    Points : 11 877
    Points
    11 877
    Par défaut
    Hmmm, y-a-t-il une question?

    Ou est-ce une contribution, parce que si c'est le cas, il faudrait:

    Commenter le code correctement et utiliser du code qui compile correctement, parce que j'obtiens avec:

    java -version
    java version "1.5.0_03"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_03-b07)
    Java HotSpot(TM) Client VM (build 1.5.0_03-b07, mixed mode)
    Ceci:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    javac go.java 
    Note: go.java uses or overrides a deprecated API.
    Note: Recompile with -Xlint:deprecation for details.
    Note: go.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    et si je compile avec -Xlint, j'ai 16 warnings...

    Donc pourquoi ce post?

  3. #3
    Rédacteur

    Avatar de Matthieu Brucher
    Profil pro
    Développeur HPC
    Inscrit en
    Juillet 2005
    Messages
    9 810
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations professionnelles :
    Activité : Développeur HPC
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2005
    Messages : 9 810
    Points : 20 970
    Points
    20 970
    Par défaut
    Pas de commentaires dans le code, des fonctions de xxx lignes...

  4. #4
    Rédacteur

    Avatar de loka
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Novembre 2004
    Messages
    2 672
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Service public

    Informations forums :
    Inscription : Novembre 2004
    Messages : 2 672
    Points : 5 509
    Points
    5 509
    Par défaut
    même chose que fearyourself...

  5. #5
    Membre régulier
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    97
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 97
    Points : 77
    Points
    77
    Par défaut
    Salut,

    Veuillez m'excusez mais en fait je me mets à la programmation
    graphique sous Java... Et au cours de mes pérégrinations sur Internet
    je suis tombé sur une appli sympa.

    J'ai alors voulu voir le code source. Et j'ai été surpris de voir
    le peu de code qu'il y avait dedans. Et je me suis dit que
    j'allais le poster ici.

    Mais j'aurai du filer le lien de l'auteur de cette applet.
    Ce que je corrige ici :
    http://iacl.ece.jhu.edu/~chenyang/java/go.html


    Ainsi vous pourrez utiliser son applet sans avoir
    à compiler et à corriger du code Java.

    Toutes mes mes excuses pour ce manque de communication...


    Mercenaire du code

  6. #6
    Membre régulier
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    97
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 97
    Points : 77
    Points
    77
    Par défaut
    Sinon encore mieux...

    Vous avez une autre applet qui embarque plusieurs
    tactiques intelligentes pour le jeu de Go.

    C'est impressionant !


    http://javaboutique.internet.com/Go/
    Mercenaire du code

  7. #7
    Rédacteur

    Avatar de Matthieu Brucher
    Profil pro
    Développeur HPC
    Inscrit en
    Juillet 2005
    Messages
    9 810
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations professionnelles :
    Activité : Développeur HPC
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2005
    Messages : 9 810
    Points : 20 970
    Points
    20 970
    Par défaut
    Ah, OK...
    Merci pour les liens 8) 8)

  8. #8
    Membre actif Avatar de keil
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    261
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France

    Informations forums :
    Inscription : Mai 2005
    Messages : 261
    Points : 214
    Points
    214
    Par défaut
    moi je voudrais savoir comment on compte le nombre de points au go, j'ai rien compris!!
    J'arrive à prendre les pièces de l'adversaire, mais une fois que le jeu est clos, comment je sais qui a gagné?
    Colère et Haine mènent à la Puissance

  9. #9
    Nouveau membre du Club
    Inscrit en
    Juin 2006
    Messages
    36
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Juin 2006
    Messages : 36
    Points : 34
    Points
    34
    Par défaut
    Tu enlèves les groupes morts, tu comptes les territoires de chacun (les intersections vides entourées par ta couleur) et tu soustraits les prisonniers de ta couleur.
    Voila en gros.

Discussions similaires

  1. jeu de dame en java
    Par pit88 dans le forum Général Java
    Réponses: 5
    Dernier message: 07/05/2008, 14h25
  2. Jeu Memorix realisé en java
    Par krachik dans le forum Général Java
    Réponses: 5
    Dernier message: 03/01/2008, 21h48
  3. Développement d'un jeu de poker en java.
    Par SmileAndFly dans le forum Développement 2D, 3D et Jeux
    Réponses: 15
    Dernier message: 02/04/2007, 20h02
  4. Jeu de gestion en java
    Par luckyvae dans le forum Développement 2D, 3D et Jeux
    Réponses: 6
    Dernier message: 27/08/2006, 18h19
  5. Réponses: 4
    Dernier message: 13/02/2006, 21h58

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo