RayCC
Cover Image

Flutter study - Tic Tac Toe

Flutter Study Coding Sample

Source code reference: https://www.geeksforgeeks.org/flutter-building-a-tic-tac-toe-game/

I made some modifications here.

lib/main.dart

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import 'package:flutter/material.dart';

import 'tictactoe.dart';

void main() {
  runApp(const MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: TicTacToe(),
      ),
    );
  }
}

lib/tictactoe.dart

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import 'package:flutter/material.dart';

class TicTacToe extends StatefulWidget {
  const TicTacToe({super.key});

  @override
  State<TicTacToe> createState() => _TicTacToeState();
}

class _TicTacToeState extends State<TicTacToe> {
  int scoreP1 = 0;
  int scoreP2 = 0;

  bool _isP1Turn = true;
  Color p1TurnColor = Colors.red;
  Color p2TurnColor = Colors.transparent;

  // Default value is 9.
  // Player 1's value is 1, Player 2's value is 2.
  // Calculate sum of row, column, diagonal,
  //   and if this value is 3 or 6, then the game is end.
  List<int> gridValue = [9, 9, 9, 9, 9, 9, 9, 9, 9];

  void _resetScore() {
    setState(() {
      scoreP1 = 0;
      scoreP2 = 0;
      _isP1Turn = true;
      gridValue = [9, 9, 9, 9, 9, 9, 9, 9, 9];
    });
    _whoTurn();
  }

  Icon? _checkedArea({required int index}) {
    if (gridValue[index] == 1) {
      return Icon(Icons.circle_outlined);
    } else if (gridValue[index] == 2) {
      return Icon(Icons.close);
    }
    return null;
  }

  void _whoTurn() {
    setState(() {
      p1TurnColor = _isP1Turn ? Colors.red : Colors.transparent;
      p2TurnColor = !_isP1Turn ? Colors.red : Colors.transparent;
    });
  }

  void _whenClick({required int index}) {
    if (gridValue[index] == 9) {
      if (_isP1Turn) {
        gridValue[index] = 1;
      } else {
        gridValue[index] = 2;
      }
      _isP1Turn = !_isP1Turn;
      _checkGame();
      _whoTurn();
    }
  }

  void _checkGame() {
    List<int> setOfLines = [
      gridValue[0] + gridValue[1] + gridValue[2], // row 1
      gridValue[3] + gridValue[4] + gridValue[5], // row 2
      gridValue[6] + gridValue[7] + gridValue[8], // row 3
      gridValue[0] + gridValue[3] + gridValue[6], // column 1
      gridValue[1] + gridValue[4] + gridValue[7], // column 2
      gridValue[2] + gridValue[5] + gridValue[8], // column 3
      gridValue[0] + gridValue[4] + gridValue[8], // diagonal 1
      gridValue[2] + gridValue[4] + gridValue[6], // diagonal 2
    ];

    if (setOfLines.contains(3)) {
      // Player1 is winner: 1+1+1 = 3
      _showDialog(winner: 1);
    } else if (setOfLines.contains(6)) {
      // Player2 is winner: 2+2+2 = 6
      _showDialog(winner: 2);
    } else if (setOfLines.every((item) => item < 9)) {
      // Draw.
      // If there is at least one empty space,
      //   the sum will always exceed 9 because the default value is 9.
      // If there are no empty spaces,
      //   the maximum sum is 6,
      //   which means that [item < 9] refers to the case
      //   where all squares are checked but there is no winner.
      _showDrawDialog();
    }
  }

  void _showDialog({required int winner}) {
    showDialog(
      barrierDismissible: false,
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text("Winner is Player$winner"),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
                setState(() {
                  if (winner == 1) {
                    scoreP1++;
                    _isP1Turn = false;
                  } else {
                    scoreP2++;
                    _isP1Turn = true;
                  }
                  gridValue = [9, 9, 9, 9, 9, 9, 9, 9, 9];
                });
              },
              child: Text("Play again"),
            ),
          ],
        );
      },
    );
  }

  void _showDrawDialog() {
    // Draw
    showDialog(
      barrierDismissible: false,
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text("Draw"),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
                setState(() {
                  gridValue = [9, 9, 9, 9, 9, 9, 9, 9, 9];
                });
              },
              child: Text("Play again"),
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: [
          Text("Tic Tac Toe"),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                "Player 1(O): $scoreP1",
                style: TextStyle(
                  backgroundColor: p1TurnColor,
                ),
              ),
              SizedBox(
                width: 30,
              ),
              Text(
                "Player 2(X): $scoreP2",
                style: TextStyle(
                  backgroundColor: p2TurnColor,
                ),
              ),
            ],
          ),
          ElevatedButton(
            onPressed: _resetScore,
            child: Text("Reset Score"),
          ),
          // Tic Tac Toe game.
          Expanded(
            child: GridView.count(
              crossAxisCount: 3,
              children: List.generate(
                9,
                (index) {
                  return Container(
                    decoration: BoxDecoration(
                      border: Border.all(
                        color: Colors.black,
                      ),
                    ),
                    child: GestureDetector(
                      onTap: () {
                        _whenClick(index: index);
                      },
                      child: _checkedArea(index: index),
                    ),
                  );
                },
              ),
            ),
          ),
        ],
      ),
    );
  }
}