Thread: Draw a grid using the following characters?

  1. #16
    Registered User
    Join Date
    Aug 2013
    Posts
    16
    Oh, it works! Thank you! ^_^ I'm gonna start underestanding it, still have other functions to create >_<'

  2. #17
    Registered User
    Join Date
    Aug 2013
    Posts
    16
    Oh, also sir. What does "static const" mean?

  3. #18
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    static means it's visible to code only within the translation unit (source file) in which it's declared.

    const is short for constant, and means you can't change it.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #19
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by programmingnewb View Post
    Oh, also sir. What does "static const" mean?
    const means that the values are constant.

    static, in this case, means that the object will only be "visible" in this file; i.e. if you linked with another .c file then the object/variable-name would not be accessible to the other file -- it restricts scope.

  5. #20
    Registered User
    Join Date
    Aug 2013
    Posts
    16
    Oh, okay, thank you! Finally understand something!

  6. #21
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Be aware of the difference between const-qualified variables and constants: Question 11.8

  7. #22
    Registered User
    Join Date
    Aug 2013
    Posts
    16
    Hi again! Quiiick question

    Code:
    void drawgrid(char grid[])
      {
    	  int i;
    		int j;
        for (i = -1; i < GRIDHEIGHT; i++)
    		  {
    		      printf ("\n");
    			for (j = -1; j < GRIDWIDTH;j++)
                {
                printf("%c", randbetween('A','D') );
                }
        }
      }
    How do I use ASCII characters instead of letters (A, D)?
    I tried to use a heart but everything got messed up

  8. #23
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by programmingnewb View Post
    Hi again! Quiiick question
    ...
    How do I use ASCII characters instead of letters (A, D)?
    I tried to use a heart but everything got messed up
    I wouldn't recommend it. ASCII is not defined such that it is entirely printable. However, everything from 32 ' ' to 127 (exclusive) is.
    Last edited by whiteflags; 08-25-2013 at 01:01 AM.

  9. #24
    Registered User
    Join Date
    Aug 2013
    Posts
    16
    Hey, I tried to use 5 letters instead of 2, and it says there are too many arguments for my randbetween function?

    Code:
    int randbetween(int min, int max)
      {
    	  return rand() % (max - min + 1) + min;
    	}
    
    
    void drawgrid(char grid[])
      {
    	int i;
    	int j;
    
    
        for (i = -1; i < GRIDHEIGHT; i++)
    		  {
    				printf("\n");
    			  for (j = -1; j < GRIDWIDTH; j++)
    				  {
    					  printf("%c", randbetween('K', 'L', 'E', 'X', 'S') ); 
    					  
    					}
    			}
    	}

  10. #25
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > and it says there are too many arguments for my randbetween function?
    Your compiler is correct.
    The function expects 2 arguments, and you passed 5.

    You've got two hands, I throw 5 tennis balls at you and say "catch", what are you going to do?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #26
    Registered User
    Join Date
    Aug 2013
    Posts
    16
    I figured it out ^_^Thanks

  12. #27
    Registered User
    Join Date
    Aug 2013
    Posts
    16
    Hi there! I was able to draw the grid, and as we all know it's a Bejeweled-like game. The goal is to match 3, 4, or 5 symbols of the same type. However, in my grid it's still possible for it to generate lines with 3, 4, or 5 pre-made matches. I want it to be impossible for the program to generate this, so what do I include?

    Code:
    int randbetween(int min, int max)
      {
    	int i;
    	i = rand() % (max - min + 1) + min;
    	  
    	 if ( i == 0 )
    		{
    			return 'K';
    		}
    	
    	else if ( i == 1 )
    		{
    			return 'L';
    		}
    	else if( i == 2 )
    	
    	{
    			return 'E';
    		}
    	
    	else if (i == 3)
    		{
    			return 'X';
    		}
    	
    	else (i == 4);
    		{
    			return 'S';
    		}
    	
    	}
    
    
    void drawgrid(char grid[])
      {
    	 int i;
    	int j;
    
    
        for (i = -1; i < GRIDHEIGHT; i++)
    		  {
    				printf("\n");
    			  for (j = -1; j < GRIDWIDTH; j++)
    				  {
    					  printf("%c", randbetween(0,4) ); 
    					  
    					}
    			}
    	}

  13. #28
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I have NO idea what "Bejeweled" is about. Use [code*] tags [/code*] (without the *'s), and show what you have now, and what you want. That way the diagram will keep it's proper spacing.

    I'm sure the solution is simple, but "like Bejeweled" doesn't cut it for me.

  14. #29
    Registered User
    Join Date
    Aug 2013
    Posts
    16
    Bejeweled is the same as Candy Crush (if you've played it before):
    -You are given a grid with (let's say) 4 different symbols, and they are randomly printed in such a way that no 3, 4, or 5 symbols of the same type are printed side by side.
    -The goal is to match 3, 4, or 5 symbols of the same type, and once a match has been made, the matched symbols are cleared out and new, random symbols are put in their place.

    Hopefully that's understandable?

    I want my program NOT to print symbols of the same type side by side each other, like the 3 A's in the second row:

    ABCDA
    BDAAA
    CDABC
    DABCD

    I want it the program to print a grid, such that no 3, 4, or 5 symbols are printed side by side like this:

    ABCDA
    BCDAA
    ABDCB
    CABDA

    This is what I have so far:

    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    #define GRIDWIDTH 6
    #define GRIDHEIGHT 6
    #define NUMSYMBOLS 5
    
    
    void clearscreen();
    int randbetween(int min, int max);
    void drawgrid(char grid[]);
    
    
    int main()
      {
    	  /* Initialize environment */
    		time_t seconds;
    		int iter;
    		char grid[GRIDWIDTH][GRIDHEIGHT];
    		char outgrid[GRIDWIDTH][GRIDHEIGHT];
    		int rowchoice;
    		int colchoice;
    		char matchfound;
    		char maxzeroes;
    		char foundzeroes;
    		int eliminated;
    		int i;
    		int j;
    		int k;
    
    
    		time(&seconds);
    		srand((unsigned int) seconds);
    
    
    		drawgrid(outgrid[randbetween(0,4)]);
    		printf("\nEnter a choice, 1 for up, 2 for down, 3 for left, 4 for right");
    		scanf ("%c",&rowchoice );
    	}
    
    
    void clearscreen()
      {
    	  if (system("cls"))
    		  {
    			  system("clear");
    			}
    	}
    
    
    int randbetween(int min, int max)
      {
    	int i;
    	i = rand() % (max - min + 1) + min;
    	  
    	 if( i == 0 )
    	{
    		return 'K';
    	}
    	
    	else if( i == 1 )
    	{
    		return 'L';
    	}
    	else if( i == 2 )
    	{
    			return 'E';
    	}
    	
    	else if( i == 3 )
    	{
    		return 'X';
    	}
    	
    	else( i == 4 );
    	{
    			return 'S';
    	}
    }
    	
    
    
    void drawgrid(char grid[])
      {
    	int i;
    	int j;
    
    
        for (i = -1; i < GRIDHEIGHT; i++)
    		  {
    			printf("\n");
    			  for (j = -1; j < GRIDWIDTH; j++)
    				  {
    					  printf("%c", randbetween(0,4) ); 
    					  
    					}
    			}
    	}
    Last edited by programmingnewb; 08-25-2013 at 05:43 PM.

  15. #30
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A simple idea would be to make your game grid up initially with letters (in any order), and then use a while loop to keep making swaps, until you have no repeats in the same row, next to each other.

    I don't believe that it would take too many swaps to randomize it as you want.
    Last edited by Adak; 08-25-2013 at 06:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a grid
    By karimoftos in forum C Programming
    Replies: 6
    Last Post: 04-19-2011, 11:32 AM
  2. Grid
    By mortalc in forum C Programming
    Replies: 7
    Last Post: 03-12-2011, 06:23 AM
  3. Plz, teach me how to draw a grid!
    By Messiah in forum C++ Programming
    Replies: 5
    Last Post: 06-30-2010, 11:16 AM

Tags for this Thread