Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Fardale
Prog2
Commits
c4b4e844
Commit
c4b4e844
authored
Mar 13, 2015
by
William Babonnaud
Browse files
Ajout de flip.scala
parents
Changes
1
Hide whitespace changes
Inline
Side-by-side
flip.scala
0 → 100644
View file @
c4b4e844
import
scala.util.Random
import
Array._
import
java.awt.Color
class
flip
{
// Cotés du terrain.
private
var
tailleX
=
5
private
var
tailleY
=
5
// Matrice des boutons.
var
boutons
=
ofDim
[
MonBouton
](
tailleX
,
tailleY
)
// Pour reparamétrer la configuration du terrain
def
reparametrage
(
x
:
Int
,
y
:
Int
)
=
{
tailleX
=
x
tailleY
=
y
boutons
=
ofDim
[
MonBouton
](
tailleX
,
tailleY
)
}
// Pour fixer la couleur d'un bouton.
// Le champ "libre" de MonBouton permettra de représenter la couleur :
// A true, la case est blanche ; à false elle est noire.
def
fixCouleur
(
i
:
Int
,
j
:
Int
)
=
{
if
(
boutons
(
i
)(
j
).
estLibre
)
{
boutons
(
i
)(
j
).
background
=
Color
.
white
}
else
{
boutons
(
i
)(
j
).
background
=
Color
.
black
}
}
// Génération d'une grille de départ aléatoire.
def
initialise
=
{
/* !!! */
for
(
i
<-
0
to
(
tailleX
-
1
))
{
for
(
j
<-
0
to
(
tailleY
-
1
))
{
boutons
(
i
)(
j
)
=
new
MonBouton
(
i
,
j
)
}
}
var
x
=
Random
.
nextBoolean
/* !!! */
for
(
i
<-
0
to
(
tailleX
-
1
))
{
for
(
j
<-
0
to
(
tailleY
-
1
))
{
if
(
x
)
{
boutons
(
i
)(
j
).
chgLibre
}
fixCouleur
(
i
,
j
)
x
=
Random
.
nextBoolean
}
}
}
// Génération aléatoire avec graine
def
initialise
(
n
:
Int
)
=
{
/* !!! */
for
(
i
<-
0
to
(
tailleX
-
1
))
{
for
(
j
<-
0
to
(
tailleY
-
1
))
{
boutons
(
i
)(
j
)
=
new
MonBouton
(
i
,
j
)
}
}
var
ran
=
new
Random
(
n
)
var
x
=
ran
.
nextBoolean
/* !!! */
for
(
i
<-
0
to
(
tailleX
-
1
))
{
for
(
j
<-
0
to
(
tailleY
-
1
))
{
if
(
x
)
{
boutons
(
i
)(
j
).
chgLibre
}
fixCouleur
(
i
,
j
)
x
=
Random
.
nextBoolean
}
}
}
// Modification de la couleur d'un bouton.
def
chgBouton
(
i
:
Int
,
j
:
Int
)
=
{
boutons
(
i
)(
j
).
chgLibre
fixCouleur
(
i
,
j
)
}
// Renvoire true si toutes les cases du terrain sont blanches
def
victoire
:
Boolean
=
{
/* !!! */
for
(
i
<-
0
to
(
tailleX
-
1
))
{
for
(
j
<-
0
to
(
tailleY
-
1
))
{
if
(!
boutons
(
i
)(
j
).
estLibre
)
{
false
}
}
}
true
}
// Conséquence du clic d'un bouton.
def
clique_action
(
i
:
Int
,
j
:
Int
)
:
Boolean
=
{
chgBouton
(
i
,
j
)
if
(
i
>
0
)
{
chgBouton
(
i
-
1
,
j
)
}
if
(
i
<
tailleX
-
1
)
{
chgBouton
(
i
+
1
,
j
)
}
if
(
j
>
0
)
{
chgBouton
(
i
,
j
-
1
)
}
if
(
j
<
tailleY
-
1
)
{
chgBouton
(
i
,
j
+
1
)
}
victoire
}
}
Write
Preview
Supports
Markdown
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