1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
/***********************
This is the part that causes the error
************************/
void Ant::moveAnt(int a, int boardRow, int boardCol, char** board)
{
if (a==0)//very first loop
{
compass=1; //always starts facing north
}
//ant faces north
if (compass==1)
{
//ant on a white space
if (board[xRow][yCol] != '#')//SEGMENTATION FAULT IS HERE
{
compass = 2; //turn right 90 degrees
board[xRow][yCol] = '#'; //change space to black
if ((yCol+1)<boardCol) //keep movement in bounds upper
{
board[xRow][yCol+1] = '*'; //move ant forward
yCol = yCol+1; //update ant location
}
}
.
.
.
.
.
/****************************
header file for ant class
****************************/
// Ant.hpp is the ant class specification file
#ifndef ANT_HPP
#define ANT_HPP
// ant class declaration
class Ant
{
private:
//keeps track of where the ant is
int xRow;
int yCol;
//tracks ant orientation
int compass;
public:
int antRow(int row);
int antCol(int column);
int randAntRow(int row, int seed);
int randAntCol(int column, int seed);
void moveAnt(int a, int boardRow, int boardCol, char** board);
};
#endif
/************************
main
************************/
#include <iostream>
#include "menu.hpp"
#include "getInt.hpp"
#include "Ant.hpp"
int main()
{
while (true)
{
Ant ant; //create Ant object
int play, randomize, boardRow, boardCol, gameSteps, sRow, sCol;
menu(play, randomize, boardRow, boardCol, gameSteps, sRow, sCol); //will get user input
if (play==1) //user chose to play
{
//dynamically create 2D array
char** board = NULL;
board = new char* [boardRow];
for (int count=0; count < boardRow; count++)
{
board[count]=new char[boardCol];
}
//intitialize array to empty
for (int i=0; i<boardRow; i++)
{
for (int j=0; j<boardCol; j++)
{
board[i][j] = '.';
}
}
//place ant on board
//subtract 1 or else it will display off by one
board[sRow][sCol] = '*';
//start moving the ant
for (int a=0; a<gameSteps; a++)
{
std::cout << "Step " << a << std::endl;
//print array
for (int x = 0; x<boardRow; x++)
{
for (int y = 0; y<boardCol; y++)
{
std::cout << board[x][y] << ' ';
}
std::cout <<std::endl;
}
//move ant
ant.moveAnt(a, boardRow, boardCol, board);
}
.
.
.
.
.
|