-->

/* file:Mainform.cs

* Benutzer: kner

* Memoryspiel

*/

using System;

using System.Windows.Forms;


namespace pMemory

{

public partial class MainForm

{

[STAThread]

public static void Main(string[] args)

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new MainForm());

}

public MainForm()

{

InitializeComponent();

Game game = new Game(2,3,imageList1,this);

}

}}




/* File Game.cs

* Kner 2006

* Date: 05.12.2006

*/

using System;

using System.Windows.Forms;

using System.Collections;


namespace pMemory

{

/// <summary>

/// Description of Class1.

/// </summary>

public class Game

{

private Display display;

public Game(int Cols, int Rows, ImageList il, MainForm f)

{

display = new Display(Rows*Cols/2+5, Rows, Cols);

display.Clear();

f.Controls.Add(display);

/* Bildliste aList aufbauen mit jeweils zwei gleichen Bildnummern

* die verwürfelt sind; Bildnummern beginnen bei 1

*/

ArrayList aList = new ArrayList ();

for (int i=0; i<Cols*Rows/2; i++) {

aList.Add (i+1);

aList.Add(i+1);

}

//Shuffle (Gegenteil von Sort) verwürfelt die Arrayliste

Random rand = new Random ();

int idx;

Object h;

for (int i = 1; i < aList.Count; ++i) {

idx = rand.Next(i+1); //Next(3) liefert eine Zufallszahl zwischen 0 und 2

//Dreieckstausch

h = aList[i];

aList[i] = aList[idx];

aList[idx] = h;

}

// Karten erzeugen und am Formular montieren

int nr=0;

for (int i= 0; i<Rows; i++)

for (int j=0; j<Cols; j++)

{

Card c = new Card((int)aList[nr],0, il, display);

c.Width = f.ClientRectangle.Width / Cols;

c.Height = (f.ClientRectangle.Height -20) / Rows;

c.Top = i*c.Height+20;

c.Left = j*c.Width;

f.Controls.Add(c);

nr++;

}

}

}

}




/* file: Card.cs

* Benutzer: kner

*/

using System.Windows.Forms;

namespace pMemory

{

public class Card:Label

{ private int backImg;

private int frontImg;

private static bool secondClick = false;

private static bool clickAble = true;

private static Card lastClicked = null;

private static ImageList CardImages;

private static Display display;

private Timer timeout = new Timer();

public Card(int f, int b, ImageList il, Display display)

{

Card.display = display;

backImg = b;

frontImg = f;

CardImages = il;

BorderStyle = BorderStyle.FixedSingle;

timeout.Interval = 1000;

Image = CardImages.Images[backImg];

this.Name = "Card"+f.ToString()+b.ToString();

timeout.Tick += new System.EventHandler(timeoutReached);

Click += new System.EventHandler(clicked);

}

private void turn(){

this.Image = CardImages.Images[frontImg];

}

private void hide(){

this.Image = CardImages.Images[backImg];

clickAble = true;

}

private void show(){

turn();

if (secondClick){

if (lastClicked.frontImg == frontImg)

display.incPairs();

else {

// nur umdrehen, wenn falsch

clickAble = false;

timeout.Start();

lastClicked.timeout.Start();

display.incTrials();

}

}

secondClick = !secondClick;

lastClicked = this; //merken, wer zuletzt geklickt worden ist

}

private void timeoutReached(object sender, System.EventArgs e){

timeout.Stop();

hide();

}

private void clicked(object sender, System.EventArgs e){

if (clickAble) show();

}

}}


/* file: Display.cs

* User: Kner 09.12.2006

*/

namespace pMemory

{

public class Display:System.Windows.Forms.Label

{

int trials;

int pairs;

int MaxTrials;

int MaxPairs;

public Display(int maxTrials, int Rows, int Cols){

MaxTrials = maxTrials;

MaxPairs = Rows*Cols / 2;

Height = 20;

}

public void Clear(){

trials=0; pairs=0;

DisplayPoints();

}

public void incTrials(){

trials++;

if (trials>MaxTrials && pairs < MaxPairs) Text = "Looser";

else DisplayPoints();

}

public void incPairs(){

trials++;

pairs++;

if (pairs>=MaxPairs) Text = "Winner";

else DisplayPoints();

}

public void DisplayPoints(){

Text = trials.ToString()+":"+pairs.ToString();

}

}}