0% found this document useful (0 votes)
40 views3 pages

Cod Joc Memorue

This document contains the code for a memory game application written in C#. It defines classes and methods to initialize the game, display images randomly on picture boxes, start a timer, handle clicks on the picture boxes to reveal matches, reset the game, and more. Key aspects include using a timer to limit game time, randomly assigning and hiding images, tracking the first guess, and checking for matches between two clicked pictures.

Uploaded by

Diana DD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views3 pages

Cod Joc Memorue

This document contains the code for a memory game application written in C#. It defines classes and methods to initialize the game, display images randomly on picture boxes, start a timer, handle clicks on the picture boxes to reveal matches, reset the game, and more. Key aspects include using a timer to limit game time, randomly assigning and hiding images, tracking the first guess, and checking for matches between two clicked pictures.

Uploaded by

Diana DD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

using

using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
WindowsFormsApplication3.Properties;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private bool _allowClick = true;
private PictureBox _firstGuess;
private readonly Random _random = new Random();
private readonly
Timer _clickTimer = new Timer();
int ticks = 20;
readonly Timer timer = new Timer { Interval = 1000 };
public Form1()
{
InitializeComponent();
SetRandomImages();
HideImages();
StartGameTimer();
_clickTimer.Interval = 1000;
_clickTimer.Tick += _clickTimer_Tick;

}
private PictureBox[] PictureBoxes
{
get { return Controls.OfType<PictureBox>().ToArray(); }
}
private static IEnumerable<Image> Images
{
get
{
return new Image[]
{
Resources.img1,
Resources.img2,
Resources.img3,
Resources.img4,
Resources.img5,
Resources.img6,
Resources.img7,
Resources.img8,
Resources.img0
};
}
}
private void StartGameTimer()
{
timer.Start();
timer.Tick += delegate

ticks--;
if (ticks == -1)
{
timer.Stop();
MessageBox.Show("Timpul s-a scurs.", "Joc de memorie", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
ResetImages();
}
var time = TimeSpan.FromSeconds(ticks);
lblTime.Text = "00:" + time.ToString("ss");
};
}
private void ResetImages()
{
foreach (var pic in PictureBoxes)
{
pic.Tag = null;
pic.Visible = true;
}
HideImages();
SetRandomImages();
ticks = 20;
timer.Start();
}
private void HideImages()
{
foreach (var pic in PictureBoxes)
{
pic.Image = Resources.img0;
//pic.Image=(Image)pic.Tag;
}
}
private PictureBox GetFreeSlot()
{
int num;
do
{
num = _random.Next(0, PictureBoxes.Count());
}while (PictureBoxes[num].Tag != null);
return PictureBoxes[num];
}
private void SetRandomImages()
{
foreach (var image in Images)
{
GetFreeSlot().Tag = image;
GetFreeSlot().Tag = image;
}
}

private void Form1_Load(object sender, EventArgs e)


{
}
private void ClickImage(object sender, EventArgs e)
{
if (!_allowClick) return;
var pic = (PictureBox)sender;
if (_firstGuess == null)
{
_firstGuess = pic;
pic.Image = (Image)pic.Tag;
return;
}
pic.Image = (Image)pic.Tag;
if (pic.Image == _firstGuess.Image && pic != _firstGuess)
{
pic.Visible = _firstGuess.Visible = false;
{
_firstGuess = pic;
}
HideImages();
}
else
{
_allowClick = false;
_clickTimer.Start();
}
_firstGuess = null;
if (PictureBoxes.Any(p => p.Visible)) return;
timer.Stop();
MessageBox.Show("Ai reusit!", "Joc de memorie", MessageBoxButtons.OK,
MessageBoxIcon.Information);
Close();
}
private void _clickTimer_Tick(object sender, EventArgs e)
{
HideImages();
_allowClick = true;
_clickTimer.Stop();
}
}
}

You might also like