لدي خبره في html , css , js لدي مشروع عباره عن Prisoner's Dilemma Game
اريد ربط الباك اند بأصفحه html
اكواد الباك اند ( بايثون )
vvvvvvvv
# -*- coding: utf-8 -*-"""
Created on Sat Feb 17 20:56:38 2024
@author: ibra9
"""# final in ibra laptopfrom qiskit importQuantumCircuit,Aer, execute
import random
def encode_message(bits, bases):
message =[]for i in range(len(bits)):
qc =QuantumCircuit(1,1)if bases[i]==0:if bits[i]==0:passelse:
qc.x(0)else:if bits[i]==0:
qc.h(0)else:
qc.x(0)
qc.h(0)
message.append(qc)return message
def measure_message(message, bases):
backend =Aer.get_backend('qasm_simulator')
measurements =[]for i in range(len(message)):if bases[i]==0:
message[i].measure(0,0)else:
message[i].h(0)
message[i].measure(0,0)
job = execute(message[i], backend, shots=1)
result = job.result()
measured_bit = int(list(result.get_counts(message[i]).keys())[0])
measurements.append(measured_bit)return measurements
def compare_bases(alice_bases, bob_bases, s):
indices =[]if(s ==1):for i in range(len(alice_bases)):if alice_bases[i]== bob_bases[i]:
indices.append(i)return indices
else:return0def bb84_protocol(num_bits, s):
alice_bits =[int(bit)for bit in''.join(random.choices(['0','1'], k=num_bits))]
alice_bases =[int(bit)for bit in''.join(random.choices(['0','1'], k=num_bits))]
alice_message = encode_message(alice_bits, alice_bases)
bob_bases =[int(bit)for bit in''.join(random.choices(['0','1'], k=num_bits))]
bob_measurements = measure_message(alice_message, bob_bases)
matching_indices = compare_bases(alice_bases, bob_bases, s)if matching_indices:
alice_key =[alice_bits[i]for i in matching_indices]
bob_key =[bob_measurements[i]for i in matching_indices]return alice_key, bob_key
return alice_bits, bob_bases
def run( s, num_bits=10):
alice_key, bob_key = bb84_protocol(num_bits, s)if s ==1:return s
else:return s
def playerdecision(playername):whileTrue:
decision = input(f"{playername}, do you Cooperate (C) or Defect (D)? ").upper()if decision =='C':return run(1)elif decision =='D':return run(0)print("Invalid input, please choose 'C' for Cooperate or 'D' for Defect.")def gameoutcome(player1_decision, player2_decision):if player1_decision ==1and player2_decision ==1:return(3,3)# Both cooperateelif player1_decision ==1and player2_decision ==0:return(0,5)# Player 1 cooperates, Player 2 defectselif player1_decision ==0and player2_decision ==1:return(5,0)# Player 1 defects, Player 2 cooperateselse:return(1,1)# Both defectdef main():print("Prisoner's Dilemma Game")print("Two prisoners have a choice to Cooperate (C) or Defect (D) over three rounds.")print("If both cooperate, they each get 3 years off their sentence per round.")print("If one defects and the other cooperates, the defector goes free (0 years) and the cooperator gets 5 years per round.")print("If both defect, they each get 1 year off their sentence per round.")
total_player1_score, total_player2_score =0,0for round_number in range(1,4):print(f"\n--- Round {round_number} ---")
player1_decision = playerdecision("Player 1")
player2_decision = playerdecision("Player 2")
player1_score, player2_score = gameoutcome(player1_decision, player2_decision)
total_player1_score += player1_score
total_player2_score += player2_score
print(f"Player 1's decision this round: {'Cooperate' if player1_decision == 1 else 'Defect'}")print(f"Player 2's decision this round: {'Cooperate' if player2_decision == 1 else 'Defect'}")print(f"Player 1's score this round: {player1_score}")print(f"Player 2's score this round: {player2_score}")print("\nFinal Results after 3 rounds:")print(f"Player 1's total years sentence score: {total_player1_score}")print(f"Player 2's total years sentence score: {total_player2_score}")if __name__ =="__main__":
main()
تم التعديل في بواسطة El Sayed El Tohamy تعديل عنوان السؤال ليكون مناسبًا
السؤال
Ziyad Al Maghribi
لدي خبره في html , css , js لدي مشروع عباره عن Prisoner's Dilemma Game
اريد ربط الباك اند بأصفحه html
اكواد الباك اند ( بايثون )
vvvvvvvv
تعديل عنوان السؤال ليكون مناسبًا
7 أجوبة على هذا السؤال
Recommended Posts
انضم إلى النقاش
يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.