#include //Connect Five const int yrp = -1; //your piece const int myp = 1; //my piece const int empty = 0; //empty const int maxsize = 20; //array size int n; //upper limit of input array int board[maxsize][maxsize]; //array holds piece positions int score[maxsize][maxsize]; //array holds board values related to position // _adderf_ : give the number of friendly and enemy pieces in a five space // span, adderf returns a numerical score. // For instance, 5 friendly pieces = 10000 while 3 enemy = -300 int adderf (int yours, int mine) { int temp; temp = mine - yours; if ( (mine == 0) && (yours ==0) ) return 0; else if ((mine != 0) && (yours == 0)) { if (mine == 5) return 10000; else if (mine == 4) return 300; else if (mine == 3) return 100; else if (mine == 2) return 25; else return 5; } else if ((mine == 0) && (yours != 0)) { if (yours == 4) return -2000; else if (yours == 3) return -300; else if (yours == 2) return -50; else return -10; } else return temp; } // _hori_ : given a board position, it totals the friendly and foe pieces // in the 5 spaces including and to the right of the position // It then passes these results to _adderf_ and returns that score. int hori (int board[maxsize][maxsize], int i, int j) { int yours = 0; int mine = 0; int x = i; int y = j; int temp = 0; for (x=i ; x <= (i+4) ; x++) { if (board[x][y] == yrp) yours++; else if (board[x][y] == myp) mine++; } temp = adderf(yours, mine); return temp; } // _vert_ : same as _hori_, but vertical (including position and down column) int vert (int board[maxsize][maxsize], int i, int j) { int yours = 0; int mine = 0; int x = i; int y; int temp = 0; for (y=j ; y <= (j+4) ; y++) { if (board[x][y] == yrp) yours = yours + 1; else if (board[x][y] == myp) mine = mine + 1; } temp = adderf(yours, mine); return temp; } // _diagl_ : same but down and left int diagl (int board[maxsize][maxsize], int i , int j) { int yours = 0; int mine = 0; int a; int temp = 0; for (a=0; a<=4; a++) { if (board[(i-a)][(j+a)] == yrp) yours = yours + 1; else if (board[(i-a)][(j+a)] == myp) mine = mine + 1; } temp = adderf(yours, mine); return temp; } // _diagr_ : same but down and right int diagr (int board[maxsize][maxsize], int i , int j) { int yours = 0; int mine = 0; int a; int temp = 0; for (a=0; a<=4; a++) { if (board[(i+a)][(j+a)] == yrp) yours = yours + 1; else if (board[(i+a)][(j+a)] == myp) mine = mine + 1; } temp = adderf(yours, mine); return temp; } //_value_ : Returns the total score of a given board. Cycles through //each position (nested i, j for loops) and calls the appropiate //helper functions - _hori_, _vert_, _diagl_, and _diagr_ based //on position relative to board borders int value (int board[20][20]) { int value = 0; int i, j; for (j=0 ; j<=n ; j++) { for (i=0 ; i<=n ; i++) { if ( (i <= (n-4)) && (j <= (n-4)) && (i >= 4) ) value = value + hori(board, i, j) + vert(board, i, j) + diagl(board, i, j) + diagr(board, i, j); else if ( (i <= (n-4)) && (j <= (n-4)) ) value = value + hori(board, i, j) + vert(board, i, j) + diagr(board, i, j); else if ( (i >= 4) && (j <= (n-4)) ) value = value + vert (board, i, j) + diagl(board, i, j); else if (i <= (n-4)) value = value + hori(board, i, j); else if (j <= (n-4)) value = value + vert(board, i, j); } } return value; } int main () { int max[3]; //holds final move ([0],[1]) and associated score ([2]) int a, b; int i,j; //indexers int propersize; //human size of board cin >> propersize; //reads in boards size n = propersize - 1; // Reads in board for (j=0 ; j<=n ; j++) { for (i=0 ; i<=n ; i++) cin >> board [i][j]; } // Cycles through each board position, places a friendly piece there, // and assigns a score based on _value_. // a used board position is automatically given -20000 for (b=0 ; b<=n ; b++) { for (a=0 ; a<=n ; a++) { if (board[a][b]==empty) { board[a][b] = myp; score[a][b] = value(board); board[a][b] = empty; } else score[a][b] = -20000; } } // Max subroutine: assigns board position (from score array) with highest // score to max[3] array for print out. max[0]=0; max[1]=0; max[2]=-20001; for (j=0; j<=n; j++) { for (i=0 ; i<=n; i++) if (score[i][j] > max[2]) { max[0] = i; max[1] = j; max[2] = score [i][j]; } } cout << max[0] << " " << max[1] << endl; board [max[0]][max[1]] = myp; return 0; }