帮忙改个程序,怎么也找不到哪里错了
帮忙改个程序,以下是代码
#include <iostream.h>
#include <stdlib.h>
#include <math.h>
#define ground 0 //实地
#define home 1 //巢穴
#define food 2 //食物
#define water 3 //水源
#define poison 4 //毒药
#define home_row 5
#define home_col 6 //蚁窝的位置
#define poison_num 7 //毒药数量
#define food_num 8 //事物数量
#define water_num 9 //水源数量
#define row_max 20
#define col_max 20 //最大的行数和列数
int main()
{
enum stat{for_food,go_home,for_water,death}state;
int i ,j ;
int map[row_max][col_max];
struct AI
{
int lifes; //生命数量/有几条命
enum stat state; //当前状态
int start_row; //开始行位置
int start_col; //开始列位置
}ant;
AI ant; //蚂蚁
ant.lifes = 1;
ant.state = for_food;
ant.start_row = rand()%20;
ant.start_col = rand()%20;
int ant_row = ant.start_row;
int ant_col = ant.start_col;
for(i = 0;i < row_max; i++) //at the first , set the map
for(j = 0;j < col_max; j++)
map[i][j] = ground;
for(i = 0; i < food_num ; i++) //set food 's position
map[rand()%20,rand()%20] = food;
for(i = 0; i < water_num ; i++) //set water 's position
map[rand()%20,rand()%20] = water;
for(i = 0; i < poison_num ; i++) //set poison 's position
map[rand()%20,rand()%20] = poison;
map[home_row,home_col] = home; //set home 's position
void forfood();
void gohome();
void forwater();
void dead();
void forfood() //找食物
{
int row_step = rand()%2-1;
int col_step = rand()%2-1;
ant_row += row_step;
ant_col += col_step;
if(ant_row < 0)ant_row = 0;
if(ant_row > (row_max-1))ant_row = (row_max-1);
if(ant_col < 0)ant_col = 0;
if(ant_col > (col_max-1))ant_col = (col_max-1);
if(map[ant_row,ant_col] == food)
{
map[ant_row,ant_col] = ground;
map[rand()%20,rand()%20] = food;
ant.state = go_home;
}
if(map[ant_row,ant_col] == poison)
{
map[rand()%20,rand()%20] = poison;
ant.state = death;
}
};
void gohome() //回家
{
if(ant_row > home_row)
ant_row--;
else ant_row++;
if(ant_col > home_col)
ant_col--;
else ant_col++;
if(map[ant_row,ant_col] == home)
{
ant.lifes++;
ant.state = for_water;
}
if(map[ant_row,ant_col] == poison)
{
map[ant_row,ant_col] = ground;
map[rand()%20,rand()%20] = poison;
ant.state = death;
}
};
void forwater() //找水
{
int row_step = rand()%2-1;
int col_step = rand()%2-1;
ant_row += row_step;
ant_col += col_step;
if(ant_row < 0)ant_row = 0;
if(ant_row > (row_max-1))ant_row = (row_max-1);
if(ant_col < 0)ant_col = 0;
if(ant_col > (col_max-1))ant_col = (col_max-1);
if(map[ant_row,ant_col] == water)
{
map[ant_row,ant_col] = ground;
map[rand()%20,rand()%20] = water;
ant.state = for_food;
}
if(map[ant_row,ant_col] == poison)
{
map[rand()%20,rand()%20] = poison;
ant.state = death;
}
};
void dead() //死亡
{
ant.lifes--;
if(ant.lifes > 0)
{
ant.start_row = home_row;
ant.start_col = home_col;
ant_row = ant.start_row;
ant_col = ant.start_col;
ant.state = for_food;
}
else
cout < < "GAME OVER ! " < </n;
};
while(ant.lifes > 0)
switch(ant.state)
{
case for_food: forfood();break;
case go_home: gohome();break;
case for_water: forwater();break;
case death: dead();break;
}
return 0;
}
以下是调试信息,看的莫名其妙,用的是DEV-CPP
编译器: Default compiler
执行 g++.exe...
g++.exe "E:\陆\个人\开发\DEV\Ants.cpp " -o "E:\陆\个人\开发\DEV\Ants.exe " -I "C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include " -I "C:\Dev-Cpp\include\c++\3.4.2\backward " -I "C:\Dev-Cpp\include\c++\3.4.2\mingw32 " -I "C:\Dev-Cpp\include\c++\3.4.2 " -I "C:\Dev-Cpp\include " -L "C:\Dev-Cpp\lib "
In file included from C:/Dev-Cpp/include/c++/3.4.2/backward/iostream.h:31,
from E:\陆\个人\开发\DEV\Ants.cpp:1:
C:/Dev-Cpp/include/c++/3.4.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h> . To disable this warning use -Wno-deprecated.
E:\陆\个人\开发\DEV\Ants.cpp: In function `int main() ':
E:\陆\个人\开发\DEV\Ants.cpp:36: error: redeclaration of `main()::AI ant '
E:\陆\个人\开发\DEV\Ants.cpp:34: error: `main()::AI ant ' previously declared here
E:\陆\个人\开发\DEV\Ants.cpp:49: error: incompatible types in assignment of `int ' to `int[20] '
E:\陆\个人\开发\DEV\Ants.cpp:52: error: incompatible types in assignment of `int ' to `int[20] '
E:\陆\个人\开发\DEV\Ants.cpp:55: error: incompatible types in assignment of `int ' to `int[20] '
E:\陆\个人\开发\DEV\Ants.cpp:57: error: incompatible types in assignment of `int ' to `int[20] '
E:\陆\个人\开发\DEV\Ants.cpp:64: error: expected primary-expression before "void "
E:\陆\个人\开发\DEV\Ants.cpp:64: error: expected `; ' before "void "
E:\陆\个人\开发\DEV\Ants.cpp:88: error: expected primary-expression before "void "
E:\陆\个人\开发\DEV\Ants.cpp:88: error: expected `; ' before "void "
E:\陆\个人\开发\DEV\Ants.cpp:111: error: expected primary-expression before "void "
E:\陆\个人\开发\DEV\Ants.cpp:111: error: expected `; ' before "void "
E:\陆\个人\开发\DEV\Ants.cpp:135: error: expected primary-expression before "void "
E:\陆\个人\开发\DEV\Ants.cpp:135: error: expected `; ' before "void "
执行结束
[解决办法]
单从编译上看,这样没错误了:
//#include <iostream.h>
//#include <stdlib.h>
//#include <math.h>
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
#define ground 0 //实地
#define home 1 //巢穴
#define food 2 //食物
#define water 3 //水源
#define poison 4 //毒药
#define home_row 5
#define home_col 6 //蚁窝的位置
#define poison_num 7 //毒药数量
#define food_num 8 //事物数量
#define water_num 9 //水源数量
#define row_max 20
#define col_max 20 //最大的行数和列数
enum {for_food,go_home,for_water,death}state;
struct AI
{
int lifes; //生命数量/有几条命
int state; //当前状态
int start_row; //开始行位置
int start_col; //开始列位置
}ant;
int ant_row = ant.start_row;
int ant_col = ant.start_col;
int map[row_max][col_max];
int main()
{
int i ,j ;
//AI ant; //蚂蚁
ant.lifes = 1;
ant.state = for_food;
ant.start_row = rand()%20;
ant.start_col = rand()%20;
for(i = 0;i < row_max; i++) //at the first , set the map
for(j = 0;j < col_max; j++)
map[i][j] = ground;
for(i = 0; i < food_num ; i++) //set food 's position
map[rand()%20][rand()%20] = food;
for(i = 0; i < water_num ; i++) //set water 's position
map[rand()%20][rand()%20] = water;
for(i = 0; i < poison_num ; i++) //set poison 's position
map[rand()%20][rand()%20] = poison;
map[home_row][home_col] = home; //set home 's position
void forfood();
void gohome();
void forwater();
void dead();
while(ant.lifes > 0)
switch(ant.state)
{
case for_food: forfood();break;
case go_home: gohome();break;
case for_water: forwater();break;
case death: dead();break;
}
return 0;
}
void forfood() //找食物
{
int row_step = rand()%2-1;
int col_step = rand()%2-1;
ant_row += row_step;
ant_col += col_step;
if(ant_row < 0)ant_row = 0;
if(ant_row > (row_max-1))ant_row = (row_max-1);
if(ant_col < 0)ant_col = 0;
if(ant_col > (col_max-1))ant_col = (col_max-1);
if(map[ant_row][ant_col] == food)
{
map[ant_row][ant_col] = ground;
map[rand()%20][rand()%20] = food;
ant.state = go_home;
}
if(map[ant_row][ant_col] == poison)
{
map[rand()%20][rand()%20] = poison;
ant.state = death;
}
};
void gohome() //回家
{
if(ant_row > home_row)
ant_row--;
else ant_row++;
if(ant_col > home_col)
ant_col--;
else ant_col++;
if(map[ant_row][ant_col] == home)
{
ant.lifes++;
ant.state = for_water;
}
if(map[ant_row][ant_col] == poison)
{
map[ant_row][ant_col] = ground;
map[rand()%20][rand()%20] = poison;
ant.state = death;
}
};
void forwater() //找水
{
int row_step = rand()%2-1;
int col_step = rand()%2-1;
ant_row += row_step;
ant_col += col_step;
if(ant_row < 0)ant_row = 0;
if(ant_row > (row_max-1))ant_row = (row_max-1);
if(ant_col < 0)ant_col = 0;
if(ant_col > (col_max-1))ant_col = (col_max-1);
if(map[ant_row][ant_col] == water)
{
map[ant_row][ant_col] = ground;
map[rand()%20][rand()%20] = water;
ant.state = for_food;
}
if(map[ant_row][ant_col] == poison)
{
map[rand()%20][rand()%20] = poison;
ant.state = death;
}
};
void dead() //死亡
{
ant.lifes--;
if(ant.lifes > 0)
{
ant.start_row = home_row;
ant.start_col = home_col;
ant_row = ant.start_row;
ant_col = ant.start_col;
ant.state = for_food;
}
else
cout < < "GAME OVER !\n ";
};