public void Draw(BattleGrid grid)
        {
            for (int i = 0; i < BattleGrid.ROWS; i++)
            {
                for (int j = 0; j < BattleGrid.COLS; j++)
                {
                    /* alors c'est pas très beau de faire un nouveau rectangle, mais j'ai pas trop réussi à récupérer les infos du rectangle précédent... */
                    if(grid.States[i,j] == GridStates.Ship)
                    {
                        Rectangle r = new Rectangle() { HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, VerticalAlignment = System.Windows.VerticalAlignment.Stretch, Stroke = Brushes.White, StrokeThickness = 1, Fill = Brushes.Black };
                        layout.Children.Add(r);
                        Grid.SetRow(r, i);
                        Grid.SetColumn(r, j);
                    }
                    else if (grid.States[i, j] == GridStates.Hit)
                    {
                        Rectangle r = new Rectangle() { HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, VerticalAlignment = System.Windows.VerticalAlignment.Stretch, Stroke = Brushes.White, StrokeThickness = 1, Fill = Brushes.Red };
                        layout.Children.Add(r);
                        Grid.SetRow(r, i);
                        Grid.SetColumn(r, j);
                    }
                    else if (grid.States[i, j] == GridStates.Missed)
                    {
                        Rectangle r = new Rectangle() { HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, VerticalAlignment = System.Windows.VerticalAlignment.Stretch, Stroke = Brushes.White, StrokeThickness = 1, Fill = Brushes.LightBlue };
                        layout.Children.Add(r);
                        Grid.SetRow(r, i);
                        Grid.SetColumn(r, j);
                    }

                }
            }
        }
Exemple #2
0
 void IPlayer.Play(BattleGrid grid)
 {
     if (x < 10 && y < 10)
     {
         grid.Shoot(x, y);
         if (x < 9)
         {
             x++;
         }
         else if (y < 9)
         {
             x = 0;
             y++;
         }
     }
 }
        public BattleGridControl()
        {
            InitializeComponent();
            int i;
            for (i = 0; i < BattleGrid.ROWS; i++)
            {
                layout.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(20) });
            }
            for (i = 0; i < BattleGrid.COLS; i++)
            {
                layout.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(20) });
            }
            for (i = 0; i < BattleGrid.ROWS; i++)
            {
                for (int j = 0; j < BattleGrid.COLS; j++)
                {
                    Rectangle r = new Rectangle() { HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, VerticalAlignment = System.Windows.VerticalAlignment.Stretch, Stroke = Brushes.White, StrokeThickness = 1, Fill = Brushes.Blue };
                    layout.Children.Add(r);
                    Grid.SetRow(r, i);
                    Grid.SetColumn(r, j);
                }
            }

            BattleGrid g = new BattleGrid();

            // Ajout de bateaux
            g.NewGrid();

            // Tirs
            for (i = 0; i < 10; i++)
            {
                g.Shoot(2, i);
                g.Shoot(3, i);
            }

            g.Shoot(6, 2);
            g.Shoot(1, 1);
            g.Shoot(4, 1);
            g.Shoot(1, 4);

            // Draw
            this.Draw(g);
        }
        static void Main(string[] args)
        {
            /*
             *    PERMIERS ESSAIS
             */

            // Création d'un navire horizontal aux coordonnées (2, 5), de taille 3
            //Ship b = new Ship(2, 5, 3, Orientations.Horizontal);

            // IsAtCoordinates() doit renvoyer true si le bateau est aux coordonnées données
            /*
            System.Console.WriteLine(b.IsAtCoordinates(2, 5));
            System.Console.WriteLine(b.IsAtCoordinates(3, 5));
            System.Console.WriteLine(b.IsAtCoordinates(4, 5));
            */

            // IsAtCoordinates() doit renvoyer false si le bateau n'est pas aux coordonnées données
            /*
            System.Console.WriteLine(b.IsAtCoordinates(1, 5));
            System.Console.WriteLine(b.IsAtCoordinates(5, 5));
            System.Console.WriteLine(b.IsAtCoordinates(2, 6));
            */

            // Création d'une grille vierge
            //BattleGrid g = new BattleGrid();
            // Ajout du navire à la grille
            //g.AddShip(b);

            // Vérification qu'il y a de l'eau dans toutes les cases (doit renvoyer Water)
            /*
            for (int x = 0; x < 10; x++)
                for (int y = 0; y < 10; y++)
                    System.Console.WriteLine(x + "," + y + "=" + g.States[x, y]);
            */

            // Vérification de l'état des cases (doit tjs renvoyer true)
            /*
            System.Console.WriteLine(g.States[2, 5] == GridStates.Ship);
            System.Console.WriteLine(g.States[3, 5] == GridStates.Ship);
            System.Console.WriteLine(g.States[4, 5] == GridStates.Ship);
            System.Console.WriteLine(g.States[1, 5] == GridStates.Water);
            System.Console.WriteLine(g.States[5, 5] == GridStates.Water);
            */

            //Affichage de la grille en console
            //g.Draw();

            /*
            // On doit pouvoir ajouter un petit navire en haut à gauche
            System.Console.WriteLine(g.CanAddShip(new Ship(0, 0, 2, Orientations.Horizontal)));
            // On ne doit pas pouvoir ajouter un navire qui en chevauche un autre
            System.Console.WriteLine(g.CanAddShip(new Ship(1, 5, 2, Orientations.Horizontal)));
            // On ne doit pas pouvoir ajouter un navire qui déborde
            System.Console.WriteLine(g.CanAddShip(new Ship(9, 1, 2, Orientations.Horizontal)));
            */

            /*
            // Un tir dans l'eau marque la case comme manquée
            System.Console.WriteLine(g.Shoot(0, 0));
            // Outre la valeur de retour, la grille doit être modifiée
            System.Console.WriteLine(g.States[0, 0]);
            // Tir sur un navire
            System.Console.WriteLine(g.Shoot(2, 5));
            */

            /*
            // A ce moment, le jeu ne fait que commencer
            System.Console.WriteLine("Game over : " + g.GameOver());
            g.Shoot(2, 5);
            g.Shoot(3, 5);
            g.Shoot(4, 5);
            // Toutes les positions ont été tirées
            System.Console.WriteLine("Game over : " + g.GameOver());
            */

            /*
             *   MODELISATION D'UN JOUEUR AUTOMATIQUE
             */

            /*
            BattleGrid g = new BattleGrid();
            g.AddShip(new Ship(1, 0, 3, Orientations.Horizontal));
            IPlayer player = new StupidPlayer();
            */

            /*
            // Dans l'eau !
            player.Play(g);

            // Touche trois fois d'affilée
            player.Play(g);
            player.Play(g);
            player.Play(g);

            // Il doit avoir gagné après trois coups d'affilée
            System.Console.WriteLine(g.GameOver());
            */

            /*
            g.AddShip(new Ship(2, 5, 3, Orientations.Horizontal));
            int comptage = 0;
            g.Draw();
            while (!g.GameOver())
            {
                player.Play(g);
                comptage++;
            }
            System.Console.WriteLine(comptage);
            g.Draw();
            */

            // Création d'une grille avec génération aléatoire puis affichage

            BattleGrid g = new BattleGrid();
            IPlayer player = new StupidPlayer();
            g.NewGrid();
            g.Draw();

            while (!g.GameOver())
            {
                player.Play(g);
            }

            g.Draw();

            Console.Read();
        }