TictactoeBoard BE

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Detailed Explanation of TicTacToeBoard Class

Package Declaration
1 /**
2 * Defines the package name for the class .
3 */
4 package com . example . tictactoe ;

Imports
1 /**
2 * Imports necessary classes from the Android SDK .
3 */
4 import android . content . Context ;
5 import android . content . res . TypedArray ;
6 import android . graphics . Canvas ;
7 import android . graphics . Paint ;
8 import android . util . AttributeSet ;
9 import android . view . MotionEvent ;
10 import android . view . View ;
11
12 import androidx . annotation . Nullable ;

Class Declaration
1 /**
2 * Defines the TicT acToeBoa rd class that extends View .
3 */
4 public class T icTacToe Board extends View {

Member Variables
1 /**
2 * Declares member variables for board color , X color , O color , winning line color , and paint object .
3 * Initializes the GameLogic object to manage the game state .
4 * cellSize calculates the size of each cell in the board .
5 */
6 private final int boardColor ;
7 private final int XColor ;
8 private final int OColor ;
9 private final int w i n n i n g L in e C o l o r ;
10
11 private final Paint paint = new Paint ();
12
13 private final GameLogic game ;
14
15 private int cellSize = getWidth () / 3;

1
Constructor
1 /**
2 * Initializes the custom view , reading attributes from XML .
3 * Sets the colors for board , X , O , and winning line from the attributes .
4 */
5 public TicTa cToeBoar d ( Context context , @Nullable AttributeSet attrs ) {
6 super ( context , attrs );
7
8 game = new GameLogic ();
9
10 TypedArray a = context . getTheme (). o b t a i n S t y l e d A t t r i b u t e s ( attrs ,
11 R . styleable . TicTacToeBoard , 0 , 0);
12
13 try {
14 boardColor = a . getColor ( R . styleable . TicTacToeBoard_boardColor , 0);
15 XColor = a . getColor ( R . styleable . TicTacToeBoard_XColor , 0);
16 OColor = a . getColor ( R . styleable . TicTacToeBoard_OColor , 0);
17 w i nn i n g L i n e C o l o r = a . getColor ( R . styleable . T i c T ac T o eB o a r d_ w i nn i n g Li n e Co l o r , 0);
18 } finally {
19 a . recycle ();
20 }
21 }

onMeasure Method
1 /**
2 * Ensures the view is square and calculates the cell size .
3 */
4 @Override
5 protected void onMeasure ( int width , int height ) {
6 super . onMeasure ( width , height );
7
8 int dimension = Math . min ( g e tM e a s u r e d W i d t h () , g e t M e a s u r e d H e i g h t ());
9 cellSize = dimension / 3;
10
11 s e t M e a s u r e d D i m e n s i o n ( dimension , dimension );
12 }

onDraw Method
1 /**
2 * Sets the paint style and draws the game board and markers .
3 */
4 @Override
5 protected void onDraw ( Canvas canvas ) {
6 paint . setStyle ( Paint . Style . STROKE );
7 paint . setAntiAlias ( true );
8
9 drawGameBoard ( canvas );
10 drawMarkers ( canvas );
11 }

onTouchEvent Method
1 /**
2 * Handles touch events to update the game state and redraw the board .
3 */
4 @Override
5 public boolean onTouchEvent ( MotionEvent event ) {
6 float x = event . getX ();
7 float y = event . getY ();
8
9 int action = event . getAction ();
10

2
11 if ( action == MotionEvent . ACTION_DOWN ) {
12 int row = ( int ) Math . ceil ( y / cellSize );
13 int col = ( int ) Math . ceil ( x / cellSize );
14
15 if ( game . u pd at eG a me Bo ar d ( row , col )) {
16 invalidate ();
17
18 if ( game . getPlayer () % 2 == 0) {
19 game . setPlayer ( game . getPlayer () - 1);
20 } else {
21 game . setPlayer ( game . getPlayer () + 1);
22 }
23 }
24
25 invalidate ();
26
27 return true ;
28 }
29 return false ;
30 }

drawGameBoard Method
1 /**
2 * Draws the lines for the Tic Tac Toe grid .
3 */
4 private void drawGameBoard ( Canvas canvas ) {
5 paint . setColor ( boardColor );
6 paint . setSt rokeWidt h (16);
7
8 for ( int c = 1; c < 3; c ++) {
9 canvas . drawLine ( cellSize * c , 0 , cellSize * c , canvas . getWidth () , paint );
10 }
11
12 for ( int r = 1; r < 3; r ++) {
13 canvas . drawLine (0 , cellSize * r , canvas . getWidth () , cellSize * r , paint );
14 }
15 }

drawMarkers Method
1 /**
2 * Draws X or O markers on the board based on the game state .
3 */
4 private void drawMarkers ( Canvas canvas ) {
5 for ( int r = 0; r < 3; r ++) {
6 for ( int c = 0; c < 3; c ++) {
7 if ( game . getGameBoard ()[ r ][ c ] != 0) {
8 if ( game . getGameBoard ()[ r ][ c ] == 1) {
9 drawX ( canvas , r , c );
10 } else {
11 drawO ( canvas , r , c );
12 }
13 }
14 }
15 }
16 }

drawX Method
1 /**
2 * Draws an X marker on the board .
3 */
4 private void drawX ( Canvas canvas , int row , int col ) {
5 paint . setColor ( XColor );

3
6
7 canvas . drawLine (( float ) (( col + 1) * cellSize - cellSize * 0.2) ,
8 ( float ) ( row * cellSize + cellSize * 0.2) ,
9 ( float ) ( col * cellSize + cellSize * 0.2) ,
10 ( float ) (( row + 1) * cellSize - cellSize * 0.2) , paint );
11 canvas . drawLine (( float ) ( col * cellSize + cellSize * 0.2) ,
12 ( float ) ( row * cellSize + cellSize * 0.2) ,
13 ( float ) (( col + 1) * cellSize - cellSize * 0.2) ,
14 ( float ) (( row + 1) * cellSize - cellSize * 0.2) , paint );
15 }

drawO Method
1 /**
2 * Draws an O marker on the board .
3 */
4 private void drawO ( Canvas canvas , int row , int col ) {
5 paint . setColor ( OColor );
6
7 canvas . drawOval (( float ) ( col * cellSize + cellSize * 0.2) ,
8 ( float ) ( row * cellSize + cellSize * 0.2) ,
9 ( float ) (( col * cellSize + cellSize ) - cellSize * 0.2) ,
10 ( float ) (( row * cellSize + cellSize ) - cellSize * 0.2) , paint );
11 }

You might also like