Initial Commit
This commit is contained in:
		
							
								
								
									
										14
									
								
								index.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								index.html
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,14 @@ | ||||
| <html> | ||||
|     <head> | ||||
|         <link rel="stylesheet" href="index.css"> | ||||
|     </head> | ||||
|  | ||||
|     <body> | ||||
|         <h1>Blackjack</h1> | ||||
|         <button onclick="sendData('hit')">Hit</button> | ||||
|         <button onclick="sendData('stand')">Stand</button> | ||||
|         <button onclick="sendData('reset')">Reset</button> | ||||
|         <p id="response"></p> | ||||
|     </body> | ||||
|     <script src="index.js"></script> | ||||
| </html> | ||||
							
								
								
									
										16
									
								
								index.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								index.js
									
									
									
									
									
										Normal file
									
								
							| @@ -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") | ||||
							
								
								
									
										116
									
								
								index.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										116
									
								
								index.py
									
									
									
									
									
										Normal file
									
								
							| @@ -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) | ||||
		Reference in New Issue
	
	Block a user