import java.awt.*;
import java.awt.Font;
import java.awt.event.*;
public class test2
{
public static void main(String[] args)
{
new Dots4Win();
}
}
class Dots4Win extends Frame
{
private int FirstPlayer = 0 ;
private int SecondPlayer = 0 ;
private int counter = 0 ;
private int ROW = 8 ;
private int COLM = 8 ;
private Font PlayerTurnFont = new Font("",Font.BOLD,18), // you can choose the font uou like
ScoresFont = new Font("",Font.BOLD,20);
private Rectangle dots[][] = new Rectangle[ROW][COLM] ;
/* 0 denotes black; 1 denotes red; 2 denotes blue */
private int status[][] = new int[ROW][COLM] ;
// tests if the box is inside the game table
boolean IsInside(int Row,int Col){
return (Row >= 0) && (Row < this.ROW) && (Col >= 0) && (Col < this.COLM);
}
// tests if the box is a neighbour to the current box
boolean IsNeighbour(int Row1, int Col1, int Row2, int Col2){
if((Row1 == Row2)&&(Col1 == Col2)) // not the same dot
return false;
else // but a neighbour
return ((Row1 == Row2)|| (Col1 == Col2));
}
/* tests if some dot neighbours are with the same color */
void TestDotNeighbours(int Row, int Col,Graphics gc){
int DotStatus = status[Row][Col];
if(DotStatus == 0) // if it's balck
return;
int PointsToAdd = 0;
Color OldColor = gc.getColor(),LineColor;
// choosing the color of the line to be drawn
if(DotStatus == 1)
LineColor = Color.RED;
else if(DotStatus == 2)
LineColor = Color.BLUE;
else
LineColor = Color.BLACK;
gc.setColor(LineColor);
// testing the neighbours
for(int i=Row-1;i<=Row+1;i++){
for(int j=Col-1;j<=Col+1;j++){
if(IsNeighbour(Row,Col,i,j)&&(IsInside(i,j))){
if(status[i][j]== DotStatus) {
gc.drawLine( dots[Row][Col].x+9, dots[Row][Col].y+9, dots[i][j].x+9, dots[i][j].y+9 ) ;
PointsToAdd ++;
}//if
}//if
}//for j
}//for i
// restore the old color
gc.setColor(OldColor);
// adding the points to ...
if(DotStatus == 1) // first player
this.FirstPlayer += PointsToAdd;
else if(DotStatus == 2) // second player
this.SecondPlayer += PointsToAdd;
}
Dots4Win()
{
setTitle( " Dot Game" );
setLocation( new Point( 200, 200 ) ) ;
setSize( 480, 480 );
setBackground( Color.gray );
setResizable( false ) ; // this prevents re-sizing of window
for ( int i = 0; i < ROW ; i++ )
{
for ( int j = 0; j < COLM; j++ )
{
dots[i][j] = new Rectangle( 50 + 50*i, 50 + 50*j, 18, 18 ) ;
}
}
for ( int i = 0; i < ROW ; i++ )
{
for ( int j = 0; j < COLM; j++ )
{
status[i][j] = 0 ;
}
}
addMouseListener( new MouseCatcher() ) ; // register a mouse listener
addWindowListener( new WindowCatcher( ) ); // register a window listener
setVisible( true );
}
public void paint( Graphics gc )
{
/* saves the old color and font to be able to restore them */
Color OldColor = gc.getColor();
Font OldFont = gc.getFont();
String strText = "Red Player:";
String strText1 = "Blue Player:";
if(counter % 2 == 0)// first player
{
gc.setFont(PlayerTurnFont) ;
gc.setColor(Color.WHITE);
gc.drawString( strText, 40, 450 );
// restore font
gc.setFont(OldFont);
gc.setColor(Color.BLUE);
gc.drawString( strText1, 280, 450 );
}
else{
gc.setFont(PlayerTurnFont) ;
gc.setColor(Color.WHITE);
gc.drawString( strText1, 200, 450 );
// restore font
gc.setFont(OldFont);
gc.setColor(Color.RED);
gc.drawString( strText, 50, 450 );
}
//restore the old font and color
gc.setColor(OldColor);
gc.setFont(OldFont);
for ( int i = 0; i < ROW ; i++ )
{
for ( int j = 0; j < COLM; j++ )
{
if ( status[i][j] == 0 )
{
gc.setColor( Color.black ) ;
}
else if ( status[i][j] == 1 )
{
gc.setColor(Color.red ) ;
}
else
{
gc.setColor( Color.blue ) ;
}
gc.fillOval( dots[i][j].x, dots[i][j].y, dots[i][j].width, dots[i][j].height ) ;
}
}
// we test the neighbours of each Dot so we can calculate the points of each player
this.FirstPlayer = 0;
this.SecondPlayer = 0;
for ( int i = 0; i < ROW ; i++ )
{
for ( int j = 0; j < COLM; j++ )
{
TestDotNeighbours(i,j,gc) ;
}
}//for
// we divide the result by 2 because each line is counted twice (From Dot A --> Dot B and From Dot B --> Dot A)
this.FirstPlayer = (int) Math.round(this.FirstPlayer / 2.0);
this.SecondPlayer = (int) Math.round(this.SecondPlayer / 2.0);
// and finally we print the results
OldFont = gc.getFont();
OldColor = gc.getColor();
gc.setFont(ScoresFont);
gc.setColor(Color.YELLOW);
gc.drawString( String.format("%d", this.FirstPlayer), 150, 450 );
gc.drawString( String.format("%d", this.SecondPlayer), 350, 450 );
gc.setFont(OldFont);
gc.setColor(OldColor);
}//paint
class MouseCatcher extends MouseAdapter
{
public void mousePressed(MouseEvent evt )
{
Point ptMouse = evt.getPoint() ;
for ( int i = 0; i < ROW ; i++ )
{
for ( int j = 0; j < COLM; j++ )
{
if ( dots[i][j].contains( ptMouse ) && (status[i][j] == 0) ) // check you're on a black dot
{
if ( counter % 2 == 0 )
{
status[i][j] = 1 ;
}
else
{
status[i][j] = 2 ;
}
counter++ ;
repaint();
}
}
}
}
}
}
class WindowCatcher extends WindowAdapter
{
@Override
public void windowClosing( WindowEvent evt )
{
evt.getWindow( ).dispose( ); // releases this window's resources
System.exit( 0 );
}
}