Пример #1
0
        public override Board place(Board board)
        {
            int nboat   = GameManager.getnBoatsToPlace() - 1;
            int element = GameManager.getElement();

            Boat       currentBoat = this.boatList[nboat];                // bateau que l'on place
            SeaElement target      = ButtonBuffer.getPressedSeaElement(); // case où on veut placer le bateau

            if (target == null)
            {
                return(null);
            }
            if (target.button.Name[1] != '1')
            {
                return(null);
            }
            if (target.state != State.Water)
            {
                return(null);
            }

            int[,] listInterdit = new int[10, 10];// liste d'élément incliquables
            for (int ligne = 0; ligne < 10; ligne++)
            {
                for (int colonne = 0; colonne < 10; colonne++)
                {
                    listInterdit[ligne, colonne] = 0;
                }
            }
            for (int ligne = 0; ligne < 10; ligne++)
            {
                for (int colonne = 0; colonne < 10; colonne++)
                {
                    if (board.SeaElementList.ElementAt(ligne * 10 + colonne).state == State.Boat)
                    {
                        listInterdit[ligne, colonne] = 1;
                        //ajout des cases de bord des bateaux dans la liste d'éléments interdits
                        for (int i = 0; i < 3; i++)
                        {
                            for (int j = 0; j < 3; j++)
                            {
                                if ((0 < ligne + i) && (11 > ligne + i) && (0 < colonne + j) && (11 > colonne + j))
                                {
                                    listInterdit[ligne + i - 1, colonne + j - 1] = 1;
                                }
                            }
                        }
                    }
                }
            }

            int x = target.posX - 1;
            int y = target.posY - 1;

            if (listInterdit[x, y] == 1)
            {
                return(null);
            }

            // on compte le nombre de cases valides à droite
            Boolean isValid    = true;
            int     validRight = 0;
            int     checkX     = x + 1;

            while (checkX < 10 && isValid)
            {
                if (listInterdit[checkX, y] == 1)
                {
                    isValid = false;
                }
                else
                {
                    checkX++;
                    validRight++;
                }
            }

            // on compte le nombre de cases valides à gauche
            isValid = true;
            int validLeft = 0;

            checkX = x - 1;
            while (checkX >= 0 && isValid)
            {
                if (listInterdit[checkX, y] == 1)
                {
                    isValid = false;
                }
                else
                {
                    checkX--;
                    validLeft++;
                }
            }

            // on compte le nombre de cases valides en haut
            isValid = true;
            int validUp = 0;
            int checkY  = y - 1;

            while (checkY >= 0 && isValid)
            {
                if (listInterdit[x, checkY] == 1)
                {
                    isValid = false;
                }
                else
                {
                    checkY--;
                    validUp++;
                }
            }

            // on compte le nombre de cases valides en bas
            isValid = true;
            int validDown = 0;

            checkY = y + 1;
            while (checkY < 10 && isValid)
            {
                if (listInterdit[x, checkY] == 1)
                {
                    isValid = false;
                }
                else
                {
                    checkY++;
                    validDown++;
                }
            }

            Boolean validHorizontal = (validLeft + validRight + 1) >= currentBoat.size;
            Boolean validVertical   = (validUp + validDown + 1) >= currentBoat.size;

            // s'il y a assez de place pour placer le bateau, on valide la case cliquée
            if (!validHorizontal && !validVertical)
            {
                return(null);
            }
            ButtonBuffer.setPressedSeaElement(null);
            currentBoat.position[element] = target;
            board.SeaElementList.ElementAt(x * 10 + y).state = State.Sunk;

            if (element == 0)
            {
                // on rend inclicables toutes les cases sauf celles où on peut continuer le placement du bateau
                for (int ligne = 0; ligne < 10; ligne++)
                {
                    for (int colonne = 0; colonne < 10; colonne++)
                    {
                        //rendre tous les élements de non proche en proche non clicable
                        if (listInterdit[colonne, ligne] == 1)
                        {
                            board.SeaElementList.ElementAt(colonne * 10 + ligne).clickable = false;
                        }
                        else if (ligne == y + 1 && colonne == x && validVertical)
                        {
                            board.SeaElementList.ElementAt(colonne * 10 + ligne).clickable = true;
                        }
                        else if (ligne == y - 1 && colonne == x && validVertical)
                        {
                            board.SeaElementList.ElementAt(colonne * 10 + ligne).clickable = true;
                        }
                        else if (ligne == y && colonne == x + 1 && validHorizontal)
                        {
                            board.SeaElementList.ElementAt(colonne * 10 + ligne).clickable = true;
                        }
                        else if (ligne == y && colonne == x - 1 && validHorizontal)
                        {
                            board.SeaElementList.ElementAt(colonne * 10 + ligne).clickable = true;
                        }
                        else if (ligne == y && colonne == x)
                        {
                            board.SeaElementList.ElementAt(colonne * 10 + ligne).clickable = true;
                        }
                        else
                        {
                            board.SeaElementList.ElementAt(colonne * 10 + ligne).clickable = false;
                        }
                    }
                }
            }
            else if (element == currentBoat.size - 1)
            {
                for (int ligne = 1; ligne < 11; ligne++)
                {
                    for (int colonne = 1; colonne < 11; colonne++)
                    {
                        //rendre tous les élements cliquables
                        board.SeaElementList.ElementAt((colonne - 1) * 10 + (ligne - 1)).clickable = true;
                    }
                }
            }
            else
            {
                Boolean Horizontal = true;
                int     x0         = currentBoat.position[0].posX - 1;
                int     y0         = currentBoat.position[0].posY - 1;
                if (x0 == x)
                {
                    Horizontal = false;
                }
                if (Horizontal)
                {
                    //quand on pose le 2nd élement on enlève les case cliquable de l'autre sens
                    if (element == 1)
                    {
                        if (y0 > 0)
                        {
                            board.SeaElementList.ElementAt((x0) * 10 + (y0 - 1)).clickable = false;
                        }
                        if (y0 < 9)
                        {
                            board.SeaElementList.ElementAt((x0) * 10 + (y0 + 1)).clickable = false;
                        }
                    }
                    // après avoir verifié que les prochaines cases existaient et qu'on pouvait y mettre un élément ont les rends cliquables
                    if (x > 0)
                    {
                        if (listInterdit[x - 1, y] != 1)
                        {
                            board.SeaElementList.ElementAt((x - 1) * 10 + y).clickable = true;
                        }
                    }
                    if (x < 9)
                    {
                        if (listInterdit[x + 1, y] != 1)
                        {
                            board.SeaElementList.ElementAt((x + 1) * 10 + y).clickable = true;
                        }
                    }
                }
                else
                {
                    //quand on pose le 2nd élement on enlève les case cliquable de l'autre sens
                    if (element == 1)
                    {
                        if (x0 > 0)
                        {
                            board.SeaElementList.ElementAt((x0 - 1) * 10 + (y0)).clickable = false;
                        }
                        if (x0 < 9)
                        {
                            board.SeaElementList.ElementAt((x0 + 1) * 10 + (y0)).clickable = false;
                        }
                    }
                    // après avoir verifié que les prochaines cases existaient et qu'on pouvait y mettre un élément ont les rends cliquables
                    if (y > 0)
                    {
                        if (listInterdit[x, y - 1] != 1)
                        {
                            board.SeaElementList.ElementAt(x * 10 + (y - 1)).clickable = true;
                        }
                    }
                    if (y < 9)
                    {
                        if (listInterdit[x, y + 1] != 1)
                        {
                            board.SeaElementList.ElementAt(x * 10 + (y + 1)).clickable = true;
                        }
                    }
                }
            }
            this.boatList[nboat] = currentBoat;
            return(board);
        }
