Thread: Truly random characters (scrabble)

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    16

    Question Truly random characters (scrabble)

    Hi,

    I am in the process of creating a Scrabble game. I am a beginner with C. I made a code that generates a certain amount of specific letters and store it in array:

    Code:
     #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <windows.h>
    
    char RandA[100];
    int count[27]={9,2,2,4,12,2,3,2,9,1,1,4,2,6,8,2,1,6,4,6,4,2,2,1,2,1,2};
    char alpha[27]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P', 'Q', 'R','S','T','U','V', 'W', 'X','Y','Z',' '};
    int total=98;
    
    void TileGenerate() {
    
    char ran; int i, j;
    
    	srand((unsigned) time(NULL));
    
    
    	while(total) {	
    		ran = 'A' + rand() % 26;
    			
    		for (j=0; j<27; j++) { //create random matrix
    			if (ran==alpha[j] && count[j]>0) {
    				RandA[total]=ran;
    				--count[j];
    				--total;
    			}
    		}
    	}
    
    	for (i=1; i<=100; i++) { //print random matrix
    		if (i%10==0)
    			printf("\n");
    		ran = 'A' + rand() % 26;
    		printf("%c ", RandA[i-1]);
    	}
     
    
    
    return;
    }
    When I run the code, the letters created are random but the first line usually contain 'E' and 'A'. The count of E and A in scrabble is very high so it's very likely you will see them. But most of them just come up in the first row. Sometimes I get all E's in the first row. The other rows appear very random.

    Any suggestions?

    Helix

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Any suggestions?
    ran = 'A' + rand() % 26;
    printf("%c",ran);

    See how those are equally distributed.

    The problem you have is that towards the end of the loop, all that are left in your letters to choose from are the ones with a high count to start with. All the ones with counts of 1 or 2 have already been taken

    Code:
    void TileGenerate() {
        char ran; int i, j, k;
    
        srand((unsigned) time(NULL));
    
        /* put all the letters in the bag */
        k = 0;
        for ( i = 0 ; i < 27 && k < 100 ; i++ ) {
            for ( j = 0 ; j < count[i] && k < 100 ; j++ ) {
                RandA[k++] = alpha[i];
            }
        }
    
        /* now shuffle */
        for ( i = 0 ; i < 100 ; i++ ) {
            int p1 = rand() % 100;
            int p2 = rand() % 100;
            int temp = RandA[p1];
            RandA[p1] = RandA[p2];
            RandA[p2] = temp;
        }
    
        for (i=1; i<=100; i++) { //print random matrix
            if (i%10==0)
                printf("\n");
            ran = 'A' + rand() % 26;
            printf("%c ", RandA[i-1]);
        }
        return;
    }
    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.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you want it to really be like Scrabble, then you need to make an array storing a set number of each letter, and pull from it. Because Scrabble(TM) doesn't use random letters. It uses a fixed amount of each letter which are drawn randomly.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    How do draw a tile randomly?

    From Salem's post, I understand how to shuffle an array. My idea is to create an array with predefined letters like you mentioned. Shuffle them. And them draw them 1 by 1. But this is not drawing the tiles randomly. You r drawing letters (which are placed randomly) in a linear fashion.

    Also, is there a system() command to prematurely quit the program?

    Thanks guys. I greatly appreciate your help

    Helix

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you must end immediately, try the exit function. If you've shuffled the array, then it is in effect drawing them randomly. If you don't like doing that, then randomly jump to a spot in the array. If there's nothing there (meaning, you've set it to whatever you set it to when you've already used that letter), then roll foreward down the list until you run into a letter and use it. There are numerous ways to do it.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you must end immediately, try the exit function. If you've shuffled the array, then it is in effect drawing them randomly. If you don't like doing that, then randomly jump to a spot in the array. If there's nothing there (meaning, you've set it to whatever you set it to when you've already used that letter), then roll foreward down the list until you run into a letter and use it. There are numerous ways to do it. You could also shrink the list as you pull from it.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by Salem
    > Any suggestions?
    ran = 'A' + rand() % 26;
    printf("%c",ran);
    See how those are equally distributed.
    [/code]

    rand()%N does not give intergers uniformly/equally distributed over the set
    Code:
    {0,.....,N-1}
    If RAND_MAX is large and N is small compared to RAND_MAX it will be a good approximation to an uniform distribution over the above set,(in my computer its value is only 32767) but if N is comparable to RAND_MAX then the deviation from uniformity is going to be significant.
    The one who says it cannot be done should never interrupt the one who is doing it.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    But the tiles in the bag are already shuffled, so don't you just have a simple counter

    int numTilesDrawn = 0;
    char tile = RandA[numTilesDrawn++];
    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.

  9. #9
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    The only thing I see with the code is that you randomize both p1 and p2 at the same time. I don't know how well this works for you, I am making a card game and my shuffling routine basically just

    increment through the array
    creates a random index number between 0 and max_elements
    then trades the current element with the random one.

    You can increment through the array as many times as you want to make it more random.
    Last edited by manofsteel972; 03-14-2004 at 06:12 AM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    16

    Scrabble Continued...

    The shuffling and drawing works great thx...

    Before proceeding ahead with my project and later encountering problems better ask this now.

    The scrabble game can be against 2 - 4 people. So how do u shift from one person to another. I was thinking this:

    Code:
    while(true)
    void GamePlay( which Player's turn?  and his tiles and his input) {
    do the stuff
    }
    Btw: input will be only letter(s) and location (row/col). Only the first person will enter in a complete word to start the game.

    So another thing, in scrabble you can make up multiple words both vertically and horizontally. So suppose a person enters in a letter which makes two words. How will the computer know what words they are? Make it check both vertically and horizontally till it find a null space on the board?

    Thanks for continuing help,
    Helix

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: Scrabble Continued...

    Originally posted by vex_helix
    The scrabble game can be against 2 - 4 people. So how do u shift from one person to another. I was thinking this:

    Code:
    while(true)
    void GamePlay( which Player's turn?  and his tiles and his input) {
    do the stuff
    }
    Have a variable called PlayerNumber. Whe the current player is finished, increment PayerNumber. When PalyerNumber == PlayerTotal, set it back to 0.

    Now all your drawn characters can be put in an array with PlayerNumber as an index.

    Originally posted by vex_helix
    So another thing, in scrabble you can make up multiple words both vertically and horizontally. So suppose a person enters in a letter which makes two words. How will the computer know what words they are? Make it check both vertically and horizontally till it find a null space on the board?
    Assume the letters are played across. As each letter is played check up & down for valid words
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting the characters from each word from a text file
    By flipguy_ph in forum C Programming
    Replies: 6
    Last Post: 04-27-2009, 05:56 PM
  2. problem with reading characters
    By csvraju in forum C Programming
    Replies: 4
    Last Post: 03-31-2009, 07:59 AM
  3. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  4. Random word problem
    By goron350 in forum C++ Programming
    Replies: 2
    Last Post: 05-14-2005, 03:44 PM
  5. Question for Random class
    By joenching in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2005, 11:22 PM