{"id":204994,"date":"2025-05-29T12:35:32","date_gmt":"2025-05-29T04:35:32","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204994\/"},"modified":"2025-05-29T12:35:32","modified_gmt":"2025-05-29T04:35:32","slug":"%e6%9c%89%e8%b6%a3%e7%9a%84%e7%bb%88%e7%ab%af%e9%aa%b0%e5%ad%90%e6%b8%b8%e6%88%8f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204994\/","title":{"rendered":"\u6709\u8da3\u7684\u7ec8\u7aef\u9ab0\u5b50\u6e38\u620f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u6709\u8da3\u7684\u7ec8\u7aef\u9ab0\u5b50\u6e38\u620f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300a\u6709\u8da3\u7684\u7ec8\u7aef\u9ab0\u5b50\u6e38\u620f\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241121\/1732156965673e9e258d0e7.jpg\" class=\"aligncenter\"><\/p>\n<p>\u8fd9\u662f\u65e9\u671f\u9879\u76ee\u4e4b\u4e00\u3002\u5728\u7f16\u7a0b\u65b9\u9762\uff0c\u6211\u4ecd\u5728\u638c\u63e1\u5404\u79cd\u5143\u7d20\u3002<\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u6709\u8da3\u7684\u9ab0\u5b50\u6e38\u620f\uff0c\u662f\u6211\u6839\u636ekindom come deliverence\u7684\u9ab0\u5b50\u6e38\u620f\u5236\u4f5c\u7684\u3002\u6211\u4ec5\u5728\u7ec8\u7aef\u4e2d\u521b\u5efa\u5b83\u3002\u4e3b\u8981\u662f\u56e0\u4e3a\u6211\u4ecd\u5728\u5c1d\u8bd5\u638c\u63e1 open gl \u548c\u5176\u4ed6\u56fe\u5f62\u8f93\u5165\u3002<\/p>\n<p>\u975e\u5e38\u6b22\u8fce\u4efb\u4f55\u53cd\u9988\u3002<\/p>\n<p><\/p>\n<pre>import random\n\n\n# menu to welcome the player\ndef menu():\n    print(\"\"\"\n    Welcome to dice\\n\n      Would you like to: \\n\n         1. Review the rule, \\n\n         2. play a new game \\n\n         3. review scoring of dice \\n\n         \"\"\")\n    try:\n        menu_choice = input(\"\")\n    except EOFError:\n        print(\"No input received. Please run the program in an interactive environment.\")\n        return\n    if menu_choice == \"1\":\n        print_rules()\n    elif menu_choice == \"2\":\n        new_game()\n    elif menu_choice == \"3\":\n        print_scroing_values()\n        second_meu()\n    else:\n        print(\"Invalid choice please choose again\")\n        second_meu()\n\n\n#second menu to allow for a alteration of language\n\ndef second_meu():\n    print(\"\"\"\n  What would you like to do now?\n      Would you like to: \\n\n         1. Review the rule, \\n\n         2. play a new game \\n\n         3. review scoring of dice \\n\n         \"\"\")\n    menu_choice = input(\"Please enter your choice: \")\n    if menu_choice == \"1\":\n        print_rules()\n    elif menu_choice == \"2\":\n        new_game()\n    elif menu_choice == \"3\":\n        print_scroing_values()\n        second_meu()\n    else:\n        print(\"Invalid choice please choose again\")\n        second_meu()\n\n#explantion of rules\ndef print_rules():\n    print(\"\"\"\n  A player's turn always begins by throwing all six dice. The player then selects and set aside scoring dice, and at least one die must always be set aside. Then the player can throw the remaining dice again and the situation repeats. \\n\n\n  Scoring combinations are counted only for the current throw, not the entire turn.\\n\n\n  The key element of the game is that if a throw does not produce a single scoring die, then the player's turn is over and all points scored up to that throw are forfeit. It is then the opposing player's turn to throw. \\n\n\n  For that reason, it's best to end your turn before the risk that not a single die will score gets too high. Sometimes it's better not to set aside all the scoring dice you you've thrown, so you stand a better chance of scoring higher on the next throw.\\n\\n\n  \"\"\")\n    second_meu()\n\n#and the scroing system\ndef print_scroing_values():\n    print(\"\"\"Scoring is as follows:\n    - a single 1 is worth 100 points; \\n\n    - a single 5 is worth 50 points; \\n\n    - three of a kind is worth 100 points multiplied by the given number, e.g. three 4s are worth 400 points; \\n\n    - three 1s are worth 1,000 points;\\n\n    - four or more of a kind is worth double the points of three of a kind, so four 4s are worth 800 points, five 4s are worth 1,600 points etc.\\n\n    - full straight 1-6 is worth 1500 points.\\n\n    - partial straight 1-5 is worth 500 points.\\n\n    - partial straight 2-6 is worth 750 points.\\n\\n \"\"\")\n\n\n# This die clas allows funtionality to roll a six sided dice and output the value.\nclass die:\n    def __init__(self):\n        self.value = 0\n\n    def __repr__(self):\n        return f\"{self.value}\"\n\n    def roll(self):\n        self.value = random.randint(1, 6)\n\n\n\n#here is where the class objects are created and organised into a list for ease of use.\ndie1 = die()\ndie2 = die()\ndie3 = die()\ndie4 = die()\ndie5 = die()\ndie6 = die()\n\ndice = [die1, die2, die3, die4, die5, die6]\n\n\n#player class hold the dice values, the player name a method for rolling all 6 dice at one and rerolling specific dice.\nclass player:\n    def __init__(self, name, dice_list, score=4000):\n        self.name = name\n        self.score = score\n        self.dice_list = dice_list\n\n    def deduct_score(self, deduction):\n        self.score -= deduction\n        return self.score\n\n    def roll_d6(self):\n        roll_string: str = \"\"                  #this funtion rolls all the dice coverts them to string and labels them 1 to 6 producing eg 1: 6, 2: 6, 3: 1, 4: 2, 5: 3, 6: 2\n        i = 1\n        for die in dice:\n            die.roll()\n            data = die.value\n            str_data = str(data)\n            str_i = str(i)\n            roll_string += str_i + \": \" + str_data + \", \"\n            i += 1\n        return roll_string\n\n    def print_d6(self):                                     #just print the values\n        roll_string: str = \"\"\n        i = 1\n        for die in dice:\n            data = die.value\n            str_data = str(data)\n            str_i = str(i)\n            roll_string += str_i + \": \" + str_data + \", \"\n            i += 1\n        return roll_string\n\n\n    def re_roll(self, index):                         #re rolls dice speficed\n        index-=1\n        dice[index].roll()\n        return dice[index].value\n\n\n#This is the main game loop it has a lot of moving parts. Take your time reviewing.\ndef new_game():\n    print(\"Hi so what is your name?\\n\")\n    human_name = input(\"\")\n    human_player = player(human_name, dice, 4000)   #creating objects for both human and computer players in the player class\n    print(\"who do you wish to play against?\")\n    computer_name = input(\"\")\n    computer_player = player(computer_name, dice, 4000)\n    play = True\n    while (play):\n        print(\"\"\"ok here is your roll: \n    you roll a: \"\"\")\n        print(human_player.roll_d6())     #use of the player class function roll_d6 to give a string of rolled dice\n        print(\"Time to score you dice\")\n        total_dice_score = possible_to_score(human_player.dice_list)   #this function is below and check to see if any of the dice can score\n        print(total_dice_score)\n        print(\"Whould you like to re-roll you any dice? Y\/N\")  #allowing the player a chance to re roll dice\n        lroll = input(\"\")\n        roll = lroll.upper()\n        if (roll == \"Y\"):\n            dice_choice(human_player)\n        #print(dice)\n        print(\"Time to score you dice\")\n        total_dice_score = possible_to_score(dice)\n        print(total_dice_score)\n        human_player.deduct_score(total_dice_score)\n        print(f\"Your score is now {human_player.score}\")\n        print(f\"Ok it's {computer_player.name} go they rolled\")\n        print(computer_player.roll_d6())\n        print(\"They scored:\")\n        total_dice_score = possible_to_score(dice)\n        print(total_dice_score)\n        computer_player.deduct_score(total_dice_score)\n        print(f\"{computer_player.name} score is now {computer_player.score}\")\n        input(\"\")\n        if human_player.score &lt;= 0 or computer_player.score &lt;= 0:\n            if human_player.score &lt;= 0:\n                print(\"You win well done!!\")\n            else:\n                print(\"You lose to bad.\")\n            play = False\n\n\n\ndef possible_to_score(dice):    #dice is a alis for eaither human_player.dice_list or computer_player.dice_list which would look like [1, 2, 3, 4, 5, 6] with random numbers beteen 1 and 6 in each index.\n    dice_score = 0\n    numbers = count(dice)   #this function count the number of each dice rolled is is a little complex\n    #print(dice)             #more functionality checking\n    #print(numbers)\n    isone = one(numbers)      #each of these are seperate function that check for andy 1s, 5s, any kinds e.g. dice that rolled the same number like four 5s, a full straight or a parital stright\n    isfive = five(numbers)\n    isthree_of_kind = three_of_kind(numbers)\n    isfour_of_kind = four_of_kind(numbers)\n    isfive_of_kind = five_of_kind(numbers)\n    issix_of_kind = six_of_kind(numbers)\n    isfull_straight = full_straight(numbers)\n    isone_to_five = one_to_five(numbers)\n    istwo_to_six = two_to_six(numbers)\n    #print(isone, isfive, isthree_of_kind, isfour_of_kind, isfive_of_kind, issix_of_kind, isfull_straight, isone_to_five, istwo_to_six)      #used this to check the function was working in construction\n    if (isone == True):\n        dice_score = 10\n\n    if (isfive == True):\n        dice_score = 50\n\n    if (isthree_of_kind[0] == True):\n        dice_score = 100 * isthree_of_kind[1]    #these function woudl assign score to the dice depeding on valibles\n\n    if (isfour_of_kind[0] == True):\n        dice_score = 200 * isfour_of_kind[1]\n\n    if (isfive_of_kind[0]):\n        dice_score = 400 * isfive_of_kind[1]\n\n    if (issix_of_kind[0]):\n        dice_score = 800 * issix_of_kind[1]\n\n    if (isfull_straight == True):\n        temp_dice_score = 1500\n        if temp_dice_score &gt; dice_score:\n            dice_score = temp_dice_score\n\n    if (isone_to_five == True):\n        temp_dice_score = 500\n        if temp_dice_score &gt; dice_score:\n            dice_score = temp_dice_score\n    if (istwo_to_six == True):\n        temp_dice_score = 600\n\n        if temp_dice_score &gt; dice_score:\n            dice_score = temp_dice_score\n    return dice_score\n\ndef one(counts):\n    if counts[0] &gt;= 1:\n        return True\n    else:\n        return False\n\ndef five(counts):\n    if counts[4] &gt;= 1:\n        return True\n    else:\n        return False\n\n\ndef three_of_kind(counts):\n    if 3 in counts:\n        return True, counts.index(3)\n    else:\n        return False, None\n\n\ndef four_of_kind(counts):\n    if 4 in counts:\n        return True, counts.index\n    else:\n        return False, None\n\n\ndef five_of_kind(counts):\n    if 5 in counts:\n        return True, counts.index\n    else:\n        return False, None\n\n\ndef six_of_kind(counts):\n    if 6 in counts:\n        return True, counts.index\n    else:\n        return False, None\n\n\ndef full_straight(counts):\n    if all(value == 1 for value in counts):\n        return True\n    else:\n        return False\n\ndef one_to_five(counts):\n    if counts[0] &lt;= 1 &amp; counts[1] &lt;= 1 &amp; counts[2] &lt;= 1 &amp; counts[3] &lt;= 1 &amp; counts[4] &lt;= 1:\n        return True\n    else:\n        return False\n\n\ndef two_to_six(counts):\n    if counts[1] &lt;= 1 &amp; counts[2] &lt;= 1 &amp; counts[3] &lt;= 1 &amp; counts[4] &lt;= 1 &amp; counts[5] &lt;= 1:\n        return True\n    else:\n        return False\n\ndef count(dice):                   #dice is a alis for eaither human_player.dice_list or computer_player.dice_list which would look like [1, 2, 3, 4, 5, 6] with random numbers beteen 1 and 6 in each index.\n    value_counts = count_values(dice)\n    num_ones = value_counts[1]         #the job of this to take the 1: prefix to all the counts to leave behind only the count itself\n    num_twos = value_counts[2]\n    num_threes = value_counts[3]\n    num_fours = value_counts[4]\n    num_fives = value_counts[5]\n    num_sixes = value_counts[6]\n    numbers_list = [num_ones, num_twos, num_threes, num_fours, num_fives, num_sixes]\n    return numbers_list   #this goes back to new game\n\ndef count_values(dice_list):\n    counts = {i: 0 for i in range(1, 7)}     #this created this {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0}\n    for die in dice_list:\n        counts[die.value] += 1 #assins each of the dice to a value in numerical order eg of output {1: 1, 2: 1, 3: 2, 4: 1, 5: 1, 6: 0}\n    return counts\n\ndef dice_choice(player):    #alis for human_player\n    rolling = True\n    print(\"Please type the dice you want to re-roll after each choice press enter. When you finish type exit and press enter.\")\n    while (rolling):\n        player_input = input(\"\")\n        if player_input.isdigit():                     #checks is the input is a number\n            number = int(player_input)\n            if 1 &lt;= number &lt;= 6:                        #checks if it falls between 1 and 6\n                player.re_roll(number)                         #rolls the dice specified\n            else:\n                print(\"Invalid entry must be a value between 1 and 6\")\n        elif player_input == \"exit\":\n            print(f\"Your new values are: {player.print_d6()} .\")              #outputs the results\n            rolling = False\n        else:\n            print(\"invalid entry must be a number or exit, please try again.\")\n\n\nmenu()\n\n\n<\/pre>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u6709\u8da3\u7684\u7ec8\u7aef\u9ab0\u5b50\u6e38\u620f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u8da3\u7684\u7ec8\u7aef\u9ab0\u5b50\u6e38\u620f \u672c\u7bc7\u6587\u7ae0\u4e3b\u8981&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204994","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204994","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204994"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204994\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}