Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


A little help on my programming

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Left2Dollars
Newbie cheater
Reputation: 0

Joined: 19 Jul 2008
Posts: 20

PostPosted: Mon Jul 21, 2008 4:42 am    Post subject: A little help on my programming Reply with quote

hi guys...I'm kind of new here...so yeah can u guys help me to find out whats wrong with my coding for c++ ? it gave me some weird output though....

i am required to write and document a number puzzle game. This game will allow the player to arrange the randomly shuffled numbers from 1 to 15 in a given layout...

the player will win when he shuffles the number correctly when he arranges it in order from 1 to 15

so here goes the code....i think the error lies somewhere on "shuffleNumb"

Code:
#include<iostream>
using namespace std;
#include<conio.h>
#include<time.h>

#define LEFT 75
#define RIGHT 77
#define UP 72
#define DOWN 80


int winlist[4][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
int guess[4][4];
int freeCellx,freeCelly,Current,upper;

bool isFound(int,int,int);
void shuffleNum();
void dispNum();
void moveleft();
void moveright();
void moveup();
void movedown();
void swap();
bool isWin();

int main()
{


   char key;
   long secs = 60; // seconds
   shuffleNum();   
   clock_t delay = secs * CLOCKS_PER_SEC ;
    clock_t start = clock();
   Current = 0;
   upper = 0 ;
   do
   {
      if(clock() - start >= delay)
      {
         cout<<"Time Out"<<endl;
         getch();
         break;
      }
      dispNum();
      key = getch();
      if(!isalnum(key))
      key = getch();

      switch(key)
      {
      case LEFT:
         {
            moveleft();
         break;
         }
      case RIGHT:
         {
            moveright();
         break;
         }
      case UP:
         {
            moveup();
         break;
         }
      case DOWN:
         {
            movedown();
         break;
         }
      case 's':
         {
            swap();
          break;
         }

      }
      if(isWin())
      {
         dispNum();
         cout<<"Winner"<<endl;
         getch();
         key='w';
      } 
   }while(key!='w');
   return 0;
}

void shuffleNum()
{
   int i,j,num;
    int lowest=1, highest=16;
    int range=(highest-lowest)+1;
   srand((unsigned)time(NULL));
   i=0,j=0;
   do
   {
      num =  lowest+int(range*rand()/(RAND_MAX + 1.0));

      if(!isFound(num,i,j))
      {
            for(i=0;i<4;i++)
            {
               for(j=0;j<4;j++)
               {
                  guess[i][j]=num;
               }
            }

                  if(num==16)
                  {
                     freeCellx = i;
                     freeCelly= j;
                     
                     if(Current==freeCellx||upper==freeCelly) Current++,upper++;
                  }
         i++;
         j++;
         
      }
   
         
   }while(i<16);
   cout<<endl;
   return;
}


bool isFound(int n,int c,int d)
{
   bool flagFound = false;
   for(int i=0;i<=c;i++)
   {
      for (int j=0;j<=d;j++)
      {
         if(guess[i][j]==n)
         {
            flagFound =true;
            break;
         }
      }
   }
   return flagFound;
}



void dispNum()
{
   
   int i,j;

   
   int array[4][4]={{1,2,3,4},
               {5,6,7,8},
               {9,10,11,12},
               {13,14,15,16}};
   system("cls");
   for(i=0;i<4;i++)
   {
      for(j=0;j<4;j++)
      {
               
         cout.width(5);
         cout.setf(ios::right);
      if(i==Current&&j==upper)
         cout<<"(";
      if( guess[i][j] ==16)
         cout<<char(2);
      else
         cout<<guess[i][j];
      if(i==Current&&j==upper)
         cout<<")";
   

      }
      cout<<endl;
   }
      
   cout<<endl;
}

void moveleft()
{
   if(Current > 0 )
   {
      Current--;
   }
}

void moveright()
{
   if(Current < 4 )
   {
      Current++;
   }
}

void moveup()
{
   if(upper > 0 )
   {
      upper++;
   }
}

void movedown()
{
   if(upper < 4 )
   {
      upper--;
   }
}


void swap()
{
   int t,j;
   t = guess[freeCellx][freeCelly];
   guess[freeCellx][freeCelly] = guess[Current][upper];
   guess[freeCellx][freeCelly] = t;
   t=freeCellx;
   freeCellx = Current;
   Current = t;
   
   j = guess[freeCellx][freeCelly];
   guess[freeCellx][freeCelly] = guess[Current][upper];
   guess[freeCellx][freeCelly] = j;
   j=freeCelly;
   freeCelly = upper;
   upper = j;
   return;
}

bool isWin()
{
   int i,j;
   int array[4][4]={{1,2,3,4},
               {5,6,7,8},
               {9,10,11,12},
               {13,14,15,16}};
   bool win = true;
   for(i=0;i<4;i++)
   {
      for(j=0;j<4;j++)
      if(guess[i][j]!=array[i][j])
      {
         win = false;
         break;
      }
   }
   return win;
}


thx in advanced Very Happy
Back to top
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Mon Jul 21, 2008 6:27 am    Post subject: Reply with quote

Debug your code and you'll find the problem. For an "outsider" it'll take a lot more time to read thru your code and learn it and that's the only way they could solve your problem. Documenting your code would help outsiders to read your code... Anyway, probably the easiest way to solve the problem is you to debug it, because you do know how the program should work, because it's designed by you, isn't it?

Btw.. If they're teaching you C++, aren't they teaching you object-oriented programming too? I kinda see no point in teaching C++, if no objects are mentioned. If you wanna impress your teacher, maybe you should take a bit more object-oriented way to code your game? :)
Back to top
View user's profile Send private message
Left2Dollars
Newbie cheater
Reputation: 0

Joined: 19 Jul 2008
Posts: 20

PostPosted: Mon Jul 21, 2008 6:30 am    Post subject: Reply with quote

lols...yeah...but i m just doing in console mode for the mean time..object orientated will come later...
Back to top
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Mon Jul 21, 2008 8:09 am    Post subject: Reply with quote

Left2Dollars wrote:
lols...yeah...but i m just doing in console mode for the mean time..object orientated will come later...
Object-oriented doesn't mean that it has to have GUI. Recheck what does object-oriented mean from your book/teacher.
Back to top
View user's profile Send private message
Left2Dollars
Newbie cheater
Reputation: 0

Joined: 19 Jul 2008
Posts: 20

PostPosted: Mon Jul 21, 2008 8:16 am    Post subject: Reply with quote

hmm ok...but i have to do dis in win 32 console application though...any suggestions ?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites