Battleship AI Implementation - c++

The figures in the walkthrough portion of this article detail the algorithm fairly well.
I've developed the following short program:
#include <iostream>
using namespace std;
int main() {
int arr['K' - 'A'][10 - 0] = {0};
int largestShip;
//grid
cout << " ";
for(int i = 0; i < 10; i++) {
cout << (i + 1) % 10;
}
cout << endl << " ____________" << endl;;
for(char c = 'A'; c < 'K'; c++) {
cout << c << " |";
for(int i = 0; i < 10; i++) {
cout << arr[c - 'A'][i];
}
cout << "|" << endl;
}
cout << endl;
//endgrid
//heatmap
largestShip = 5;
for(char c = 'A'; c < 'K'; c++) {
for(int i = 0; (i + largestShip - 1) < 10; i++) {
for(int j = 0; j < largestShip; j++) {
arr[c - 'A'][j + i]++;
}
}
}
for(char c = 'A'; (c + largestShip - 1) < 'K'; c++) {
for(int i = 0; i < 10; i++) {
for(int j = 0; j < largestShip; j++) {
arr[c - 'A' + j][i]++;
}
}
}
//endheatmap
//showheatmap
cout << " ";
for(int i = 0; i < 10; i++) {
cout << (i + 1) % 10;
}
cout << endl << " ____________" << endl;;
for(char c = 'A'; c < 'K'; c++) {
cout << c << " |";
for(int i = 0; i < 10; i++) {
cout << arr[c - 'A'][i] % 10;
}
cout << "|" << endl;
}
cout << endl;
//endshowheatmap
return 0;
}
It sets the initial probability of the squares whose precedence is set by the size of its corresponding integer values in the array of values.
However, I'm not very well versed in probability theory, and am uncertain how to approach and understand the necessary methods for manipulating the probability of the squares (and by association, their values) in order to accurately assign the probability at any given square after some shot is taken at that square.
An explanation of the effects of a shot on the probability values of the squares within the grid would be helpful.

Related

How can I merge three functions into one? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have written next code but 3 functions must be replaced by 1 and I don't know how to.
The program creates 3 arrays but only 1 function must calculate negative numbers of each column and find the max element in each column. Here's the code:
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
int n = 0;
const int m = 3, k = 3, b = 4, u = 5;
int i, j;
void calc(float** array, int i, int j );
void calc1(float** array, int i, int j);
void calc2(float** array, int i, int j);
int main()
{
float** array = new float* [m];
for (int l = 0; l < m; l++) {
array[l] = new float[k];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < m; i++) {
for (int j = 0; j < k; j++) {
array[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < k; j++) {
cout << setprecision(2) << setw(4) << array[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(array, i, j); // FUNCTION !!!
float** arr = new float* [b];
for (int l = 0; l < b; l++) {
arr[l] = new float[b];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < b; i++) {
for (int j = 0; j < b; j++) {
arr[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < b; i++)
{
for (int j = 0; j < b; j++) {
cout << setprecision(2) << setw(4) << arr[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(arr, i, j); // FUNCTION !!!
float** ar = new float* [u];
for (int l = 0; l < u; l++) {
ar[l] = new float[u];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < u; i++) {
for (int j = 0; j < u; j++) {
ar[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < u; i++)
{
for (int j = 0; j < u; j++) {
cout << setprecision(2) << setw(4) << ar[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc2(ar, i, j); // FUNCTION !!!
}
void calc(float** array, int i, int j) {
int max = array[0][0];
for (int j = 0; j < k; j++)
{
max = array[0][0];
for (int i = 0; i < k; i++) {
if (array[i][j] > max)
max = array[i][j];
if (array[i][j] < 0) {
n += 1;
}
}
cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
}
}
void calc1(float** arr, int i, int j) {
int max = arr[0][0];
for (int j = 0; j < b; j++)
{
max = arr[0][0];
for (int i = 0; i < b; i++) {
if (arr[i][j] > max)
max = arr[i][j];
if (arr[i][j] < 0) {
n += 1;
}
}
cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
}
}
void calc2(float** ar, int i, int j) {
int max = ar[0][0];
for (int j = 0; j < u; j++)
{
max = ar[0][0];
for (int i = 0; i < u; i++) {
if (ar[i][j] > max)
max = ar[i][j];
if (ar[i][j] < 0) {
n += 1;
}
}
cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
}
}
The parameters to calc() should be the number of rows and columns in the array. Then it should use these as the limits in the for loops.
Also, since you're calculating total negative and maximum for each column, you must reset these variables each time through the column loop.
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
const int m = 3, k = 3, b = 4, u = 5;
void calc(float** array, int rows, int cols);
int main()
{
float** array = new float* [m];
for (int l = 0; l < m; l++) {
array[l] = new float[k];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < m; i++) {
for (int j = 0; j < k; j++) {
array[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < k; j++) {
cout << setprecision(2) << setw(4) << array[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(array, m, k); // FUNCTION !!!
float *arr = new float* [b];
for (int l = 0; l < b; l++) {
arr[l] = new float[b];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < b; i++) {
for (int j = 0; j < b; j++) {
arr[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < b; i++)
{
for (int j = 0; j < b; j++) {
cout << setprecision(2) << setw(4) << arr[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(arr, b, b); // FUNCTION !!!
float** ar = new float* [u];
for (int l = 0; l < u; l++) {
ar[l] = new float[u];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < u; i++) {
for (int j = 0; j < u; j++) {
ar[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < u; i++)
{
for (int j = 0; j < u; j++) {
cout << setprecision(2) << setw(4) << ar[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(ar, u, u); // FUNCTION !!!
}
void calc(float** array, int rows, int cols) {
for (int j = 0; j < cols; j++)
{
int n = 0;
int max = array[0][j];
for (int i = 1; i < rows; i++) {
if (array[i][j] > max)
max = array[i][j];
if (array[i][j] < 0) {
n += 1;
}
}
cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
}
}

An exception just caused my code to stop debug, and showing on a simple statement

Please, can anyone tell me whats wrong in this code.
I have no idea about these kind of exceptions.
Compiler is just showing this -
Exception thrown: read access violation. a was 0x1D54112. occurred
Is this due to an if statement is inside a loop, which is also inside another loop ??
Here is the code -
#include<iostream>
using namespace std;
int main()
{
int n;
cout << "Enter the number of walkers - ";
cin >> n;
int* a = new int[n];
cout << endl << "Enter the distances of walkers from Rick : ";
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
int temp;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - 1; j++)
{
if (a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
cout << endl << "The distance of walkers from Rick are - ";
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
int c = 0, i = 0, k = 0;
while (c == 0 || i < n)
{
a[i] = 0; //exception is being shown here.
for (int j = i+1; j < n; j++)
{
a[j] -= 1; //and here too!!.
if (j % 6 == 0) a[j]--;
if (a[j] == 0)
{
c = 1;
k = j;
}
}
i++;
}
if (c == 0) cout << endl << "Rick now go and save Carl and Judas" << endl;
else if (c == 1)
{
cout << endl << "Goodbye Rick" << endl;
cout << "He was able to kill " << k << " walkers. " << endl;
}
}

Printin a X with *

i wanna print a X with * , i have done the left side of the X but i don't know how to print the other side (flip/mirror) .
if you run this codes it will print just the left side of (X) and now i wanna print the right side of (X) ? so what should i do to complete the (X) using stars(*)? thank you guys.
i was wondering is it possible to do this?(i'm a newbie to programming)
#include <iostream>
// Expected output pattern:
//
// * *
// * *
// * *
// *
// * *
// * *
// * *
using namespace std;
int main() {
cout << "Printing X with star(*)" << endl;
cout << endl;
int i;
int p;
for (i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
if (j > i) break;
cout << " ";
cout << "\t";
}
cout << "\t\t\t\t";
for (p = 1; p <= 10; p++) {
cout << "*";
}
cout << endl;
}
for (i = 10; i >= 1; i--) {
for (int j = 1; j <= 10; j++) {
if (j > i) break;
cout << " ";
cout << "\t";
}
cout << "\t\t\t\t";
for (p = 1; p <= 10; p++) {
cout << "*";
}
cout << endl;
}
return 0;
}
You're on the right track, to do the right hand side you have to print more **** on each line in addition to what you already have done. It might help to think of printing each line of the X as printing some **** then some spaces then more **** and reduce the number of spaces each time you get closer to the cross-over point. Does that make sense? This might help get you further (. = space):
*......*
.*....*
..*..*
...**
and so on
This is one of many ways you could get there:
int main()
{
int size = 8;
int spacesBefore;
int spacesBetween = size;
int numStars = 1;
// Top half:
int i, j;
for ( i = 0; i < size/2; i++ ) {
spacesBetween = size - ( 2 * ( i + 1 ) );
spacesBefore = i;
for ( j = 0; j < spacesBefore; j++ ) // before
cout << " ";
for ( j = 0; j < numStars; j++ ) // * left
cout << "*";
for ( j = 0; j < spacesBetween; j++ ) // between
cout << " ";
for ( j = 0; j < numStars; j++ ) // * right
cout << "*";
cout << endl;
}
// bottom half, do the same kind of thing but changing the spacings
// ...
}
ok thank you every one that helped me , i found the answer i wanted after almost 6 hours and here is the answer:
#include <iostream>
using namespace std;
int main() {
cout << "Printing X with stars" << endl;
cout << endl;
int i;
int p;
int k;
int s;
int count = 72;
for (i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
if (j > i) break;
cout << " ";
cout << "\t";
}
cout << "\t\t\t\t";
for (p = 1; p <= 10; p++) {
cout << "* ";
}
for (k=1; k<=count; k++){
cout << " ";
}
count-=8;
for (s=1; s<=10; s++){
cout << "* ";
}
cout << endl;
}
count = 0;
for (i = 10; i >= 1; i--) {
for (int j = 1; j <= 10; j++) {
if (j > i) break;
cout << " ";
cout << "\t";
}
cout << "\t\t\t\t";
for (p = 1; p <= 10; p++) {
cout << "* ";
}
for (k=1; k<=count; k++) {
cout << " ";
}
count +=8;
for (s=1; s<=10; s++){
cout << "* ";
}
cout << endl;
if (count == 80) break;
}
return 0;
}

C++ Grid Issues

I'm printing random numbers enclosed in ascii boxes in a 6x6 grid. Having issues with printing out the grid.
Instead of having 6 columns, all my boxes and numbers are being printed out in 1 column. Been troubleshooting but cant seem to find the issue. Below is the code. Appreciate your assistance.
int main(void)
{
cout << "Magic Grid\n" << endl;
int arrayxy [6][6];
srand((unsigned)time(0));
int lowest=1111, highest=9999;
int range=(highest-lowest)+1;
// Fill array with random values
for (int i = 0; i < 6; ++i)
{
for(int j = 0; j < 6; ++j)
{
arrayxy[i][j] = lowest+int(range*rand()/(RAND_MAX + 1.0));
}
}
// Print array as grid
for (int i = 0; i < 6; ++i)
{
for(int j = 0; j < 6; ++j)
{
cout << char(218);
for (int y=0; y< 4; y++)
{
cout << char(196);
}
cout << char(191) <<endl;
cout << char(179) << arrayxy[i][j] << char(179) << endl;
cout << char(192);
for (int z=0; z< 4; z++)
{
cout << char(196);
}
cout << char(217) <<endl;
}
cout << endl;
}
cout << endl;
}
You should endl after all the columns get printed out, NOT before.
This is how I fixed your code that gives, hopefully, what you wanted to have.
For the record, I specifically changed those ASCII to * and & to make the console result more readable. You can change them back to what you want those characters to be again.
void WriteFrontLine(std::size_t count)
{
for (int i = 0; i < count; i++)
{
cout << '*';
cout << '&' << '&' << '&' << '&';
cout << '*';
}
cout << endl;
}
void WriteEndLine(std::size_t count)
{
for (int i = 0; i < count; i++)
{
cout << '*';
cout << '&' << '&' << '&' << '&';
cout << '*';
}
cout << endl;
}
int main(void)
{
cout << "Magic Grid\n" << endl;
int arrayxy[6][6];
srand((unsigned)time(0));
int lowest = 1111, highest = 9999;
int range = (highest - lowest) + 1;
// Fill array with random values
for (int i = 0; i < 6; ++i)
{
for (int j = 0; j < 6; ++j)
{
arrayxy[i][j] = lowest + int(range*rand() / (RAND_MAX + 1.0));
}
}
// Print array as grid
for (int i = 0; i < 6; ++i)
{
WriteFrontLine(6);
for (int j = 0; j < 6; ++j)
{
cout << '*' << arrayxy[i][j] << '*';
}
cout << endl;
WriteEndLine(6);
cout << endl;
}
cout << endl;
}

c++ forbids comparison between pointer and integer

i have a problem on this code
the errors says ISO c++ forbids comparison between pointer and integer
here is the exercise problem
-the answers to a true false test are as follows TTFFT given a two dimensional answer array in which each row corresponds to the answers provided on one test, write a function that accepts the two dimensional array and the number of tests as parameters and returns a one dimensional array containing the grades for each test.(each question is worth 5 points so that the maxinum possible grade is 25)
any answers will help.
this is the code
#include <iostream>
using namespace std;
int numberofgrades(char [][5], int []);
int main()
{
char testanswers[5][5] = {0};
int testgrades[5] = {0};
int counting1, counting2, counting3;
counting1 = 1;
counting2 = 1;
counting3 = 1;
cout << "this program will record the testgrades you entered: "
<< endl;
for(int i = 0; i < 5; i++)
{
for(int k = 0; k < 5; k++)
{
cout << "enter answers for test no. " << counting1 << ": ";
cin >> testanswers[i][k];
}
counting1++;
}
numberofgrades(testanswers,testgrades);
cout << "testing..." << endl;
cout << endl;
for(int o = 0; o < 5; o++)
{
cout << "test no. " << counting2;
for(int l = 0; l < 5; l++)
{
cout << " " << testanswers[o][l];
}
counting2++;
cout << endl;
}
cout << "testing...." << endl;
cout << endl;
cout << "the total number of scores per test no. is: "
<< endl;
for(int t = 0; t < 5; t++)
{
cout << "test no. " << t << ": " << testgrades[t] << endl;
}
return 0;
}
int numberofgrades(char t1a [][5], int tg1[])
{
for(int j = 0; j < 5; j++)
{
for(int i = 0; i < 5; i++)
{
if(t1a[i] == 't') //this is the problem
{
tg1[j] = tg1[j] + 5;
}
else if(t1a[i] == 'T')
{
tg1[j] = tg1[j] + 5;
}
else if(t1a[i] == 'f')
{
tg1[j] = tg1[j] + 0;
}
else if(t1a[i] == 'F')
{
tg1[j] = tg1[j] + 0;
}
else{
cout << "invalid letter exiting the program"
<< endl;
return 0;
}
}
}
return tg1[5];
}