Sign in to follow this  
Drusselnahg

What's wrong with that ?

Recommended Posts

So, I want to make an old snake-game but I have a problem I can't solve. Here's the code: snake.cpp
#include "snake.h"

int Points=0;
integ map[mX,mY];

void Looser(){
    clrscr();
    printf("You loose with "); cout<<Points; printf(" points.\n");
    system("pause"); exit(0);
};

bool kb(snake &Snk){
  int input=getch();
  if(input==27)return false;
  if(input!=224)return true;
  input=getch();
  switch(input){
     case 75: Snk.Move_dir(4) ;break;
     case 72: Snk.Move_dir(8) ;break;
     case 80: Snk.Move_dir(2) ;break;
     case 77: Snk.Move_dir(6) ;break;
  };
  return true;
};

bool Change_map(const point P,const integ c){
     if( P.x<=0 || P.x>mX || P.y<=0 || P.y>mY ) return false;
     map[P.x,P.y]=c; 
     gotoxy(P.x,P.y);
     switch(c){
       case 0:printf(" "); break;
       case 1:printf("#"); break;     
       case 2:printf("*"); break;
       case 3:printf("."); break;
       case 4:printf("o"); break;
       case 5:printf("O"); break;
     };
};



void gotoxy(const integ x, const integ y)
{
   COORD coord;
   coord.X = x; coord.Y = y;
   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
};

void snake::Move_dir(const integ newDir){
   this->direc=newDir;
};

integ snake::Walk(){
    point P,N;
    integ m,l;   
    l=this->l;
    P=this->Cords[l];
    N=P;
    switch(this->direc){
       case 2:N.y++; break;
       case 4:N.x--; break;
       case 6:N.x++; break;
       case 8:N.y--; break;
    };
    m=map[N.x,N.y];
    Change_map(this->Cords[l],4);
    if(m==3){
       this->l=++l;
    }else{
       Change_map(this->Cords[0],0);
       for(integ i=0;i<l;i++){
          this->Cords[i]=this->Cords[i+1];
       }
    }
    this->Cords[l]=N;
    Change_map(N,5);
    return m;
    
};

snake::snake(){
  this->l=0;
  this->direc=6;
  this->GodMode=false;
  this->Cords[0].x=(int)(mX/2);
  this->Cords[0].y=(int)(mY/2);
  Change_map(this->Cords[0],5);
};

void Init_map(){
   point P;
   for(P.x=1;P.x<=mX;P.x++){
      for(P.y=1;P.y<=mY;P.y++){
         if( P.x==1  ||  P.x==mX || P.y==1  || P.y==mY){
             Change_map(P,1);
          }else{
             Change_map(P,0);
          };      
      }
   }
   
}

void Wait(long millisec) {
  struct timeb debut, fin;


  ftime(&debut);
  do
  {
    ftime(&fin);
  } while ((fin.time - debut.time) * 1000L + fin.millitm - debut.millitm < millisec);
};

int main(int argc, char *argv[])
{

     long A; time(&A); srand(A);

  Init_map();
  snake mySnake;
  integ m;
  long int v=50;
  bool again=true;
  point H;

  while(again){
       Wait(v); 
       if(kbhit())again=kb(mySnake);
       m=mySnake.Walk();
                            
                            
      
      switch(m){
          case 1:Looser();break;
          case 3:Points++;break;
          case 4:Looser();break;
      }
      
       if(rand()%10==0){
         H.x=rand()%(mX-2)+2;
         H.y=rand()%(mY-2)+2;
         Change_map(H,3);
       }

  };
  
  return 0;
}

and snake.h:
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef unsigned short int integ;
void gotoxy(const integ x,const integ y);
#include <conio.c>
#include <sys\timeb.h>
const integ mX=78;
const integ mY=24;
const integ mS=1000;

class point{
   public:
   integ x;
   integ y;   
};

class snake{
   public:
      integ Walk();
      void Move_dir(const integ newDirection);
      snake();
      bool GodMode;
   private:
      point  Cords[mS];
      integ l;
      integ direc;
};

Share this post


Link to post
Share on other sites
integ map[mX,mY];
I think you wanted integ map[mX][mY];. The comma operator evaluates both of its arguments and returns the second, so integ map[mX,mY]; is semantically equivalent to integ map[mY];. Equally, when you index into the map you will need to use map[x][y], not map[x,y].

Extra comments:
  • Using using namespace in a header is generally evil (although less so in a single-user-header project). It should certainly not be followed by further #include directives. Good advice would be to only use using namespace in source files, not headers. The reason for this is that there is no way to undo a using declaration, so any other file which includes that header will be implicitly using namespace as well, whether it wanted to or not.

  • You are quite inconsistent in your use of case, indentation, naming and brace placement. Find a style that suits you and stick with it, doing so makes code much easier to read.

  • The header <stdio.h> is deprecated in C++ and replaced with <cstdio>

  • I expect you wanted to #include <conio.h>, not #include <conio.c>.

EDIT: In future, please use [source] tags when posting large blocks of code.

Σnigma

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this