0% found this document useful (0 votes)
3 views

Document Android Studio

The document contains a Flutter application for a Tic Tac Toe game. It defines the main app structure, game logic, and user interface, allowing players to take turns and check for winning conditions. The game can be reset, and it displays the current game status and player turns.

Uploaded by

bikaydaniel33
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Document Android Studio

The document contains a Flutter application for a Tic Tac Toe game. It defines the main app structure, game logic, and user interface, allowing players to take turns and check for winning conditions. The game can be reset, and it displays the current game status and player turns.

Uploaded by

bikaydaniel33
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Import ‘package :flutter/material.

dart’ ;

Void main() => runApp(TicTacToeApp()) ;

Class TicTacToeApp extends StatelessWidget {

@override

Widget build(BuildContext context) {

Return MaterialApp(

Title : ‘Tic Tac Toe’,

Theme : ThemeData(

primarySwatch : Colors.blue,

),

Home : TicTacToeGame(),

);

Class TicTacToeGame extends StatefulWidget {

@override

_TicTacToeGameState createState() => _TicTacToeGameState() ;

Class _TicTacToeGameState extends State<TicTacToeGame> {

// Initialiser la grille vide

List<String> _board = List.filled(9, ‘’) ;

// Variable pour suivre le joueur actuel (X ou O)

String _currentPlayer = ‘X’ ;


// Variable pour suivre l’état du jeu

String _gameStatus = ‘’ ;

// Fonction pour gérer les pressions sur les cellules

Void _handleTap(int index) {

If (_board[index] == ‘’ && _gameStatus == ‘’) {

setState(() {

_board[index] = _currentPlayer ;

_checkGameStatus() ;

_currentPlayer = _currentPlayer == ‘X’ ? ‘O’ : ‘X’ ;

}) ;

// Fonction pour vérifier l’état du jeu

Void _checkGameStatus() {

// Vérifier les lignes, colonnes et diagonales

List<List<int>> winningCombinations = [

[0, 1, 2],

[3, 4, 5],

[6, 7, 8],

[0, 3, 6],

[1, 4, 7],

[2, 5, 8],

[0, 4, 8],

[2, 4, 6],

];

For (var combination in winningCombinations) {


If (_board[combination[0]] == _board[combination[1]] &&

_board[combination[1]] == _board[combination[2]] &&

_board[combination[0]] != ‘’) {

setState(() {

_gameStatus = ‘${_board[combination[0]]} gagne !’ ;

}) ;

Return ;

// Vérifier si la grille est pleine sans gagnant

If ( !_board.contains(‘’)) {

setState(() {

_gameStatus = ‘Match nul !’ ;

}) ;

// Fonction pour réinitialiser le jeu

Void _resetGame() {

setState(() {

_board = List.filled(9, ‘’) ;

_currentPlayer = ‘X’ ;

_gameStatus = ‘’ ;

}) ;

@override

Widget build(BuildContext context) {


Return Scaffold(

appBar : AppBar(

title : Text(‘Tic Tac Toe’),

),

Body : Column(

mainAxisAlignment : MainAxisAlignment.center,

children : [

_buildBoard(),

SizedBox(height : 20),

Text(

_gameStatus,

Style : TextStyle(fontSize : 24, fontWeight : FontWeight.bold),

),

SizedBox(height : 20),

ElevatedButton(

onPressed : _resetGame,

child : Text(‘Réinitialiser le jeu’),

),

],

),

);

Widget _buildBoard() {

Return GridView.builder(

shrinkWrap : true,

gridDelegate : SliverGridDelegateWithFixedCrossAxisCount(

crossAxisCount : 3,

),
itemCount : 9,

itemBuilder : (context, index) {

return GestureDetector(

onTap : () => _handleTap(index),

child : Container(

margin : EdgeInsets.all(4.0),

color : Colors.blue[50],

child : Center(

child : Text(

_board[index],

Style : TextStyle(fontSize : 40, fontWeight : FontWeight.bold),

),

),

),

);

},

);

You might also like