Click here to Skip to main content
15,881,719 members
Home / Discussions / C#
   

C#

 
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 
QuestionUnhandled Argument Exception error in System.Drawing.dll Pin
zs6msd8-Nov-17 0:05
zs6msd8-Nov-17 0:05 
AnswerRe: Unhandled Argument Exception error in System.Drawing.dll Pin
OriginalGriff8-Nov-17 0:28
mveOriginalGriff8-Nov-17 0:28 
Questionlibrary for connecting oracle from c#form Pin
Member 135093657-Nov-17 20:04
Member 135093657-Nov-17 20:04 
AnswerRe: library for connecting oracle from c#form Pin
Pete O'Hanlon7-Nov-17 21:34
mvePete O'Hanlon7-Nov-17 21:34 
GeneralRe: library for connecting oracle from c#form Pin
Member 135093658-Nov-17 21:26
Member 135093658-Nov-17 21:26 
GeneralRe: library for connecting oracle from c#form Pin
Member 135093658-Nov-17 21:26
Member 135093658-Nov-17 21:26 
AnswerRe: library for connecting oracle from c#form Pin
Mycroft Holmes8-Nov-17 14:18
professionalMycroft Holmes8-Nov-17 14:18 
QuestionGetting Data from web : C# failed while Excel works Pin
PozzaVecia5-Nov-17 9:06
PozzaVecia5-Nov-17 9:06 
AnswerRe: Getting Data from web : C# failed while Excel works Pin
PozzaVecia5-Nov-17 9:19
PozzaVecia5-Nov-17 9:19 
QuestionWrite audio to USB Modem via COM Port Pin
ManojKumarShah20174-Nov-17 22:45
ManojKumarShah20174-Nov-17 22:45 
QuestionRe: Write audio to USB Modem via COM Port Pin
Eddy Vluggen4-Nov-17 23:34
professionalEddy Vluggen4-Nov-17 23:34 
AnswerRe: Write audio to USB Modem via COM Port Pin
OriginalGriff4-Nov-17 23:35
mveOriginalGriff4-Nov-17 23:35 
GeneralRe: Write audio to USB Modem via COM Port Pin
ManojKumarShah20175-Nov-17 0:57
ManojKumarShah20175-Nov-17 0:57 
GeneralRe: Write audio to USB Modem via COM Port Pin
OriginalGriff5-Nov-17 1:05
mveOriginalGriff5-Nov-17 1:05 
GeneralRe: Write audio to USB Modem via COM Port Pin
ManojKumarShah20175-Nov-17 2:31
ManojKumarShah20175-Nov-17 2:31 
GeneralRe: Write audio to USB Modem via COM Port Pin
OriginalGriff5-Nov-17 2:40
mveOriginalGriff5-Nov-17 2:40 

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.