1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Memory
{
public partial class Form1 : Form
{
private const int MAX_LINE = 4;
private const int MAX_COL = 4;
//String[] tabs = new String[MAX_LINE * MAX_COL];
int[] tab = { 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 };
static DataGridViewCell clique1 =null;
static DataGridViewCell clique2 =null;
int cpt;
static List<Point> lstFound = new List<Point>();
public Form1()
{
InitializeComponent();
}
private void createGrid()
{
DataTable dt = new DataTable();
for (int row = 0; row < MAX_LINE; row++)
dt.Rows.Add();
for (int col = 0; col < MAX_COL; col++)
dt.Columns.Add();
grid.DataSource = dt;
for (int col = 0; col < MAX_COL; col++)
grid.Columns[col].Width = (grid.Width / MAX_COL);
for (int row = 0; row < MAX_LINE; row++)
grid.Rows[row].Height = grid.Height / MAX_LINE;
}
private void Form1_Load(object sender, EventArgs e)
{
createGrid();
}
private void bnvp_Click(object sender, EventArgs e)
{
tabrandom();
tabremplissage();
}
private void tabremplissage()
{
//Random aleatoire = new Random();//
int i = 0;
for (int col = 0; col < MAX_COL; col++)
{
for (int ligne = 0; ligne < MAX_LINE; ligne++)
{
grid.Rows[ligne].Cells[col].Value = tab[i];
i++;
}
}
}
public void tabrandom()
{
Random aleatoire = new Random();
int ind1;
int ind2;
for (int i = 0; i <= 200; i++)
{
ind1 = aleatoire.Next(0, 16);
ind2 = aleatoire.Next(0, 16);
Swap(ind1,ind2);
}
}
private void Swap(int ind1,int ind2)
{
int tabswap;
tabswap = tab[ind1];
tab[ind1] = tab[ind2];
tab[ind2] = tabswap;
}
private void label2_Click(object sender, EventArgs e)
{
}
private void lsc1_Click(object sender, EventArgs e)
{
}
private void bquitter_Click(object sender, EventArgs e)
{
Close();
}
private void grid_CellClick(object sender, DataGridViewCellEventArgs e)
{
int ligne = e.RowIndex;
int col = e.ColumnIndex;
int score=1;
DataGridViewCell cell = this.grid.Rows[ligne].Cells[col];
if (clique1 == null && clique2 == null) //premiere fois qu'on clique
{
clique1 = cell; ;
clique1.Style.BackColor = Color.Red;
grid.Refresh();
}
else if(clique1 !=null && clique2 == null && clique1 !=clique2)
{
clique2 = cell;
clique2.Style.BackColor = Color.Red;
if (clique1.Value == clique2.Value)
{
//desactiver le clik
// clique1.ReadOnly = true;
// clique2.ReadOnly = true;
clique2.Style.BackColor = Color.Red;
clique1.Style.BackColor = Color.Red;
lstFound.Add(new Point(ligne, col));
grid.Refresh();
grid.ClearSelection();
}
else
{
clique1.Style.BackColor = Color.White;
clique2.Style.BackColor = Color.White;
}
grid.ClearSelection();
grid.Refresh();
clique1 = null;
clique2 = null;
}
foreach (Point p in lstFound)
{
// this.grid.Rows[p.X].Cells[p.Y].Style.BackColor = Color.Red;
}
// grid.Refresh();
// grid.Rows[ligne].Cells[col].Style.ForeColor = Color.Black;
}
private void lsc2_Click(object sender, EventArgs e)
{
}
private void grid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
} |
Partager