Click here to Skip to main content
15,895,746 members
Articles / Programming Languages / Visual Basic

A Simple Cross-platform Program

Rate me:
Please Sign up or sign in to vote.
2.41/5 (7 votes)
13 Aug 2009CPOL4 min read 52K   506   24  
How to write a program that allows a simple game to run on Linux and Windows
void solutionconstruct();  //setup start array
void setnumber(int x, int y, int number);  //number selected in the sudoku
void generatesudoku(int start);  //generate a sudoku from "start"
void checkselection(void);  //see if one or two numbers can be selected
int findsingle(void);  //select standalone number and return if found
void getgame(int level);   //select hard,medium or easy random game
void solvehere();  //solve existing sudoku

//constructor to setup sudokuarray
void solutionconstruct()
{
//fill the "0" step with pencil 1-9 in all the small squares -81 of them
char number =1;
int xl = 0;
for(xl;xl<=2;xl++)
{
 int yl = 0;
 for(yl;yl<=2;yl++)
 {
  int xs = 0;
  for(xs;xs<=2;xs++)
  {
   int ys = 0;
   for(ys;ys<=2;ys++)
   {
    int yn = 0;
    for(yn;yn<=2;yn++)
	{ 
     int xn = 0;
     for(xn;xn<=2;xn++)
	 {
      sudokuarray[0][xl][yl][xs][ys][xn][yn] = number;//fill the first step
      number = number + 1;  //put in 1 to 9
	 }//next xn
	}//next yn
      number=1;  //1 to 9 in box now do next
   }//next ys
  }//next xs
 }//next yl
}//next xl...............................all done
}//end of constructor

void setnumber(int x, int y, int number)
{//values 0 t0 8
	if(sudokustep < 81)  // cant go on if last number selected
    {
int selected = 0;  //check to see if number already selected
int nopossible = 0;  //check to see if no possibilities to select exist
int crossedout = 0;  //check to see if the number has been crossed out
if(sudokuarray[sudokustep][x/3][y/3][x%3][y%3][number%3][number/3] == 0)
crossedout = 1;  //the number has been crossed out
   int yn = 0;
   for(yn;yn<=2;yn++)  // in this number box
	{ 
     int xn = 0;
     for(xn;xn<=2;xn++)
	 {      
      if (sudokuarray[sudokustep][x/3][y/3][x%3][y%3][xn][yn] > 9) 
		  selected = 1; //this number already selected
      if (sudokuarray[sudokustep][x/3][y/3][x%3][y%3][xn][yn] > 0)
		  nopossible = 1;  //at least one possible number exists
	 }//next xn
	}//next yn
   if((selected < 1) && (nopossible > 0) && (crossedout < 1))  //number not selected 
   {//could move on to next step if a its a possible and its not crossed out
int xl = 0;
for(xl;xl<=2;xl++)    //copy the current step values 
{
 int yl = 0;
 for(yl;yl<=2;yl++)   //to the next step
 {
  int xs = 0;
  for(xs;xs<=2;xs++)
  {
   int ys = 0;
   for(ys;ys<=2;ys++)
   {
    int yn = 0;
    for(yn;yn<=2;yn++)
	{ 
     int xn = 0;  
     for(xn;xn<=2;xn++)
	 {
      sudokuarray[sudokustep + 1][xl][yl][xs][ys][xn][yn] = 
      sudokuarray[sudokustep][xl][yl][xs][ys][xn][yn]; //array copy
	 }//next xn
	}//next yn
   }//next ys
  }//next xs
 }//next yl
}//next xl...............................all done
sudokustep = sudokustep +1; //work on the next step update the number box
char savenumber=sudokuarray[sudokustep][x/3][y/3][x%3][y%3][number%3][number/3] + 10;
int j = 0;
for (j; j <= 2; j++)  //set all to zero except selected number in box
{
 int i = 0;
 for ( i; i <= 2; i++)
 {
	 if(sudokuarray[sudokustep][x/3][y/3][x%3][y%3][i][j] <10)
   sudokuarray[sudokustep][x/3][y/3][x%3][y%3][i][j] = 0;
 }//end of i for
}// end of j for
j = 0;
for (j; j <= 2; j++)  //horizontal line cannot have this number
{
 int i = 0;
 for (i;  i <= 2; i++)
 {
	 if(sudokuarray[sudokustep][j][y/3][i][y%3][number%3][number/3] < 10)
   sudokuarray[sudokustep][j][y/3][i][y%3][number%3][number/3] = 0;
 }//end of i for
}// end of j for
j = 0; 
for (j; j <= 2; j++)  //vertical line cannot have this number
{
 int i = 0;
 for ( i; i <= 2; i++)
 {
	 if(sudokuarray[sudokustep][x/3][j][x%3][i][number%3][number/3] < 10)
   sudokuarray[sudokustep][x/3][j][x%3][i][number%3][number/3] = 0;
 }//end of i for
}// end of j for
j = 0;
for (j; j <= 2; j++)  //box of 9 numbers cannot have this number
{
 int i = 0;
 for ( i; i <= 2; i++)
 {
	 if(sudokuarray[sudokustep][x/3][y/3][j][i][number%3][number/3] < 10)
   sudokuarray[sudokustep][x/3][y/3][j][i][number%3][number/3] = 0;
 }//end of i for
}// end of j for

//restore the selected number
sudokuarray[sudokustep][x/3][y/3][x%3][y%3][number%3][number/3]=savenumber;
}//end of number not already selected & possible & not crossed out selected if
}// end of less than 81 step if
}// end of set number function

void generatesudoku(int start)  //generate a sudoku from "start"
{
sudokustep = start; //start the sudoku solution
int blank = 0;  //if get blank cant solve
while((blank == 0) && (sudokustep < 80))  //check for blank
{
int x = rand() % 9;   //get random x value
int y = rand() % 9;   //get random y value
int number = rand() % 9;   //get random number value
setnumber(x,y,number);  //select if can in solution
checkselection();   //select any stanalone numbers
int xl = 0;
for(xl;xl<=2;xl++)
{
 int yl = 0;
 for(yl;yl<=2;yl++)
 {
  int xs = 0;
  for(xs;xs<=2;xs++)
  {
   int ys = 0;
   for(ys;ys<=2;ys++)
   {
    int numberselected = 0;  //find how many zero's in this box
    int yn = 0;
    for(yn;yn<=2;yn++)
	{ 
     int xn = 0;
     for(xn;xn<=2;xn++)
	 {
     if( sudokuarray[sudokustep][xl][yl][xs][ys][xn][yn] == 0)
       numberselected += 1;  //count number in this box
	 }//next xn
	}//next yn
	 if(numberselected == 9)
		 blank=1;
   }//next ys
  }//next xs
 }//next yl
}//next xl...............................all done
}//end of check for blank
}//end of generate a sudoku

void checkselection(void)  //see if one or two numbers can be selected
{
if(findsingle() == 0)  //find if one can be selected
findsingle();
}// end of check selection

int findsingle(void)  //select standalone number and return if found
{
int bignumber = 0;  //holds the selected number
int nonselect = 1;  //flag for standalone number = 0 for true
int xl = 0;
for(xl;xl<=2;xl++)
{
 int yl = 0;
 for(yl;yl<=2;yl++)
 {
  int xs = 0;
  for(xs;xs<=2;xs++)
  {
   int ys = 0;
   for(ys;ys<=2;ys++)
   {
    int numberselected = 0;  //find how many zero's in this box
    int yn = 0;
    for(yn;yn<=2;yn++)
	{ 
     int xn = 0;
     for(xn;xn<=2;xn++)
	 {
     if( sudokuarray[sudokustep][xl][yl][xs][ys][xn][yn] == 0)
       numberselected += 1;  //count number in this box
	 else
		 bignumber = sudokuarray[sudokustep][xl][yl][xs][ys][xn][yn];
	 }//next xn
	}//next yn
	 if((numberselected == 8) && (bignumber < 10))
	 {
	 setnumber(xl *3 + xs,yl *3 + ys,bignumber -1);  //select the stadalone
     nonselect = 0;  //we have found a stad alone number
	 }//end of number selected if
   }//next ys
  }//next xs
 }//next yl
}//next xl...............................all done
return nonselect;  
}//end of select standalone number

void getgame(int level)   //select hard,medium or easy random game
{
sudokustep = 0;  //start
while(sudokustep <81 )  //try until have complete solution
{
generatesudoku(0);
}//end of complete solution
sudokustep = level;  //hard, medium or easy sudoku
}//end of get game

void solvehere()  //solve existing sudoku
{
int start = sudokustep;  //starting position
int count = 0;  //if goes on to long cant solve
while(sudokustep <81 )  //try until have complete solution
{
generatesudoku(start);
count = count + 1;
if(count > 500)
break;   //gone on to long cant solve
findsingle();  //have to do last here
}//end of complete solution
}//end of solve existing sudoku


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Australia Australia
I identify with the starfish.
I may be really stupid and have to use visual basic but at least I'm happy.

Comments and Discussions