{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CSCI 127, Lab 12, Fall 2023" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Student Name:\n", "### Today's Date: " ] }, { "cell_type": "markdown", "metadata": { "id": "Gd8YViX1yei1" }, "source": [ "---\n", "# ASSIGNMENT INFORMATION\n", "___\n", "## Logistics\n", "* Due: Tuesday, November 14th no later than 11:58 p.m.\n", "* Partner Information: Complete this assignment individually.\n", "* Submission Instructions: Upload your solution, named YourFirstName-YourLastName-Lab11.***ipynb*** to the BrightSpace Lab 11 Dropbox.\n", "* Deadline Reminder: Once this deadline passes, BrightSpace will no longer accept your Python submission and you will no longer be able to earn credit. Thus, if you are not able to fully complete the assignment, submit whatever you have before the deadline so that partial credit can be earned.\n", "## Learning outcomes\n", "* Gain experience using NumPy arrays to solve problems.\n", "* Gain familiarity with Jupyter Notebooks.\n", "## Background\n", "* In a modified game of Yahtzee, five *eight-sided dice are rolled\n", "once.*\n", "* For this assignment, you will *simulate this modified game of Yahtzee to determine how likely certain outcomes are.*\n", "* In this modified version of Yahtzee, a **Low Roll** occurs when each of the five dice is either a 1 or an 2. For example, 1-1-1-1-1 or 2-2-1-2-1.\n", "* In this modified version of Yahtzee, a **Three of a Kind** occurs when three of the dice show the same number. The other two dice must not show this number and must also be different from one another. For example, 4-7-4-4-2 but not 4-7-4-4-7.\n", "* In this modified version of Yahtzee, a **Large Straight** occurs when the five numbers can be arranged consecutively (for example, 1-3-4-2-5 or 5-7-4-6-3). Hint: the numpy library contains a sort function.\n", "## Instructions\n", "* In the section called **Class and function definitions**, *add the missing methods* such that when the ```main``` is called, it might produce the following output. *You will need to write new code, but do not change any of the existing code.*\n", "\n", "```\n", "Number of Rolls: 20000\n", "---------------------\n", "Number of Low Rolls: 18\n", "Percent: 0.09%\n", "\n", "Number of Three of a Kinds: 1995\n", "Percent: 9.97%\n", "\n", "Number of Large Straights: 261\n", "Percent: 1.30%\n", "```\n", "\n", "* After you have made all the additions required to produce the correct output, rename this Jupyter Notebeook file according to the instructions above. Be sure to use the *.ipynb* extension.\n", "* Upload your Jupyter Notebook file to the BrightSpace Lab 11 Dropbox.\n", "## Grading\n", "* 2.5 points – Correct file type uploaded; instructions followed; code commented.\n", "* 2.5 points - Low Rolls are identified correctly.\n", "* 2.5 points - Three of a Kinds are identified correctly.\n", "* 2.5 points - Large Straights are identified correctly." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "___\n", "# PROGRAM BEGINS HERE\n", "___" ] }, { "cell_type": "markdown", "metadata": { "id": "JbComCfcygC8" }, "source": [ "## Imports\n", "The following libraries are required and imported." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "AMyqCbrM1PBL" }, "outputs": [], "source": [ "import numpy as np\n", "import random" ] }, { "cell_type": "markdown", "metadata": { "id": "W_u9vsEc2kcA" }, "source": [ "## Class and function definitions\n", "Create a class called ```Die```." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "3JKHnqWU1GPm" }, "outputs": [], "source": [ "class Die:\n", "\n", " def __init__(self, sides):\n", " \"\"\"A constructor method to create a die\"\"\"\n", " self.sides = sides\n", "\n", " def roll(self):\n", " \"\"\"A general method to roll the die\"\"\"\n", " return random.randint(1, self.sides)" ] }, { "cell_type": "markdown", "metadata": { "id": "2mR65UDK14Pd" }, "source": [ "Create a class called ```Yahtzee```." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "fkXU6Ow_13cm" }, "outputs": [], "source": [ "class Yahtzee:\n", "\n", " def __init__(self, sides):\n", " \"\"\"A constructor method that can record 5 dice rolls\"\"\"\n", " self.rolls = np.zeros(5, dtype=np.int16)\n", " self.sides = sides\n", "\n", " def roll_dice(self):\n", " \"\"\"A general method that rolls 5 dice\"\"\"\n", " for i in range(len(self.rolls)):\n", " self.rolls[i] = Die(self.sides).roll()\n", "\n", " def count_outcomes(self):\n", " \"\"\"A helper method that determines how many 1s, 2s, etc. were rolled\"\"\"\n", " counts = np.zeros(self.sides + 1, dtype=np.int16)\n", " for roll in self.rolls:\n", " counts[roll] += 1\n", " return counts" ] }, { "cell_type": "markdown", "metadata": { "id": "auTfWK8A2H6u" }, "source": [ "Define a function called ```main```." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "id": "BRTN6iNs2bNj" }, "outputs": [], "source": [ "def main(how_many):\n", "\n", " low_rolls = 0\n", " three_of_a_kinds = 0\n", " large_straights = 0\n", " game = Yahtzee(8) # 8-sided dice\n", "\n", " for i in range(how_many):\n", " game.roll_dice()\n", " if game.is_it_low_roll():\n", " low_rolls += 1\n", " elif game.is_it_three_of_a_kind():\n", " three_of_a_kinds += 1\n", " elif game.is_it_large_straight():\n", " large_straights += 1\n", "\n", " print(\"Number of Rolls:\", how_many)\n", " print(\"---------------------\")\n", " print(\"Number of Low Rolls:\", low_rolls)\n", " print(\"Percent:\", \"{:.2f}%\\n\".format(low_rolls * 100 / how_many))\n", " print(\"Number of Three of a Kinds:\", three_of_a_kinds)\n", " print(\"Percent:\", \"{:.2f}%\\n\".format(three_of_a_kinds * 100 / how_many))\n", " print(\"Number of Large Straights:\", large_straights)\n", " print(\"Percent:\", \"{:.2f}%\".format(large_straights * 100 / how_many))\n" ] }, { "cell_type": "markdown", "metadata": { "id": "zC-d-nML3Q5s" }, "source": [ "## Yahtzee outcomes\n", "Execute the ```main``` function ```n``` times." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 333 }, "id": "7GAEaemW3cHu", "outputId": "699a190e-f913-4648-dfa8-c09a8b331bec" }, "outputs": [ { "ename": "AttributeError", "evalue": "'Yahtzee' object has no attribute 'is_it_low_roll'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[5], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m n \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m10000\u001b[39m\n\u001b[0;32m----> 2\u001b[0m main(n)\n", "Cell \u001b[0;32mIn[4], line 10\u001b[0m, in \u001b[0;36mmain\u001b[0;34m(how_many)\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(how_many):\n\u001b[1;32m 9\u001b[0m game\u001b[38;5;241m.\u001b[39mroll_dice()\n\u001b[0;32m---> 10\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m game\u001b[38;5;241m.\u001b[39mis_it_low_roll():\n\u001b[1;32m 11\u001b[0m low_rolls \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m\n\u001b[1;32m 12\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m game\u001b[38;5;241m.\u001b[39mis_it_three_of_a_kind():\n", "\u001b[0;31mAttributeError\u001b[0m: 'Yahtzee' object has no attribute 'is_it_low_roll'" ] } ], "source": [ "n = 10000\n", "main(n)" ] } ], "metadata": { "colab": { "provenance": [], "toc_visible": true }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 4 }