diff --git a/__pycache__/app.cpython-314.pyc b/__pycache__/app.cpython-314.pyc new file mode 100644 index 0000000..2194920 Binary files /dev/null and b/__pycache__/app.cpython-314.pyc differ diff --git a/app.py b/app.py new file mode 100644 index 0000000..e3dd321 --- /dev/null +++ b/app.py @@ -0,0 +1,160 @@ +#!/usr/bin/env python +# encoding: utf-8 +""" +2048 Game with Flask GUI +""" +import random +from flask import Flask, render_template, jsonify, request, session + +app = Flask(__name__) +app.secret_key = '2048_game_secret_key' + +def init(): + matrix = [0 for i in range(16)] + random_lst = random.sample(range(16), 2) + matrix[random_lst[0]] = matrix[random_lst[1]] = 2 + return matrix + +def move(matrix, direction): + mergedList = [] + score = 0 + if direction == 'w': + for i in range(16): + j = i + while j - 4 >= 0: + if matrix[j-4] == 0: + matrix[j-4] = matrix[j] + matrix[j] = 0 + elif matrix[j-4] == matrix[j] and j not in mergedList and j-4 not in mergedList: + matrix[j-4] *= 2 + score += matrix[j-4] + matrix[j] = 0 + mergedList.append(j-4) + mergedList.append(j) + j -= 4 + elif direction == 's': + for i in range(15, -1, -1): + j = i + while j + 4 < 16: + if matrix[j+4] == 0: + matrix[j+4] = matrix[j] + matrix[j] = 0 + elif matrix[j+4] == matrix[j] and j not in mergedList and j+4 not in mergedList: + matrix[j+4] *= 2 + score += matrix[j+4] + matrix[j] = 0 + mergedList.append(j) + mergedList.append(j+4) + j += 4 + elif direction == 'a': + for i in range(16): + j = i + while j % 4 != 0: + if matrix[j-1] == 0: + matrix[j-1] = matrix[j] + matrix[j] = 0 + elif matrix[j-1] == matrix[j] and j not in mergedList and j-1 not in mergedList: + matrix[j-1] *= 2 + score += matrix[j-1] + matrix[j] = 0 + mergedList.append(j-1) + mergedList.append(j) + j -= 1 + else: + for i in range(15, -1, -1): + j = i + while j % 4 != 3: + if matrix[j+1] == 0: + matrix[j+1] = matrix[j] + matrix[j] = 0 + elif matrix[j+1] == matrix[j] and j not in mergedList and j+1 not in mergedList: + matrix[j+1] *= 2 + score += matrix[j+1] + matrix[j] = 0 + mergedList.append(j) + mergedList.append(j+1) + j += 1 + return matrix, score + +def insert(matrix): + getZeroIndex = [] + for i in range(16): + if matrix[i] == 0: + getZeroIndex.append(i) + if not getZeroIndex: + return matrix + + randomZeroIndex = random.choice(getZeroIndex) + max_num = max(matrix) + + if max_num > 128: + candidates = [2, 4, 8, 16, 32] + weights = [60, 25, 10, 4, 1] + else: + candidates = [2, 4, 8] + weights = [70, 25, 5] + + total = sum(weights) + r = random.randint(1, total) + for num, w in zip(candidates, weights): + if r <= w: + matrix[randomZeroIndex] = num + break + r -= w + + return matrix + +def isOver(matrix): + if 0 in matrix: + return False + else: + for i in range(16): + if i % 4 != 3: + if matrix[i] == matrix[i+1]: + return False + if i < 12: + if matrix[i] == matrix[i+4]: + return False + return True + +def has_2048(matrix): + return 2048 in matrix + +def get_game_state(): + if 'matrix' not in session: + session['matrix'] = init() + session['score'] = 0 + return { + 'matrix': session['matrix'], + 'score': session['score'], + 'game_over': isOver(session['matrix']), + 'win': has_2048(session['matrix']) + } + +@app.route('/') +def index(): + return render_template('index.html') + +@app.route('/new_game', methods=['POST']) +def new_game(): + session['matrix'] = init() + session['score'] = 0 + return jsonify(get_game_state()) + +@app.route('/move', methods=['POST']) +def make_move(): + data = request.get_json() + direction = data.get('direction') + + old_matrix = list(session['matrix']) + matrix, add_score = move(session['matrix'], direction) + + if matrix != old_matrix: + insert(matrix) + session['score'] += add_score + + session['matrix'] = matrix + return jsonify(get_game_state()) + +if __name__ == '__main__': + app.run(debug=True) diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..6c246f6 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,238 @@ + + +
+ + +