Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Fardale
Prog2
Commits
175daf98
Commit
175daf98
authored
Mar 06, 2015
by
Fardale
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of gitlab.crans.org:arrighi/prog2
parents
ff81a6be
5c1631d2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
0 additions
and
209 deletions
+0
-209
boum.scala~
boum.scala~
+0
-209
No files found.
boum.scala~
deleted
100644 → 0
View file @
ff81a6be
import scala.util.Random
import Array._
import javax.swing.ImageIcon
class Demineur {
// Taille du champ de mine. Par défaut, réglé en "Facile".
private var tailleX = 9
private var tailleY = 9
// Nombre de mines dans le champ. Réglé en "Facile"
private var nb_mines = 10
// Matrice représentant le champ de mines. Ses valeurs sont comprises entre 0 et 9.
// 0-8 : Nombre de mines autour de la case vide.
// 9 : Signale l'emplacement d'une mine sur la case.
// Par défaut, on est réglé sur facile.
private var champ = ofDim[Int](tailleX,tailleY)
// Matrice des boutons associés aux cases (il me semble plus pertinent de l'inclure dans le jeu).
private var boutons = ofDim[MonBouton](tailleX,tailleY)
// Réinitialise les paramètres ci-dessus en cas de changement de difficulté.
def reparametrage (x: Int, y: Int, n: Int) = {
tailleX = x
tailleY = y
nb_mines = n
champ = ofDim[Int](tailleX, tailleY)
boutons = ofDim[MonBouton](tailleX, tailleY)
}
// Pour récupérer les valeurs tailleX et tailleY
def getTailleX = tailleX
def getTailleY = tailleY
// Pour récupérer la matrice de boutons.
def getBoutons = boutons
// (Re)crée un champ de boutons sans toucher aux valeurs
def creation_boutons = {
for (i <- 0 to (tailleX-1)) {
for (j <- 0 to (tailleY-1)) {
boutons(i)(j) = new MonBouton(i,j)
}
}
}
// Initialisation basique d'un champ de mine.
// On crée les boutons ; puis on génère nb_mines positions aléatoires, et on
// incrémente les cases alentour pour chaque mine placée.
def initialise = {
creation_boutons
for (i <- 0 to (tailleX-1)) {
for (j <- 0 to (tailleY-1)) {
champ(i)(j) = 0
}
}
var x = Random.nextInt(tailleX)
var y = Random.nextInt(tailleY)
for (i <- 0 to nb_mines - 1) {
while(champ(x)(y) == 9) {
x = Random.nextInt(tailleX)
y = Random.nextInt(tailleY)
}
champ(x)(y) = 9
if (x != 0 && champ(x-1)(y) != 9) {
champ(x-1)(y) += 1
}
if (x != (tailleX-1) && champ(x+1)(y) != 9) {
champ(x+1)(y) += 1
}
if (y != 0 && champ(x)(y-1) != 9) {
champ(x)(y-1) += 1
}
if (y != (tailleY-1) && champ(x)(y+1) != 9) {
champ(x)(y+1) += 1
}
if (x != 0 && y != 0 && champ(x-1)(y-1) != 9) {
champ(x-1)(y-1) += 1
}
if (x != 0 && y != (tailleY-1) && champ(x-1)(y+1) != 9) {
champ(x-1)(y+1) += 1
}
if (x != (tailleX-1) && y != 0 && champ(x+1)(y-1) != 9) {
champ(x+1)(y-1) += 1
}
if (x != (tailleX-1) && y != (tailleY-1) && champ(x+1)(y+1) != 9) {
champ(x+1)(y+1) += 1
}
}
}
// Même fonction, mais avec la composante "random seed" en paramètre.
def initialise (n: Int) = {
creation_boutons
var ran = new Random(n)
for (i <- 0 to (tailleX-1)) {
for (j <- 0 to (tailleY-1)) {
champ(i)(j) = 0
}
}
var x = ran.nextInt(tailleX)
var y = ran.nextInt(tailleY)
for (i <- 0 to nb_mines) {
while(champ(x)(y) == 9) {
x = ran.nextInt(tailleX)
y = ran.nextInt(tailleY)
}
champ(x)(y) = 9
if (x != 0) {
if (champ(x-1)(y) != 9) { champ(x-1)(y) += 1 }
}
if (x != (tailleX-1)) {
if (champ(x+1)(y) != 9) { champ(x+1)(y) += 1 }
}
if (y != 0) {
if (champ(x)(y-1) != 9) { champ(x)(y-1) += 1 }
}
if (y != (tailleY-1)) {
if (champ(x)(y+1) != 9) { champ(x)(y+1) += 1 }
}
if (x != 0 && y != 0) {
if (champ(x-1)(y-1) != 9) { champ(x-1)(y-1) += 1 }
}
if (x != 0 && y != (tailleY-1)) {
if (champ(x-1)(y+1) != 9) { champ(x-1)(y+1) += 1 }
}
if (x != (tailleX-1) && y != 0) {
if (champ(x+1)(y-1) != 9) { champ(x+1)(y-1) += 1 }
}
if (x != (tailleX-1) && y != (tailleY-1)) {
if (champ(x+1)(y+1) != 9) { champ(x+1)(y+1) += 1 }
}
}
}
def propage(x: Int, y: Int){
if (x > 0) {
if(boutons(x-1)(y).enabled){clique_action(x-1,y)}
if(y > 0){
if(boutons(x-1)(y-1).enabled){clique_action(x-1,y-1)}
}
if(y < tailleY-1){
if(boutons(x-1)(y+1).enabled){clique_action(x-1,y+1)}
}
}
if (x < tailleX-1) {
if(boutons(x+1)(y).enabled){clique_action(x+1,y)}
if(y > 0){
if(boutons(x+1)(y-1).enabled){clique_action(x+1,y-1)}
}
if(y < tailleY-1){
if(boutons(x+1)(y+1).enabled){clique_action(x+1,y+1)}
}
}
if(y<0){
if(boutons(x)(y-1).enabled){clique_action(x,y-1)}
}
if(y<tailleY-1){
if(boutons(x)(y+1).enabled){clique_action(x,y+1)}
}
}
// Définit les conséquences d'un clic sur un bouton.
def clique_action (x: Int, y: Int) = {
champ(x)(y) match {
case 0 => {boutons(x)(y).icon = new ImageIcon(getClass.getResource("case_zero.png"))
boutons(x)(y).enabled = false
boutons(x)(y).disabledIcon = new ImageIcon(getClass.getResource("case_zero.png"))
propage(x,y)}
case 1 => boutons(x)(y).icon = new ImageIcon(getClass.getResource("case_un.png"))
boutons(x)(y).enabled = false
boutons(x)(y).disabledIcon = new ImageIcon(getClass.getResource("case_un.png"))
case 2 => boutons(x)(y).icon = new ImageIcon(getClass.getResource("case_deux.png"))
boutons(x)(y).enabled = false
boutons(x)(y).disabledIcon = new ImageIcon(getClass.getResource("case_deux.png"))
case 3 => boutons(x)(y).icon = new ImageIcon(getClass.getResource("case_trois.png"))
boutons(x)(y).enabled = false
boutons(x)(y).disabledIcon = new ImageIcon(getClass.getResource("case_trois.png"))
case 4 => boutons(x)(y).icon = new ImageIcon(getClass.getResource("case_quatre.png"))
boutons(x)(y).enabled = false
boutons(x)(y).disabledIcon = new ImageIcon(getClass.getResource("case_quatre.png"))
case 5 => boutons(x)(y).icon = new ImageIcon(getClass.getResource("case_cinq.png"))
boutons(x)(y).enabled = false
boutons(x)(y).disabledIcon = new ImageIcon(getClass.getResource("case_cinq.png"))
case 6 => boutons(x)(y).icon = new ImageIcon(getClass.getResource("case_six.png"))
boutons(x)(y).enabled = false
boutons(x)(y).disabledIcon = new ImageIcon(getClass.getResource("case_six.png"))
case 7 => boutons(x)(y).icon = new ImageIcon(getClass.getResource("case_sept.png"))
boutons(x)(y).enabled = false
boutons(x)(y).disabledIcon = new ImageIcon(getClass.getResource("case_sept.png"))
case 8 => boutons(x)(y).icon = new ImageIcon(getClass.getResource("case_huit.png"))
boutons(x)(y).enabled = false
boutons(x)(y).disabledIcon = new ImageIcon(getClass.getResource("case_huit.png"))
case 9 => for (i <- 0 to (tailleX-1)) {
for (j <- 0 to (tailleY-1)) {
if (champ(i)(j) == 9) {
boutons(i)(j).icon = new ImageIcon(getClass.getResource("case_mine.png"))
boutons(i)(j).enabled = false
boutons(i)(j).disabledIcon = new ImageIcon(getClass.getResource("case_mine.png"))
}
}
}
println("Vous avez perdu !")
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment