Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
5.5 kB
4
Indexable
Never
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 test
{
    public partial class frmBingo : Form
    {
		private int intRows = 5;


		private int intMinHeight = 180;
		private int intMinWidth = 150;

		int intWidth = 0;
		int intHeight = 0;

		private int intTotalCells = 0;

		public bool varTeam = false;

		private int intCalledNumbers;

		private Dictionary<string, Label> dicCards = new
		   Dictionary<string, Label>();

		private Random rndRand = new Random();

		enum ActiveState
		{
			Active = -16744448,    // Green//

			InActive = -16711681   // Cyan//
		}

		public frmBingo()
		{
			InitializeComponent();

			intTotalCells = intRows * intRows;

			startScherm();
		}


		public Label lblLabel()
		{
			intWidth = (tblBingo.Width / intRows) - 2;
			intHeight = (tblBingo.Height / (intRows + 1)) - 2;

			Label lbl = new Label();

			lbl.AutoSize = false;
			lbl.Width = intWidth;
			lbl.Height = intHeight;
			lbl.TextAlign = ContentAlignment.MiddleCenter;
			lbl.BorderStyle = BorderStyle.FixedSingle;
			lbl.Tag = ActiveState.InActive;

			return lbl;
		}
		private void lblClick(System.Object sender, System.EventArgs e)
		{

			Populate(sender);

		}

		private void Populate(System.Object sender)
		{
			Label lbl = (Label)sender;

			if ((ActiveState)lbl.Tag == ActiveState.InActive)
			{
				intCalledNumbers += 1;

				lbl.Tag = ActiveState.Active;
				lbl.BackColor = Color.FromArgb((int)ActiveState.Active);

				lblCurrent.Text = lbl.Text;
				lblAllPicks.Text = lblAllPicks.Text.Insert(0, " . " +
				   lbl.Text);

				if (lblAllPicks.Text.Length > 60)
				{
					int iIndex = lblAllPicks.Text.LastIndexOf(" ");
					lblAllPicks.Text = lblAllPicks.Text.Substring(0,
					   iIndex);
				}

				System.Drawing.Point dp = lbl.Location;

				dp.X += tblBingo.Location.X + 4;
				dp.Y += tblBingo.Location.Y + 4;

			}
		}

		public void startScherm()
		{

			lblCurrent.TextAlign = ContentAlignment.MiddleRight;

			tblBingo.Controls.Clear();
			tblBingo.ColumnStyles.Clear();
			tblBingo.RowStyles.Clear();

			if (tblBingo.Width < intMinWidth)
				tblBingo.Width = intMinWidth;

			if (tblBingo.Height < intMinHeight)
				tblBingo.Height = intMinHeight;

			float sngColumns = System.Convert.ToSingle
			   (100 / (double)intRows);
			float sngRows = System.Convert.ToSingle
			   (100 / (double)(intRows + 1));

			tblBingo.ColumnCount = intRows;
			tblBingo.RowCount = intRows + 1;

			for (int idx = 0; idx <= intRows - 1; idx++)

				tblBingo.ColumnStyles.Add(new ColumnStyle(SizeType
				   .Percent, sngColumns));

			for (int idx = 0; idx <= intRows; idx++)

				tblBingo.RowStyles.Add(new RowStyle(SizeType
				   .Percent, sngRows));

			int intTeller = 0;

			for (int iNum = 0; iNum <= 50; iNum++)
			{
				if (varTeam == true)
				{
					if (iNum % 2 == 0)
					{
						Console.WriteLine(iNum);
						for (int iCol = 0; iCol <= intRows - 1; iCol++)
						{
							Label lbl = lblLabel();

							for (int iRow = 1; iRow <= intRows; iRow++)
							{
								lbl = lblLabel();
								intTeller++;


								tblBingo.Controls.Add(lbl, iCol, iRow);
								if (!dicCards.ContainsKey(lbl.Text = (iNum).ToString()))
								{
									dicCards.Add(lbl.Text, lbl);
								}
							}
						}

						btnReset.PerformClick();
					}
				}

				if (varTeam == false)
				{
					if (iNum % 2 != 0)
					{
						Console.WriteLine(iNum);
						for (int iCol = 0; iCol <= intRows - 1; iCol++)
						{
							Label lbl = lblLabel();

							for (int iRow = 1; iRow <= intRows; iRow++)
							{
								lbl = lblLabel();
								intTeller++;


								tblBingo.Controls.Add(lbl, iCol, iRow);
								if (!dicCards.ContainsKey(lbl.Text = (iNum).ToString()))
								{
									dicCards.Add(lbl.Text, lbl);
								}
							}
						}

						btnReset.PerformClick();
					}
				}
			}

		}

		private void btnPick_Click(object sender, EventArgs e)
		{

			checkWinnaar();

			if (intCalledNumbers >= intTotalCells)
			{

				lblCurrent.Text = "Game over";

				return;

			}

			List<KeyValuePair<string, Label>> lstNotCalled;

			lstNotCalled = (from kvp in dicCards
							where (ActiveState)kvp.Value.Tag ==
							   ActiveState.InActive
							select kvp).ToList();

			if (lstNotCalled.Count > 0)
			{

				int idx = rndRand.Next(lstNotCalled.Count);
				KeyValuePair<string, Label> foo = lstNotCalled[idx];

				Populate(foo.Value);

			}

		}

		private void btnReset_Click(object sender, EventArgs e)
		{

			foreach (KeyValuePair<string, Label> kvp in dicCards)
			{

				kvp.Value.BackColor =
				   Color.FromArgb((int)ActiveState.InActive);
				kvp.Value.Tag = ActiveState.InActive;

			}

			intCalledNumbers = 0;

			lblCurrent.Text = "Reset";
			lblAllPicks.Text = "";

			btnPick.Select();

		}

		private void frmBingo_Shown(object sender, EventArgs e)
		{
			btnPick.PerformClick();
			btnPick.PerformClick();
			btnPick.PerformClick();
			btnPick.PerformClick();
			btnPick.PerformClick();
			btnPick.PerformClick();
			btnPick.PerformClick();
			btnPick.PerformClick();
		}
	}
}