Last active
February 13, 2022 12:43
-
-
Save Lagui-dev/5409cb00187a4d9c9c2302d751b66802 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(const MyApp()); | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return const MaterialApp( | |
home: HomePage(), | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
const HomePage({Key? key}) : super(key: key); | |
@override | |
_HomePageState createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
bool oTurn = true; | |
// 1st player is O | |
List<String> displayElement = ['', '', '', '', '', '', '', '', '']; | |
int oScore = 0; | |
int xScore = 0; | |
int filledBoxes = 0; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
backgroundColor: Colors.lightBlue, | |
body: Column( | |
children: <Widget>[ | |
Expanded( | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Padding( | |
padding: const EdgeInsets.all(30.0), | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
const Text( | |
'Joueur - ❌', | |
style: TextStyle( | |
fontSize: 25, | |
fontWeight: FontWeight.bold, | |
color: Colors.black), | |
), | |
Text( | |
xScore.toString(), | |
style: | |
const TextStyle(fontSize: 40, color: Colors.white), | |
), | |
], | |
), | |
), | |
Padding( | |
padding: const EdgeInsets.all(30.0), | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
const Text( | |
'Joueur - ⭕', | |
style: TextStyle( | |
fontSize: 25, | |
fontWeight: FontWeight.bold, | |
color: Colors.black), | |
), | |
Text( | |
oScore.toString(), | |
style: | |
const TextStyle(fontSize: 40, color: Colors.white), | |
), | |
], | |
), | |
), | |
], | |
), | |
), | |
Expanded( | |
flex: 4, | |
child: GridView.builder( | |
itemCount: 9, | |
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( | |
crossAxisCount: 3), | |
itemBuilder: (BuildContext context, int index) { | |
return GestureDetector( | |
onTap: () { | |
_tapped(index); | |
}, | |
child: Container( | |
decoration: BoxDecoration( | |
border: Border.all(color: Colors.blueAccent)), | |
child: Center( | |
child: Text( | |
displayElement[index], | |
style: const TextStyle( | |
color: Colors.white, fontSize: 65), | |
), | |
), | |
), | |
); | |
}), | |
), | |
Expanded( | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
ElevatedButton( | |
onPressed: _clearScoreBoard, | |
child: const Text("Effacer les scores"), | |
), | |
], | |
), | |
) | |
], | |
), | |
); | |
} | |
void _tapped(int index) { | |
setState(() { | |
if (oTurn && displayElement[index] == '') { | |
displayElement[index] = '⭕'; | |
filledBoxes++; | |
} else if (!oTurn && displayElement[index] == '') { | |
displayElement[index] = '❌'; | |
filledBoxes++; | |
} | |
oTurn = !oTurn; | |
_checkWinner(); | |
}); | |
} | |
void _checkWinner() { | |
// Checking rows | |
if (displayElement[0] == displayElement[1] && | |
displayElement[0] == displayElement[2] && | |
displayElement[0] != '') { | |
_showWinDialog(displayElement[0]); | |
} | |
if (displayElement[3] == displayElement[4] && | |
displayElement[3] == displayElement[5] && | |
displayElement[3] != '') { | |
_showWinDialog(displayElement[3]); | |
} | |
if (displayElement[6] == displayElement[7] && | |
displayElement[6] == displayElement[8] && | |
displayElement[6] != '') { | |
_showWinDialog(displayElement[6]); | |
} | |
// Checking Column | |
if (displayElement[0] == displayElement[3] && | |
displayElement[0] == displayElement[6] && | |
displayElement[0] != '') { | |
_showWinDialog(displayElement[0]); | |
} | |
if (displayElement[1] == displayElement[4] && | |
displayElement[1] == displayElement[7] && | |
displayElement[1] != '') { | |
_showWinDialog(displayElement[1]); | |
} | |
if (displayElement[2] == displayElement[5] && | |
displayElement[2] == displayElement[8] && | |
displayElement[2] != '') { | |
_showWinDialog(displayElement[2]); | |
} | |
// Checking Diagonal | |
if (displayElement[0] == displayElement[4] && | |
displayElement[0] == displayElement[8] && | |
displayElement[0] != '') { | |
_showWinDialog(displayElement[0]); | |
} | |
if (displayElement[2] == displayElement[4] && | |
displayElement[2] == displayElement[6] && | |
displayElement[2] != '') { | |
_showWinDialog(displayElement[2]); | |
} else if (filledBoxes == 9) { | |
_showDrawDialog(); | |
} | |
} | |
void _showWinDialog(String winner) { | |
showDialog( | |
barrierDismissible: false, | |
context: context, | |
builder: (BuildContext context) { | |
return AlertDialog( | |
title: Text(winner + " est le gagnant !"), | |
actions: [ | |
TextButton( | |
child: const Text("Jouer à nouveau"), | |
onPressed: () { | |
_clearBoard(); | |
Navigator.of(context).pop(); | |
}, | |
) | |
], | |
); | |
}); | |
if (winner == '⭕') { | |
oScore++; | |
} else if (winner == '❌') { | |
xScore++; | |
} | |
} | |
void _showDrawDialog() { | |
showDialog( | |
barrierDismissible: false, | |
context: context, | |
builder: (BuildContext context) { | |
return AlertDialog( | |
title: const Text("Perdu"), | |
actions: [ | |
TextButton( | |
child: const Text("Jouer à nouveau"), | |
onPressed: () { | |
_clearBoard(); | |
Navigator.of(context).pop(); | |
}, | |
) | |
], | |
); | |
}); | |
} | |
void _clearBoard() { | |
setState(() { | |
for (int i = 0; i < 9; i++) { | |
displayElement[i] = ''; | |
} | |
}); | |
filledBoxes = 0; | |
} | |
void _clearScoreBoard() { | |
setState(() { | |
xScore = 0; | |
oScore = 0; | |
for (int i = 0; i < 9; i++) { | |
displayElement[i] = ''; | |
} | |
}); | |
filledBoxes = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment