The game currently runs, but the problem im having is that for some reason, its not detecting the range between the ships correctly, and i know there's something in my code that im doing wrong, but no matter how much i tr to go through it, i just can figure out whats wrong. So can you guys help me debugging this and suggest what i should do? Thanks
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
struct ships
{
int agility; //the amount of moves it has each turn
int armor; //the "health points" of each ship
int weapons; //amount of weapons it has
int range; //how far a ship can shoot from its current position
};
void initGrid(char [][10], int, int, int, int); //assign characters to different aspects of the grid
void printBoard (char[][10]); //print the grid with the respective characters
void assignAttributes (ships & p1); //asks players for the attributes of his/her ship
void cpuShooting (ships & p1, ships cpu); //calculates how much damage a shot will do each time the cpu fires depending on weapons
void playerShooting (ships p1, ships & cpu); //calculates how much damage a shot will do each time the player fires depending on weapons
void movement (char [][10], int&, int&, int&, int&, ships&p1, ships&cpu); //asks player for his moves
void cpumovement (char [][10], int&, int&, int&, int&, ships&cpu, ships&p1); //makes cpu move randomly
bool gameOver (ships p1, ships cpu); //makes the game end when armor for plaer or cpu is less than -5
int main(int argc, char *argv[])
{
srand(time(0));
char choice;
ships p1;
ships cpu;
char gameboard[10][10];
//attributes for the player's ship
p1.agility;
p1.armor;
p1.weapons;
p1.range;
//randomly assigned attributes for the cpu ship
cpu.agility = rand()%6+1;
cpu.armor = rand()%6+1;
cpu.range = rand()%6+1;
cpu.weapons = rand()%4+1;
//player and cpu starting positions
int p1_row = 1;
int p1_col = 1;
int cpu_row = 8;
int cpu_col = 8;
cout << "\n\n";
cout << "\t Heyo, wanna play the game? (Y = yes, N = exit): ";
cin >> choice;
cout << "\n\n";
switch (choice)
{
case 'Y':
case 'y':
cout << " \t Instructions: ""w"" is up, ""a"" is left, ""d"" is right, and ""s"" is down \n";
initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
printBoard(gameboard);
assignAttributes (p1);
while(gameOver (p1, cpu))
{
movement (gameboard, p1_row, p1_col, cpu_row, cpu_col, p1, cpu);
cpumovement (gameboard, p1_row, p1_col, cpu_row, cpu_col, cpu, p1);
}
cout << "Game over! \n";
break;
case 'N':
case 'n':
exit(0);
break;
default:
cout << "Wrong input. It's either: Y to play, or N to quit.\n";
}
system("PAUSE");
return EXIT_SUCCESS;
}
void initGrid(char gameboard[][10], int p1_row, int p1_col, int cpu_row, int cpu_col)
{
for(int r = 0; r < 10; r++)
{
for (int c = 0; c < 10; c++)
{
if (r == 0 || r == 9)
gameboard[r][c] = 205;
else if(c == 0 || c == 9)
gameboard[r][c] = 186;
else
gameboard[r][c] = ' ';
}
}
gameboard[0][0] = 201;
gameboard[9][0] = 200;
gameboard[0][9] = 187;
gameboard[9][9] = 188;
gameboard[p1_row][p1_col] = 193; //single T- player
gameboard[cpu_row][cpu_col] = 208; //double TT- computer
}
void printBoard (char gameboard[][10])
{
for (int r = 0; r < 10; r++)
{
for (int c = 0; c < 10; c++)
{
cout << gameboard[r][c];
}
cout << endl;
}
}
void movement (char gameboard [][10], int& p1_row, int& p1_col, int& cpu_row, int& cpu_col, ships & p1, ships & cpu)
{
char move;
for(int moves = 0; moves < p1.agility; moves++)
{
cout << "Please enter your moves : ";
cin >> move;
switch (move)
{
case 'w': //if "move" is assigned w or W .. move up on the board
case 'W':
p1_row = p1_row - 1;
gameboard[p1_col][p1_row] = 193;
system("CLS");
initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
printBoard(gameboard);
if (p1_row <= 0)
{
p1_row = p1_row + 9;
gameboard[p1_col][p1_row] = 193;
system("CLS");
initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
printBoard(gameboard);
}
break;
case 'a': //if "move" is assigned a or A.. move left on the board
case 'A':
p1_col = p1_col - 1;
gameboard[p1_col][p1_row] = 193;
system("CLS");
initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
printBoard(gameboard);
if (p1_col <= 0)
{
p1_col = p1_col + 9;
gameboard[p1_col][p1_row] = 193;
system("CLS");
initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
printBoard(gameboard);
}
break;
case 'd': //if "move" is assigned d or D.. move right on the board
case 'D':
p1_col = p1_col + 1;
gameboard[p1_col][p1_row] = 193;
system("CLS");
initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
printBoard(gameboard);
if (p1_col >= 9)
{
p1_col = p1_col - 9;
gameboard[p1_col][p1_row] = 193;
system("CLS");
initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
printBoard(gameboard);
}
break;
case 's': //if "move" is assigned s or S.. move down on the board
case 'S':
p1_row = p1_row + 1;
gameboard[p1_col][p1_row] = 193;
system("CLS");
initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
printBoard(gameboard);
if (p1_row >= 10)
{
p1_row = p1_row - 9;
gameboard[p1_col][p1_row] = 193;
system("CLS");
initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
printBoard(gameboard);
}
break;
case 'f':
case 'F':
if (cpu_row <= p1.range && cpu_col <= p1.range)
{
playerShooting (p1, cpu);
cout << "CPU's armor is: " << cpu.armor;
}
else
{
cout << "You are too far away! \n";
}
break;
default:
cout << "You put in a wrong move. \n";
break;
}
}
}
void assignAttributes (ships & p1)
{
cout << "Please enter agility (only from 0 to 6): ";
cin >> p1.agility;
cout << endl;
while(p1.agility < 0 || p1.agility > 6)
{
cout << "Too low or too high! PLease enter agility again (only from 0 to 6): ";
cin >> p1.agility;
cout << endl;
}
cout << "Please enter armor (only from 0 to 6): ";
cin >> p1.armor;
cout << endl;
while(p1.armor < 0 || p1.armor > 6)
{
cout << "Too low or too high! PLease enter armor again (only from 0 to 6): ";
cin >> p1.armor;
cout << endl;
}
cout << "Please enter weapons (only from 0 to 4!!!): ";
cin >> p1.weapons;
cout << endl;
while(p1.weapons < 0 || p1.weapons > 4)
{
cout << "Too low or too high! PLease enter weapons again (only from 0 to 4!!!): ";
cin >> p1.weapons;
cout << endl;
}
cout << "Please enter range (only from 0 to 6): ";
cin >> p1.range;
cout << endl;
while(p1.range < 0 || p1.range > 6)
{
cout << "Too low or too high! PLease enter range again (only from 0 to 6): ";
cin >> p1.range;
cout << endl;
}
/*assigns random attributes to the commputers ship
cpu.agility = rand()%6+1;
cpu.armor = rand()%6+1;
cpu.weapons = rand()%4+1;
cpu.range = rand()%6+1;
*/
}
void cpumovement (char gameboard [][10], int& p1_row, int& p1_col, int& cpu_row, int& cpu_col, ships & cpu, ships & p1) //random cpu ship movement
{
int s;
for(int moves = 0; moves <= cpu.agility; moves++)
{
s = rand()%5+1;
switch (s)
{
case 1: //if "s" is assigned 1.. move up on the board
cpu_row = cpu_row - 1;
gameboard[cpu_col][cpu_row] = 208;
system("CLS");
initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
printBoard(gameboard);
if (cpu_row <= 0)
{
cpu_row = cpu_row + 9;
gameboard[cpu_row][cpu_row] = 193;
system("CLS");
initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
printBoard(gameboard);
}
break;
case 2: //if "s" is assigned 2.. move left on the board
cpu_col = cpu_col - 1;
gameboard[cpu_col][cpu_row] = 208;
system("CLS");
initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
printBoard(gameboard);
if (cpu_col <= 0)
{
cpu_col = cpu_col + 9;
gameboard[cpu_col][cpu_row] = 193;
system("CLS");
initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
printBoard(gameboard);
}
break;
case 3: //if "s" is assigned 3.. move right on the board
cpu_col = cpu_col + 1;
gameboard[cpu_col][cpu_row] = 208;
system("CLS");
initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
printBoard(gameboard);
if (cpu_col >= 10)
{
cpu_col = cpu_col - 9;
gameboard[cpu_col][cpu_row] = 193;
system("CLS");
initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
printBoard(gameboard);
}
break;
case 4: //if "s" is assigned 4.. move down on the board
cpu_row = cpu_row + 1;
gameboard[cpu_col][cpu_row] = 208;
system("CLS");
initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
printBoard(gameboard);
if (cpu_row >= 10)
{
cpu_row = cpu_row - 9;
gameboard[cpu_col][cpu_row] = 193;
system("CLS");
initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
printBoard(gameboard);
}
break;
case 5:
if (p1_row <= cpu.range && p1_col <= cpu.range)
{
cpuShooting (p1, cpu);
cout << "Player's armor is: " << cpu.armor;
}
else
{
cout << "The computer was too far away! \n";
}
break;
}
}
}
bool gameOver (ships p1, ships cpu)
{
bool over = true;
if(p1.armor < -6 || cpu.armor < -6)
{
over = false;
}
return over;
}
void cpuShooting (ships & p1, ships cpu)
{
if(cpu.weapons == 1)
{
p1.armor = p1.armor - 1;
}
else if(cpu.weapons == 2)
{
p1.armor = p1.armor - 2;
}
else if(cpu.weapons == 3)
{
p1.armor = p1.armor - 3;
}
else if(cpu.weapons == 4)
{
p1.armor = p1.armor - 4;
}
}
void playerShooting (ships p1, ships & cpu)
{
if(p1.weapons == 1)
{
cpu.armor = cpu.armor - 1;
}
else if(p1.weapons == 2)
{
cpu.armor = cpu.armor - 2;
}
else if(p1.weapons == 3)
{
cpu.armor = cpu.armor - 3;
}
else if(p1.weapons == 4)
{
cpu.armor = cpu.armor - 4;
}
}
This post has been edited by halopower67: 10 December 2010 - 09:15 PM

New Topic/Question
Reply




MultiQuote









|