elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Recopilación Tutoriales y Manuales Hacking, Seguridad, Privacidad, Hardware, etc


  Mostrar Temas
Páginas: [1]
1  Programación / Java / Ayuda para establecer un sistema de turnos en un juego en: 3 Junio 2011, 00:14 am
Buenas noches a todos.

Soy novato en Java y esta es mi primera intervención en el foro. Necesitaría ayuda para crear un método de modo a poder establecer turnos en el juego que estoy desenvolviendo.
El juego (Hex) tiene como objetivo establecer caminos entre las extremidades del tablero atribuidas a cada jugador, para ello cada jugador pinta los hexágonos del color que le fue atribuido con un click de ratón. Una vez que un jugador completa una ruta entre las extremidades gana la partida.
Dentro de mi ignorancia, intenté hacer lo siguiente pero el susodicho método de turnos no funciona correctamente:
Código:

import java.awt.Color;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

    public class Hex_UDP extends JPanel {
        protected Color verm;
        protected Color azul;
       protected Color selectedcolor;

        private static final int NUM_POLYGONS = 7*7;

        private List<MapPolygon> polygons;

        private static final int WIDTH = 1200;
        private static final int HEIGHT = 800;
        //private Random random = new Random();

        int i=50;

boolean Player1;
boolean Player2;

        public Hex_UDP() {
       
            Player1 = false;
            Player2 = false;
         int x = 100;
         int y = 100;
            polygons = new LinkedList<MapPolygon>();
            for (int a = 0; a < NUM_POLYGONS; a++) {
                 if(a==7){
                        x = 145;
                        y = 175;
                   }
                    if(a==14){
                        x = 190;
                        y = 250;
                   }
                    if(a==21){
                        x = 235;
                        y = 325;
                   }
                     if(a==28){
                        x = 280;
                        y = 400;
                   }
                     if(a==35){
                        x = 325;
                        y = 475;
                   }
                      if(a==42){
                        x = 370;
                        y = 550;
                   }
                int x1 = (int) (x + 50 * Math.sin(1 * 2 * Math.PI / 6));
                int x2 = (int) (x + 50 * Math.sin(2 * 2 * Math.PI / 6));
                int x3 = (int) (x + 50 * Math.sin(3 * 2 * Math.PI / 6));
                int x4 = (int) (x + 50 * Math.sin(4 * 2 * Math.PI / 6));
                int x5 = (int) (x + 50 * Math.sin(5 * 2 * Math.PI / 6));
                int x6 = (int) (x + 50 * Math.sin(6 * 2 * Math.PI / 6));
                x=x+88;
                int y1 = (int) (y + 50 * Math.cos(1 * 2 * Math.PI / 6));
                int y2 = (int) (y + 50 * Math.cos(2 * 2 * Math.PI / 6));
                int y3 = (int) (y + 50 * Math.cos(3 * 2 * Math.PI / 6));
                int y4 = (int) (y + 50 * Math.cos(4 * 2 * Math.PI / 6));
                int y5 = (int) (y + 50 * Math.cos(5 * 2 * Math.PI / 6));
                int y6 = (int) (y + 50 * Math.cos(6 * 2 * Math.PI / 6));
                         
                verm = Color.red;
                azul = Color.blue;

                i++;

                    if(i%2==0){
                        Player1=true;
                        selectedcolor=verm;

                    }
                        else{
                          Player1=false;
                         Player2=true;
                         selectedcolor=azul;
                }

         
                polygons.add(new MapPolygon(new int[]{x1,x2,x3,x4,x5,x6}, new int[]{y1,y2,y3,y4,y5,y6}, 6, selectedcolor));

            }

            addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {

                    for (MapPolygon mapPiece : polygons) {
                        if (mapPiece.contains(e.getPoint())) {
                            mapPiece.setSelected(!mapPiece.isSelected());
                            repaint();
                            System.out.println(mapPiece);
                            break;
                        }
                    }
                }
            });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(WIDTH, HEIGHT);
        }


        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            final Color outlineColor = Color.BLACK;
            for (MapPolygon mapPiece : polygons) {
                if (mapPiece.isSelected()) {


                  g.setColor(mapPiece.getFillColor());
                   g.fillPolygon(mapPiece);
                   g.setColor(Color.blue);
                   selectedcolor=null;

                }
                else {
                    g.setColor(outlineColor);
                    g.drawPolygon(mapPiece);
                 
                }
            }

    g.drawLine(0, 30, 56, 73);
    g.drawLine(727, 30, 671, 74);
    g.drawLine(0, 30, 727, 30);
    g.drawLine(270, 618, 326, 575);
    g.drawLine(940, 575, 996, 618);
    g.drawLine(270, 618, 996, 618);
    g.drawLine(0, 30, 270, 618);
    g.drawLine(727, 30, 996, 618);

    g.setColor(Color.blue);
    g.fillOval(145, 310, 35, 35);
    g.fillOval(817, 310, 35, 35);
    g.setColor(Color.red);
    g.fillOval(389, 33, 35, 35);
    g.fillOval(660, 582, 35, 35);

        }


        public static void main(String[] args) {
            JFrame frame = new JFrame();
            JPanel panel = new Hex_UDP();
            frame.getContentPane().add(panel);
            frame.pack();
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }

        private class MapPolygon extends Polygon {

            private boolean selected;
            private Color fillColor;

            public MapPolygon(int[] xpoints, int[] ypoints, int npoints, Color color) {
                super(xpoints, ypoints, npoints);
                this.fillColor = color;
                this.selected = false;
            }

            public Color getFillColor() {
                return fillColor;
            }

            public void setFillColor(Color fillColor) {
                this.fillColor = fillColor;
            }

            public boolean isSelected() {
                return selected;
            }

            public void setSelected(boolean selected) {
                this.selected = selected;
            }
        }

    }


Perdón si sobrepasé el tamaño máximo de código en el foro.
Muchas gracias por vuestras sugerencias.
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines