[Home]
/************************************************************************************
What will you do if some one asks you to write a game, given just five minutes.
Well I just thought of it and wrote this simple code within 10 minutes :
Box Maniac Written in 10 minutes by Venkatesh.S
************************************************************************************/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define SIZE 3
int i_Array[SIZE+1][SIZE+1];
int i_Randomize( );
int i_Check( );
int i_Print( );
void main( )
{
int i_Input = 1, i_Moves = 0;
int i_X = SIZE;
int i_Y = SIZE;
i_Randomize( );
while ( !i_Check( ) )
{
clrscr( );
printf( "Box Maniac : Written by Venkatesh.S\n" );
i_Print( );
printf( "Instructions\n1->Move Up\n2->Move Down\n3->Move Left\n4->Move Right\n0 or any other number->Quit");
printf( "\nMoves : %d\n", i_Moves++ );
printf( "\nWaiting for your input\n:" );
scanf( "%d", &i_Input );
if ( i_Input == 0 || i_Input > 4 ) break;
switch( i_Input )
{
case 1: /* Up */
if ( i_X < SIZE )
{
i_Array[i_X][i_Y] = i_Array[i_X + 1][i_Y];
i_Array[i_X + 1][i_Y] = 0;
i_X = i_X + 1;
}
else
printf("\nNo way to move up \n" );
break;
case 2: /* Down */
if ( i_X > 1 )
{
i_Array[i_X][i_Y] = i_Array[i_X - 1][i_Y];
i_Array[i_X - 1][i_Y] = 0;
i_X = i_X - 1;
}
else
printf("\n No way to move down \n" );
break;
case 3: /* Left */
if ( i_Y < SIZE )
{
i_Array[i_X][i_Y] = i_Array[i_X][i_Y + 1];
i_Array[i_X][i_Y + 1] = 0;
i_Y = i_Y + 1;
}
else
printf("\nNo way to left \n" );
break;
case 4: /* Right */
if ( i_Y > 1 )
{
i_Array[i_X][i_Y] = i_Array[i_X][i_Y - 1];
i_Array[i_X][i_Y - 1] = 0;
i_Y = i_Y - 1;
}
else
printf("\n No way to move down \n" );
break;
default:
break;
}
}
if ( i_Check( ) ) printf( "You Won. " );
printf( "Bye" );
getch( );
}
int i_Randomize( )
{
int i_Magic[SIZE*SIZE+1];
int i, j, k = 0, l;
for ( i = 1; i < SIZE*SIZE; i++ )
{
randomize( );
k = ( rand( ) % ( (SIZE*SIZE) - 1) ) + 1;
j = 0;
while ( j == 0 )
{
j = 1;
if ( i == 1 )
j = 1;
else
{
for ( l = 1; l < i; l++ )
if ( i_Magic[l] == k ) j = 0;
}
if ( j == 1 ) i_Magic[i] = k;
k = ( rand( ) % ( (SIZE*SIZE) - 1) ) + 1;
}
}
i_Magic[SIZE*SIZE] = 0;
k = 1;
for ( i = 1; i <= SIZE; i++ )
for ( j = 1; j <= SIZE; j++ )
{
i_Array[i][j] = i_Magic[k];
k++;
}
return 1;
}
int i_Print( )
{
int i, j, k;
int i_Printline;
for ( i_Printline = 0; i_Printline < (SIZE * 8) + 2; i_Printline++ ) printf( "-" );
printf("\n");
for ( i = 1; i <= SIZE; i++ )
{
printf( "|" );
for ( j = 1; j <= SIZE; j++ )
{
printf( "\t%d", i_Array[i][j] );
}
printf( "|\n" );
}
for ( i_Printline = 0; i_Printline < (SIZE * 8) + 2; i_Printline++ ) printf( "-" );
printf("\n");
return 1;
}
int i_Check( )
{
int i, j, k = 1;
for ( i = 1; i <= SIZE; i++ )
{
for ( j = 1; j <= SIZE; j++ )
{
if ( ( i == SIZE ) && ( j == SIZE - 1 ) ) return 1;
if ( i_Array[i][j] != k++ ) return 0;
}
printf( "\n" );
}
return 1;
}
|
[Home]
Syntax highlighted by Code2HTML, v. 0.9.1