• Complain

M K - Kids can code, Elders also can: Create a simple game using Python

Here you can read online M K - Kids can code, Elders also can: Create a simple game using Python full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2021, publisher: UNKNOWN, genre: Home and family. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

No cover
  • Book:
    Kids can code, Elders also can: Create a simple game using Python
  • Author:
  • Publisher:
    UNKNOWN
  • Genre:
  • Year:
    2021
  • Rating:
    3 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 60
    • 1
    • 2
    • 3
    • 4
    • 5

Kids can code, Elders also can: Create a simple game using Python: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Kids can code, Elders also can: Create a simple game using Python" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

M K: author's other books


Who wrote Kids can code, Elders also can: Create a simple game using Python? Find out the surname, the name of the author of the book and a list of all author's works by series.

Kids can code, Elders also can: Create a simple game using Python — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Kids can code, Elders also can: Create a simple game using Python" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Table of Contents

Random number generation ........................................................................................................11
Preface
The power of imitating!
I am talking about people beginning to learn coding. Observe the children. Children start singing by imitating, dancing by imitating, speaking by imitating- almost every skill by imitating. The grammar and nuances come later.
For learning coding also, you do not have to wait to learn every piece of its grammar. Get going, start doing small, small things. Learn things on the go. When you start enjoying the small, small wins, you will be fueled, and you will surmount every obstacle.
So, take the plunge now!
Premarajan
Part I: The game Tic-tac-toe
Lets plunge:
Let us try to learn some basic concepts of Python along with coding of the game of TicTac-Toe. The game rules for Tic-Tac-Toe are:
The game board of 9 cells is formed by a grid of 3 rows and 3 columns.
Two players choose and mark the cells by turn.
Whoever completes a continuous row, column or diagonal first, wins.
A player cannot select a cell which has already been marked.
If all cells get marked without either of the player not winning, the game is a Tie.
Let us consider a game played against the computer. Let us assign X to the computer and O to the player for marking their choices.
The game design
Make the Game Board
Display the game rules
Display the board
Choose one player and open the game
Select the cell and mark it
Test if the selection is valid
Test if the input is in range
Test if the input is valid
Test if the cell is already taken
Mark the cell chosen and pass the turn till either wins or cells are exhausted
Ending of the game
X wins
O wins
Tie
Declare the result
Option to start a new game or to exit
Coding of the game
Let us head to coding of the game straight away.
The above code defines the structure of the game We have imported two modules - photo 1The above code defines the structure of the game We have imported two modules - photo 2
The above code defines the structure of the game. We have imported two modules, time and random and defined the variables game_still_going, board, line1 and line2 . We have completed coding of the function beg_game() . In this function, we have defined a global variable, winner and it has been given a value None . This variable has been defined as global so that if the game has to be replayed before exiting, the value will be initialized. This also contains the instructions for playing the game and an invitation to play. This includes a 5-0 countdown to signal the start of the game as well.
This is followed by defining the functions, display_board() , my_turn() , your_turn() , check_winner() and check_if_tie() . While we have defined the structure , we have not written the codes for any of the functions, other than a statement pass. We have placed a pass statement under each of these functions, so that no error will be displayed on account of non-completion of the respective codes.
Then the function play_game() is introduced. There, we have added the code for printing the local time and called the function beg_game() . We have also called the function display_board() .However at this stage, there is nothing to be displayed. We have also called the built-in function input() in the last line so that the screen waits for the input before exiting the program. Once we press enter, the program will be exited. Finally, we call the function play_game() .
On executing the code that we have completed so far; we get the following output: Time Local time and countdown Python time module To make available the - photo 3
Time Local time and countdown
Python time module
To make available, the functions relating to time, we must import the time module first. Python works on the concept of epoch which starts from January 1 st , 1970, 00.00.00. If we ask for the time(>>>time.time()), python gives the number of seconds elapsed starting from the epoch. If we want the local time, we must convert the seconds elapsed. We can use the following function for this:
>time.ctime()
It takes seconds elapsed as the argument and converts it to date and time. Please see the screen shot below for example:
Countdown
Have a look at the code Here we are implementing a 5 second countdown through - photo 4
Have a look at the code. Here we are implementing a 5 second countdown through a for loop. We have saved the value 5 to the variable num_seconds . We have specified the range value as num_seconds+1 so that the number 5 also will be printed. As we want the countdown to start at 5 and end with 0 , we have used reversed order. Also, the printing happens only if the value of countdown is > 0. Look at the print statement. It prints the number and then prints ... The end= command, ensures all printings are on the same line. flush = True ensures that printing takes place immediately without buffering. After print, it waits for 1 second as commanded by time.sleep(1) . If countdown = 0, it just prints Go! in the same line. So, we have an effective implementation of countdown.
Display board
Now let us code the display board function and remove the pass statement.
You call this function from within the myturn function The following game - photo 5 You call this function from within the my_turn() function. The following game board will be exhibited.
We have used the variables line1line2 board and a few print statements to - photo 6
We have used the variables, line1,line2, board and a few print statements, to display the game board. Please note that the variable board contains a list consisting of numbers 1-9. The list index starts from 0, and at board[0] , the number is 1, and at board[1] the number is 2 and so on.
So far, we have been setting the environment for the game by importing the time module and random module. We have also defined the required variables. We have completed coding of two functions beg_game() and display_board() . The game rules are set, and the game board is ready!
Now, according to the rules set, the computer has to make the first move. To implement this, let us code the function my_turn() .
The code above completes the third function my_turn() . Please have a look at the last function play_game() shown in the game structure at the beginning. Call the function my_turn() after beg_game() . play_game() will now look as follows:
play_game():
print(time.ctime())
beg_game()
my_turn()
input()
If you run our program now, it will give the following output:
Let us examine the function myturn in detail To start with all cells are - photo 7
Let us examine the function my_turn() in detail.
To start with, all cells are vacant but when the game progress, cells get filled up. So, you must know which cells are vacant to make your choice. For recording the positions which are vacant, a list ch_list is created. In order to identify the cells which are not already chosen, a for loop is created to iterate through the list named board, which has values from 1-9. When a list element whose value is not equal to X or O is encountered, its value is appended to ch_list . So, the computer must choose one position from the ch_list . The computer makes its choice from the ch_list at random. In fact, for the game to be realistic, the computer must use a little intelligence, which we will introduce later. For the time being, let us go by random choice rather than by reasoned choice.
Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Kids can code, Elders also can: Create a simple game using Python»

Look at similar books to Kids can code, Elders also can: Create a simple game using Python. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Kids can code, Elders also can: Create a simple game using Python»

Discussion, reviews of the book Kids can code, Elders also can: Create a simple game using Python and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.