Skip to content

Instantly share code, notes, and snippets.

@stevenpetryk
Created June 16, 2015 02:09
Show Gist options
  • Save stevenpetryk/b6ea7070da0f5de25310 to your computer and use it in GitHub Desktop.
Save stevenpetryk/b6ea7070da0f5de25310 to your computer and use it in GitHub Desktop.
void DrawSquares( Graphics canvas )
{
canvas.setColor(Color.BLACK);
for( int nRow=0; nRow<NUMROWS; nRow++ )
{
for( int nCol=0; nCol<NUMCOLS; nCol++ )
{
canvas.drawRect( BOARDLEFT + nCol * SQUAREWIDTH,
BOARDTOP + nRow * SQUAREHEIGHT, SQUAREWIDTH, SQUAREHEIGHT );
if( m_nBoard[nRow][nCol] != 0 )
{
canvas.drawImage( m_imgQueen,
BOARDLEFT + nCol * SQUAREWIDTH + 8, BOARDTOP + nRow * SQUAREHEIGHT + 6, null );
}
}
}
}
void CheckColumns( Graphics canvas )
{
// Check all columns
for( int nCol=0; nCol<NUMCOLS; nCol++ )
{
int nColCount = 0;
for( int nRow=0; nRow<NUMROWS; nRow++ )
{
if( m_nBoard[nRow][nCol] != 0 )
{
nColCount++;
}
}
if( nColCount > 1 )
{
canvas.drawLine( BOARDLEFT + nCol * SQUAREWIDTH + ( SQUAREWIDTH / 2 ),
BOARDTOP + ( SQUAREHEIGHT / 2 ),
BOARDLEFT + nCol * SQUAREWIDTH + ( SQUAREWIDTH / 2 ),
BOARDTOP + SQUAREHEIGHT * 7 + ( SQUAREHEIGHT / 2 ) );
m_bClash = true;
}
}
}
void CheckRows( Graphics canvas )
{
for( int nRow=0; nRow<NUMROWS; nRow++ )
{
int nRowCount = 0;
for( int nCol=0; nCol<NUMCOLS; nCol++ )
{
if( m_nBoard[nRow][nCol] != 0 )
{
nRowCount++;
}
}
if( nRowCount > 1 )
{
canvas.drawLine( BOARDLEFT + ( SQUAREWIDTH / 2 ),
BOARDTOP + nRow * SQUAREHEIGHT + ( SQUAREHEIGHT / 2 ),
BOARDLEFT + 7 * SQUAREWIDTH + ( SQUAREWIDTH / 2 ),
BOARDTOP + nRow * SQUAREHEIGHT + ( SQUAREHEIGHT / 2 ) );
m_bClash = true;
}
}
}
void CheckDiagonal1( Graphics canvas )
{
// Check diagonal 1
for( int nRow=NUMROWS-1; nRow>=0; nRow-- )
{
int nCol = 0;
int nThisRow = nRow;
int nThisCol = nCol;
int nColCount = 0;
while( nThisCol < NUMCOLS &&
nThisRow < NUMROWS )
{
if( m_nBoard[nThisRow][nThisCol] != 0 )
{
nColCount++;
}
nThisCol++;
nThisRow++;
}
if( nColCount > 1 )
{
canvas.drawLine( BOARDLEFT + nCol * SQUAREWIDTH + ( SQUAREWIDTH / 2 ),
BOARDTOP + nRow * SQUAREHEIGHT + ( SQUAREHEIGHT / 2 ),
BOARDLEFT + ( nThisCol - 1 ) * SQUAREWIDTH + ( SQUAREWIDTH / 2 ),
BOARDTOP + ( nThisRow - 1 ) * SQUAREHEIGHT + ( SQUAREHEIGHT / 2 ) );
m_bClash = true;
}
}
for( int nCol=1; nCol<NUMCOLS; nCol++)
{
int nRow = 0;
int nThisRow = nRow;
int nThisCol = nCol;
int nColCount = 0;
while( nThisCol < NUMCOLS &&
nThisRow < NUMROWS )
{
if( m_nBoard[nThisRow][nThisCol] != 0 )
{
nColCount++;
}
nThisCol++;
nThisRow++;
}
if( nColCount > 1 )
{
canvas.drawLine( BOARDLEFT + nCol * SQUAREWIDTH + ( SQUAREWIDTH / 2 ),
BOARDTOP + nRow * SQUAREHEIGHT + ( SQUAREHEIGHT / 2 ),
BOARDLEFT + ( nThisCol - 1 ) * SQUAREWIDTH + ( SQUAREWIDTH / 2 ),
BOARDTOP + ( nThisRow - 1 ) * SQUAREHEIGHT + ( SQUAREHEIGHT / 2 ) );
m_bClash = true;
}
}
}
void CheckDiagonal2( Graphics canvas )
{
// Check diagonal 2
for( int nRow=NUMROWS-1; nRow>=0; nRow-- )
{
int nCol = NUMCOLS - 1;
int nThisRow = nRow;
int nThisCol = nCol;
int nColCount = 0;
while( nThisCol >= 0 &&
nThisRow < NUMROWS )
{
if( m_nBoard[nThisRow][nThisCol] != 0 )
{
nColCount++;
}
nThisCol--;
nThisRow++;
}
if( nColCount > 1 )
{
canvas.drawLine( BOARDLEFT + nCol * SQUAREWIDTH + ( SQUAREWIDTH / 2 ),
BOARDTOP + nRow * SQUAREHEIGHT + ( SQUAREHEIGHT / 2 ),
BOARDLEFT + ( nThisCol + 1 ) * SQUAREWIDTH + ( SQUAREWIDTH / 2 ),
BOARDTOP + ( nThisRow - 1 ) * SQUAREHEIGHT + ( SQUAREHEIGHT / 2 ) );
m_bClash = true;
}
}
for( int nCol=NUMCOLS-1; nCol>=0; nCol--)
{
int nRow = 0;
int nThisRow = nRow;
int nThisCol = nCol;
int nColCount = 0;
while( nThisCol >= 0 &&
nThisRow < NUMROWS )
{
if( m_nBoard[nThisRow][nThisCol] != 0 )
{
nColCount++;
}
nThisCol--;
nThisRow++;
}
if( nColCount > 1 )
{
canvas.drawLine( BOARDLEFT + nCol * SQUAREWIDTH + ( SQUAREWIDTH / 2 ),
BOARDTOP + nRow * SQUAREHEIGHT + ( SQUAREHEIGHT / 2 ),
BOARDLEFT + ( nThisCol + 1 ) * SQUAREWIDTH + ( SQUAREWIDTH / 2 ),
BOARDTOP + ( nThisRow - 1 ) * SQUAREHEIGHT + ( SQUAREHEIGHT / 2 ) );
m_bClash = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment