99import androidx .appcompat .app .AppCompatActivity ;
1010import java .util .Random ;
1111
12-
1312public class gameplay_ai extends AppCompatActivity {
1413 @ Override
1514 protected void onCreate (Bundle savedInstanceState ) {
1615 super .onCreate (savedInstanceState );
17- setContentView (R .layout .activity_gameplay_2 );
16+ setContentView (R .layout .activity_gameplay_ai );
17+ setBoardVisibility (View .GONE , ids );
1818 }
1919 int turn = 0 ;
20+ String playerSymbol ;
21+ String aiSymbol ;
2022 String [][] board = new String [3 ][3 ];
21- private void disableAllButtons () {
22- int [] ids = {
23- R .id .button00 , R .id .button01 , R .id .button02 ,
24- R .id .button10 , R .id .button11 , R .id .button12 ,
25- R . id . button20 , R . id . button21 , R . id . button22
26- };
23+ int [] ids = {
24+ R . id . button00 , R . id . button01 , R . id . button02 ,
25+ R .id .button10 , R .id .button11 , R .id .button12 ,
26+ R .id .button20 , R .id .button21 , R .id .button22
27+ };
28+ private void disableAllButtons ( int [] ids ) {
2729 for (int id : ids ) {
2830 findViewById (id ).setEnabled (false );
2931 }
3032 }
33+ private void setBoardVisibility (int visibility , int [] ids ) {
34+ for (int id : ids ) {
35+ findViewById (id ).setVisibility (visibility );
36+ }
37+ }
3138 public boolean checkWin (String [][] board ) {
3239 // rows
3340 for (int r = 0 ; r < 3 ; r ++) {
@@ -52,15 +59,30 @@ public boolean checkWin(String[][] board) {
5259 return false ;
5360 }
5461 public boolean checkDraw (String [][] board ) {
55- // If ANY cell is still null, it's not a draw
5662 for (int r = 0 ; r < 3 ; r ++) {
5763 for (int c = 0 ; c < 3 ; c ++) {
5864 if (board [r ][c ] == null ) return false ;
5965 }
6066 }
61- // If no empty cells AND no win → it's a draw
6267 return !checkWin (board );
6368 }
69+ public void startAsX (View v ){
70+ playerSymbol = "X" ;
71+ aiSymbol = "O" ;
72+ findViewById (R .id .buttonStartX ).setVisibility (View .GONE );
73+ findViewById (R .id .buttonStartO ).setVisibility (View .GONE );
74+ setBoardVisibility (View .VISIBLE , ids );
75+ TextView tv = findViewById (R .id .turnIndicator );
76+ tv .setText ("Your turn!" );
77+ }
78+ public void startAsO (View v ){
79+ playerSymbol = "O" ;
80+ aiSymbol = "X" ;
81+ findViewById (R .id .buttonStartX ).setVisibility (View .GONE );
82+ findViewById (R .id .buttonStartO ).setVisibility (View .GONE );
83+ setBoardVisibility (View .VISIBLE , ids );
84+ makeAIMove (findViewById (R .id .turnIndicator ));
85+ }
6486 private void makeAIMove (TextView tv ) {
6587 Random r = new Random ();
6688 int row , col ;
@@ -71,53 +93,49 @@ private void makeAIMove(TextView tv) {
7193 col = r .nextInt (3 );
7294 } while (board [row ][col ] != null );
7395
74- // Get the button by ID
7596 String buttonID = "button" + row + col ;
7697 int id = getResources ().getIdentifier (buttonID , "id" , getPackageName ());
7798 Button b = findViewById (id );
7899
79- // AI always plays O
80- board [row ][col ] = "O" ;
81- b .setText ("O" );
100+ board [row ][col ] = aiSymbol ;
101+ b .setText (aiSymbol );
82102 b .setEnabled (false );
83103 tv .setText ("Your turn!" );
84104
85105 if (checkWin (board )) {
86- tv .setText ("O wins!" );
87- disableAllButtons ();
106+ tv .setText (aiSymbol + " wins!" );
107+ disableAllButtons (ids );
88108 return ;
89109 }
90110 if (checkDraw (board )) {
91111 tv .setText ("It's a draw!" );
92- disableAllButtons ();
112+ disableAllButtons (ids );
93113 return ;
94114 }
95115 turn = 0 ; // Back to player
96116 }
97117 public void onPress (View v ){
98- Button b = (Button ) v ;
99- TextView tv = findViewById (R .id .turnIndicator );
100118 String tag = (String ) v .getTag ();
101119 int row = Character .getNumericValue (tag .charAt (0 ));
102120 int col = Character .getNumericValue (tag .charAt (2 ));
103-
104- // Set X and O accordingly
105- board [row ][col ] = "X" ;
106- b .setText ("X" );
121+ Button b = ( Button ) v ;
122+ TextView tv = findViewById ( R . id . turnIndicator );
123+ board [row ][col ] = playerSymbol ;
124+ b .setText (playerSymbol );
107125 v .setEnabled (false );
108126
109127 if (checkWin (board )) {
110- tv .setText ("X wins!" );
111- disableAllButtons ();
128+ tv .setText (playerSymbol + " wins!" );
129+ disableAllButtons (ids );
112130 return ;
113131 }
114132 if (checkDraw (board )) {
115133 tv .setText ("It's a draw!" );
116- disableAllButtons ();
134+ disableAllButtons (ids );
117135 return ;
118136 }
119- tv .setText ("Thinking..." );
120137
138+ tv .setText ("Thinking..." );
121139 getWindow ().setFlags (
122140 WindowManager .LayoutParams .FLAG_NOT_TOUCHABLE ,
123141 WindowManager .LayoutParams .FLAG_NOT_TOUCHABLE
0 commit comments