[DOCKER] Major remodeling! Practical programming learning method for beginners to learn through reform!

This article is the 22nd day article of ** Saison Information Systems Advent Calendar 2020 **.

Introduction Caution What I tried to do What I did and what I thought when I was doing it Learning method thought through the above Conclusion

Introduction

――For the time being, after finishing the introductory book for one language, I understand the basic grammar such as for statement and if statement, but I don't know what to do next. ――I heard that practice is good for learning programming, so I want to make something, but I don't know what to start with (I can't think of what I want to make in the first place) ――I copied the sample program for the time being, but I don't feel like I learned anything.

I think that there are some programming beginners who hit the wall like the above. There was a time when I was at a loss because I didn't know what to do after I first came into contact with programming when I was in college and finished the C language and Java grammar books. Although I long for people who can develop, I feel that it is very difficult to start development immediately after finishing the grammar, and I do not know what fills the gap between them. I myself have not yet found a clear answer to the above question. However, although I am currently interested in writing this article, I kept putting it off ~~ I did not have a chance to learn it. Through my experience of outputting with Rust, I found a few hints of the above questions. I felt like it was done, so I will share it in this article. I hope I can help the learning of programming beginners as much as possible.

important point

――This article only shares what steps you did while learning Rust, so I can't give you specific code or code explanations. The main thing is to share what you think through it. (If you are interested in the contents of the code, please refer to the Github repository) ――As mentioned above, the main thing is to share your thoughts, so it is not an article to the effect that you should practice the steps described here as they are. ――The following things are written only for what actually worked, so it seems that things are going smoothly, but in reality, I was wandering around and doing a lot of trial and error (I thought I'd write that as well). However, I stopped because the sentences seemed to be redundant)

What I tried to do

-Ported the code of this repository using Rust (Java code I wrote in the past) ――I want to do it if I can make it into a container (because I am studying Docker) --The original code is a console application, but I would like to turn it into a web application if possible. ――The rules are quite different from the original blackjack, so I want to get closer (if I can afford it)

What I did and what I thought when I was doing it

――When I checked Rust's web framework, it seems that Rocket and actix-web are famous. ――I'm wondering which one to use → Eventually, I thought I'd refer to the here book that I had for a long time, but I used actix-web used in the sample program of this book. Will use ――There was a sample code of the TODO app in the above book, but without copying it suddenly, I started from Hello World as step by step and proceeded little by little according to the guidance of the book. ――Finally, the following TODO application was created by copying the code of the TODO application of the book (Since it is a sutra so far, the completed one is the same as what is written in the book) 14.png

From here, we will make this app look like blackjack. If you try to write the code suddenly, it will be difficult to understand, so I will summarize the requirements. In order to lower the hurdle to think about requirements, I will first make a game based on the rules of the original Java code.

--13 cards for each of the 4 cards → In the original code, it was managed in a two-dimensional array, but if you want to make it a web application, you will need to use a database --Draw as much as you can draw the card from the dealer (In the original app, it was decided by random numbers whether to draw or not ~ ~ Since the implementation is troublesome ~ ~ Change to stop drawing when the total is 17 or more) → Original Since the code uses a while statement, it seems to be usable as it is ――If the number of dealers exceeds 21 at that point, you lose the other side. ――When the dealer's hand is confirmed, draw any number on your turn and play the size of the number ――If you exceed 21 on your turn, you lose --Scores are added as they are for playing cards --Can be restarted after the game is over --The drawn card shows on the screen what you have drawn each time

The result is the following app.

Start screen 1.PNG

Draw screen <img width = "200" alt = "3.PNG" src = "https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/364187/b186fa1b-35cd-0762" -55f7-98abbe86cfd9.png ">

Win/Loss screen 2.PNG

Current score 5.PNG

When the dealer starts with the previous relationship, there are times when the dealer loses without doing anything, so if you keep pressing RESTART until the other side does not exceed 21, you can play for the time being. ~~ It's annoying ~~ At this point, I made a lot of trial and error, so I thought I should stop it, but I still had time to spare, so I will improve it according to the following requirements to get closer to Blackjack.

--The dealer first draws two cards and turns to his turn with one card revealed. ――If you draw any number of cards and play STAY (go to the game), turn to the dealer's turn again and the dealer keeps drawing until 17 or more --If J, Q and K are subtracted, 10 points will be added. --Changed draw to HIT and stand to STAY

The result is as follows One dealer hand released 10.PNG

I've been able to play a lot, but I thought it would be more game-like if I could bet on in-game currency, so I'll add it.

As a requirement

--First, the player will be given 1000 $ (appropriate units and numbers) --You have to decide the bet amount before the game and then start --If you win, the bet amount will increase, and if you lose, it will decrease. --You can play the game again while keeping the amount you have after the game ends (data) I think I'll use the base) --Play if an invalid value is entered in the amount entry field (negative number, non-number, amount exceeding your possession, etc.) --The game is over when the amount of money you have becomes 0, and when you restart the game, start again from the amount of 1000

When implemented to meet the above requirements, the following application was created. Bet setting screen 18.png

Increases when you win 11.PNG

Game over screen 12.PNG

Now you can play a lot! A can be treated as 1 or 10, but there are still some differences from the actual rules, such as treating it as 1 in this game, but I think I was able to get closer to Blackjack. Here and there ~~ The delivery date of the Advent Calendar article is approaching ~~ I'm personally satisfied, so this development ends here.

The Rust blackjack code I created after this article is listed in this repository, so please play with it if you like. ~~ If you read the code, you can see that it is an implementation that was pushed using the database ~~ You can run it without Rust environment but with Docker environment. Specifically, after cloning or downloading the above repository

docker build -t <Any name> .
docker run -p 8080:8080 <Same name as above>

You can play by accessing localhost: 8080 after executing. (The docker run command has various options, and if you add --name, you can give the container any name you like, but I will omit the story here.)

As a point that can be improved in the future, what came to my mind

--High score function (displays the 5 best amounts of in-game currency earned) ← It seems to be implemented --A function that treats A as 1 or 10 ← The player side can make an arbitrary selection at the timing of pulling A, but it seems necessary to consider an algorithm that allows the dealer side to judge which of 1 and 10 is optimal. --Improve the appearance of the game on the browser ~~ (CSS messing around) ~~ --Addition of special rules such as double push and split

Will it be around?

Learning method that I thought through the above

I thought through this experience, but even if I think that I will develop 〇〇, I do not know what to do to get some haze. For those who are clear that they want to develop 〇〇

--Clarify the requirements of 〇〇 --Make the requirements of 〇〇 as detailed as possible (so that you can see the direction of implementation of each requirement)

I thought again that the above is important in this development. However, I think there are some people who can't think of XX that will develop XX in the first place. What I thought was effective at that time

--First, copy the existing code --After that, modify the code little by little

I thought that the method was good. (I used my own code in the past, but I think other people's sample code is fine.) I think it depends on the person who modifies it based on which code, but for example, if it is the TODO app mentioned earlier

--Ability to directly rewrite what you have entered and saved without DELETE --Try increasing the input field by one

For example, after doing Hello World on the console

--Try making Hello World a web application --Output Hello World 10 times on the console and change the display only when it is a multiple of 3

I think it's good to do what you have come up with, even if it's a minor modification. (Unexpectedly, even if you mess with the code a little, it may stop working, so it will be a lot of study)

After that, I think it would be good to implement the sample code in another language.

I personally think that even if you add some modifications to the existing code little by little with your own ingenuity, the degree of acquisition will change considerably compared to copying sutras alone.

In the title of this article, the expression "reform" is used, but by gradually remodeling the sutra-copying method, which is a study method that is inevitably more passive than copying sutras,

--Passive learning becomes active --Since the starting point is to use the existing code, there is no need to float "something" that you want to create something. ――You have to think about how to change the existing code, but the hurdle seems to be lower than thinking about what to make from 0

I think that it is very good that you can actively learn while lowering the learning hurdles like this.

At the end

Taking this opportunity of Advent Calendar as an opportunity ~~ I was accumulating ~~ It was an opportunity to resume learning Rust, and I was given such an opportunity because I learned not only the language but also various other things. I am grateful to the company. Also, this book was very helpful. This book is recommended because it contains not only the basic grammar of Rust but also practical contents such as Web application creation, containerization, GUI application and embedded system. If I had the opportunity, I would like to actively output what I learned in this way.

Recommended Posts

Major remodeling! Practical programming learning method for beginners to learn through reform!
Recommended learning method for programming beginners
[For programming beginners] What is a method?
Introduction to Practical Programming
Major remodeling! Practical programming learning method for beginners to learn through reform!
From studying programming for 2 months to releasing a web application
Recommended learning method for programming beginners
[Swift] Skill map from inexperienced to practical (roadmap)
Introduction to programming for college students (updated from time to time)
Java development for beginners to start from 1-Vol.1-eclipse setup
I tried to develop a web application from a month and a half of programming learning history
Introduction to Practical Programming