O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Coding with Minecraft

Book Description

You’ve mined for diamonds, crafted dozens of tools, and built all sorts of structures—but what if you could program robots to do all of that for you in a fraction of the time?

In Coding with Minecraft®, you’ll create a virtual robot army with Lua, a programming language used by professional game developers. Step-by-step coding projects will show you how to write programs that automatically dig mines, collect materials, craft items, and build anything that you can imagine. Along the way, you’ll explore key computer science concepts like data types, functions, variables, and more.

Learn how to:

• Program robots that make smart decisions with flow control• Reuse code so that your robots can farm any crop you want, including wheat, sugar cane, and even cacti!• Program a factory that generates infinite building supplies• Design an algorithm for creating walls and buildings of any size• Code yourself a pickaxe-swinging robotic lumberjack!• Create a robot that digs mine shafts with stairs so you can explore safely

Bonus activities in each chapter will help you take your coding skills to the next level. By the end of the book, you’ll understand how powerful coding can be and have plenty of robots at your beck and call.

Requires: Windows 7 or later; macOS 10.10 or later.

Table of Contents

  1. Cover Page
  2. Title Page
  3. Copyright Page
  4. Dedication Page
  5. About the Author
  6. About the Technical Reviewer
  7. Brief Contents
  8. Contents in Detail
  9. Acknowledgments
  10. Introduction
    1. What Are Minecraft Mods?
    2. What Is ComputerCraft?
    3. How to Use This Book
    4. What’s in This Book?
    5. Getting Help
    6. Online Resources
    7. What You Learned
  11. 1. Getting Started with ComputerCraft
    1. Installing Minecraft, ATLauncher, and ComputerCraft
    2. Running Minecraft
    3. What You Learned
  12. 2. Programming Basics
    1. Getting Started with Turtles
    2. Getting Started with Lua Programming
    3. Letting Lua Do the Math
    4. Storing Values with Variables
    5. Checking the Turtle’s Fuel Levels
    6. What You Learned
  13. 3. Talking to Your Turtle
    1. Teaching Your Turtle to Say Hello!
    2. Displaying Text with the print() Function
    3. Retrieving Turtle Names
    4. Getting Keyboard Input with the io.read() Function
    5. Giving Text a Typewriter Effect
    6. Changing Turtle Names
    7. What You Learned
  14. 4. Programming Turtles to Dance
    1. Writing a Dance Program
    2. Running the mydance Program
    3. Using Comments in Your Code
    4. Turtle Movement Functions
    5. Experimenting with Moving the Turtle
    6. Looping with for Loops
    7. Taking the Turtle for a Spin
    8. Doing a Little Hop
    9. Sharing and Downloading Programs Online
    10. What You Learned
  15. 5. Making a Better Dancer
    1. Writing a Better Dance Program
    2. Running the mydance2 Program
    3. The Boolean Data Type
    4. The nil Data Type
    5. Looping with while Loops
    6. Making Decisions with if Statements
    7. Comparing Two Values with Comparison Operators
    8. Making Alternate Decisions with elseif Statements
    9. Nested Code Blocks
    10. Making a Decision . . . or Else!
    11. What You Learned
  16. 6. Programming a Robot Lumberjack
    1. Equipping Turtles with Tools
    2. Designing a Tree-Chopping Algorithm
    3. Writing the choptree Program
    4. Running the choptree Program
    5. Detecting Blocks with the Turtle Detection Functions
    6. Terminating Programs with the error() Function
    7. Mining Blocks with the Turtle Digging Functions
    8. Comparing Blocks with the Turtle Comparison Functions
    9. Returning to the Ground
    10. Startup Programs and the shell.run() Function
    11. What You Learned
  17. 7. Creating Modules to Reuse Your Code
    1. Creating Functions with the function Statement
    2. Making a Module of Functions
    3. Looking at the Turtle’s Inventory
    4. Global and Local Scope
    5. Finding an Item with a for Loop
    6. Selecting an Empty Inventory Slot
    7. What You Learned
  18. 8. Running an Automated Tree Farm
    1. Designing a Tree-Farming Program
    2. Writing the farmtrees Program
    3. Running the farmtrees Program
    4. Tree Types in Minecraft
    5. Chunk Loading in Minecraft
    6. Loading Modules with the os.loadAPI() Function
    7. Checking for Files with the fs.exists() Function
    8. Selecting Saplings in the Turtle’s Inventory
    9. Running Other Programs with the shell.run() Function
    10. Handling Items with the Turtle’s Drop Functions
    11. Rewriting Your Code for When You Have No Bone Meal
    12. What You Learned
  19. 9. Building a Cobblestone Generator
    1. Blueprints for the Cobblestone Generator
    2. Setting Up Furnaces for Smelting the Cobblestone
    3. Writing the cobminer Program
    4. Running the cobminer Program
    5. Setting Up Your Program and Making a Constant Variable
    6. Mining the Cobblestone from the Generator
    7. Interacting with Furnaces
    8. Moving the Cobblestone Miner Back into Position
    9. What You Learned
  20. 10. Making a Stone Brick Factory
    1. Designing a Program to Craft Stone Bricks
    2. Crafting a Crafty Turtle
    3. Writing the brickcrafter Program
    4. Running the brickcrafter Program
    5. Setup for the brickcrafter Program
    6. Checking the Turtle’s Fuel
    7. Collecting Stone from the Furnaces
    8. Crafting Stone Bricks
    9. Moving the Turtle Back into Position
    10. Creating a Factory Building
    11. What You Learned
  21. 11. Constructing Walls
    1. Extending the hare Module
    2. Writing and Running the buildwall Program
    3. Loading the hare Module
    4. Using the Array Data Type
    5. Reading Command Line Arguments
    6. Creating Usage Messages
    7. Calling hare.buildWall() to Build a Wall
    8. What You Learned
  22. 12. Constructing Rooms
    1. Designing a Room-Building Algorithm
    2. Extending the hare Module
    3. Calculating the Total Number of Blocks Needed to Build a Room
    4. Writing the buildRoom() Function
    5. Writing the buildroom Program
    6. Running the buildroom Program
    7. What You Learned
  23. 13. Constructing Floors
    1. Designing the Sweeping Algorithm
    2. Passing Functions to Functions
    3. Extending the hare Module
    4. Calling the sweepFunc() Function
    5. Writing the buildFloor() Function
    6. Writing the buildfloor Program
    7. Running the buildfloor Program
    8. Creating a Patterned Floor
    9. What You Learned
  24. 14. Programming a Robotic Farm
    1. Setting Up a Wheat Field
    2. Designing the Wheat-Farming Algorithm
    3. Extending the hare Module
    4. Writing the farmwheat Program
    5. Running the farmwheat Program
    6. Setup for the farmwheat Program
    7. Writing Functions to Use in the Main Program
    8. Farming with a Loop
    9. Tips for Automating Other Kinds of Farming
    10. What You Learned
  25. 15. Programming a Staircase Miner
    1. Designing the Stair-Mining Algorithm
    2. Extending the hare Module
    3. Writing the digUntilClear() and digUpUntilClear() Functions
    4. Writing the stairminer Program
    5. Running the stairminer Program
    6. Setup for the stairminer Program
    7. Creating the First Stair Step
    8. Mining Downward
    9. Checking the Turtle’s Fuel
    10. Checking the Turtle’s Inventory
    11. Mining Upward
    12. What You Learned
  26. Function Reference
    1. fs (File System) API
    2. hare API
    3. io (Input/Output) API
    4. math API
    5. os (Operating System) API
    6. shell API
    7. string API
    8. textutils API
    9. turtle API
    10. Lua Functions
  27. Name ID Reference
    1. Finding a Block’s Name ID
    2. Distinguishing Between Blocks That Share Name IDs
    3. List of Block Name IDs
  28. INDEX