Hi guys..
I had to make scrabble as a course project .. i chose winforms .. there is a problem now .. i generated a 2d array of labels to be used as the scrabble board ... size of the array is 15 * 15 .. which means i had to register 225 action listener ( event handler ) functions ... all of them are doing the same thing .. i just wanted to ask whether i can shorten the massive amount of event handling functions using some trick ?? im uploading the project code ...
coding is done in Form1.cs file ..
Any help is appreciated ..
Regards ,
Mohsin
Ouch! Didn't really expect to find about 3000 lines of code in your attached file :-) This must have been quite a task to write all these lines that in many cases are identical with only different parameter values. This is certainly a very bad approach. Can you send me complete solution (including all the images) to my email and I will try to rewrite your code to show you how this needs to be done.
yep ASAP i ll send u the complete code...
code complete...
u ll need to change the paths for pics .. ok ..
and this is my first gui application , so plz give me some feed back and tips as well..
is that good enuff for a student of sophomore level ?
Ok, I finally managed to prepare some sample on how things should be done for your action listeners and to avoid typing unnecessary lines of code and I came up with this:
This is my Board class that represents the playing board:
Code:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Windows.Forms; namespace Scrabble { public class Board { private LabelCell[,] m_cells = null; private IDictionary<string[], string> specialCellsDic = new Dictionary<string[], string>(); public Board(Control boardContainer) { Initialize(boardContainer); } private void InitCellsDic() { specialCellsDic.Add(Settings.TwoLetters, Settings.TwoLettersImagePath); specialCellsDic.Add(Settings.ThreeLetters, Settings.ThreeLettersImagePath); specialCellsDic.Add(Settings.TwoWords, Settings.TwoWordsImagePath); specialCellsDic.Add(Settings.ThreeWords, Settings.ThreeWordsImagePath); } public LabelCell[,] Cells { get { return this.m_cells; } } public void AddCell(LabelCell cell, Point position) { if (cell != null && position != null) if (m_cells != null) { m_cells[position.X, position.Y] = cell; } else { m_cells = new LabelCell[Settings.BoardSize, Settings.BoardSize]; AddCell(cell, position); } } public static int[] GetCellPosition(string encoded) { if (!string.IsNullOrEmpty(encoded)) { return new int[] { (Convert.ToChar(encoded.Substring(0, 1))) - 65, (Convert.ToChar(encoded.Substring(1, 1))) - 65 }; } return null; } public void Initialize(Control board_ctrl) { InitCellsDic(); InitSpecialCells(board_ctrl); InitNormalCells(board_ctrl); } public void InitSpecialCells(Control board_ctrl) { foreach (KeyValuePair<string[], string> kv in specialCellsDic) { foreach (string enc in kv.Key) { int[] pos = GetCellPosition(enc); board_ctrl.Controls.Add(CreateLabel(Image.FromFile(kv.Value), pos[0], pos[1])); } } } public void InitNormalCells(Control board_ctrl) { for (int i = 0; i < Settings.BoardSize; i++) { for (int j = 0; j < Settings.BoardSize; j++) { if (m_cells[i, j] != null) continue; board_ctrl.Controls.Add(CreateLabel(Image.FromFile(Settings.StarImagePath), i, j)); } } } public Label CreateLabel(Image image, int x, int y) { LabelCell label = new LabelCell(x, y); label.Image = image; label.Size = Settings.CellSize; label.Location = new Point(x * Settings.CellSize.Width, y * Settings.CellSize.Width); AddCell(label, new Point(x, y)); return label; } } public static class Settings { /*Encoded locations explanation: A = 0, B = 1, C = 2, D = 3, E = 4, F = 5, G = 6, H = 7, I = 8, J = 9, K = 10, L = 11, M = 12, N = 13, O = 14 AA = {0,0}, BN = {1,13},..*/ public static string[] ThreeWords = { "AA", "AH", "AO", "HA", "HO", "OA", "OH", "OO" }; public static string[] TwoWords = { "BB", "CC", "DD", "EE", "BN", "CM", "DL", "EK", "NB", "MC", "LD", "KE", "NN", "MM", "LL", "KK" }; public static string[] TwoLetters = { "DA", "LA", "GC", "IC", "AD", "HD", "OD", "CG", "GG", "IG", "MG", "DH", "LH", "CI", "GI", "II", "MI", "AL", "HL", "OL", "GM", "IM", "DM", "LM" }; public static string[] ThreeLetters = { "BF", "BJ", "FB", "FF", "FJ", "FN", "JB", "JF", "JJ", "JN", "NF", "NJ"}; public static Size CellSize = new Size(40, 40); public static int BoardSize = 15; public static string StarImagePath = "star.bmp"; public static string TwoWordsImagePath = "dw.bmp"; public static string ThreeWordsImagePath = "tw.bmp"; public static string TwoLettersImagePath = "dl.bmp"; public static string ThreeLettersImagePath = "tl.bmp"; } }
This is LabelCell class that represents a cell (label in your project):
Code:using System; using System.Windows.Forms; namespace Scrabble { public class LabelCell : Label { private int x; private int y; public LabelCell() { } public LabelCell(int x, int y) { this.x = x; this.y = y; } public int X { get { return this.x; } set { this.x = value; } } public int Y { get { return this.y; } set { this.y = value; } } } }
This is my Form class that acts as a container for a playing board:
Code:using System; using System.Drawing; using System.Windows.Forms; namespace Scrabble { public partial class Form1 : Form { private Board m_board; public Form1() { InitializeComponent(); InitializeBoard(); } private void InitializeBoard() { m_board = new Board(this); SubscribeBoardEvents(m_board); } private void SubscribeBoardEvents(Board board) { if (board != null) { foreach (LabelCell cell in board.Cells) { cell.Click += new EventHandler(OnCell_Click); } } } private void OnCell_Click(object sender, EventArgs e) { if (sender is LabelCell) { LabelCell cell = sender as LabelCell; MessageBox.Show(string.Format("Hello from label [{0},{1}]", cell.X, cell.Y)); } } } }
If you need any explanation of this code, feel free to ask. I also attached my solution which should work out of the box since I've placed your images inside application's start up folder (../bin/debug). I also provided you with some label click event which send a simple hello from clicked label (eg.: "Hello from label [2,7]").
Hope this works for you.
Last edited by FlashM; 12-06-2009 at 02:15 PM.
Anyway, I was thinking, do you have any ideas on how you are going to move player's letters (labels) around playing board? I have few suggestions if you're interested.
oh for that i used simple clicks .. like the player first clicks on the selection rack and then clicks anywhere on the scrabble board .. when he clicks on the scrabble board , the value from the selection board is overwritten on the scrabble board tile .. simple ... another approach could have been drag and drop.. but since im very new to c# and my final exams are going on , i didnt really get time to ask about drag n drop or even try it myself =(
please if you could explain me this statement ..i didnt get it ..
thankyou..Code:private IDictionary<string[], string> specialCellsDic = new Dictionary<string[], string>();
This is one of the collection classes that uses Key-Value pairs. It's a dictionary class. For example: every word is your key, definition of word is then value.
In my case, type of key is array of strings (which cells are two letter cells, which are three letter and so on...) and value is string path to image file that is drawn to playing board for these cells).
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks