Étape 3: Écrire le code
VOICI LE CODE POUR LE JEU. C’est fait en c#.
using System ;
à l’aide de System.Collections.Generic ;
à l’aide de System.ComponentModel ;
using System.Data ; à l’aide de System.Drawing ;
à l’aide de System.Linq ;
using System.Text ;
à l’aide de System.Threading.Tasks ;
using System.Windows.Forms ;
à l’aide de System.Media ;
namespace WindowsFormsApplication1 {public partial class Form1 : Form {Random rnd = new Random() ;
tuiles d’int [] = new int [4] {2, 1,0,3} ;
Rangée de panneau [] = nouveau panneau [4] ;
Panneau de lignes [] [] = new Panel [4] [] ;
SoundPlayer a = new SoundPlayer(WindowsFormsApplication1.Properties.Resources._1) ;
SoundPlayer b = new SoundPlayer(WindowsFormsApplication1.Properties.Resources._2) ;
SoundPlayer c = new SoundPlayer(WindowsFormsApplication1.Properties.Resources._3) ;
SoundPlayer d = new SoundPlayer(WindowsFormsApplication1.Properties.Resources._4) ;
SoundPlayer f = new SoundPlayer(WindowsFormsApplication1.Properties.Resources._5) ;
Random r = new Random() ;
Liste tilesP = gcnew List () ;
public Form1()
{
InitializeComponent() ;
}
private void Form1_Load (object sender, EventArgs e)
{
cela. Shuffle(Tiles) ;
cela. Au premier plan = true ;
cela. FormBorderStyle = FormBorderStyle.None ;
cela. WindowState = FormWindowState.Maximized ;
int FormHeight = ceci. Size.Height ;
int FormWidth = ceci. Size.Width ;
pour (int i = 0; i < 4; i ++) {}
Panneau [] p = nouveau panneau [4] ;
pour (int j = 0; j < 4; j ++)
{
rangée [j] = new Panel() ;
rangée [j]. Size = new Size (FormWidth/4, FormHeight/4) ;
rangée [j]. Location = new Point ((i * FormWidth / 4), (j * FormHeight / 4)) ;
rangée [j]. BackColor = Color.White ;
Si (j == tiles[i]) rangée [j]. BackColor = Color.Black ;
rangée [j]. BorderStyle = BorderStyle.FixedSingle ;
cela. Controls.Add(row[j]) ;
p [j] = row [j] ;
}
les lignes [i] = p ;
}
dessiner les tuiles
}
protected override bool ProcessCmdKey (Réf Message msg, Keys keyData) {}
int FormHeight = ceci. Size.Height ;
int FormWidth = ceci. Size.Width ;
commutateur (keyData)
{
affaire Keys.A :
{
Si (tuiles [3] == 0)
{
a.Stop() ;
b.Stop() ;
c.Stop() ;
d.Stop() ;
f.Stop() ;
a.Play() ;
Random r = new Random() ;
inr int = r.Next (0, 4) ;
int [] s = new int [4] ;
Array.Copy (tuiles, s, 4) ;
pour (int i = 3; i > 0; i--)
{
les tuiles [i] = s [i - 1] ;
}
tuiles [0] = inr ;
pour (int i = 0; i < 4; i ++)
{
pour (int j = 0; j < 4; j ++) {}
Commande p = ceci. GetChildAtPoint (nouveau Point ((i * FormWidth / 4), (j * FormHeight / 4))) ; les lignes [i] [j] = p (panneau) ;
}}
pour (int i = 0; i < 4; i ++)
{
pour (int j = 0; j < 4; j ++)
{
lignes [i] [j]. Location = new Point ((i * FormWidth / 4), ((j + 1) * FormHeight / 4)) ;
}}
pour (int i = 0; i < 4; i ++)
{
les lignes [i] [0] = new Panel() ;
lignes [i] [0]. Size = new Size (FormWidth/4, FormHeight/4) ;
lignes [i] [0]. Location = new Point ((i * FormWidth / 4), (0 * FormHeight / 4)) ;
lignes [i] [0]. BackColor = Color.White ;
Si (j’ai == inr) lignes [i] [0]. BackColor = Color.Black ;
lignes [i] [0]. BorderStyle = BorderStyle.FixedSingle ;
cela. Controls.Add(rows[i][0]) ;
}}
d’autre
{
a.Stop() ;
b.Stop() ;
c.Stop() ;
d.Stop() ;
f.Stop() ;
f.Play() ;
}
retourne la valeur true ;
}
case Keys.S: {}
Si (tuiles [3] == 1)
{
a.Stop() ;
b.Stop() ;
c.Stop() ;
d.Stop() ;
f.Stop() ;
b.Play() ;
inr int = r.Next (0, 4) ;
int [] s = new int [4] ;
Array.Copy (tuiles, s, 4) ;
pour (int i = 3; i > 0; i--)
{
les tuiles [i] = s [i - 1] ;
} tuiles [0] = inr ;
pour (int i = 0; i < 4; i ++)
{
pour (int j = 0; j < 4; j ++)
{
Commande p = ceci. GetChildAtPoint (nouveau Point ((i * FormWidth / 4), (j * FormHeight / 4))) ; les lignes [i] [j] = p (panneau) ;
}
}
pour (int i = 0; i < 4; i ++)
{
pour (int j = 0; j < 4; j ++)
{
lignes [i] [j]. Location = new Point ((i * FormWidth / 4), ((j + 1) * FormHeight / 4)) ;
}
}
pour (int i = 0; i < 4; i ++)
{
les lignes [i] [0] = new Panel() ;
lignes [i] [0]. Size = new Size (FormWidth/4, FormHeight/4) ;
lignes [i] [0]. Location = new Point ((i * FormWidth / 4), (0 * FormHeight / 4)) ;
lignes [i] [0]. BackColor = Color.White ;
Si (j’ai == inr) lignes [i] [0]. BackColor = Color.Black ;
lignes [i] [0]. BorderStyle = BorderStyle.FixedSingle ;
cela. Controls.Add(rows[i][0]) ;
}}
else {}
a.Stop() ;
b.Stop() ;
c.Stop() ;
d.Stop() ;
f.Stop() ;
f.Play() ;
}
retourne la valeur true ;
}
affaire Keys.D :
{
Si (tuiles [3] == 2)
{
a.Stop() ;
b.Stop() ;
c.Stop() ;
d.Stop() ;
f.Stop() ;
c.Play() ;
Random r = new Random() ;
inr int = r.Next (0, 4) ;
int [] s = new int [4] ;
Array.Copy (tuiles, s, 4) ;
pour (int i = 3; i > 0; i--)
{
les tuiles [i] = s [i - 1] ;
}
tuiles [0] = inr ;
pour (int i = 0; i < 4; i ++)
{
pour (int j = 0; j < 4; j ++)
{
Commande p = ceci. GetChildAtPoint (nouveau Point ((i * FormWidth / 4), (j * FormHeight / 4))) ; les lignes [i] [j] = p (panneau) ;
}}
pour (int i = 0; i < 4; i ++)
{
pour (int j = 0; j < 4; j ++)
{
lignes [i] [j]. Location = new Point ((i * FormWidth / 4), ((j + 1) * FormHeight / 4)) ;
}}
pour (int i = 0; i < 4; i ++)
{
les lignes [i] [0] = new Panel() ;
lignes [i] [0]. Size = new Size (FormWidth/4, FormHeight/4) ;
lignes [i] [0]. Location = new Point ((i * FormWidth / 4), (0 * FormHeight / 4)) ;
lignes [i] [0]. BackColor = Color.White ;
Si (j’ai == inr) lignes [i] [0]. BackColor = Color.Black ;
lignes [i] [0]. BorderStyle = BorderStyle.FixedSingle ;
cela. Controls.Add(rows[i][0]) ;
} }
d’autre
{
a.Stop() ;
b.Stop() ;
c.Stop() ;
d.Stop() ;
f.Stop() ;
f.Play() ;
}
retourne la valeur true ;
} / / flèche haut enfoncé
case Keys.F: {}
Si (tuiles [3] == 3)
{
a.Stop() ;
b.Stop() ;
c.Stop() ;
d.Stop() ;
f.Stop() ;
d.Play() ;
Random r = new Random() ;
inr int = r.Next (0, 4) ;
int [] s = new int [4] ;
Array.Copy (tuiles, s, 4) ;
pour (int i = 3; i > 0; i--)
{
les tuiles [i] = s [i - 1] ;
}
tuiles [0] = inr ;
pour (int i = 0; i < 4; i ++)
{
pour (int j = 0; j < 4; j ++)
{
Commande p = ceci. GetChildAtPoint (nouveau Point ((i * FormWidth / 4), (j * FormHeight / 4))) ;
les lignes [i] [j] = p (panneau) ;
}}
pour (int i = 0; i < 4; i ++)
{
pour (int j = 0; j < 4; j ++)
{
lignes [i] [j]. Location = new Point ((i * FormWidth / 4), ((j + 1) * FormHeight / 4)) ;
}}
pour (int i = 0; i < 4; i ++)
{
les lignes [i] [0] = new Panel() ;
lignes [i] [0]. Size = new Size (FormWidth/4, FormHeight/4) ;
lignes [i] [0]. Location = new Point ((i * FormWidth / 4), (0 * FormHeight / 4)) ;
lignes [i] [0]. BackColor = Color.White ;
Si (j’ai == inr) lignes [i] [0]. BackColor = Color.Black ;
lignes [i] [0]. BorderStyle = BorderStyle.FixedSingle ;
cela. Controls.Add(rows[i][0]) ;
} }
d’autre
{
a.Stop() ;
b.Stop() ;
c.Stop() ;
d.Stop() ;
f.Stop() ;
f.Play() ;
}
retourne la valeur true ;
} }
rendement de base. ProcessCmdKey (Réf msg, keyData) ;
}
Sub Shuffle (T [] array)
{
int n = array. Longueur ; pour (int i = 0; i < n; i ++)
{
NextDouble retourne un nombre aléatoire entre 0 et 1.
// ... Il est équivalent à Math.Random () en Java.
int r = i + (int) (RND. NextDouble() * (n - i)) ;
T t = array [r] ;
tableau [r] = tableau [i] ;
tableau [i] = t ;
} } } }