# Four in a row, checking for winnig move

Hi Experts,

I am writing a board game, Connect-4, in java.
In my main class, GameModel, where I am trying to implement the logic of the game, I have a method that should check for every piece placed on the board whether it has been a winning move. I am having trouble with implementing this method.
In fact, knowing the exact location of a piece on the board, how can I check that three other pieces next to it, in a row, whether horizontally, vertically or diagonally form a winning deal?
This is what I have come up with but it is not detecting any winning position and I am also getting the ArrayOutOfBound type error message...
I would really appreciate your help.

Cheers.
``````private boolean checkHorizontal(int pRow, int pCol)
{
//mGameWon = false;

if ((mBoard[pRow][pCol] == mBoard[pRow][pCol + 1] &&
mBoard[pRow][pCol] == mBoard[pRow][pCol + 2] &&
mBoard[pRow][pCol] == mBoard[pRow][pCol + 3])
||
(mBoard[pRow][pCol] == mBoard[pRow][pCol - 1] &&
mBoard[pRow][pCol] == mBoard[pRow][pCol - 2] &&
mBoard[pRow][pCol] == mBoard[pRow][pCol - 3]))
{
mGameWon = true;
}
if (mGameWon)typeOfWin = "Horizontal win";
{
return mGameWon;
}

}

private boolean checkVertical(int pRow, int pCol)
{

if ((mBoard[pRow][pCol] == mBoard[pRow + 1][pCol] &&
mBoard[pRow][pCol] == mBoard[pRow + 2][pCol] &&
mBoard[pRow][pCol] == mBoard[pRow + 3][pCol])
||
(mBoard[pRow][pCol] == mBoard[pRow - 1][pCol] &&
mBoard[pRow][pCol] == mBoard[pRow - 2][pCol] &&
mBoard[pRow][pCol] == mBoard[pRow - 3][pCol]))
{
mGameWon = true;
}
if (mGameWon)typeOfWin = "Horizontal win";
{
return mGameWon;
}
}

private boolean checkDiagonalPositive(int pRow, int pCol)
{

if ((mBoard[pRow][pCol] == mBoard[pRow - 1][pCol + 1] &&
mBoard[pRow][pCol] == mBoard[pRow - 2][pCol + 2] &&
mBoard[pRow][pCol] == mBoard[pRow - 3][pCol + 3])
||
(mBoard[pRow][pCol] == mBoard[pRow + 1][pCol + 1] &&
mBoard[pRow][pCol] == mBoard[pRow + 2][pCol + 2] &&
mBoard[pRow][pCol] == mBoard[pRow + 3][pCol + 3]))
{
mGameWon = true;
}
if (mGameWon)typeOfWin = "Horizontal win";
{
return mGameWon;
}
}

private boolean checkDiagonalNegative(int pRow, int pCol)
{
if ((mBoard[pRow][pCol] == mBoard[pRow - 1][pCol + 1] &&
mBoard[pRow][pCol] == mBoard[pRow - 2][pCol + 2] &&
mBoard[pRow][pCol] == mBoard[pRow - 3][pCol + 3])
||
(mBoard[pRow][pCol] == mBoard[pRow + 1][pCol - 1] &&
mBoard[pRow][pCol] == mBoard[pRow + 2][pCol - 2] &&
mBoard[pRow][pCol] == mBoard[pRow + 3][pCol - 3]))
{
mGameWon = true;
}
if (mGameWon)typeOfWin = "Horizontal win";
{
return mGameWon;
}

}
private boolean checkForWin(int pRow, int pCol)
{
if ((mGameWon = checkHorizontal(pRow, pCol)) == true )
{
return mGameWon;
}
else if ((mGameWon = checkVertical(pRow, pCol)) == true)
{
return mGameWon;
}
else if ((mGameWon = checkDiagonalPositive(pRow, pCol)) == true)
{
return mGameWon;
}
else if ((mGameWon = checkDiagonalNegative(pRow, pCol)) == true)
{
return mGameWon;
}

return mGameWon;

}
``````
###### Who is Participating?

Commented:
I wrote you that I assumed that you have 6 columns and 7 rows
and you have 7 columns and 6 rows
so I exchanged places 7 and 6 in limits of boundraies
in all these four check methods - now you don't see
ArrayOutOf bounds

I still didn't understand how to play - it only seesm to read
the column, not te position -
eitehr I tpe f1 or f4 or simply f - all the same.

But anyway befire I exchanged i saw when I reached te top of the column
ArrayOutofBounds;
after I exachenged those - no more such errorr

GameModel is the only place where I needed to change
it for me together with this observer).

Anyway exchange 6 and 7 in all such cases
if(col < 0 || col >= 6){

if(row < 0 || row >= 7){

column should everywhere be with 7
row should be with 6

and you'll not have arrays out of bounds

``````package org.c4.code;

import java.awt.Point;
import java.util.Stack;

import org.c4.code.GameStack;

public class GameModel extends Subject {
/*
* constants
*
*/
public static final int BOARD_HEIGHT = 6;
public static final int BOARD_WIDTH = 7;
public static final int NOONE = -1;
public static final int EMPTY_CELL = -1;
public static final int RED = 0;
public static final int BLUE = 1;

/*
* Instance variables (fields)
* The following variables keep track of the
* state of this game.
*/

// positions on the connect 4 grid
private int [][] mBoard;
// indicates the number of the last move played in the game
private int mMoveNumber;
// flag monitoring a winning move in the game
private boolean mGameWon;
// flag monitoring a game ended in tie
private boolean mTie;

private String typeOfWin = " ";

/*
* Default Constructor
*/
public GameModel() {
mBoard = new int[BOARD_HEIGHT][BOARD_WIDTH];
init();
}

/**
* Resets the game to its starting state. This has
* the board empty, the current player set to O and
* the move number set to 0.
*/
public void init() {
mGameWon = false;
mMoveNumber = 0;
for (int i = 0; i < BOARD_HEIGHT; i++)
{
for (int j = 0; j < BOARD_WIDTH; j++)
{
mBoard[i][j] = EMPTY_CELL;
}
}
}

//getters
public int getMoveNumber()
{
return mMoveNumber;
}

public int[][] getBoard()
{
return mBoard;
}

public boolean getGameWon()
{
return mGameWon;
}

public String getTypeOfWin()
{
return typeOfWin;
}
/*
* This function returns the state of a specified cell on the board
*/
public int getCellState(int pRow, int pCol)
{

if (pRow < 0 || pRow >= BOARD_HEIGHT ||
pCol < 0 || pCol >= BOARD_WIDTH)
{
return EMPTY_CELL;
}
return mBoard[pRow][pCol];
}

/*
* This method allows to place a piece into a specified column and returns the
* row corresponding to the placement of that piece on the board
*/
public int play(int pCol)
{
mGameWon = false;
int mRow = 0;
//        int mRow = 5;

for ( ; mRow < BOARD_HEIGHT && (mBoard[mRow][pCol]) != EMPTY_CELL; mRow++)
//        for ( ; mRow < BOARD_HEIGHT && (mBoard[mRow][pCol]) != EMPTY_CELL; mRow--)
{

}

if (mRow == 6)
{
return -1;
}

//        if (mRow < 0)
//		{
//			return -1;
//		}
// put the piece in place
if ((mMoveNumber % 2) == 0)
{
mBoard[mRow][pCol] = getCurrentPlayer();
}
else
{
mBoard[mRow][pCol] = getCurrentPlayer();
}

// add move to the stack
Stack st = new Stack();
Point pts = new Point( mRow, pCol);

GameStack.performPush(st, pts);

// check whether this move is a winner

//    System.out.println("row: " + mRow + " column: " + pCol);

mGameWon = checkForWin(mRow, pCol);

if (!mGameWon)
{
// increase the count of moves
mMoveNumber++;
}

// send notification to all the listeners

return mRow;

}

private boolean checkHorizontal(int pRow, int pCol)
{
int c = getCurrentPlayer();
int count = 0;
for(int j = 0; j < 7; j++)
{
int mCol = pCol-3 + j;
if(mCol < 0 || mCol >= 7)
{
count = 0;
continue;
}
if(mBoard[pRow][mCol] == c)
{
count++;
if(count == 4)
{
mGameWon = true;
if (mGameWon)typeOfWin = "Horizontal win";
{
return mGameWon;
}
}
}
else
{
count = 0;
}

}

return false;
}

private boolean checkVertical(int pRow, int pCol){

int c = getCurrentPlayer();
int count = 0;
for(int j = 0; j < 7; j++)
{
int mRow = pRow-3 +j;
if(mRow < 0 || mRow >= 6)
{
count = 0;
continue;
}
if(mBoard[mRow][pCol] == c)
{
count++;
if(count == 4)
{
mGameWon = true;
if (mGameWon)typeOfWin = "Vertical win";
{
return mGameWon;
}
}

}
else
{
count = 0;
}

}

return false;

}

private boolean checkPositiveDiagonal(int pRow, int pCol){

int c = getCurrentPlayer();
int count = 0;
for(int j = 0; j < 7; j++)
{
int mRow = pRow-3 + j;
int mCol = pCol-3 + j;
if(mRow < 0 || mRow >= 6)
{
count = 0;
continue;
}
if(mCol < 0 || mCol >= 7)
{
count = 0;
continue;
}

if(mBoard[mRow][mCol] == c)
{
count++;
if(count == 4)
{
mGameWon = true;
if (mGameWon)typeOfWin = "Diagonal win - Positive slope";
{
return mGameWon;
}
}
}
else
{
count = 0;
}

}
return false;
}

private boolean checkNegativeDiagonal(int pRow, int pCol)
{

int c = getCurrentPlayer();
int count = 0;
for(int j = 0; j < 7; j++)
{
int mRow = pRow-3 + j;
int mCol = pCol + 3 - j;
if(mRow < 0 || mRow >= 6)
{
count = 0;
continue;
}
if(mCol < 0 || mCol >= 7)
{
count = 0;
continue;
}

// System.out.println("mRow: " + mRow + " mCol: " + mCol);
if(mBoard[mRow][mCol] == c)
{
count++;
if(count == 4)
{
mGameWon = true;
if (mGameWon)typeOfWin = "Diagonal win - Negative slope";
{
return mGameWon;
}
}
}
else
{
count = 0;
}
}

return false;

}

private boolean checkForWin(int pRow, int pCol)
{
if ((mGameWon = checkHorizontal(pRow, pCol)) == true )
{
return mGameWon;
}
else if ((mGameWon = checkVertical(pRow, pCol)) == true)
{
return mGameWon;
}
else if ((mGameWon = checkPositiveDiagonal(pRow, pCol)) == true)
{
return mGameWon;
}
else if ((mGameWon = checkNegativeDiagonal(pRow, pCol)) == true)
{
return mGameWon;
}

return mGameWon;

}

/*
* This method check whether the game has terminated with no winner.
*/
public  boolean endedInDraw()
{
boolean mDraw = false;
if (mMoveNumber == (GameModel.BOARD_HEIGHT * GameModel.BOARD_WIDTH))
{
mDraw = true;
}

return mDraw;
}

public int getCurrentPlayer()
{
return  (mMoveNumber % 2 == 0) ? RED : BLUE;
}

public int getWinner()
{
return mGameWon ? ((mMoveNumber % 2 == 0) ? RED : BLUE) : NOONE;
}

public void undo(Stack stack)
{
Stack secondStack = new Stack();
Point undonePt = new Point();
undonePt = GameStack.performPop(stack);
GameStack.performPush(secondStack, undonePt);

}

public void redo(Stack stack, Stack secondStack)
{
Point redonePt = new Point();
redonePt = GameStack.performPop(secondStack);
GameStack.performPush(stack, redonePt);
}

public void reset()
{
init();
}

}
``````
0

Commented:
Is your game played on the i8x8 board or maybe on the infinite board ?
0

Commented:
Array out of bounds I guess comes when you do not check if all those -1, -2, -3 +1,+3, etc
indices are going out of your board
0

Commented:

I don't think this would find the win
if your starting position (last move) is
in the middle of the quartet - it only checks if
it is in the beginning or the end of the quartet

The same with other methods

Also minor thing - they all write "Horizontal win"
``````private boolean checkHorizontal(int pRow, int pCol)
{
//mGameWon = false;

if ((mBoard[pRow][pCol] == mBoard[pRow][pCol + 1] &&
mBoard[pRow][pCol] == mBoard[pRow][pCol + 2] &&
mBoard[pRow][pCol] == mBoard[pRow][pCol + 3])
||
(mBoard[pRow][pCol] == mBoard[pRow][pCol - 1] &&
mBoard[pRow][pCol] == mBoard[pRow][pCol - 2] &&
mBoard[pRow][pCol] == mBoard[pRow][pCol - 3]))
{
mGameWon = true;
}
if (mGameWon)typeOfWin = "Horizontal win";
{
return mGameWon;
}

}
``````
0

Commented:
I would do something like this - assuming that board is your array
of X and O's and and the dimension of your board is int bS

Didn't check it, but i think it should work:

``````  public static void main(String[] args) {
int xo=3;  //this is, say,  x of  your current move
int yo = 2;   //  this is y of your current move

if(check(xo-3,yo,1,0,7,'X'))  System.out.println("X win horizontal");
if(check(xo,yo-3,0,1,7,'X'))  System.out.println("X win horizontal");
if(check(xo-3,yo-3,1,1,7,'X'))  System.out.println("X win diagonal");
if(check(xo-3,yo+3,1,-1,7,'X'))  System.out.println("X win antidiagonal");

}

public static boolean check (int xs, int ys, int xi, int yi, int tot, char c ){

int count = 0;

for(int j=0; j<tot; j++){
int curx = xs + j*xi;
int cury = ys + j*yi;
if(curx < 0 || curx >= bS){

count = 0;
continue;
}
if(cury < 0 ||  cury >= bS){
count = 0;
continue;
}

if(board[curx][cury] == c){count++;
if(count == 4)return true;
} else count = 0;

}

return  false;
}
``````
0

Commented:

of course you don't need to check the rest of them if you found a win like that:

if(check(xo-3,yo,1,0,7,'X')) { System.out.println("X win horizontal");
//do something with this win }

0

Commented:
This I already tested - works nicely and can be used for any board size
and any number in wiining row

``````import java.util.Random;

public class OnlyCrosses {

static int bS = 7;    // size of the board
static int winS = 4;  // size of winning row
static char[][] board = new char[bS][bS];

public static void main(String[] args) {

int count = 0;

Random r = new Random();

for(int j=0; j<bS; j++){
for(int jj=0; jj<bS; jj++ ){
board[j][jj] = '-';
}

}

while(true){
int x = r.nextInt(bS);
int y = r.nextInt(bS);
if(board[x][y] == 'x') {
continue;
}  else
{
board[x][y] = 'x';
count++;

if(check(x-winS+1,y,1,0,'x'))  {
System.out.println("");
System.out.println("X win vertical in "+ count + " moves.");
printBoard();

System.exit(0);
}
if(check(x,y-winS+1,0,1,'x'))
{       System.out.println("");
System.out.println("X win horizontal in "+ count + " moves.");
printBoard();
System.exit(0);
}
if(check(x-winS+1,y-winS+1,1,1,'x'))  {
System.out.println("");
System.out.println("X win diagonal in " + count + " moves.");
printBoard();
System.exit(0);
}
if(check(x-winS+1,y+winS-1,1,-1,'x'))  {
System.out.println("");
System.out.println("X win negative diagonal in " + count + " moves.");
printBoard();
System.exit(0);
}

}

}

}

public static void printBoard(){

System.out.println("");
for(int j=0; j<bS; j++){
for(int jj=0; jj<bS; jj++ ){
System.out.print( board[j][jj]);
}
System.out.println("");
}

System.out.println("");

}

public static boolean check (int xs, int ys, int xi, int yi, char c ){

int count = 0;
int tot = winS*2 - 1;

for(int j=0; j<tot; j++){
int curx = xs + j*xi;
int cury = ys + j*yi;
if(curx < 0 || curx >= bS){

count = 0;
continue;
}
if(cury < 0 ||  cury >= bS){
count = 0;
continue;
}

if(board[curx][cury] == c){count++;
if(count == winS)return true;
} else count = 0;

}

return  false;
}

}
``````

Output:
``````X win diagonal in 13 moves.

x--x-x-
x---x--
----xx-
--x---x
----x--
x----x-
--x----
``````
0

Author Commented:
I tried the solution your proposed (http:#36595903) but i am still getting the ArrayIndexOutOfBoundsException...

Also, to be specific, I am using a 6*7 board game.
I am supposed to call the checkForWin() method after every player has placed a piece on the board.
The GameModel class itself has a means of knowing whose player turn it is and whether to place an 'O' / 'X' (Console) or"'Red"/"Blue" image(GUI).
The only arguments I wanna pass to this "checking"method is the actual move position: row number and column number. Knowing these two, check whether the then placed piece becomes part of a winning quartet.
Thus the code I showed earlier but it's not working for me...

Cheers!
0

Commented:
Look attentively at my code - there is no problem to make out of it onw method checkForWin - just combine all four checks into one
method and return string how it won (or "nowin") if you need to play firther

The code you showed earlier, as I wrote above has many flaws
0

Author Commented:
>> The code you showed earlier, as I wrote above has many flaws

Yes, it does indeed. That's why I am asking for guidance.
Your checking method takes in a number of parameters:
>> public static boolean check (int xs, int ys, int xi, int yi, char c )
I do not want nor need to pass as many parameters to my function.
Only the row number + column number, thus the signature of my function:
private boolean functionName(int rowNumber, int columnNumber) {}
I want help grasping the way I can check that a particular move on the board becomes part or not of a row of the same piece, in which case there is a win.
I have a play(int columnNumber) method that returns the next available empty rowNumber on that column.
After each play, I want to check: "Is this a winning move?"
My method's signature should stay the same, accepting two parameters ONLY.
Should I post my GameModel class?

0

Commented:
You can return of course boolean , but betteer return string and it will say which kind of win you have
or just "continue" or something to continue the game:

`````` public static String checkForWin(int x, int y){

char c = board[x][y];

if(check(x-winS+1,y,1,0,c))  {

return (c +  " win vertical in "+ count + " moves.");

}
if(check(x,y-winS+1,0,1,c))
{
return (c + " win horizontal in "+ count + " moves.");

}
if(check(x-winS+1,y-winS+1,1,1,c))  {

return (c + " win diagonal in " + count + " moves.");

}
if(check(x-winS+1,y+winS-1,1,-1,c))  {

return (c + " win negative diagonal in " + count + " moves.");

}

return "continue";

}
``````
0

Commented:

This is definitely the way to go and this method should - the only complexity which you
disclosed just now  is non-square board so
inside that check method uyou need to be careful where is your limitation on x and where is on y
0

Commented:
This one works for 6x6 board - this is not exactly the game,
as I have only crosses, but the idea is the same
execute it and you'll see
- you just need
to take checkForWin() and check() method from here
and they will work together.

Let me check a little bit an I figure out for non-square board - it is
just that checks for the board boundaries in the check method should be adjusted correcrtly,
because with square board they on both sides check with one number
Other than that it all should work on non-square either

``````import java.util.Random;

public class OnlyCrosses {

static int bShor = 6;
static int bSver = 6;// size of the board
static int winS = 4;  // size of winning row
static char[][] board = new char[bSver][bShor];
static  int countMoves = 0;

public static void main(String[] args) {

Random r = new Random();

for(int j=0; j<bSver; j++){
for(int jj=0; jj<bShor; jj++ ){
board[j][jj] = '-';
}

}

while(true){
int x = r.nextInt(bSver);
int y = r.nextInt(bShor);
if(board[x][y] == 'x') {
continue;
}  else
{
board[x][y] = 'x';
countMoves++;
String s = checkForWin(x,y);

if(!s.equals("continue")){
System.out.println(s);
break;
}

}

}
printBoard();

}

public static String checkForWin(int x, int y){

char c = board[x][y];

if(check(x-winS+1,y,1,0,c))  {

return (c +  " win vertical in "+ countMoves + " moves.");

}
if(check(x,y-winS+1,0,1,c))
{
return (c + " win horizontal in "+ countMoves + " moves.");

}
if(check(x-winS+1,y-winS+1,1,1,c))  {

return (c + " win diagonal in " + countMoves + " moves.");

}
if(check(x-winS+1,y+winS-1,1,-1,c))  {

return (c + " win negative diagonal in " + countMoves + " moves.");

}

return "continue";

}

public static void printBoard(){

System.out.println("");
for(int j=0; j<bSver; j++){
for(int jj=0; jj<bShor; jj++ ){
System.out.print( board[j][jj]);
}
System.out.println("");
}

System.out.println("");

}

public static boolean check (int xs, int ys, int xi, int yi, char c ){

int count = 0;
int tot = winS*2 - 1;

for(int j=0; j<tot; j++){
int curx = xs + j*xi;
int cury = ys + j*yi;
if(curx < 0 || curx >= bSver){

count = 0;
continue;
}
if(cury < 0 ||  cury >= bShor){
count = 0;
continue;
}

if(board[curx][cury] == c){count++;
if(count == winS)return true;
} else count = 0;

}

return  false;
}

}
``````
0

Commented:
This actually works for 6x7 - at least in my variant of game:

``````import java.util.Random;

public class OnlyCrosses {

static int bShor = 6;
static int bSver = 7;// size of the board
static int winS = 4;  // size of winning row
static char[][] board = new char[bSver][bShor];
static  int countMoves = 0;

public static void main(String[] args) {

Random r = new Random();

for(int j=0; j<bSver; j++){
for(int jj=0; jj<bShor; jj++ ){
board[j][jj] = '-';
}

}

while(true){
int x = r.nextInt(bSver);
int y = r.nextInt(bShor);
if(board[x][y] == 'x') {
continue;
}  else
{
board[x][y] = 'x';
countMoves++;
String s = checkForWin(x,y);

if(!s.equals("continue")){
System.out.println(s);
break;
}

}

}
printBoard();

}

public static String checkForWin(int x, int y){

char c = board[x][y];

if(check(x-winS+1,y,1,0,c))  {

return (c +  " win vertical in "+ countMoves + " moves.");

}
if(check(x,y-winS+1,0,1,c))
{
return (c + " win horizontal in "+ countMoves + " moves.");

}
if(check(x-winS+1,y-winS+1,1,1,c))  {

return (c + " win diagonal in " + countMoves + " moves.");

}
if(check(x-winS+1,y+winS-1,1,-1,c))  {

return (c + " win negative diagonal in " + countMoves + " moves.");

}

return "continue";

}

public static void printBoard(){

System.out.println("");
for(int j=0; j<bSver; j++){
for(int jj=0; jj<bShor; jj++ ){
System.out.print( board[j][jj]);
}
System.out.println("");
}

System.out.println("");

}

public static boolean check (int xs, int ys, int xi, int yi, char c ){

int count = 0;
int tot = winS*2 - 1;

for(int j=0; j<tot; j++){
int curx = xs + j*xi;
int cury = ys + j*yi;
if(curx < 0 || curx >= bSver){

count = 0;
continue;
}
if(cury < 0 ||  cury >= bShor){
count = 0;
continue;
}

if(board[curx][cury] == c){count++;
if(count == winS)return true;
} else count = 0;

}

return  false;
}

}
``````

Output:

``````x win vertical in 15 moves.

---xx-
xxx--x
x--x--
x----x
xx--x-
------
---xx-
``````

Just check that you in your code
name coordinates the same way as i do(I mean x and y and not y and x)

and then checkForWin(x,y)

togeteher with this check(...) method should work
for you as well
0

Commented:
That is how you want to change your
checkHorozontal.
Of course you eed to dosimilar with all four of them

``````private boolean checkHorizontal(int pRow, int pColumn){

char c = mBoard[pRow][pColumn];
int count = 0;
for(int j=0; j<7; j++){
int col = pColumn-3 +j;
if(col < 0 || col >= 6){
count = 0;
continue;
}
if(mBoard[pRow][col] == c){
count++;
if(count == 4)return true;
} else count = 0;

}

return false;

}
``````
0

Commented:

I think this is how your original methods should work
This is in the assumption that you have 6 columns and 7 rows
If it is 7 columns and 5 rows then you need to change the
upper boundary checks in each method

``````  private boolean checkHorizontal(int pRow, int pColumn){

char c = mBoard[pRow][pColumn];
int count = 0;
for(int j=0; j<7; j++){
int col = pColumn-3 +j;
if(col < 0 || col >= 6){
count = 0;
continue;
}
if(mBoard[pRow][col] == c){
count++;
if(count == 4)return true;
} else count = 0;

}

return false;

}

private boolean checkVertical(int pRow, int pColumn){

char c = mBoard[pRow][pColumn];
int count = 0;
for(int j=0; j<7; j++){
int row = pRow-3 +j;
if(row < 0 || row >= 7){
count = 0;
continue;
}
if(mBoard[row][pColumn] == c){
count++;
if(count == 4)return true;
} else count = 0;

}

return false;

}

private boolean checkPositiveDiagonal(int pRow, int pColumn){

char c = mBoard[pRow][pColumn];
int count = 0;
for(int j=0; j<7; j++){
int row = pRow-3 +j;
int col = pColumn-3 + j;
if(row < 0 || row >= 7){
count = 0;
continue;
}
if(col < 0 || col >= 6){
count = 0;
continue;
}

if(mBoard[row][col] == c){
count++;
if(count == 4)return true;
} else count = 0;

}

return false;

}

private boolean checkNegativeDiagonal(int pRow, int pColumn){

char c = mBoard[pRow][pColumn];
int count = 0;
for(int j=0; j<7; j++){
int row = pRow-3 +j;
int col = pColumn+3 - j;
if(row < 0 || row >= 7){
count = 0;
continue;
}
if(col < 0 || col >= 6){
count = 0;
continue;
}

if(mBoard[row][col] == c){
count++;
if(count == 4)return true;
} else count = 0;

}

return false;

}
``````
0

Author Commented:
I am still having problems with checking the winning quartet.
I have used the code you provided (http:#36596645), I have modified it to fit the context of my class.
I have attached a copy of what I have so far in terms of code.
Could you please give it a test run?
I am still getting the ArrayIndexOutOfBoundException and also, especially when playing to get a diagonal win.
Also, when I place pieces on the board, let's say for an horizontal win, for player X, if I play this sequence of column letter: e, f, g, d; it doesn't pick this as a winning quartet when placing the last piece d.
I just realize that I can attach the file to this post.
Is there a way I can send you the project?
Please let me know... because I think you could then be able to guide me better and more specifically.
Thanks.
0

Commented:

paste just the code or attach java file(s)
0

Author Commented:
Here are the files.
Cheers.

GameModel.java
Main.java
GameStack.java
Subject.java
Observer.java
0

Commented:
GameModel and Main is the same file - ?
0

Commented:
Which one to use ? Or some file is missing?
0

Author Commented:
>> GameModel and Main is the same file - ?
No, it's not the same file.
0

Commented:

Click on those files above - in both cases I see the same line - they cannot be oreset in the same project:

public class GameModel extends Subject {

public class GameModel extends Subject {
0

Author Commented:
I have sent all the files in the project.
GameModel contains the whole logic of the game.
Main extends GameModel,
Running the Main.java class runs the application.
0

Author Commented:
Sorry...
I sent the wrong file.
Here is the Main.java class
Main.java
0

Author Commented:
Thank you very much. I really appreciate your support and your contribution.
Cheers!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.