Tuesday, January 8, 2013

Logic Programming with Prolog

Logic programming uses mathematical logic to represent and execute computer programs.   This style of programming is becoming more widely used yet is quite different from that supported by many other programming languages.  In this article you'll get an introduction to logic programming and then learn how to create logic programs using Prolog, the most widely used logic programming language.

Logic Programming

Logic programming is a type of declarative programming that incorporates mathematical logic, implemented using queries and relations. When you develop a program using the logic programming approach you express an algorithm, a computation, etc. (in essence, its logic) in terms of fundamental relations (represented as facts and rules), and one or more propositions or goals. Next you load the program and ask questions (i.e. queries) using the language's interpreter. The interpreter then attempts to find the collection of fundamental relations that prove the goal(s) and thus provide answers to the questions, i.e. solutions. This programming style differs from imperative programming languages where you write programs that implement a solution by specifying a sequence of instructions. With imperative programming you are computing a result based on a specific set of instructions that implement a solution, whereas with logic programming you deduce a result based on relations that implement solutions.

With logic programming, your code describes relations (connections between one or more things), logical propositions that evaluate the truth of one or more relations, performs proof searches to prove logical propositions, combines relations through resolution, and computes values for variables through unification.  For example, the following relation

automobile(bmw)

Maps "bmw" to autombile; in other words BMW is a type of automobile.  A relation could have multiple elements, for example the relation

square(5, 25)

asserts that the square of the number 5 (i.e. 5 x 5) equals 25.  The above relations are facts; another type of relation is a rule.  Relations may include code that implements algorithms, performs computations, etc.  Relations are used to prove logical propositions; the following logical proposition

automobile(m3) < automobile(bmw), bmw(m3)

asserts that an M3 is an automobile.  To prove it's true, the relations automobile(bmw) and bmw(m3) must be proved true.  Proof search, the process of evaluating relations to find a set that proves a proposition, is conducted to perform this.

Prolog

Prolog is the most commonly used logic programming language, its name stands for Programming in Logic. It was developed in the early 1970s and initially associated with natural language processing.  Prolog is widely used for artificial intelligence and is also used in other areas such as expert systems, games, theorem proving, and control systems

A Prolog interpreter runs within the context of a database of relations consisting of Horn clauses; each clause is comprised of facts and rules (that contain your program code). Using the interpreter you run queries against these clauses to have them evaluated and hence perform computations.

There are numerous Prolog implementations available; SWI-Prolog, a free, widely used Prolog environment that runs on Mac, Windows, and Linux systems, can be downloaded here.   Below is a simple program written with Prolog that computes a number in the Fibonacci sequence.

Fibonacci Sequence Program Implemented with Prolog

There are plenty of resources online to help you get started with logic programing and Prolog, also checkout the Programming for Everyone website for a more detailed overview of the language

Programming for Everyone is designed to give the reader an introduction to computer programming. The book is written for a very general audience and focuses on providing you with a detailed understanding of the basic concepts.  It's also great for programmers who want to learn about other programming areas (e.g. logic programming, computer graphics, games, etc.) they may not have experience in.  Whatever your age or background, Programming for Everyone will help you to understand computer programming!   

Thursday, January 3, 2013

Developing Computer Games with Kobold2D

Everyone loves playing video games, and each of us has their favorites.  Some of us are old-school and enjoy playing the classics like Pac-Man and Space Invaders, while others may be into the latest multiplayer online games.  Perhaps some of you would even like to create your own video games, but how do you get started?  In this blog we’ll provide a brief overview of video game elements and design, and then introduce you to Kobold2D, a popular framework for building video games targeted for the Apple OS X and iOS platforms.

Introduction

Video games come in a variety of genres, each of which has general characteristics that distinguish it.  The most common game genres are:
  • Action – these games feature real-time player interactions that emphasize physical tests of skill, and typically require quick reflexes and careful timing to complete challenges.
  • Adventure - Adventure games focus on storytelling, and usually feature exploration, puzzle solving, and collecting items.
  • Action-Adventure - Action-adventure game combines elements of both the adventure and action game genres.  They typically feature problem-solving and physical challenges.
  • Role-Playing - In role-playing games the player controls one or a small group of characters and lives as this character(s) in a fictional world.
  • Simulation - These games attempt to replicate real-world experiences.  The simulation follows real-world rules as much as possible, and the players employ their understanding of these rules when confronted with situations in the game.  Common categories of simulation games include vehicle, life, and sports.
  • Strategy - Strategy games typically have a player managing a limited set of resources to achieve a specific goal.  These games can be turn-based or real-time.
  • Casual - These games are those commonly played by casual gamers; they tend to be short, without a story line, and are geared towards relaxation.
  • Massive Multiplayer Online (MMO) - MMO games may feature thousands of simultaneous players and basically add the capability for online gaming to the genres specified above.

Developing a Video Game

One of the key factors to understand when developing video games is their continuous nature. Specifically, many games are designed such that the game environment (i.e. game world) is continuously updated (e.g. animated), and also responds to inputs in real-time.  The animation is performed by the sequential display of images, each of which is (typically) slightly different from the previous one, at a rate that gives the illusion of motion.  The inputs can be received from both the player (via input devices) and input sensors.  Game programs are commonly designed with a game loop to implement this continuous, real-time behavior.


During game startup the program is initialized.  The update stage of the loop receives inputs and updates the game state appropriately.  The game state updates typically involve updating the game ‘virtual world’ per the game physics and game play components.  The draw stage renders the updated game scene (i.e. its graphics, sound, and special effects) to the output device(s).  The wait stage is then entered if there is time left over before starting the loop again.  The game loop continues until the game ends or the player stops the game.

Introducing Kobold2D

Kobold2D is a free, open-source framework for building computer games and graphical/interactive applications.  It is a flexible, feature-filled framework that is designed to make game programming simpler.  Kobold2D is an extended version of the Cocos2d game framework, augmented with additional tools, developer resources, and other software libraries that make for more efficient and productive game development.  Kobold2D is designed for developing Apple OS X and iOS games; its minimum hardware and software requirements are an Intel-based Mac computer running Mac OS X 10.6 or later.  An iOS device is also required if developing games targeted for these devices.  The Xcode IDE and Objective-C language are used for programming with Kobold2D.

Features

The main features of Kobold2D include the software libraries, the installer, documentation, project templates, along with several example games.  These features make it easy to quickly begin developing games.

Creating an Example Game

The Kobold2D website has numerous template projects that you can use to get started.  An example tic-tac-toe game developed with Kobold2d is also available for download at the Programming for Everyone website (source code files); the book also includes a detailed overview of the game's design.  In an upcoming blog I'll take you step-by-step through the process of developing this game; in the meantime use these resources to get started with game programming using Kobold2D!

Programming for Everyone is designed to give the reader an introduction to computer programming. The book is written for a very general audience and focuses on providing you with a detailed understanding of the basic concepts.  It's also great for programmers who want to learn about other programming areas (e.g. logic programming, computer graphics, games, etc.) they may not have experience in.  Whatever your age or background, Programming for Everyone will help you to understand computer programming!