First off, I am new to C++ myself, but am willing to try and help. The following code initializes the rgiBoard array to the size of the SIZEOFBOARD variable. It then places four 'players' somewhere in the array. It then gets input from the user as to which player they want to find. It then searches the array to see where that player is located and prints it out on the screen.
I didn't really see where the variable, counter, was really used in the code. Since you are testing the value of counter, it should really be if(counter == 1). Still, since counter is increased every iteration, you are only going to fulfill the condition if rgiBoard[0][0] == 1. Upon the next iteration, counter will = 2. I hope I am making sense to you
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;
#define SIZEOFBOARD 12
#define P1 1
#define P2 2
#define P3 3
#define P4 4
int a;
int rgiBoard[SIZEOFBOARD][SIZEOFBOARD];
void PlacePlayer();
void PlacePlayer(int a)
{
srand ( time(NULL) );
int okay = 0;
int x;
int y;
while(okay == 0)
{
x = rand() %SIZEOFBOARD;
y = rand() %SIZEOFBOARD;
if (rgiBoard[x][y] == 0)
{
rgiBoard[x][y] = a;
okay = 1;
}
}
}
int main ()
{
int counter = 0;
int PlayerToFind = 0;
for(int i = 1;i < 5;i++)
{
PlacePlayer(i);
}
while(PlayerToFind <1 or PlayerToFind >4)
{
cout << "Enter the number of the player you want to find (1 - 4):";
cin >> PlayerToFind;
}
for(int i=0; i<SIZEOFBOARD; i++)
{
for(int j=0; j<SIZEOFBOARD; j++)
{
counter++;
if (rgiBoard[i][j] == PlayerToFind)
{
cout << "Found " << PlayerToFind << " at " << i << " " << j << " .";
}
}
}
return 0;
}
I am using CodeBlocks with the MingW compiler, if it makes any difference. I hope this is helpful.
EDIT: Your question was technically to find a constant (like P1) in the array, which could easily be done by changing
if (rgiBoard[i][j] == PlayerToFind)
to
if (rgiBoard[i][j] == P1)
. It would take away the ability to find players 2 - 4 though.