text
stringlengths
0
897
```
That alone was pretty neat — a lot faster than using a text editor to create the first one, then using File Explorer to manually copy it 13 times! But that's just setting up our example. Now let's suppose you're working with some other tool that needs files like these to be named with the index padded to 5 digits, an...
This one is a little longer, so it's probably worth creating a script file (perhaps call it *renamer.ms* or something similar).
{caption:File renamer}
```miniscript
for f in file.children
// make sure it's one of the files we care about
// (i.e. starts with Data, and ends with .dat)
if f[:4] != "Data" or f[-4:] != ".dat" then
print "Ignoring " + f
continue
end if
// Extract the number (after "Data" but before ".dat")
num = f[4:-4]
// Zero-pad it, and compute the new file name
num = ("00000" + num)[-5:]
newName = "Data-" + num + ".dat"
// Rename the file, and print what we did
file.move f, newName
print "Renamed " + f + " to " + newName
end for
```
Run this with MiniScript in the same folder as your data files, and voilà! They are all instantly renamed according to the needed pattern.
I've actually had needs like this from time to time. If you haven't yet, it may not fully sink in how cool it is — but someday, you'll be faced with the need for something like this, and you'll be glad you have the ability to do it with code.
A> **Chapter Review**
A> - You learned about the `file` module in command-line MiniScript (also found in Mini Micro).
A> - You know know how to create, delete, read, write, move, and rename files from your MiniScript code.
A> - You'll be watching for opportunities to use this new ability to make the computer do boring, repetitive file-related tasks for you.
{chapterHead: "Day 14: Sea Battle Game", startingPageNum:153}
{width: "50%"}
![](Chapter14.svg)
Q> I don’t understand how people live without creating.
Q>— Samuel L. Jackson (actor and producer)
A> **Chapter Objectives**
A> - Rest and recouperate.
A> - Enter and play a somewhat larger MiniScript game.
A> - Review & synthesize what you've learned so far.
If you've been doing one chapter a day, you've now been building your programming skills for two weeks. If you've been doing some other pace, that's fine too! Either way, you've now learned all the core basics of programming:
- strings, numbers, lists, and maps
- variables
- built-in functions like `print`, `input`, and `val`
- looping with `while` and `for`
- branching with `if`, `else if`, and `else`
- defining your own functions
- object-oriented programming with classes
- reading, writing, and manipulating files
And you've learned how to do all this both on the MiniScript Try-It! page on the web, as well as on the command line. That's a lot of skill!
Of course just knowing something is different from being able to do it well. If you've never played the piano, someone could very quickly explain how to press the keys in order to make sound come out, and even to play some simple songs. But you would obviously have plenty of room for improvement through practice. Pr...
You can get some idea of a programmer's level by showing them a page of code.
1. The non-programmer can't really make heads or tails of it at all.
2. A very new programmer will understand most or all of the syntax, and how to look up the bits they don't understand, but may still not grasp what the program is doing.
3. An intermediate programmer will understand what the program's doing and how it works, though they may not have been able to write such a program themselves.
4. An advanced programmer could have written the same program from scratch, given only a clear description of the desired behavior.
You're probably at level 2 or so at this point, working towards level 3. That's a fine place to be, and it's a place every advanced programmer has been. To progress further, all you need is practice. Just as when learning a musical instrument, the more you practice, the faster you will improve.
One of the easiest and most quickly rewarding ways to practice is by typing in programs written by advanced programmers. This is no different from a musical composer, who begins by playing music composed by others. In following the footsteps of the creators before you, you learn a great deal about how to make your ow...
So today we're going to take a break from learning new things, and instead review and practice with a moderately-sized MiniScript game. I encourage you not to skip typing this in on your own computer. Typing it in engages your brain substantially more than just reading it. Also, you are sure to make some mistakes in...
![](battleship.svg)
## The *Sea Battle* Game
The game today is a classic guessing game for two players, though in our implementation, one of those players will be the computer! Each player has five ships of various sizes, arranged on a 9 by 11 grid. The position of your opponent's ships are unknown to you. On each turn, you guess a grid cell to fire at, and yo...
This game will be too big for the Try-It! page, so you'll need to use command-line MiniScript (or you can use Mini Micro, if you aready know how to do so). Create a text file called "battle.ms", and enter the following:
{caption: "Listing 1 (Sea Battle begins)."}
```miniscript
// Sea Battle
print "Welcome to SEA BATTLE!"
print
```
Save this somewhere that you can easily find the path to it on the command line. You could save it right next to the `miniscript` executable if you want, and then running it would be a matter of changing to that directory (with `cd`), and then entering:
```terminal
miniscript battle.ms
```
For readers still not too used to the command line, this may be the hardest step, since I can't tell you exactly how to do it — it depends on what sort of computer you have, and where you have stored command-line MiniScript and your `battle.ms` file. If you need to, go back and review Chapters 8 and 9, where we learne...