Пример #2
0
        public override Board place(Board board)
        {
            int nboat = GameManager.getnBoatsToPlace() - 1;;

            int[,] listInterdit = new int[10, 10];// liste d'élément incliquables
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    listInterdit[i, j] = 0; // au début tout est cliquable
                }
            }

            for (int ligne = 0; ligne < 10; ligne++)
            {
                for (int colonne = 0; colonne < 10; colonne++)
                {
                    if (board.SeaElementList.ElementAt(ligne * 10 + colonne).state == State.Boat)
                    {
                        listInterdit[ligne, colonne] = 1;
                        //ajout des cases de bord des bateaux dans la liste d'éléments interdits
                        for (int i = 0; i < 3; i++)
                        {
                            for (int j = 0; j < 3; j++)
                            {
                                if ((0 < ligne + i) && (11 > ligne + i) && (0 < colonne + j) && (11 > colonne + j))
                                {
                                    listInterdit[ligne + i - 1, colonne + j - 1] = 1;
                                }
                            }
                        }
                    }
                }
            }

            Boat       currentBoat = this.boatList[nboat];
            SeaElement target;
            Random     random    = new Random();
            int        direction = random.Next(3); // 0 pour gauche, 1 pour haut, 2 pour droite, 3 pour bas
            int        aleaX;
            int        aleaY;

            if (direction == 0)
            {
                aleaX = currentBoat.size + random.Next(10 - currentBoat.size); // on place au hasard la première case du bateau puis on place les autres à gauche
                aleaY = random.Next(10) + 1;
                for (int element = 0; element < currentBoat.size; element++)
                {
                    target = board.SeaElementList[10 * (aleaX - 1 - element) + (aleaY - 1)];
                    if (listInterdit[target.posX - 1, target.posY - 1] == 1)
                    {
                        return(null);
                    }
                    else
                    {
                        currentBoat.position[element] = target;
                    }
                }
            }

            else if (direction == 1)
            {
                aleaX = random.Next(10) + 1;  // on place au hasard la première case du bateau puis on place les autres en haut
                aleaY = random.Next(10 - currentBoat.size) + 1;
                for (int element = 0; element < currentBoat.size; element++)
                {
                    target = board.SeaElementList[10 * (aleaX - 1) + (aleaY - 1 + element)];
                    if (listInterdit[target.posX - 1, target.posY - 1] == 1)
                    {
                        return(null);
                    }
                    else
                    {
                        currentBoat.position[element] = target;
                    }
                }
            }

            else if (direction == 2)
            {
                aleaX = random.Next(10 - currentBoat.size) + 1;  // on place au hasard la première case du bateau puis on place les autres à droite
                aleaY = random.Next(10) + 1;
                for (int element = 0; element < currentBoat.size; element++)
                {
                    target = board.SeaElementList[10 * (aleaX - 1 + element) + (aleaY - 1)];
                    if (listInterdit[target.posX - 1, target.posY - 1] == 1)
                    {
                        return(null);
                    }
                    else
                    {
                        currentBoat.position[element] = target;
                    }
                }
            }

            else if (direction == 3)
            {
                aleaX = random.Next(10) + 1;  // on place au hasard la première case du bateau puis on place les autres en bas
                aleaY = currentBoat.size + random.Next(10 - currentBoat.size);
                for (int element = 0; element < currentBoat.size; element++)
                {
                    target = board.SeaElementList[10 * (aleaX - 1) + (aleaY - 1 - element)];
                    if (listInterdit[target.posX - 1, target.posY - 1] == 1)
                    {
                        return(null);
                    }
                    else
                    {
                        currentBoat.position[element] = target;
                    }
                }
            }
            this.boatList[nboat] = currentBoat;
            for (int element = 0; element < currentBoat.size; element++)
            {
                board.SeaElementList[10 * (currentBoat.position[element].posX - 1) + (currentBoat.position[element].posY - 1)]       = currentBoat.position[element];
                board.SeaElementList[10 * (currentBoat.position[element].posX - 1) + (currentBoat.position[element].posY - 1)].state = State.Boat;
            }
            return(board);
        }