Tuesday 31 May 2016

c++ - there is a problem in placing the ships and hitting also ,,please any one can solve this

#include
#include
#include
#include using namespace std; char userarr[10][10]; char comparr[10][10]; const int WIDTH = 10, HEIGHT = 10; void checkhit(char comparr[][10]) { // char comparr[WIDTH][HEIGHT]; char ch = 'A'; cout << " "; for (char ch = 'A'; ch < 'K'; ch++) cout << " " << char(ch); cout << endl; for (int i = 0; i < 10; i++) {

cout << i << " "; for (int j = 0; j < 10; j++) { //comparr[i][j] = 'o'; cout << comparr[i][j] << " "; } cout << endl; } }


void sirfcheck() { system("cls"); int temp = 0; char ch = 'A'; cout << " "; for (char ch = 'A'; ch < 'K'; ch++) cout << " " << char(ch); cout << endl; for (int i = 0; i < 10; i++, temp++) {

cout << temp << " "; for (int j = 0; j < 10; j++) { cout << comparr[i][j] << " "; } cout << endl; }

} void check() { cout << "-----------------user--------------------"<> hit1>> hit2; int hit3 = rand() % 10; int hit4 = rand() % 10; if (comparr[hit1][hit2] == 'A' || comparr[hit1][hit2] == 'B' || comparr[hit1][hit2] == 'C' || comparr[hit1][hit2] == 'D' || comparr[hit1][hit2] == 'E') { comparr[hit1][hit2] == 'H'; score += 1; cout << endl; cout << score<
} if (userarr[hit3][hit4] == 'A' || userarr[hit3][hit4] == 'B' || userarr[hit3][hit4] == 'C' || userarr[hit3][hit4] == 'D' || userarr[hit3][hit4] == 'E') { userarr[hit3][hit4] == 'H'; score += 1; cout << endl; cout << score << endl;

} else { userarr[hit3][hit4] == 'M'; score -=0.25; cout << endl; cout << score << endl; } } void show2array(char array1[][WIDTH]) { cout << "----------------------------------------" << endl; int temp = 0; char ch = 'A'; cout << " "; for (char ch = 'A'; ch < 'K'; ch++) cout << " " << char(ch); cout << endl; for (int i = 0; i < 10; i++, temp++) {


cout << temp << " "; for (int j = 0; j < 10; j++) { cout << array1[i][j] << " "; } cout << endl; } }



void computer(char array1[][WIDTH]) { srand(time(0)); //char array1[HEIGHT][WIDTH]; char place1[5] = { 'A','B','C','D','E' }; //string arr[5] = { "Aircraft Carrier (size 5)","Battleship (size 4)","Cruiser (size 3)","Submarine (size 3)","Destroyer (size 2)" }; char ch; int size, starting, x1 = 0; //x1 for counting cout << endl; for (int i = 0; i < WIDTH; i++) {// clear grid for (int j = 0; j < HEIGHT; j++) { array1[i][j] = 'o'; } } //cout << "You have to place 5 ships." << endl; for (int j = 1, air = 0, num = 6; j <= 5; j++, air++) { int l = 5; //cout << "Where u want to place the ship no:" << ++x1 << endl; //cout << "Enter starting and ending points of " << arr[air] << " is:"; label1: starting = rand() % 10; int first = starting; size = rand() % 10; int second = size; //if (j == 1) /*if (array1[first][second] == 'A' && array1[first][second] == 'B' &&array1[first][second] == 'C' &&array1[first][second] == 'D' &&array1[first][second] == 'E') { cout << " invalid input!"; goto label1; }*/ if (starting < size) { for (int i = starting; i < size; i++) { if (array1[starting][i] != 'o') { cout << " invalid input!"; goto label1; } } } else if (starting > size) { for (int i = size; i < starting; i++) { if (array1[size][i] != 'o') { cout << " invalid input!"; goto label1; } } } if (j == 1 && starting>5) { //cout << "Invalid placement."; goto label1; } //else if (j == 2) if (j == 2 && starting > 6) { //cout << "Invalid placement."; goto label1; } //else if (j == 3) if (j == 3 && starting > 7) { //cout << "Invalid placement."; goto label1; } //else if (j == 3) if (j == 4 && starting
> 8) { //cout << "Invalid placement."; goto label1; } //else if (j == 2) if (j == 5 && starting > 9) { //cout << "Invalid placement."; goto label1; } //cout << "Enter either y-axis or x-axis" << endl << "Press x for x-axis" << endl << "Press y for y-axis" << endl; int comp = rand() % 2 + 1;

if (comp == 1) { /*srand(time(0)); int x = rand() % (size - starting); int y = rand() % (size - starting);*/ int x = starting; int y = starting; //for(int num=5,air=0,air++) if (air == 3) num = num + 1; int lx = starting + --num;

for (int j = starting, temp1 = 0; j

/*array[x][y + 1] = 1; array[x][y + 2] = 1; array[x][y + 3] = 1; array[x][y + 4] = 1; array[x][y + 5] = 1; array[x][y + 6] = 1; array[x][y + 7] = 1; array[x][y + 8] = 1; array[x][y + 9] = 1;*/ for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) { comparr[i][j]
= array1[i][j]; } show2array(array1); } } /*void show1comp() { char array1[WIDTH][HEIGHT]; char ch = 'A'; cout << " "; for (char ch = 'A'; ch < 'K'; ch++) cout << " " << char(ch); cout << endl; for (int i
= 0; i < 10; i++) {

cout << i << " "; for (int j = 0; j < 10; j++) { array1[i][j] = 'o'; cout << array1[i][j] << " "; } cout << endl; } cout << "-------------------------------------"; computer(array1); }*/ //
=------------------------------------------------------user-------------------------------------*/ void show1(char array[][HEIGHT]) { system("cls"); int temp = 0; char ch = 'A'; cout << " "; for (char ch = 'A'; ch < 'K'; ch++) cout << " " << char(ch); cout << endl; for (int i = 0; i < 10; i++, temp++) {

cout << temp << " "; for (int j = 0; j < 10; j++) { cout << array[i][j] << " "; } cout << endl; }


} void place(char array[][HEIGHT]) { char place1[5] = { 'A','B','C','D','E' }; string arr[5] = { "Aircraft Carrier (size 5)","Battleship (size 4)","Cruiser (size 3)","Submarine (size 3)","Destroyer (size 2)" }; char ch; int size, starting, x1 = 0; //x1 for counting cout << endl; for (int i = 0; i < WIDTH; i++) // clear grid for (int j = 0; j < HEIGHT; j++) array[i][j] = 'o'; cout << "You have to place 5 ships." << endl; for (int j = 1, air = 0, num = 6; j <= 5; j++, air++) { int l = 5; cout << "Where u want to place the ship no:" << ++x1 << endl; label: cout << "Enter starting and ending points of " << arr[air] << " is:"; cin >> starting; cin >> size; //if(j==1) /*if (array[starting][size] == 'A' || array[i][j] == 'B' || array[i][j] == 'C' || array[i][j] == 'D' || array[i][j] == 'E') { cout << "invalid" << endl; goto label; }*/ if (starting < size) { for (int i = starting; i < size; i++) { if (array[starting][i] != 'o') { cout << " invalid input!"; goto label; } } } else if (starting > size) { for (int i = size; i < starting; i++) { if (array[size][i] != 'o') { cout << " invalid input!"; goto label; } } } if (j == 1 && starting>5) { cout << "Invalid placement."; goto label; } //else if(j==2) else if (j == 2 && starting > 6) { cout << "Invalid placement."; goto label; } //else if (j == 3) else if (j == 3 && starting > 7) { cout << "Invalid placement."; goto label; } //else if (j == 3) else if (j == 4 && starting > 8) { cout << "Invalid placement."; goto label; } //else if (j == 2) else if (j == 5 && starting > 9) { cout << "Invalid placement."; goto label; } cout << "Enter either y-axis or x-axis" << endl << "Press x for x-axis" << endl << "Press y for y-axis" << endl; cin >> ch;

if (ch == 'x' || ch == 'X') { /*srand(time(0)); int x = rand() % (size
- starting); int y = rand() % (size - starting);*/ int x = starting; int y = starting; //for(int num=5,air=0;;num--,air++) if (air == 3) num = num + 1; int lx = starting + --num;

for (int j = starting, temp1 = 0; j
} } else if (ch == 'y' || ch == 'Y') { /*srand(time(0)); int x = rand() % (size - starting); int y = rand() % (size - starting);*/ int x = starting; int y = starting; if (air == 3) num = num + 1; int ly = starting + --num; for (int i = starting, temp = 0; i
/*array[x][y + 1] = 1; array[x][y + 2] = 1; array[x][y + 3] = 1; array[x][y + 4] = 1; array[x][y + 5] = 1; array[x][y + 6] = 1; array[x][y + 7] = 1; array[x][y + 8] = 1; array[x][y + 9] = 1;*/ for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) { userarr[i][j]

= array[i][j]; } show1(array); } } void show() { char array[WIDTH][HEIGHT]; char ch = 'A'; cout << " "; for (char ch = 'A'; ch < 'K'; ch++) cout << " " << char(ch); cout << endl; for (int i = 0; i < 10; i++) {

cout << i << " "; for (int j = 0; j < 10; j++) { array[i][j] = 'o'; cout << array[i][j] << " "; } cout << endl; } cout << "-------------------------------------"; place(array); computer(array); } int main() { int ships = 0; show();



//show1comp(); cout << "-------------------"; check(); cout << "-----------------------------"; hitting(); system("pause"); return 0; }

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...