dalyk34 commited on
Commit
22ae33a
1 Parent(s): caa301e

Adding files

Browse files
Files changed (7) hide show
  1. data → .gitignore +0 -0
  2. README.md +33 -0
  3. app.py +30 -0
  4. data/game_logs.json +12 -0
  5. models/__init__.py +0 -0
  6. requirements.txt +1 -0
  7. tictactoe.py +32 -0
data → .gitignore RENAMED
File without changes
README.md ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Tic-Tac-Toe API
2
+
3
+ This is a simple API for playing Tic-Tac-Toe using Python and Flask.
4
+
5
+ ## Endpoints
6
+
7
+ - **POST /make_move**: Makes a move on the board.
8
+ - **Request Body**:
9
+ ```json
10
+ {
11
+ "move": <int>, # 0 to 8, representing a cell on the 3x3 board
12
+ "player": "<str>" # 'X' or 'O'
13
+ }
14
+ ```
15
+ - **Response**:
16
+ ```json
17
+ {
18
+ "board": [[<row1>], [<row2>], [<row3>]],
19
+ "message": "Move successful."
20
+ }
21
+ ```
22
+
23
+ - **GET /get_board**: Returns the current state of the board.
24
+ - **Response**:
25
+ ```json
26
+ {
27
+ "board": [[<row1>], [<row2>], [<row3>]]
28
+ }
29
+ ```
30
+
31
+ ## Running the Project
32
+
33
+ 1. Install dependencies:
app.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, jsonify, request
2
+ from tictactoe import TicTacToe
3
+
4
+ app = Flask(__name__)
5
+ game = TicTacToe()
6
+
7
+ @app.route("/make_move", methods=["POST"])
8
+ def make_move():
9
+ data = request.get_json()
10
+ move = data.get("move")
11
+ player = data.get("player")
12
+
13
+ if not move or not player:
14
+ return jsonify({"error": "Invalid input. 'move' and 'player' are required."}), 400
15
+
16
+ try:
17
+ valid_move, board_state = game.make_move(move, player)
18
+ if valid_move:
19
+ return jsonify({"board": board_state, "message": "Move successful."}), 200
20
+ else:
21
+ return jsonify({"error": "Invalid move. Try again."}), 400
22
+ except Exception as e:
23
+ return jsonify({"error": str(e)}), 500
24
+
25
+ @app.route("/get_board", methods=["GET"])
26
+ def get_board():
27
+ return jsonify({"board": game.get_board()}), 200
28
+
29
+ if __name__ == "__main__":
30
+ app.run(debug=True)
data/game_logs.json ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "game_id": 1,
4
+ "moves": [
5
+ {"player": "X", "move": 0},
6
+ {"player": "O", "move": 1},
7
+ {"player": "X", "move": 4},
8
+ {"player": "O", "move": 2}
9
+ ],
10
+ "winner": "X"
11
+ }
12
+ ]
models/__init__.py ADDED
File without changes
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ Flask==2.2.2
tictactoe.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class TicTacToe:
2
+ def __init__(self):
3
+ self.board = [" "] * 9 # 3x3 board as a flat list
4
+
5
+ def get_board(self):
6
+ return [self.board[i:i+3] for i in range(0, 9, 3)]
7
+
8
+ def make_move(self, move, player):
9
+ # Check if the move is valid
10
+ if self.board[move] != " ":
11
+ return False, self.get_board()
12
+
13
+ self.board[move] = player
14
+ if self.check_win(player):
15
+ return True, self.get_board()
16
+
17
+ if " " not in self.board:
18
+ raise Exception("It's a draw!")
19
+
20
+ return True, self.get_board()
21
+
22
+ def check_win(self, player):
23
+ win_conditions = [
24
+ [0, 1, 2], [3, 4, 5], [6, 7, 8], # Rows
25
+ [0, 3, 6], [1, 4, 7], [2, 5, 8], # Columns
26
+ [0, 4, 8], [2, 4, 6] # Diagonals
27
+ ]
28
+
29
+ for condition in win_conditions:
30
+ if all(self.board[i] == player for i in condition):
31
+ return True
32
+ return False