I have an issues with my int array, i have a game where its like a mega version of Tic Tac Toe where you need 5 to win instead of 3 and i have tried to come up with a system of checking for a winner or not (right now iv just been concerning myself with horizontal and vertical lines, im not gunna worry about diagonal atm). recent attemps i have tryed to solving this is to code the W's and B's as 0's and 1's, then put a check into a bool statement and only repeat the program while ture, but if somone gets 5 in a row then the statement turns false, this seems to work in my head but has not turned out so well in code. (code thus far)

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int n, point_B, point_W;
    int position_x, position_y;
    int game_board[700][700];
    int value(1); 
    int winner(true);
       
    do{
    cout << "Inpute Board size between 10 and 50" << endl;
    cin >> n;
    
}while(n < 10 || n>50);

for(int y=0; y<n; y++)
        for(int x=0; x<n; x++)
            game_board[x][y] = value++;
            
                for(int y=0; y<n; y++){
            cout << endl;
                 for(int x=0; x<n; x++){
                         
                         if(game_board[x][y] <= 9)
                               cout << game_board[x][y]<< "  ";
                               
                                    else
                                        cout<<game_board[x][y]<<" ";
                                        }
                                        }
cout << endl;
do{         
            for( int i(0); i<=n*n; i++){
                 if ( i%2 == 0){
                      cout << endl;
cout << "Black Enter Your Position: "; 
cin >> point_B;
   position_y = ceil(point_B/n);
   
	if (point_B%n==0)
	  {
	     position_x=n-1;
	     position_y--; 
	  }
	else
	  position_x=(point_B%n)-1;
	                  
            game_board[position_x][position_y] = -1;
                    
        
    
   for(int y=0; y<n; y++){
    cout << endl;
    for(int x=0; x<n; x++) {
        if(game_board[x][y] <= 9){
            if(game_board[x][y] == -1) {
                cout << "B  ";
            } else if(game_board[x][y] == -2) {
                cout << "W  ";
            } else cout << game_board[x][y]<< "  ";
        } else {
            cout<<game_board[x][y] << " ";
        }
    }
}


cout << endl;
}
else{
     cout << endl;
cout << "White Enter Your Position: ";
     cin >> point_W;
      position_y = ceil(point_W/n);
	if (point_W%n==0)
	  {
	     position_x=n-1;
	     position_y--;
	  }
	else
	  position_x=(point_W%n)-1;
	                  
                      game_board[position_x][position_y] = -2;
                         
    
   for(int y=0; y<n; y++){
    cout << endl;
    for(int x=0; x<n; x++) {
        if(game_board[x][y] <= 9){
            if(game_board[x][y] == -1) {
                cout << "B  ";
            } else if(game_board[x][y] == -2) {
                cout << "W  ";
            } else cout << game_board[x][y]<< "  ";
        } else {
            cout<<game_board[x][y] << " ";
        }
    }
}


cout << endl;
}
}
}while(winner);
    system("PAUSE");
	return 0;
}

This seems a lot like you have coded before you have figured out what you are going to do. So let me point out a number of strange things that you may have overlooked.

First you initialize the board to 1,2,3, etc... e.g.

for(int y=0; y<n; y++)
    for(int x=0; x<n; x++)
      game_board[x][y] = value++;

Surely at the start of a game you are going to use say 0 to represent no counter, -1 for black and 1 for white. So you want to initialize the board to zero. By setting each point to a value, that is only used when you write out the board [and makes it almost impossible for the user to see the real game], you have made your life very very difficult.

Now you ask the poor used to enter the number based on the total position on the board. That is ok, but surely the coordinates would be better.

Now to calculate if you have a line of 5. Well first thing is you ONLY need to check
each of the positions from the point that you entered a new counter. Therefore,
you only have to check (at most) 8 directions. However, you do have to note that
a line can be formed up to 4 additional counters away from the point.

So the obvious way is to check from the point with a continuous addition: I will demonstrate the idea with a horrizontal forward direction line, and you can code the rest.

int posX;  // Position X of new piece 
int posY   // Initial value
int sum(grid[posX][posY]);
 // ....
// The test is 
// (a) the line is not longer than 5 but includes the new piece
// (b) the sum is either +5 or -5 based on the played piece
for(int i=0;i<4 && sum==(i+1)*grid[posX][posY];i++)
  {
    sum+=grid[posX+i];
  }
if (sum==5*grid[posX][posY])
  winner();

You are going to have to deal with the edge condition. You may like wrap around, you may like the grid array to be bigger than the playing board, to avoid the test, or you make want to deal with the boundaries.

Comments
Nice one
This article has been dead for over six months. Start a new discussion instead.