From ac6b0d718728abeaadc4bb693962c5164ac28725 Mon Sep 17 00:00:00 2001 From: DiamondNether90 Date: Tue, 30 Sep 2025 03:21:40 +1000 Subject: [PATCH] Initial Commit --- index.css | 0 index.html | 14 +++++++ index.js | 16 ++++++++ index.py | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 146 insertions(+) create mode 100644 index.css create mode 100644 index.html create mode 100644 index.js create mode 100644 index.py diff --git a/index.css b/index.css new file mode 100644 index 0000000..e69de29 diff --git a/index.html b/index.html new file mode 100644 index 0000000..b755da2 --- /dev/null +++ b/index.html @@ -0,0 +1,14 @@ + + + + + + +

Blackjack

+ + + +

+ + + \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..9a44c99 --- /dev/null +++ b/index.js @@ -0,0 +1,16 @@ +function sendData(data) { + fetch('http://localhost:5000/api/send', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ message: data }) + }) + .then(res => res.json()) + .then(data => { + document.getElementById('response').innerText = data.reply; + }) + .catch(err => console.error('Error:', err)); + } + +sendData("reset") \ No newline at end of file diff --git a/index.py b/index.py new file mode 100644 index 0000000..3cba92c --- /dev/null +++ b/index.py @@ -0,0 +1,116 @@ +from operator import mod +from tokenize import String +from flask import Flask, request, jsonify +from flask_cors import CORS + +import random as r +import math as m + +# Suit Order: Hearts, Diamonds, Spades, Clubs +# 37 = 9 * 4 + 1 = 9 of Hearts +drawn = [] +drawnnames = [] +cards = [] +for i in range(52): + cards.append(i) + +ranks = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] +ranknames = ["Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"] +suitnames = ["Hearts", "Diamonds", "Spades", "Clubs"] +gameDone = False + +def toString(data): + str = "" + if len(data) == 1: + return data[0] + else: + for i in data: + if i != data[len(data)-1]: + str = str + i + ", " + else: + str = f"{str}and {i}" + return str + +def getScore(data): + aces = 0 + score = 0 + for i in data: + if i == 1: + aces += 1 + score += i + print(score) + while (aces > 0) & (score <= 11): + score += 10 + aces -= 1 + return score + +def loseText(score): + if score <= 21: + return "" + else: + return "You have busted! Click reset to play again." + +def botLose(data): + if getScore(data) > 21: + return "You won! Click reset to start another game." + else: + return "You lost. Click reset to start another game!" + +app = Flask(__name__) +CORS(app) +@app.route('/api/send', methods=['POST']) +def receive_data(): + data = request.get_json() + user_message = data.get('message', '') + + + global drawn + global cards + global drawnnames + global gameDone + if user_message == 'reset': + # Suit Order: Hearts, Diamonds, Spades, Clubs + # 37 = 9 * 4 + 1 = 9 of Hearts + drawn = [] + drawnnames = [] + cards = [] + gameDone = False + for i in range(52): + cards.append(i) + return_message = "Game successfully reset" + elif user_message == 'hit': + if getScore(drawn) <= 21: + drawcard = cards[r.randint(0, len(cards)-1)] + cards.remove(drawcard) + drawrank = ranks[m.floor(drawcard/4)] + + drawn.append(drawrank) + drawnnames.append(f"{ranknames[m.floor(drawcard/4)]} of {suitnames[drawcard % 4]}") + else: + gameDone = True + + return_message = f"Your card is the {drawnnames[len(drawnnames)-1]}.\n\nYour cards so far are the {toString(drawnnames)}.\n\nYour current score is {getScore(drawn)}.\n\n{loseText(getScore(drawn))}" + elif user_message == 'stand': + if gameDone: + return_message = "The game is already done! Click reset to try again." + else: + botdrawn = [] + botdrawnnames = [] + while getScore(botdrawn) <= getScore(drawn): + drawcard = cards[r.randint(0, len(cards)-1)] + cards.remove(drawcard) + drawrank = ranks[m.floor(drawcard/4)] + + botdrawn.append(drawrank) + botdrawnnames.append(f"{ranknames[m.floor(drawcard/4)]} of {suitnames[drawcard % 4]}") + + return_message = f"Your score was {getScore(drawn)}.\n\nThe bot draw the {toString(botdrawnnames)}, obtaining a score of {getScore(botdrawn)}.\n\n{botLose(botdrawn)}" + gameDone = True + else: + return_message = user_message + + print("Backend") + return jsonify({"reply": return_message}) + +if __name__ == '__main__': + app.run(debug=True) \ No newline at end of file