simple game.

Hi guys, i am making a basic game that uses a menu system for the user to move a tank represented by T,tank can move up down left and right,in the enviroment there are barriers,which the tank can shoot down,please view my code and help with errors. making use of a main(for menu),a source and header file.

[main]
#include "tank.h"
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
using namespace TankSpace;

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

srand(time(0));

if(argc!=3)
{
cerr << "Usage" << argv[0] << "[Number of rows] [Number of columns]" << endl;
return(INVALID);
}

int rows=toInteger(argv[1]);
int cols=toInteger(argv[2]);

int ** world= MakeWorld(rows,cols);
InitWorld(world,rows,cols);

int tRow=0;
int tCol=rand()%cols;
world[tRow][tCol]=TANK;

int fRow=rows-(rand()%2+1);
int fCol=rand()%cols;
world[fRow][fCol]=BARRIER;

int option=' ';

do
{
DisplayWorld(world,rows,cols);

cout << "8: UP" <<endl;
cout << "2: DOWN" <<endl;
cout << "4:LEFT"<< endl;
cout << "6:RIGHT" << endl;
cout << "x:EXIT" << endl;

cin >>option;
option = tolower(option);

Movement(world,rows,cols,tRow,tCol,option);

system("cls");

if(world[fRow][fCol]==TANK)
{
cout << "YOU WIN" << endl;
option = 'x';
}

}
while(option != 'x');

DestroyWorld(world,rows);

return 0;

[main]

[source]
#include "tank.h"
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <ctime>
using namespace std;

namespace TankSpace
{
int makeWorld(int rows,int cols)
{
int** world=new int*[rows];
for(int r=0; r< rows; r++)
{
world[r]=new int[cols];

for(int c=0;c<cols;c++)
{
world[r][c]=EMPTY;
}
}
return world;
}

void DestroyWorld(int** & world,int rows)
{
for(int r=0;r <rows;r++)
{
delete [] world [r];
}
delete [] world;
world=NULL;
}

void initWorld(int** world,int rows,int cols)
{
for(int r=0;r<rows;r++)
{
for(int c=0;c<cols;c++)
{
if(rand()%100<20)
{
world[r][c]=SENSITIVE;
}
}
}
}

void DisplayWorld(int** world,int rows,int cols)
{
char e=176;
for(int r=0;r<rows;r++)
{
for(int c=0;c<cols;c++)
{
switch (world[r][c])
{
case EMPTY:
cout << e;
break;

case TANK:
cout << "T";
break;

case BARRIER:
cout << "B";
break;

case SENSITIVE:
cout <<"S";
break;

}
}
cout << endl;
}
}

void Movement(int** world,int rows,int cols,int& tRow,int& tCol,int option)
{
switch (option)
{
case'8':
if(tRow>0 && world[tRow-1][tCol]!=SENSITIVE)
{
world[tRow][tCol]=EMPTY;
tRow--;
world[tRow][tCol]=TANK;
}
break;

case'2':
if(tRow<rows-1 && world[tRow+1][tCol]!=SENSITIVE)
{
world[tRow][tCol]=EMPTY;
tRow++;
world[tRow][tCol]=TANK;
}
break;

case '4':
if(tCol > 0 && world[tRow][tCol-1]!=SENSITIVE)
{

world[tRow][tCol]=EMPTY;
tCol--;
world[tRow][tCol]=TANK;
}
break;

case '6':
if(tCol<tCol-1 && world[tRow][tCol+1]!=SENSITIVE)
{
world[tRow][tCol]=EMPTY;
tCol++;
world [tRow][tCol]=TANK;
}
break;
}
}
}

[source]

[header]
#ifndef TANK_H_
#define TANK_H_

#include <string>

namespace TankSpace
{

const int INVALID=1;

const int EMPTY=0;
const int TANK=1;
const int BARRIER=2;
const int SENSITIVE=3;

int toInteger(std::string str);

int** MakeWorld(int rows,int cols);
void DestroyWorld(int** & world,int rows);
void InitWorld(int** world, int rows,int cols);
void DisplayWorld(int** world,int rows,int cols);

void Movement(int** world,int rows,int cols,int& tRow,int& tCol,int opetion);

}



#endif /* TANK_H_ */

[header]
This will not compile - some major errors are:
- Function definitions in source do not match their declaration in header:
InitWorld and MakeWorld are implemented with lowercase letter in front.
- MakeWorld in source file is defined as returning wrong type (int instead of int**)
- toInteger is not implemented at all.

Also, Movement will not work: you use switch and expect char values for '8','2','4','6'. You should use unquoted digits for that.
Last edited on
Topic archived. No new replies allowed.