Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionPublic Class frmBombe #Region "Classe clsCase" Private Class clsCase Private pPosition As Point Private pTaille As Size Private pEntourage As Boolean = False Private pFond As Boolean = False Private pClicAutorise As Boolean = True Private pContientUneBombe As Boolean = False Private pTexte As String = "" Public Sub New() End Sub Public Property Position() As Point Get Return pPosition End Get Set(ByVal value As Point) pPosition = value End Set End Property Public Property Taille() As Size Get Return pTaille End Get Set(ByVal value As Size) pTaille = value End Set End Property Public Property Entourage() As Boolean Get Return pEntourage End Get Set(ByVal value As Boolean) pEntourage = value End Set End Property Public Property Fond() As Boolean Get Return pFond End Get Set(ByVal value As Boolean) pFond = value End Set End Property Public Property ClicAutorise() As Boolean Get Return pClicAutorise End Get Set(ByVal value As Boolean) pClicAutorise = value End Set End Property Public Property ContientUneBombe() As Boolean Get Return pContientUneBombe End Get Set(ByVal value As Boolean) pContientUneBombe = value End Set End Property Public Property Texte() As String Get Return pTexte End Get Set(ByVal value As String) pTexte = value End Set End Property Public Function ClicDedans(ByVal pt As Point) As Boolean Dim rect As New Rectangle(Position, Taille) If rect.Contains(pt) True And ClicAutorise True Then Return True End Function Public Sub Dessiner(ByVal grf As Graphics) If Fond = True Then Dim rect As Rectangle = New Rectangle(Position, Taille) rect.Inflate(-2, -2) grf.FillRectangle(Brushes.Turquoise, rect) End If If Entourage = True Then grf.DrawRectangle(Pens.Black, New Rectangle(Position, Taille)) End If If Texte <> "" Then Dim strFormat As New StringFormat() strFormat.Alignment = StringAlignment.Center strFormat.LineAlignment = StringAlignment.Center Dim police As New Font("Microsoft Sans Serif", 12, FontStyle.Bold, GraphicsUnit.Point, CType(0, Byte)) grf.DrawString(Texte, police, Brushes.Black, New Rectangle(Position, Taille), strFormat) End If End Sub End Class #End Region Private pCase(7, 7) As clsCase Private pCaseEntree As String Private pcaseSortie As String Private pNombreTirs As Integer Private pNombreEssais As Integer Private pNombreBombesTrouvees As Integer Private Sub frmBombe_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Définir la feuille Me.Size = New Size(440, 460) Me.Text = "Bombe !" Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedDialog Me.MaximizeBox = False Me.MinimizeBox = False Me.SetStyle((ControlStyles.DoubleBuffer Or (ControlStyles.AllPaintingInWmPaint Or ControlStyles.ResizeRedraw)), True) 'Préparer le jeu Initialiser() End Sub Private Sub Initialiser() 'Définir le quadrillage Dim cTaille As New Size(50, 50) For i As Integer = 0 To 7 For j As Integer = 0 To 7 Dim cCase As New clsCase cCase.Taille = cTaille cCase.Position = New Point(10 + i * cTaille.Width, 10 + j * cTaille.Height) If i > 0 And i < 7 And j > 0 And j < 7 Then cCase.Entourage = True ElseIf (i <> 0 Or j <> 0) And (i <> 0 Or j <> 7) And (i <> 7 Or j <> 0) And (i <> 7 Or j <> 7) Then cCase.Fond = True Else cCase.ClicAutorise = False End If pCase(i, j) = cCase Next Next 'Choisir les 3 cases qui contiendront une bombe Dim pHasard As New Random For i As Integer = 1 To 3 Dim x As Integer Dim y As Integer Do x = pHasard.Next(1, 6) y = pHasard.Next(1, 6) Loop Until pCase(x, y).ContientUneBombe = False pCase(x, y).ContientUneBombe = True Next 'Initialisation des variables pNombreEssais = 0 pNombreTirs = 0 pNombreBombesTrouvees = 0 End Sub Private Sub frmBombe_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint Dim bmp As New Bitmap(Me.Width, Me.Height) Dim grf As Graphics = Graphics.FromImage(bmp) 'Dessiner les cases For i As Integer = 0 To 7 For j As Integer = 0 To 7 pCase(i, j).Dessiner(grf) Next Next 'Basculer à l'écran et purger e.Graphics.DrawImage(bmp, 0, 0) grf.Dispose() bmp.Dispose() End Sub Private Sub frmBombe_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown For i As Integer = 0 To 7 For j As Integer = 0 To 7 If pCase(i, j).ClicDedans(e.Location) = True Then If pCase(i, j).Fond = True Then 'Un tir If pCaseEntree <> "" And pcaseSortie <> "" Then 'On efface l'essai précédent pCase(Asc(pCaseEntree.Substring(0, 1)) - 64, Integer.Parse(pCaseEntree.Substring(1, 1))).Texte = "" pCase(Asc(pcaseSortie.Substring(0, 1)) - 64, Integer.Parse(pcaseSortie.Substring(1, 1))).Texte = "" End If 'Incrémenter le nombre tirs (on peut penser à afficher cette variable dans un label au bas de l'écran) pNombreTirs += 1 'Nouvelle case "IN" pCaseEntree = Chr(64 + i) & Chr(48 + j) pcaseSortie = "" pCase(i, j).Texte = "IN" 'Définir un sens de progression pour le tir Dim x As Integer = i Dim y As Integer = j Dim progressionH As Integer = 0 Dim progressionV As Integer = 0 If i 0 Then progressionH +1 If i 7 Then progressionH -1 If j 0 Then progressionV +1 If j 7 Then progressionV -1 Do x += progressionH y += progressionV If pCase(x, y).ContientUneBombe = True Then 'La case en face contient une bombe progressionH = progressionH * (-1) progressionV = progressionV * (-1) x += progressionH y += progressionV End If 'Dévier la trajectoire si passe à proximité d'une bombe 'Pas encore fait If x 0 Or y 0 Or x = 7 Or y = 7 Then 'Progression terminée pcaseSortie = Chr(64 + x) & Chr(48 + y) If pCase(x, y).Texte = "" Then pCase(x, y).Texte = "OUT" Else pCase(x, y).Texte &= vbCrLf & "OUT" End If End If Loop Until pcaseSortie <> "" ElseIf pCase(i, j).Entourage = True Then 'Recherche d'un bombe If pCase(i, j).Texte = "" Then 'Incrémenter le nombre d'essais (on peut penser à afficher cette variable dans un label au bas de l'écran) pNombreEssais += 1 'Y a-t-il une bombe à cet endroit ? If pCase(i, j).ContientUneBombe = False Then 'Un essai pour rien pCase(i, j).Texte = "." Else pCase(i, j).Texte = "X" Beep() pNombreBombesTrouvees += 1 If pNombreBombesTrouvees = 3 Then Me.Refresh() Dim message As String = "Vous avez gagné en utilisant " & pNombreTirs.ToString & " tir(s) et " & pNombreEssais.ToString & " essai(s)." If MessageBox.Show(message & vbCrLf & "Voulez-vous rejouer ?", "Gagné", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then Initialiser() Else Application.Exit() End If End If End If End If End If Me.Refresh() Return End If Next Next End Sub End Class
Public Class frmBombe #Region "Classe clsCase" Private Class clsCase Private pPosition As Point Private pTaille As Size Private pEntourage As Boolean = False Private pFond As Boolean = False Private pClicAutorise As Boolean = True Private pContientUneBombe As Boolean = False Private pTexte As String = "" Public Sub New() End Sub Public Property Position() As Point Get Return pPosition End Get Set(ByVal value As Point) pPosition = value End Set End Property Public Property Taille() As Size Get Return pTaille End Get Set(ByVal value As Size) pTaille = value End Set End Property Public Property Entourage() As Boolean Get Return pEntourage End Get Set(ByVal value As Boolean) pEntourage = value End Set End Property Public Property Fond() As Boolean Get Return pFond End Get Set(ByVal value As Boolean) pFond = value End Set End Property Public Property ClicAutorise() As Boolean Get Return pClicAutorise End Get Set(ByVal value As Boolean) pClicAutorise = value End Set End Property Public Property ContientUneBombe() As Boolean Get Return pContientUneBombe End Get Set(ByVal value As Boolean) pContientUneBombe = value End Set End Property Public Property Texte() As String Get Return pTexte End Get Set(ByVal value As String) pTexte = value End Set End Property Public Function ClicDedans(ByVal pt As Point) As Boolean Dim rect As New Rectangle(Position, Taille) If rect.Contains(pt) True And ClicAutorise True Then Return True End Function Public Sub Dessiner(ByVal grf As Graphics) If Fond = True Then Dim rect As Rectangle = New Rectangle(Position, Taille) rect.Inflate(-2, -2) grf.FillRectangle(Brushes.Turquoise, rect) End If If Entourage = True Then grf.DrawRectangle(Pens.Black, New Rectangle(Position, Taille)) End If If Texte <> "" Then Dim strFormat As New StringFormat() strFormat.Alignment = StringAlignment.Center strFormat.LineAlignment = StringAlignment.Center Dim police As New Font("Microsoft Sans Serif", 12, FontStyle.Bold, GraphicsUnit.Point, CType(0, Byte)) grf.DrawString(Texte, police, Brushes.Black, New Rectangle(Position, Taille), strFormat) End If End Sub End Class #End Region Private pCase(7, 7) As clsCase Private pCaseEntree As String Private pcaseSortie As String Private pNombreTirs As Integer Private pNombreEssais As Integer Private pNombreBombesTrouvees As Integer Private Sub frmBombe_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Définir la feuille Me.Size = New Size(440, 460) Me.Text = "Bombe !" Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedDialog Me.MaximizeBox = False Me.MinimizeBox = False Me.SetStyle((ControlStyles.DoubleBuffer Or (ControlStyles.AllPaintingInWmPaint Or ControlStyles.ResizeRedraw)), True) 'Préparer le jeu Initialiser() End Sub Private Sub Initialiser() 'Définir le quadrillage Dim cTaille As New Size(50, 50) For i As Integer = 0 To 7 For j As Integer = 0 To 7 Dim cCase As New clsCase cCase.Taille = cTaille cCase.Position = New Point(10 + i * cTaille.Width, 10 + j * cTaille.Height) If i > 0 And i < 7 And j > 0 And j < 7 Then cCase.Entourage = True ElseIf (i <> 0 Or j <> 0) And (i <> 0 Or j <> 7) And (i <> 7 Or j <> 0) And (i <> 7 Or j <> 7) Then cCase.Fond = True Else cCase.ClicAutorise = False End If pCase(i, j) = cCase Next Next 'Choisir les 3 cases qui contiendront une bombe Dim pHasard As New Random For i As Integer = 1 To 3 Dim x As Integer Dim y As Integer Do x = pHasard.Next(1, 6) y = pHasard.Next(1, 6) Loop Until pCase(x, y).ContientUneBombe = False pCase(x, y).ContientUneBombe = True 'La ligne ci-dessous permet de faire apparaître les bombes. Utile pour les tests 'pCase(x, y).Texte = "X" Next 'Initialisation des variables pNombreEssais = 0 pNombreTirs = 0 pNombreBombesTrouvees = 0 End Sub Private Sub frmBombe_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint Dim bmp As New Bitmap(Me.Width, Me.Height) Dim grf As Graphics = Graphics.FromImage(bmp) 'Dessiner les cases For i As Integer = 0 To 7 For j As Integer = 0 To 7 pCase(i, j).Dessiner(grf) Next Next 'Basculer à l'écran et purger e.Graphics.DrawImage(bmp, 0, 0) grf.Dispose() bmp.Dispose() End Sub Private Sub frmBombe_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown For i As Integer = 0 To 7 For j As Integer = 0 To 7 If pCase(i, j).ClicDedans(e.Location) = True Then If pCase(i, j).Fond = True Then 'Un tir If pCaseEntree <> "" And pcaseSortie <> "" Then 'On efface l'essai précédent pCase(Asc(pCaseEntree.Substring(0, 1)) - 64, Integer.Parse(pCaseEntree.Substring(1, 1))).Texte = "" pCase(Asc(pcaseSortie.Substring(0, 1)) - 64, Integer.Parse(pcaseSortie.Substring(1, 1))).Texte = "" End If 'Incrémenter le nombre de tirs (on peut penser à afficher cette variable dans un label au bas de l'écran) pNombreTirs += 1 'Nouvelle case "IN" pCaseEntree = Chr(64 + i) & Chr(48 + j) pcaseSortie = "" pCase(i, j).Texte = "IN" 'Définir un sens de progression pour le tir Dim orientation As String = "" 'Nord Est Sud Ouest If i 0 Then orientation "E" If i 7 Then orientation "O" If j 0 Then orientation "S" If j 7 Then orientation "N" Dim pt As New Point(i, j) Do pt = Avancer(pt, orientation, 1) If pCase(pt.X, pt.Y).ContientUneBombe = True Then 'Tourner de 180° (demi-tour) si le tir rencontre une bombe orientation = Tourner(orientation, 180) pt = Avancer(pt, orientation, 1) Else 'On revient à la case précédente pt = Avancer(pt, orientation, -1) 'Tourner de 90° ou de 270° si le tir passe à proximité d'une bombe Select Case orientation Case "N" If pCase(pt.X - 1, pt.Y).ContientUneBombe = True Then orientation = Tourner(orientation, 90) ElseIf pCase(pt.X + 1, pt.Y).ContientUneBombe = True Then orientation = Tourner(orientation, 270) End If Case "E" If pCase(pt.X, pt.Y - 1).ContientUneBombe = True Then orientation = Tourner(orientation, 90) ElseIf pCase(pt.X, pt.Y + 1).ContientUneBombe = True Then orientation = Tourner(orientation, 270) End If Case "S" If pCase(pt.X + 1, pt.Y).ContientUneBombe = True Then orientation = Tourner(orientation, 90) ElseIf pCase(pt.X - 1, pt.Y).ContientUneBombe = True Then orientation = Tourner(orientation, 270) End If Case "O" If pCase(pt.X, pt.Y + 1).ContientUneBombe = True Then orientation = Tourner(orientation, 90) ElseIf pCase(pt.X, pt.Y - 1).ContientUneBombe = True Then orientation = Tourner(orientation, 270) End If End Select pt = Avancer(pt, orientation, 1) End If If pt.X 0 Or pt.Y 0 Or pt.X = 7 Or pt.Y = 7 Then 'Progression terminée, le tir est ressorti pcaseSortie = Chr(64 + pt.X) & Chr(48 + pt.Y) If pCase(pt.X, pt.Y).Texte = "" Then pCase(pt.X, pt.Y).Texte = "OUT" Else pCase(pt.X, pt.Y).Texte &= vbCrLf & "OUT" End If End If Loop Until pcaseSortie <> "" ElseIf pCase(i, j).Entourage = True Then 'Recherche d'un bombe If pCase(i, j).Texte = "" Then 'Incrémenter le nombre d'essais (on peut penser à afficher cette variable dans un label au bas de l'écran) pNombreEssais += 1 'Y a-t-il une bombe à cet endroit ? If pCase(i, j).ContientUneBombe = False Then 'Un essai pour rien pCase(i, j).Texte = "." Else pCase(i, j).Texte = "X" Beep() pNombreBombesTrouvees += 1 If pNombreBombesTrouvees = 3 Then Me.Refresh() Dim message As String = "Vous avez gagné en utilisant " & pNombreTirs.ToString & " tir(s) et " & pNombreEssais.ToString & " essai(s)." If MessageBox.Show(message & vbCrLf & "Voulez-vous rejouer ?", "Gagné", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then Initialiser() Else Application.Exit() End If End If End If End If End If Me.Refresh() Return End If Next Next End Sub Private Function Avancer(ByVal pt As Point, ByVal orientation As String, ByVal sens As Integer) As Point Select Case orientation Case "N" : pt.Offset(0, -sens) Case "E" : pt.Offset(+sens, 0) Case "S" : pt.Offset(0, +sens) Case "O" : pt.Offset(-sens, 0) End Select Return pt End Function Private Function Tourner(ByVal orientation As String, ByVal valeur As Integer) As String Dim pointsCardinaux As String = "NESONESO" Dim positionDepart As Integer = pointsCardinaux.IndexOf(orientation) positionDepart += valeur / 90 Return pointsCardinaux.Substring(positionDepart, 1) End Function End Class