Thread: Sudoku problem!!

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    45

    Sudoku problem!!

    Hello Again.

    I want to make Sudoku Game In C.

    But I have problem for solution. Please help me.

    Code:
      #include <stdio.h>#include <stdlib.h>
    
     
    
    enum {BORDER,EMPTY};
    
    const int ConvertTo25[25]= {
    
    	8,9,10,11,12,
    
    	15,16,17,18,19,
    
    	22,23,24,25,26,
    
    	29,30,31,32,33
    
    };
    
     
    
    void GetDifferentRandomNumber(int *board)
    
    {
    
    	int index = 0;
    
    	int loopIndex;
    
    	int moveOk = 0;
    
    	int randomNumber[5];
    
    	int newNumber;
    
    	while(moveOk!=5)
    
    	{
    
    		for(index = 0;index < 5;)
    
    		{
    
    				newNumber = (1+rand()%5);
    
    				if(ControlDifferentNumber(&randomNumber,newNumber)==1)
    
    				{
    
    					randomNumber[index]	= newNumber;
    
    					board[ConvertTo25[index]] = randomNumber[index];
    
    					index++;
    
    				}
    
    		}
    
    	moveOk=5;
    
    	}
    
    }
    
    int ControlDifferentNumber(int *randomNumber, int newNumber)
    
    {
    
    	int index = 0;
    
    	for(index = 0; index<5 ; index++)
    
    	{
    
    		if(randomNumber[index]==newNumber)
    
    		return 0;
    
    	}
    
    	return 1;
    
    }
    
     
    
    void PrintBoard(const int *board)
    
    {
    
    	int index = 0;
    
    	char pChar[2]="|-";
    
    	printf("\n");
    
    	for(index = 0; index<25; ++index)
    
    	{
    
    		if(index!=0 && index%5==0)
    
    		{
    
    			printf("\n\n");
    
    		}
    
    		printf("%5d",board[ConvertTo25[index]]);
    
    	}
    
    		printf("\n");
    
    }
    
     
    
    void InitiazeBoard(int *board)
    
    {
    
    	int index;
    
    	for(index = 0; index<49; ++index)
    
    	{
    
    		board[index] = BORDER;
    
    	}
    
    	
    
    	for(index = 0; index<25; ++index)
    
    	{
    
    		board[ConvertTo25[index]] = EMPTY;
    
    	}
    
    }
    
    void RunGame()
    
    {
    
    	int board[49];
    
    	InitiazeBoard(&board[0]);
    
    	GetDifferentRandomNumber(&board[0]);
    
    	PrintBoard(&board[0]);
    
    }
    
    int main(void) {
    
    	srand(time(NULL));
    
    	RunGame();
    
    	return 0;
    
    }
    Sudoku problem!!-ekran-al-nt-s-jpg
    Last edited by Salem; 02-09-2015 at 03:09 PM. Reason: fixed horrible colour/font - use "paste as text" in future

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    What are 5, 25 and 49 supposed to be?

    Especially since a sudoku board is 9x9.
    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
    Registered User
    Join Date
    Nov 2014
    Posts
    45
    Yes, I know but It must be easy so 5x5. Help please

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So start with
    Code:
    #define SIZE 5
    And replace all those magic numbers with SIZE or SIZE*SIZE, or whatever seems appropriate.
    It will go a long way to making the code readable.

    Also, saying "it doesn't work" without saying what you want to achieve is pretty meaningless as well.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sudoku?
    By Roadrunner2015 in forum C Programming
    Replies: 9
    Last Post: 01-29-2014, 11:59 PM
  2. My sudoku solver problem
    By barramundi9 in forum C Programming
    Replies: 7
    Last Post: 08-28-2011, 12:04 PM
  3. Like Sudoku, but not.
    By quzah in forum Game Programming
    Replies: 4
    Last Post: 08-27-2011, 05:33 AM
  4. Sudoku problem
    By Swarvy in forum Game Programming
    Replies: 4
    Last Post: 12-06-2008, 07:12 AM
  5. Sudoku Array Problem
    By Ginger_Ale in forum C++ Programming
    Replies: 10
    Last Post: 10-31-2005, 05:25 PM