@@ -10,8 +10,6 @@ class HomePage extends StatefulWidget {
10
10
class _HomePageState extends State <HomePage > {
11
11
12
12
13
-
14
-
15
13
List <GameButton > buttonList;
16
14
var player1, player2, activePlayer;
17
15
@@ -21,7 +19,7 @@ class _HomePageState extends State<HomePage> {
21
19
buttonList = doInit ();
22
20
}
23
21
24
- List <GameButton > doInit (){
22
+ List <GameButton > doInit () {
25
23
//making a list of the moves player1 has choosed. This will help in judgement of the winner
26
24
player1 = new List ();
27
25
player2 = new List ();
@@ -42,120 +40,139 @@ class _HomePageState extends State<HomePage> {
42
40
return gameButtons;
43
41
}
44
42
45
- void playGame (GameButton gb){
46
-
43
+ void playGame (GameButton gb) {
47
44
//setting the state because every time I press the button I want the changes on the screen to be saved instantaneously
48
45
setState (() {
49
- if (activePlayer== 1 ) {
46
+ if (activePlayer == 1 ) {
50
47
gb.text = 'X' ;
51
48
gb.bg = Colors .red;
52
49
activePlayer = 2 ;
53
50
player1.add (gb.id);
54
51
//adding the id of the gameButton currently pressed by player1 to the list,so that we can evaluate later.
55
52
}
56
53
else {
57
-
58
54
gb.text = "0" ;
59
55
gb.bg = Colors .black;
60
56
activePlayer = 1 ;
61
57
player2.add (gb.id);
62
58
//adding the id of the gameButton currently pressed by player2 to the list,so that we can evaluate later.
63
- }
64
- gb.enabled = false ; //because we want to disable the button which are played
65
- int winner = checkWinner ();
59
+ }
60
+ gb.enabled =
61
+ false ; //because we want to disable the button which are played
62
+ int winner = checkWinner ();
66
63
67
- //TIED GAME Check
68
- if (winner == - 1 ){
69
- if (buttonList.every ((p)=> p.text!= "" )){
64
+ //TIED GAME Check
65
+ if (winner == - 1 ) {
66
+ if (buttonList.every ((p) => p.text != "" )) {
70
67
showDialog (
71
68
context: context,
72
- builder: (_) => new CustomDialog ("Game Tied" , "Press The Reset Button to Start Again" , resetGame)
69
+ builder: (_) =>
70
+ new CustomDialog (
71
+ "Game Tied" , "Press The Reset Button to Start Again" ,
72
+ resetGame)
73
73
);
74
74
}
75
75
}
76
76
});
77
- }
77
+ }
78
78
79
79
80
- int checkWinner (){
80
+ int checkWinner () {
81
81
int winner = - 1 ;
82
82
83
83
//WINNER Check Code
84
84
85
- if (player1.contains (1 ) && player1.contains (2 ) && player1.contains (3 )) //ROW 1
86
- winner= 1 ;
87
- if (player2.contains (1 ) && player2.contains (2 ) && player2.contains (3 )) //ROW 1
88
- winner= 2 ;
85
+ if (player1.contains (1 ) && player1.contains (2 ) &&
86
+ player1.contains (3 )) //ROW 1
87
+ winner = 1 ;
88
+ if (player2.contains (1 ) && player2.contains (2 ) &&
89
+ player2.contains (3 )) //ROW 1
90
+ winner = 2 ;
89
91
90
92
91
- if (player1.contains (4 ) && player1.contains (5 ) && player1.contains (6 )) //ROW 2
92
- winner= 1 ;
93
- if (player2.contains (4 ) && player2.contains (5 ) && player2.contains (6 )) //ROW 2
94
- winner= 2 ;
93
+ if (player1.contains (4 ) && player1.contains (5 ) &&
94
+ player1.contains (6 )) //ROW 2
95
+ winner = 1 ;
96
+ if (player2.contains (4 ) && player2.contains (5 ) &&
97
+ player2.contains (6 )) //ROW 2
98
+ winner = 2 ;
95
99
96
100
97
- if (player1.contains (7 ) && player1.contains (8 ) && player1.contains (9 )) //ROW 3
98
- winner= 1 ;
99
- if (player2.contains (7 ) && player2.contains (8 ) && player2.contains (9 )) //ROW 3
100
- winner= 2 ;
101
+ if (player1.contains (7 ) && player1.contains (8 ) &&
102
+ player1.contains (9 )) //ROW 3
103
+ winner = 1 ;
104
+ if (player2.contains (7 ) && player2.contains (8 ) &&
105
+ player2.contains (9 )) //ROW 3
106
+ winner = 2 ;
101
107
102
- if (player1.contains (1 ) && player1.contains (4 ) && player1.contains (7 )) //COLUMN 1
103
- winner= 1 ;
104
- if (player2.contains (1 ) && player2.contains (4 ) && player2.contains (7 )) //COLUMN 1
105
- winner= 2 ;
108
+ if (player1.contains (1 ) && player1.contains (4 ) &&
109
+ player1.contains (7 )) //COLUMN 1
110
+ winner = 1 ;
111
+ if (player2.contains (1 ) && player2.contains (4 ) &&
112
+ player2.contains (7 )) //COLUMN 1
113
+ winner = 2 ;
106
114
107
115
108
- if (player1.contains (2 ) && player1.contains (5 ) && player1.contains (8 )) //COLUMN 2
109
- winner= 1 ;
110
- if (player2.contains (2 ) && player2.contains (5 ) && player2.contains (8 )) //COLUMN 2
111
- winner= 2 ;
116
+ if (player1.contains (2 ) && player1.contains (5 ) &&
117
+ player1.contains (8 )) //COLUMN 2
118
+ winner = 1 ;
119
+ if (player2.contains (2 ) && player2.contains (5 ) &&
120
+ player2.contains (8 )) //COLUMN 2
121
+ winner = 2 ;
112
122
113
123
114
- if (player1.contains (3 ) && player1.contains (6 ) && player1.contains (9 )) //COLUMN 3
115
- winner= 1 ;
116
- if (player2.contains (3 ) && player2.contains (6 ) && player2.contains (9 )) //COLUMN 3
117
- winner= 2 ;
124
+ if (player1.contains (3 ) && player1.contains (6 ) &&
125
+ player1.contains (9 )) //COLUMN 3
126
+ winner = 1 ;
127
+ if (player2.contains (3 ) && player2.contains (6 ) &&
128
+ player2.contains (9 )) //COLUMN 3
129
+ winner = 2 ;
118
130
119
131
120
- if (player1.contains (1 ) && player1.contains (5 ) && player1.contains (9 )) //DIAGONAL 1
121
- winner= 1 ;
122
- if (player2.contains (1 ) && player2.contains (5 ) && player2.contains (9 )) //DIAGONAL 1
123
- winner= 2 ;
132
+ if (player1.contains (1 ) && player1.contains (5 ) &&
133
+ player1.contains (9 )) //DIAGONAL 1
134
+ winner = 1 ;
135
+ if (player2.contains (1 ) && player2.contains (5 ) &&
136
+ player2.contains (9 )) //DIAGONAL 1
137
+ winner = 2 ;
124
138
125
139
126
- if (player1.contains (3 ) && player1.contains (5 ) && player1.contains (7 )) //DIAGONAL 2
127
- winner= 1 ;
128
- if (player2.contains (3 ) && player2.contains (5 ) && player2.contains (7 )) //DIAGONAL 2
129
- winner= 2 ;
130
-
140
+ if (player1.contains (3 ) && player1.contains (5 ) &&
141
+ player1.contains (7 )) //DIAGONAL 2
142
+ winner = 1 ;
143
+ if (player2.contains (3 ) && player2.contains (5 ) &&
144
+ player2.contains (7 )) //DIAGONAL 2
145
+ winner = 2 ;
131
146
132
147
133
148
//check for winner
134
149
135
- if (winner!= - 1 )
136
- {
137
- if (winner == 1 ){
150
+ if (winner != - 1 ) {
151
+ if (winner == 1 ) {
138
152
showDialog (
139
- context: context,
140
- builder: (_) => new CustomDialog ("Player 1 Won!!" , "Reset the Game" , resetGame));
141
- }
142
- else {
143
- showDialog (
144
- context: context,
145
- builder: (_) => new CustomDialog ("Player 2 Won!!" , "Reset the Game" , resetGame));
146
- }
147
-
153
+ context: context,
154
+ builder: (_) =>
155
+ new CustomDialog ("Player 1 Won!!" , "Reset the Game" , resetGame));
156
+ }
157
+ else {
158
+ showDialog (
159
+ context: context,
160
+ builder: (_) =>
161
+ new CustomDialog ("Player 2 Won!!" , "Reset the Game" , resetGame));
162
+ }
148
163
}
149
- return winner;
164
+ return winner;
150
165
}
151
166
152
- void resetGame (){
153
- if (Navigator .canPop (context)) Navigator .pop (context); //For closing the AlertDialog
154
- {
155
- setState (() {
156
- buttonList = doInit (); //Initializing the buttons again to the start by calling the doInit()
157
- });
158
- }
167
+ void resetGame () {
168
+ if (Navigator .canPop (context)) Navigator .pop (
169
+ context); //For closing the AlertDialog
170
+ {
171
+ setState (() {
172
+ buttonList =
173
+ doInit (); //Initializing the buttons again to the start by calling the doInit()
174
+ });
175
+ }
159
176
}
160
177
161
178
@override
@@ -165,49 +182,67 @@ void playGame(GameButton gb){
165
182
title: Center (child: new Text ('Tic Tac Toe' )),
166
183
backgroundColor: Colors .black,
167
184
),
168
- body: new Column (
169
- mainAxisAlignment : MainAxisAlignment .center,
170
- crossAxisAlignment : CrossAxisAlignment .stretch ,
185
+ body: new Stack (
186
+ fit : StackFit
187
+ .expand ,
171
188
children: < Widget > [
172
- Expanded (
173
- child: new GridView .builder (
174
- padding: const EdgeInsets .all (10.0 ),
175
- gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount (
176
- crossAxisCount: 3 ,
177
- childAspectRatio: 1.0 ,
178
- crossAxisSpacing: 9.0 ,
179
- mainAxisSpacing: 9.0 ,
180
-
181
- ),
182
- itemCount: buttonList.length,
183
- itemBuilder: (context,index){
184
- return new SizedBox (
185
- width: 100.0 ,
186
- height: 100.0 ,
187
- child: new RaisedButton (
188
- padding: const EdgeInsets .all (8.0 ),
189
- onPressed: buttonList[index].enabled ? ()=> playGame (buttonList[index]): null ,
190
- child: new Text (
191
- buttonList[index].text,
192
- style: new TextStyle (color: Colors .white, fontSize: 20.0 ),
193
- ),
194
- color: buttonList[index].bg,
195
- disabledColor: buttonList[index].bg,
196
- ),
197
- );
198
- },
199
189
200
- ),
190
+ new Image (
191
+ image: new AssetImage ('images/finalttt.jpg' ),
192
+ fit: BoxFit .cover, //covers the entire screen top to bottom
193
+ color: Colors .black54,
194
+ colorBlendMode: BlendMode .darken,
201
195
),
202
- new RaisedButton (
203
- child: Center (child: new Text ("Reset" , style: new TextStyle (color: Colors .black),)),
204
- color: Colors .red,
205
- onPressed: resetGame
206
- )
207
196
197
+ new Column (
198
+ mainAxisAlignment: MainAxisAlignment .center,
199
+ crossAxisAlignment: CrossAxisAlignment .stretch,
200
+ children: < Widget > [
201
+ Expanded (
202
+ child: new GridView .builder (
203
+ padding: const EdgeInsets .all (10.0 ),
204
+ gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount (
205
+ crossAxisCount: 3 ,
206
+ childAspectRatio: 1.0 ,
207
+ crossAxisSpacing: 9.0 ,
208
+ mainAxisSpacing: 9.0 ,
209
+
210
+ ),
211
+ itemCount: buttonList.length,
212
+ itemBuilder: (context, index) {
213
+ return new SizedBox (
214
+ width: 100.0 ,
215
+ height: 100.0 ,
216
+ child: new RaisedButton (
217
+ padding: const EdgeInsets .all (8.0 ),
218
+ onPressed: buttonList[index].enabled ? () =>
219
+ playGame (buttonList[index]) : null ,
220
+ child: new Text (
221
+ buttonList[index].text,
222
+ style: new TextStyle (
223
+ color: Colors .white, fontSize: 20.0 ),
224
+ ),
225
+ color: buttonList[index].bg,
226
+ disabledColor: buttonList[index].bg,
227
+ ),
228
+ );
229
+ },
230
+
231
+ ),
232
+ ),
233
+ new RaisedButton (
234
+ child: Center (child: new Text (
235
+ "Reset" , style: new TextStyle (color: Colors .black),)),
236
+ color: Colors .red,
237
+ onPressed: resetGame
238
+ )
239
+
240
+ ],
241
+ ),
208
242
],
243
+
209
244
),
210
245
211
246
);
212
247
}
213
- }
248
+ }
0 commit comments