Let's reproduce the math teaching tool "Jamaica" ❗️ vol.02 "Notes for creating functions in Python"

Introduction

This series is about a certain ** math teaching tool "Jamaica" </ font> ** It is a record of struggling to achieve the following objectives.

** ▶ ︎ </ font> 1. ** *** You will be able to run the "Jamaica" game with a program that uses image display. *** *** ** 2. ** *** Create a program to search and present the existence and contents of solutions for any combination of dice in "Jamaica". *** ***

Last review

Last time, I dealt with the following contents.

*** §1. Reproduce "Jamaica" </ font> *** *** Task.001 Display "Dice roll" as an image *** *** Task.002 "Display the dice roll image according to a random number" ***

This will randomly roll the dice, It is now possible to display the dice roll images that correspond to the rolls side by side.

** _____ [Reference article] _____ ** [^ ref001] Recollection | Let's reproduce the math teaching tool "Jamaica" ❗️'s previous article (vol.01)

>Results of 7 dice[6 5 1 4 2 4 3]

qiita004_jamaica_task002_diceroll.png

§1. Reproduce "Jamaica" </ font> sequel

Task.003 "Creating a function for displaying dice roll images"

■ Summarize the realistic movement of rolling dice in Jamaica by function definition. ➡︎ In order to play Jamaica as a game, it is better to be able to execute it with simple code. ➡︎ On the other hand, the code for displaying the previous Jamaican dice roll seemed to be long. ➡︎ Simplify by defining the following functions according to the Python function definition grammar.

A function that specifies the drive's fig file storage location: figplace()
Function to import required library: libimports()
Np the roll of 7 dice.Function to output in array format: jmc_diceroll()
Function to display Jamaican dice roll result as an image: jmc_display()

** _____ [Reference article] _____ ** [^ ref002] Task003 | Define and call a function in Python (def, return) ➡︎ I referred to the function definition method and the output of the return value.

task003_jmcfunctions1


###A function that returns a character string that specifies the location where the image file required for dice roll display is saved.
def figplace():
  wheredrive = '/content/drive/My Drive/Mathematics/MathProgramming/MathPro-Jamaica/'
  wherefigs = wheredrive + 'Jamaica-figs/'
  return wherefigs

###Function for importing necessary libraries frequently (no return value)
def libimports():
  from google.colab import drive 
  drive.mount('/content/drive',force_remount=False) #Get a mount to access Google Drive files on Google Colabatory (set the second argument to True to force a mount again)
  import matplotlib
  import matplotlib.pyplot as plt
  import matplotlib.image as mpimg
  import numpy as np

###A function that outputs the results of 7 random dice, multiplies the result of the 6th dice by 10, and returns the dice result using Jamaica.
def jmc_diceroll(printOption):
  jmcDice = np.random.randint(1,7,7) #Np randomly generated 7 random variables with a value between 1 and 7.array(1*7 row vector)To output
  jmcDice[5] = jmcDice[5]*10 #Multiply the number of the 6th 2-digit black dice by 10 to get the Jamaican dice result.
  if printOption == True:
    print('Jamaican Dice Roll Results:', jmcDice) #When printOption is True, the result of dice roll is output.
  return jmcDice

###A function that displays the Jamaican dice result as an image with the ratio of the image storage location, the dice roll result, and the size of the displayed image as an argument (no return value)
def jmc_display(figPlace,jmcDice,sizeRatio): #1:Image storage location,2:Dice result,3:Ratio of image size to default
  row = 1 #Number of rows in the frame matrix to display the image
  col = jmcDice.shape[0] #Number of columns in the frame matrix to display the image
  fsizeDefault = 15 #Default value for each image size
  fsize = round(fsizeDefault * sizeRatio) #Display image size=Default value*Ratio of image size to default
  plt.figure(figsize=(fsize,fsize)) #Specifying parameters to adjust the size of each image
  for index in range(row * col):
    plt.subplot(row, col, index+1)
    if index < 5: #Index is 0 to 4, that is, the 1st to 5th dice are white dice and image output
      imgname = 'dice-w-' + str(jmcDice[index]) + '.png'
      whereimg = figPlace + imgname
      img = mpimg.imread(whereimg)
      imgplot = plt.imshow(img)
      plt.axis('off')
    elif index < 7: #index is from 5 to 6, that is, 6,The 7th dice is a black dice for image output
      imgname = 'dice-b-' + str(jmcDice[index]) + '.png'
      whereimg = figPlace + imgname
      img = mpimg.imread(whereimg)
      imgplot = plt.imshow(img)
      plt.axis('off')
    else:
      continue

task003_jmcdisplay


### function trial START ###
libimports()
figPlace = figplace()
jmcDice = jmc_diceroll(printOption=True)
jmc_display(figPlace, jmcDice, sizeRatio=1.4)
### function trial END ###
> Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
>Jamaican Dice Roll Results:[ 1  5  6  5  5 30  2]

qiita005_jamaica_task003_jmcdisplay.png

Next time preview

This time, I mainly described how to define a function in Python and a memorandum of code that simply displays the result of dice roll in Jamaica.

Next time, finally *** Definition of solution search function </ font> based on "Jamaica" </ font> dice roll results *** Step into.

The title is ** vol.03 "Aiming for a solution in Jamaica-Round 1-" **.

REF. Basic information about Jamaica

Q1. What is math teaching tool "Jamaica" </ font>?

A1. "Jamaica" is sold as an arithmetic teaching tool with the theme of "number play" using dice </ font>.

■ *** This is a general product that is also sold on mail-order sites such as Amazon and Yahoo! Shopping. *** *** ➡︎ Click here for the Amazon sales page (https://www.amazon.co.jp/dp/4902756161/ref=cm_sw_r_tw_dp_U_x_XrEPEbCJ5VRST). ➡︎ Please refer to the figure below for the image (quoted from the above page) qiita1_jamaica_red.jpg

■ *** There are two types of dice, white and black, and 5 and 2 dice are included, respectively. *** *** ➡︎ A white dice x 5 and a black dice x 1 are attached to the ring part, and a black dice x 1 is attached to the center part.

color/ Color quantity/ amount Numbers listed/ Numbers
White/ white 5 pieces/ 5 dice 1, 2, 3, 4, 5, 6
black/ black 1 piece/ 1 dice 1, 2, 3, 4, 5, 6
black/ black 1 piece/ 1 dice 10, 20, 30, 40, 50, 60

Q2. What is Jamaica's how to play </ font>?

A2. Perform four arithmetic operations </ font> using each of the white dice x 5 numbers once to make the sum of the black dice x 2 numbers.

For example, if the white dice roll is (1,2,3,5,5) and the black dice roll is (10,6), You can add all the rolls of the white dice to create the equation 1 + 2 + 3 + 5 + 5 = 10 + 6.

qiita002_jamaica_sample_sizedown.png

■ *** White dice x 5 numbers can only be used once each. *** *** ➡︎ For example, if the white dice roll is (2,4,5,3,3), you can use it for calculation. 2 is 1 time, 3 is 2 times, 4 is 1 time, 5 is 1 time. Do not use any more. (You can't use 2 twice or 3 three times.) ➡︎ Of course, ** "Do not use the specified number of times" is also a law **. (In the above example, you can't use 3 only once.)

■ *** Four arithmetic operations are addition, subtraction, multiplication and division </ font>. *** *** ➡︎ ** "Addition" ** </ font> is "addition", that is, ** "addition" **. For example, calculate the sum (result of addition) such as 2 + 3 = 5. </ font> ➡︎ ** "Decrease" ** </ font> is "subtraction", that is, ** "subtraction" **. For example, calculate the difference (subtraction result) such as 5-2 = 3. </ font> ➡︎ ** "Multiplication" ** </ font> is "multiplication", that is, ** "multiplication" **. For example, calculate the product (the result of multiplication) such as 3 × 4 = 12. </ font> ➡︎ ** "division" ** </ font> is "division", that is, ** "division" **. For example, calculate the quotient (division result *) such as 4/2 = 2. </ font>

  • There are rules that allow only divisible operations, that is, rules that do not allow irreducible fractions, and rules that allow irreducible fractions as well. [1] ** In the case of a rule that does not allow irreducible fractions ** When the white dice roll (2,4,6,3,5), you can calculate 6/3 = 2, but The result 6/4 = 3/2 is not available. [2] ** In the case of a rule that allows irreducible fractions ** When the white dice roll (2,4,6,3,5), not only can the calculation 6/3 = 2 be performed, but also The result 6/4 = 3/2 is also available.
  • ** Some dice rolls cannot be solved without using irreducible fractions. ** </ font> Example) When the white dice roll (2,2,4,4,6) and the black dice roll (30,3) The equation {(2- 2/4) + 4} x 6 = 30 + 3 holds. </ font>

■ *** The four arithmetic operations can be performed in any order. *** *** ➡︎ You can add two numbers first and then multiply by another number. In other words, you can use parentheses any number of times, anytime, anywhere. Example) When the white dice roll (3,6,4,4,1) and the black dice roll (20,6) The equation {(3 + 6) + (4 x 4)} + 1 = 20 + 6 holds. </ font>

Reference article summary

[^ ref001]: Recollection | Let's reproduce the math teaching tool "Jamaica" ❗️'s previous article (vol.01) [^ ref002]: Task003 | Define and call a function in Python (def, return)

Recommended Posts