[GO] Let's reproduce the math teaching tool "Jamaica" ❗️ vol.01 "Notes for displaying images 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". *** ***

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 ➡︎ Please refer to the table below for detailed information on publishers (quoted from the above page).

category/ Category information/ Information
the publisher/ publisher Tomoe Soroban(2006/12/8)
language/ language Japanese/ Japanese
ISBN-10 4902756161
ISBN-13 978-4902756166
Release date/ release date 2006/12/8
Packing size/ packing size 13 × 10.6 × 2 cm

■ *** 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>

§1. Reproduce "Jamaica" </ font>

Since I bought a playground equipment called Jamaica, I thought it would be fun to use the real thing, but gradually *** "I want to reproduce with code ..." *** *** "I want to share the screen of an online meeting together ..." *** I came to think so.

So, with the desire to post the first Qiita, this series was born.

The series title is ** Let's reproduce the math teaching tool "Jamaica" </ font> ❗️ ** is.

Take a look at the records of coding beginners struggling to recreate Jamaica.

*"Remarks"* * [1] This series uses a service called Google Colabatory to prepare and publish the results of trial and error in the Jupyter Notebook file (.ipynb). * * [2] The Python version is 3.6.9. This was confirmed using the code below. *

confirmPythonVer


import platform
print('python ' + platform.python_version())
> python 3.6.9

↑↑↑ * The reference URL of the above code is from here * </ font> [^ ref001] * [3] Other required libraries will be described each time they are used. * </ font>

Task.001 "Displaying the dice roll as an image"

■ Realistic Jamaica uses a total of seven two-color dice. ■ The breakdown of dice colors and numbers is as follows. (Repost)

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

■ I want to display the roll of this dice as an image.

Step 1. Prepare an image of the dice. (This time I created it with Microsoft PowerPoint. The extension is .png) Step 2. Save the dice image in the specified folder on Google Drive. '/content/drive/My Drive/Mathematics/MathProgramming/MathPro-Jamaica/Jamaica-figs/' Step 3. Try to display each of the prepared dice images. ↓ ↓ ↓ See below for specific file names.

file name:'dice-' 'color(White=w,black=b)' '-' 'Number (1-6, 10-60)' '.png'
Example) dice-w-4.png =4 of white dice
Example) dice-b-20.png =20 of black dice

** _____ [Reference article] _____ ** [^ ref002] Task001 | How to display images in Python ➡︎ The basic grammar of image display is quoted from here. [^ ref003] Task001 | Arrange images in a grid and erase axis labels ➡︎ An article with the code that seems to be the best as a way to display multiple images side by side in a list.

task001_black10


import matplotlib
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

wheredrive = '/content/drive/My Drive/Mathematics/MathProgramming/MathPro-Jamaica/'
wherefigs = wheredrive + 'Jamaica-figs/'
whereimg = wherefigs + 'dice-b-10.png'
print('Image file reference:',whereimg)
img = mpimg.imread(whereimg)
imgplot = plt.imshow(img)
plt.axis('off') #'on'When set to, vertical and horizontal grid axes are additionally displayed for each image.
>Image file reference:/content/drive/My Drive/Mathematics/MathProgramming/MathPro-Jamaica/Jamaica-figs/dice-b-10.png

qiita003_jamaica_task001_black10.png

task001_white1to6


import numpy as np

row = 1 #Number of rows in the frame matrix to display the image
col = 6 #Number of columns in the frame matrix to display the image
plt.figure(figsize=(10,10)) #Specifying parameters to adjust the size of each image
for num in range(row * col):
    plt.subplot(row, col, num+1) #Creating a frame to display an image in this loop
    imgname = 'dice-w-' + str(num+1) + '.png'
    whereimg = wherefigs + imgname
    img = mpimg.imread(whereimg)
    imgplot = plt.imshow(img)
    plt.axis('off') #'on'When set to, vertical and horizontal grid axes are additionally displayed for each image.

qiita003_jamaica_task001_white1to6.png

Task.002 "Display the dice roll image according to a random number"

■ In realistic Jamaica, roll the dice randomly. ■ The operation of rolling a total of 7 dice is performed by outputting random numbers. Procedure 1. Prepare 7 variables that randomly output positive integers of 1 or more and 6 or less. Step 2. Associate the 1st to 5th results in the first half with the image display of the white dice. Step 3. Associate the 6th result with the image display of the 2-digit black dice. Step 4. Associate the 7th result with the image display of the 1-digit black dice.

** _____ [Reference article] _____ ** [^ ref004] Task002 | Random decimals / integers generated by Python, random, randrange, randint, etc. ➡︎ You can see various modules related to random number generation, so it is good to organize. [^ ref005] Task002 | randint that can make various dice ➡︎ Everyone is checking if the probability of the dice event that everyone cares about is equally certain.

task002_diceroll


row = 1 #Number of rows in the frame matrix to display the image
col = 7 #Number of columns in the frame matrix to display the image
dices = np.random.randint(1,7,row * col) #The first variable is the minimum value of the population, and the second variable is the maximum value of the population.+The 1st and 3rd variables are the number of random numbers to be output
print('Results of 7 dice',dices)

plt.figure(figsize=(20,20))
for index in range(row * col):
  plt.subplot(row, col, index+1)
  if index < 5: #Up to 5 dice (when index is 0 or more and 4) are white and 1 digit
    imgname = 'dice-w-' + str(dices[index]) + '.png'
    whereimg = wherefigs + imgname
    img = mpimg.imread(whereimg)
    imgplot = plt.imshow(img)
    plt.axis('off')
  elif index < 6: #The 6th dice (when index is 5) is black and has 2 digits
    imgname = 'dice-b-' + str(dices[index]) + str(0) + '.png'
    whereimg = wherefigs + imgname
    img = mpimg.imread(whereimg)
    imgplot = plt.imshow(img)
    plt.axis('off')
  elif index < 7: #The 7th dice (when index is 6) is black and 1 digit
    imgname = 'dice-b-' + str(dices[index]) + '.png'
    whereimg = wherefigs + imgname
    img = mpimg.imread(whereimg)
    imgplot = plt.imshow(img)
    plt.axis('off')
  else:
    continue
>Results of 7 dice[6 5 1 4 2 4 3]

qiita004_jamaica_task002_diceroll.png

Next time preview

This time, I mainly wrote a memorandum on how to display an image in Python and how to simulate a random roll of dice.

Next time, I will write a memorandum to organize these codes by function definitions.

The title is ** vol.02 "Notes for creating functions in Python" **.

Reference article summary

[^ ref001]: Demo | Check the version information of the library included in colaboratory by default [^ ref002]: Task001 | How to display images in Python [^ ref003]: Task001 | Arrange images in a grid and remove axis labels [^ ref004]: Task002 | Random decimals / integers generated by Python, random, randrange, randint, etc. [^ ref005]: Task002 | randint that can make various dice

Recommended Posts