Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Word Search Style Game

Word Search Style Game

Scheduled Pinned Locked Moved C#
helpcsharpcssgraphicsgame-dev
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VinceAshbySmith
    wrote on last edited by
    #1

    Hi guys wonder if i can get a bit of help. I'm in the process of writing a word search style game in c#. I've managed to draw a grid and being able to highlight a cell within that grid. However where i'm getting stuck is creating the grid of letters. At the moment this is all purely aesthetic, there is no logic behind it yet. So any ideas on how to create a grid of letters to go into my drawn grid, would be most helpful. Say i have a 5x5 grid what i want to be able to do is place a letter in each of those grid cells, so something like this: a b c d e a b c d e a b c d e a b c d e a b c d e I hope this makes sense and someone has an idea. I've included my code to draw my gridlines and highlight a cell in that grid below! Regards Vince public partial class GUI : Form { private int gridWidth = 600; private int gridHeight = 600; private int squareSize = 20; private int maxCol = 60; private int maxRow = 60; private Color customColor; private Boolean[,] cellSelected = new Boolean[60, 60]; public GUI() { InitializeComponent(); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { Graphics gfx = e.Graphics; this.customColor = Color.LightSlateGray; Pen myPen = new Pen(this.customColor); // Create a horizontal line for (int i = 0; i <= this.gridWidth; i = i + this.squareSize) { gfx.DrawLine(myPen, 0, i, this.gridHeight, i); } // Create a vertical line for (int i = 0; i <= this.gridWidth; i = i + this.squareSize) { gfx.DrawLine(myPen, i, 0, i, this.gridHeight); } // Draw cells on grid int cellRow = 0; while (cellRow < this.maxRow) { int cellCol = 0; while (cellCol < this.maxCol) { if (this.cellSelected[cellCol, cellRow] == true) { this.customColor = Color.FromArgb(100,100,100); SolidBrush myBrush = new SolidBrush(customColor); gfx.FillRectangle(myBrush, (cellCol * this.squareSize) + 1, (cellRow * this.squareSize) + 1, this.squareSize - 1, this.squareSize - 1); myBrush.Dispose(); } cel

    L J 2 Replies Last reply
    0
    • V VinceAshbySmith

      Hi guys wonder if i can get a bit of help. I'm in the process of writing a word search style game in c#. I've managed to draw a grid and being able to highlight a cell within that grid. However where i'm getting stuck is creating the grid of letters. At the moment this is all purely aesthetic, there is no logic behind it yet. So any ideas on how to create a grid of letters to go into my drawn grid, would be most helpful. Say i have a 5x5 grid what i want to be able to do is place a letter in each of those grid cells, so something like this: a b c d e a b c d e a b c d e a b c d e a b c d e I hope this makes sense and someone has an idea. I've included my code to draw my gridlines and highlight a cell in that grid below! Regards Vince public partial class GUI : Form { private int gridWidth = 600; private int gridHeight = 600; private int squareSize = 20; private int maxCol = 60; private int maxRow = 60; private Color customColor; private Boolean[,] cellSelected = new Boolean[60, 60]; public GUI() { InitializeComponent(); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { Graphics gfx = e.Graphics; this.customColor = Color.LightSlateGray; Pen myPen = new Pen(this.customColor); // Create a horizontal line for (int i = 0; i <= this.gridWidth; i = i + this.squareSize) { gfx.DrawLine(myPen, 0, i, this.gridHeight, i); } // Create a vertical line for (int i = 0; i <= this.gridWidth; i = i + this.squareSize) { gfx.DrawLine(myPen, i, 0, i, this.gridHeight); } // Draw cells on grid int cellRow = 0; while (cellRow < this.maxRow) { int cellCol = 0; while (cellCol < this.maxCol) { if (this.cellSelected[cellCol, cellRow] == true) { this.customColor = Color.FromArgb(100,100,100); SolidBrush myBrush = new SolidBrush(customColor); gfx.FillRectangle(myBrush, (cellCol * this.squareSize) + 1, (cellRow * this.squareSize) + 1, this.squareSize - 1, this.squareSize - 1); myBrush.Dispose(); } cel

      J Offline
      J Offline
      Jason C Bourne
      wrote on last edited by
      #2

      You should try a more object-oriented approach, you are really complexifying your life. Split this code in multiple small elements responsible of a single responsibility. The rectangle that holds a letter is one object. If you make a UserControl for this simple responsibility, you can delegate the complexity of catching MouseEnter/MouseLeave/etc... events in the control itself. Via two small event handlers, you can highlight and unhighlight your control without having to determine any coordinate. The "host" of you multiple rectangles is another UserControl. Its only responsibility is to put all the small rectangles in a bigger rectangle. Eventually, it computes the size that a single "letter rectangle" can have, as it knows it own size and the total number of smaller rectangles to put in itself. And finally, generating letters is another responsibility. The generation of the letters occurs outside, is provided to the big UserControl, and it will instantiate the correct number of small rectanges, providing the text and the size. Don't play with positioning that you compute yourself, use docking. All this code can be reduced to 10 lines.

      Jean-Christophe Grégoire

      1 Reply Last reply
      0
      • V VinceAshbySmith

        Hi guys wonder if i can get a bit of help. I'm in the process of writing a word search style game in c#. I've managed to draw a grid and being able to highlight a cell within that grid. However where i'm getting stuck is creating the grid of letters. At the moment this is all purely aesthetic, there is no logic behind it yet. So any ideas on how to create a grid of letters to go into my drawn grid, would be most helpful. Say i have a 5x5 grid what i want to be able to do is place a letter in each of those grid cells, so something like this: a b c d e a b c d e a b c d e a b c d e a b c d e I hope this makes sense and someone has an idea. I've included my code to draw my gridlines and highlight a cell in that grid below! Regards Vince public partial class GUI : Form { private int gridWidth = 600; private int gridHeight = 600; private int squareSize = 20; private int maxCol = 60; private int maxRow = 60; private Color customColor; private Boolean[,] cellSelected = new Boolean[60, 60]; public GUI() { InitializeComponent(); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { Graphics gfx = e.Graphics; this.customColor = Color.LightSlateGray; Pen myPen = new Pen(this.customColor); // Create a horizontal line for (int i = 0; i <= this.gridWidth; i = i + this.squareSize) { gfx.DrawLine(myPen, 0, i, this.gridHeight, i); } // Create a vertical line for (int i = 0; i <= this.gridWidth; i = i + this.squareSize) { gfx.DrawLine(myPen, i, 0, i, this.gridHeight); } // Draw cells on grid int cellRow = 0; while (cellRow < this.maxRow) { int cellCol = 0; while (cellCol < this.maxCol) { if (this.cellSelected[cellCol, cellRow] == true) { this.customColor = Color.FromArgb(100,100,100); SolidBrush myBrush = new SolidBrush(customColor); gfx.FillRectangle(myBrush, (cellCol * this.squareSize) + 1, (cellRow * this.squareSize) + 1, this.squareSize - 1, this.squareSize - 1); myBrush.Dispose(); } cel

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You can draw text using the .DrawString method. That would go in the _Paint handler;

                // Draw cells on grid
                int cellRow = 0;
                while (cellRow < this.maxRow)
                {
                    int cellCol = 0;
                    while (cellCol < this.maxCol)
                    {
                        if (this.cellSelected\[cellCol, cellRow\] == true)
                        {
                            this.customColor = Color.FromArgb(100, 100, 100);
        
                            SolidBrush myBrush = new SolidBrush(customColor);
                            gfx.FillRectangle(myBrush, (cellCol \* this.squareSize) + 1, (cellRow \* this.squareSize) + 1, this.squareSize - 1, this.squareSize - 1);
                            gfx.DrawString("X", this.Font, Brushes.Wheat, new Point(cellCol \* this.squareSize, cellRow \* this.squareSize));
                            myBrush.Dispose();
                        }
                        else
                        {   // Cell is not selected
                            gfx.DrawString("X", this.Font, Brushes.Green, new Point(cellCol \* this.squareSize, cellRow \* this.squareSize));
                        }
                        cellCol++;
                    }
                    cellRow++;
        

        I are troll :)

        V 1 Reply Last reply
        0
        • L Lost User

          You can draw text using the .DrawString method. That would go in the _Paint handler;

                  // Draw cells on grid
                  int cellRow = 0;
                  while (cellRow < this.maxRow)
                  {
                      int cellCol = 0;
                      while (cellCol < this.maxCol)
                      {
                          if (this.cellSelected\[cellCol, cellRow\] == true)
                          {
                              this.customColor = Color.FromArgb(100, 100, 100);
          
                              SolidBrush myBrush = new SolidBrush(customColor);
                              gfx.FillRectangle(myBrush, (cellCol \* this.squareSize) + 1, (cellRow \* this.squareSize) + 1, this.squareSize - 1, this.squareSize - 1);
                              gfx.DrawString("X", this.Font, Brushes.Wheat, new Point(cellCol \* this.squareSize, cellRow \* this.squareSize));
                              myBrush.Dispose();
                          }
                          else
                          {   // Cell is not selected
                              gfx.DrawString("X", this.Font, Brushes.Green, new Point(cellCol \* this.squareSize, cellRow \* this.squareSize));
                          }
                          cellCol++;
                      }
                      cellRow++;
          

          I are troll :)

          V Offline
          V Offline
          VinceAshbySmith
          wrote on last edited by
          #4

          Hi eddy thats useful thanks, however i want to be able to set the letters myself, as at the moment this will paint an x in every cell. So say i want to have the word "hello" in the grid somewhere surrounded by random letters. The random letter thing, i think i've got an idea in, but without hard coding the actual word in, i'm not sure how to place the word in the grid somewhere?

          J L 2 Replies Last reply
          0
          • V VinceAshbySmith

            Hi eddy thats useful thanks, however i want to be able to set the letters myself, as at the moment this will paint an x in every cell. So say i want to have the word "hello" in the grid somewhere surrounded by random letters. The random letter thing, i think i've got an idea in, but without hard coding the actual word in, i'm not sure how to place the word in the grid somewhere?

            J Offline
            J Offline
            Jason C Bourne
            wrote on last edited by
            #5

            Don't do that like that, compute the letters list where you want, give it to your "grid container" UserControl which it is supposed to know the list of "letter controls" that it contains. Loop in your letters list and give a letter to each sub-control. Each sub-control is supposed to have a stupid Text property, in which you can set the value of a label that would be docked full in your sub-control. Really, don't put all this useless complexity like that.

            Jean-Christophe Grégoire

            V 1 Reply Last reply
            0
            • J Jason C Bourne

              Don't do that like that, compute the letters list where you want, give it to your "grid container" UserControl which it is supposed to know the list of "letter controls" that it contains. Loop in your letters list and give a letter to each sub-control. Each sub-control is supposed to have a stupid Text property, in which you can set the value of a label that would be docked full in your sub-control. Really, don't put all this useless complexity like that.

              Jean-Christophe Grégoire

              V Offline
              V Offline
              VinceAshbySmith
              wrote on last edited by
              #6

              Hi Jean-Christophe, i'm only quite new to C# and programming as a whole and i'm not really understanding what your meaning. Maybe a good see what you mean with some pseudo code or just initial code to get me on my way?

              J 1 Reply Last reply
              0
              • V VinceAshbySmith

                Hi Jean-Christophe, i'm only quite new to C# and programming as a whole and i'm not really understanding what your meaning. Maybe a good see what you mean with some pseudo code or just initial code to get me on my way?

                J Offline
                J Offline
                Jason C Bourne
                wrote on last edited by
                #7

                Okay sorry, give me some more time and I will mail you a sample project ;-)

                Jean-Christophe Grégoire

                V 1 Reply Last reply
                0
                • J Jason C Bourne

                  Okay sorry, give me some more time and I will mail you a sample project ;-)

                  Jean-Christophe Grégoire

                  V Offline
                  V Offline
                  VinceAshbySmith
                  wrote on last edited by
                  #8

                  Thank you Jean-Christophe, i really appreciate your help. I think once i see what your meaning i will be able to get a grasp of it! Many thanks

                  J 1 Reply Last reply
                  0
                  • V VinceAshbySmith

                    Hi eddy thats useful thanks, however i want to be able to set the letters myself, as at the moment this will paint an x in every cell. So say i want to have the word "hello" in the grid somewhere surrounded by random letters. The random letter thing, i think i've got an idea in, but without hard coding the actual word in, i'm not sure how to place the word in the grid somewhere?

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    string drawThis = "Hello world";
                    int cellRow = 0;
                    while (cellRow < this.maxRow)
                    {
                    int cellCol = 0;
                    while (cellCol < this.maxCol)
                    {
                    if (this.cellSelected[cellCol, cellRow] == true)
                    {
                    this.customColor = Color.FromArgb(100, 100, 100);

                            SolidBrush myBrush = new SolidBrush(customColor);
                            gfx.FillRectangle(myBrush, (cellCol \* this.squareSize) + 1, (cellRow \* this.squareSize) + 1, this.squareSize - 1, this.squareSize - 1);
                            if (cellCol < drawThis.Length)
                                gfx.DrawString(
                                    drawThis\[cellCol\].ToString(), 
                                    this.Font, 
                                    Brushes.Wheat, 
                                    new Point(cellCol \* this.squareSize, cellRow \* this.squareSize));
                            myBrush.Dispose();
                        }
                        else
                        {   // Cell is not selected
                            if (cellCol < drawThis.Length)
                                gfx.DrawString(
                                    drawThis\[cellCol\].ToString(), 
                                    this.Font, 
                                    Brushes.Green, 
                                    new Point(cellCol \* this.squareSize, cellRow \* this.squareSize));
                        }
                        cellCol++;
                    }
                    cellRow++;
                    

                    }

                    ..and yes, there are more paths to Rome :)

                    I are troll :)

                    1 Reply Last reply
                    0
                    • V VinceAshbySmith

                      Thank you Jean-Christophe, i really appreciate your help. I think once i see what your meaning i will be able to get a grasp of it! Many thanks

                      J Offline
                      J Offline
                      Jason C Bourne
                      wrote on last edited by
                      #10

                      Hi VinceAshbySmith, I made your sample project, how can I provide the zip file ?

                      Jean-Christophe Grégoire

                      V 1 Reply Last reply
                      0
                      • J Jason C Bourne

                        Hi VinceAshbySmith, I made your sample project, how can I provide the zip file ?

                        Jean-Christophe Grégoire

                        V Offline
                        V Offline
                        VinceAshbySmith
                        wrote on last edited by
                        #11

                        Hi Jean-Christophe not sure if you got my email, so if you could send the code sample to vinny.as@tiscali.co.uk, that would be great!

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups