Click here to Skip to main content
15,910,083 members
Home / Discussions / C#
   

C#

 
AnswerRe: in c# school management project Pin
jschell15-Nov-17 7:03
jschell15-Nov-17 7:03 
QuestionDetermine Distance between HD Camera and Object Pin
Member 1041074814-Nov-17 4:09
Member 1041074814-Nov-17 4:09 
AnswerRe: Determine Distance between HD Camera and Object Pin
Richard MacCutchan14-Nov-17 4:51
mveRichard MacCutchan14-Nov-17 4:51 
AnswerRe: Determine Distance between HD Camera and Object Pin
Dave Kreskowiak14-Nov-17 5:24
mveDave Kreskowiak14-Nov-17 5:24 
AnswerRe: Determine Distance between HD Camera and Object Pin
Gerry Schmitz15-Nov-17 5:39
mveGerry Schmitz15-Nov-17 5:39 
Questionmy projects link button not work in iphone 6s but desktop work properly why? Pin
Member 1352056914-Nov-17 2:27
Member 1352056914-Nov-17 2:27 
AnswerRe: my projects link button not work in iphone 6s but desktop work properly why? Pin
Pete O'Hanlon14-Nov-17 3:37
mvePete O'Hanlon14-Nov-17 3:37 
AnswerRe: my projects link button not work in iphone 6s but desktop work properly why? Pin
Richard Deeming14-Nov-17 3:37
mveRichard Deeming14-Nov-17 3:37 
AnswerRe: my projects link button not work in iphone 6s but desktop work properly why? Pin
Richard MacCutchan14-Nov-17 4:50
mveRichard MacCutchan14-Nov-17 4:50 
AnswerRe: my projects link button not work in iphone 6s but desktop work properly why? Pin
Gerry Schmitz15-Nov-17 5:42
mveGerry Schmitz15-Nov-17 5:42 
Questionregex Pin
arkiboys14-Nov-17 1:50
arkiboys14-Nov-17 1:50 
AnswerRe: regex Pin
Richard Deeming14-Nov-17 2:08
mveRichard Deeming14-Nov-17 2:08 
AnswerRe: regex Pin
Kenneth Haugland14-Nov-17 2:09
mvaKenneth Haugland14-Nov-17 2:09 
GeneralRe: regex Pin
BillWoodruff14-Nov-17 14:43
professionalBillWoodruff14-Nov-17 14:43 
GeneralRe: regex Pin
Richard Deeming16-Nov-17 7:51
mveRichard Deeming16-Nov-17 7:51 
QuestionBest machine learning AI library to assess multiple inputs with different weights (non-linear) ? Pin
Member 978298312-Nov-17 23:06
Member 978298312-Nov-17 23:06 
AnswerRe: Best machine learning AI library to assess multiple inputs with different weights (non-linear) ? Pin
Gerry Schmitz13-Nov-17 4:44
mveGerry Schmitz13-Nov-17 4:44 
GeneralRe: Best machine learning AI library to assess multiple inputs with different weights (non-linear) ? Pin
Member 978298313-Nov-17 4:59
Member 978298313-Nov-17 4:59 
GeneralRe: Best machine learning AI library to assess multiple inputs with different weights (non-linear) ? Pin
Gerry Schmitz13-Nov-17 5:01
mveGerry Schmitz13-Nov-17 5:01 
Questioncan't find the source of Collection was Modified Exception Pin
Alexander Kindel11-Nov-17 19:46
Alexander Kindel11-Nov-17 19:46 
AnswerRe: can't find the source of Collection was Modified Exception Pin
OriginalGriff11-Nov-17 20:10
mveOriginalGriff11-Nov-17 20:10 
GeneralRe: can't find the source of Collection was Modified Exception Pin
Alexander Kindel11-Nov-17 20:14
Alexander Kindel11-Nov-17 20:14 
GeneralRe: can't find the source of Collection was Modified Exception Pin
OriginalGriff11-Nov-17 20:23
mveOriginalGriff11-Nov-17 20:23 
QuestionHow to simplify my code? Pin
Member 1347915010-Nov-17 12:09
Member 1347915010-Nov-17 12:09 
OK, so I'm creating a simple card game. So far I can move my cards with mouse and this work as it should, but on the other hand I feel that most of my code is wrong because I had to repeat the same method for each object. So I'd like to simplify my code. Tried to use lists, collections and 'foreach' loops, but couldn't make it work. How can I make my code smaller, simpler and more elegant?

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

class MainForm : Form {

	class Card {
		public int Type;
		public Rectangle Position;
		public Bitmap Image;

		public Card(int Type, Rectangle Position) {
			this.Type = Type;
			switch (Type) {
				case 1:
					Image = new Bitmap("1.png");
					break;
				case 2:
					Image = new Bitmap("2.png");
					break;
				case 3:
					Image = new Bitmap("3.png");
					break;
				case 4:
					Image = new Bitmap("4.png");
					break; }
			this.Position = Position; } }

	Card RedCard = new Card(1, new Rectangle(0, 32, 32, 32));
	Card BlueCard = new Card(2, new Rectangle(0, 64, 32, 32));
	Card GreenCard = new Card(3, new Rectangle(0, 96, 32, 32));
	Card YellowCard = new Card(4, new Rectangle(0, 128, 32, 32));
	Card RedCard2 = new Card(1, new Rectangle(0, 160, 32, 32));

	int IsTaken = 0, OffsetX = 0, OffsetY = 0;
	PictureBox Table = new PictureBox();

	void DrawTable(object sender, PaintEventArgs e) {
		Color FirstColor = Color.FromArgb(46, 135, 46);
		e.Graphics.FillRectangle(new SolidBrush(FirstColor), 0, 0, 400, 400); }
		
// So far everything looks OK, but the next method feels wrong, because I have to call e.Graphics.DrawImage for each object of the class 'Card'. Is it possible to do something like: for each object of 'Card' { e.Graphics.DrawImage(objectName.Image, objectName.Position) } ?

	void DrawCards(object sender, System.Windows.Forms.PaintEventArgs e) {
		e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
		e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
		e.Graphics.DrawImage(RedCard.Image, RedCard.Position);
		e.Graphics.DrawImage(BlueCard.Image, BlueCard.Position);
		e.Graphics.DrawImage(GreenCard.Image, GreenCard.Position);
		e.Graphics.DrawImage(YellowCard.Image, YellowCard.Position);
		e.Graphics.DrawImage(RedCard2.Image, RedCard2.Position); }

// Can I simplify this somehow?
		
	int IsCursorOverCard(int X, int Y) {
		if ((X > RedCard.Position.Left) && (X <= RedCard.Position.Right) && (Y > RedCard.Position.Top) && (Y <= RedCard.Position.Bottom))
			return 1;
		else if ((X > BlueCard.Position.Left) && (X <= BlueCard.Position.Right) && (Y > BlueCard.Position.Top) && (Y <= BlueCard.Position.Bottom))
			return 2;
		else if ((X > GreenCard.Position.Left) && (X <= GreenCard.Position.Right) && (Y > GreenCard.Position.Top) && (Y <= GreenCard.Position.Bottom))
			return 3;
		else if ((X > YellowCard.Position.Left) && (X <= YellowCard.Position.Right) && (Y > YellowCard.Position.Top) && (Y <= YellowCard.Position.Bottom))
			return 4;
		else if ((X > RedCard2.Position.Left) && (X <= RedCard2.Position.Right) && (Y > RedCard2.Position.Top) && (Y <= RedCard2.Position.Bottom))
			return 5;
		else
			return 0; }

// I need redrawing method just to put taken card on top of other cards. But do I really need one method for each object? This looks wrong.
		
	void RedrawRedCard(object sender, System.Windows.Forms.PaintEventArgs e) {
		e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
		e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
		e.Graphics.DrawImage(RedCard.Image, RedCard.Position); }

	void RedrawBlueCard(object sender, System.Windows.Forms.PaintEventArgs e) {
		e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
		e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
		e.Graphics.DrawImage(BlueCard.Image, BlueCard.Position); }

	void RedrawGreenCard(object sender, System.Windows.Forms.PaintEventArgs e) {
		e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
		e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
		e.Graphics.DrawImage(GreenCard.Image, GreenCard.Position); }

	void RedrawYellowCard(object sender, System.Windows.Forms.PaintEventArgs e) {
		e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
		e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
		e.Graphics.DrawImage(YellowCard.Image, YellowCard.Position); }

	void RedrawRedCard2(object sender, System.Windows.Forms.PaintEventArgs e) {
		e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
		e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
		e.Graphics.DrawImage(RedCard2.Image, RedCard2.Position); }

// Can I simplify this along with IsCursorOverCard method?
		
	void TakeCard(object sender, MouseEventArgs e) {
		switch (IsCursorOverCard(e.X, e.Y)) {
			case 1:
				this.Table.Paint += new PaintEventHandler(this.RedrawRedCard);
				IsTaken = 1;
				OffsetX = RedCard.Position.X - e.X;
				OffsetY = RedCard.Position.Y - e.Y;
				break;
			case 2:
				this.Table.Paint += new PaintEventHandler(this.RedrawBlueCard);
				IsTaken = 2;
				OffsetX = BlueCard.Position.X - e.X;
				OffsetY = BlueCard.Position.Y - e.Y;
				break;
			case 3:
				this.Table.Paint += new PaintEventHandler(this.RedrawGreenCard);
				IsTaken = 3;
				OffsetX = GreenCard.Position.X - e.X;
				OffsetY = GreenCard.Position.Y - e.Y;
				break;
			case 4:
				this.Table.Paint += new PaintEventHandler(this.RedrawYellowCard);
				IsTaken = 4;
				OffsetX = YellowCard.Position.X - e.X;
				OffsetY = YellowCard.Position.Y - e.Y;
				break;
			case 5:
				this.Table.Paint += new PaintEventHandler(this.RedrawRedCard2);
				IsTaken = 5;
				OffsetX = RedCard2.Position.X - e.X;
				OffsetY = RedCard2.Position.Y - e.Y;
				break; } }

// Same here.
				
	void MoveCard(object sender, MouseEventArgs e) {
		switch (IsTaken) {
			case 0:
				if (IsCursorOverCard(e.X, e.Y) == 0)
					Table.Cursor = Cursors.Default;
				else
					Table.Cursor = Cursors.Hand;
				break;
			case 1:
				RedCard.Position.X = e.X + OffsetX;
				RedCard.Position.Y = e.Y + OffsetY;
				Table.Invalidate();
				break;
			case 2:
				BlueCard.Position.X = e.X + OffsetX;
				BlueCard.Position.Y = e.Y + OffsetY;
				Table.Invalidate();
				break;
			case 3:
				GreenCard.Position.X = e.X + OffsetX;
				GreenCard.Position.Y = e.Y + OffsetY;
				Table.Invalidate();
				break;
			case 4:
				YellowCard.Position.X = e.X + OffsetX;
				YellowCard.Position.Y = e.Y + OffsetY;
				Table.Invalidate();
				break;
			case 5:
				RedCard2.Position.X = e.X + OffsetX;
				RedCard2.Position.Y = e.Y + OffsetY;
				Table.Invalidate();
				break;	} }

// The rest is OK :-)
				
	void PutCard(object sender, MouseEventArgs e) {
		IsTaken = 0; }

	MainForm() {
		this.SuspendLayout();
		this.Width = 1024;
		this.Height = 768;
		this.Controls.Add(this.Table);
		this.Table.Location = new Point(0, 0);
		this.Table.Size = new Size(400, 400);
		this.Table.Paint += new PaintEventHandler(this.DrawTable);
		this.Table.Paint += new PaintEventHandler(this.DrawCards);
		this.Table.MouseDown += new MouseEventHandler(this.TakeCard);
		this.Table.MouseMove += new MouseEventHandler(this.MoveCard);
		this.Table.MouseUp += new MouseEventHandler(this.PutCard);
		this.ResumeLayout(); }

	static void Main() {
		Application.Run(new MainForm()); } }

AnswerRe: How to simplify my code? Pin
Afzaal Ahmad Zeeshan10-Nov-17 12:26
professionalAfzaal Ahmad Zeeshan10-Nov-17 12:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.