Intro To Tic Tac Toe Rb

Objectives

  1. Learn about building Tic Tac Toe in Ruby.

Introduction

In this course, we're going to learn how to build Tic Tac Toe in Ruby. Just in case you're unfamiliar with the game, let's discuss the rules first. Tic Tac Toe is played on a 3 x 3 grid of available cells, and the goal of the game is to place three of your tokens (X or O) on the grid in a row, including diagonals.

The following example game is won by the first player, X:

X Wins

Why build Tic Tac Toe?

I'm sure you're really excited about building the next Facebook or Instagram, but like most things in life, we have to crawl before we walk, and walk before we run---and build Tic Tac Toe before we build large and complex applications.

Tic Tac Toe is a simple game that flexes the basic concepts in programming. For example, a Tic Tac Toe program requires data structures for storing the board and conditional logic for knowing whose turn it is or if someone has won.

Tic Tac Toe's simple rules are very easy to decompose into small, discrete functions that we can put together to create a complete game. This process of decomposition and synthesis, breaking something down and putting it back together, is absolutely essential to programming.

The simple interface Tic Tac Toe requires, a basic 3 x 3 grid, is also easy to port to multiple interfaces. We can build a command line version of Tic Tac Toe or a web-based version. We can even build a SMS version of Tic Tac Toe if we wanted.

Web Tic Tac Toe

CLI Tic Tac Toe

Beyond being able to move easily between interfaces, we can also move between programming styles, building Tic Tac Toe in a procedural or functional style first, then moving that to an Object Oriented version, and finally, adding our Tic Tac Toe model to a Sinatra Web Application.

Once you've built your basic 2-player version of Tic Tac Toe, you are free to extend it by creating a 1-player version and trying to program an unbeatable AI.

Pseudo-Code for Tic Tac Toe

The general procedure for Tic Tac Toe is as follows:

  1. Create a structure to store and represent the state of the board.
  2. Welcome the players.
  3. Get the user's move in the form of some input.
  4. Make sure their move is valid.
  5. Check to see if the game has been won or came to a draw.
  6. Change the player turn and let the next player make a move.
  7. Repeat until the game is over.

As you progress through this course, we'll break all these requirements down into simple, cumulative labs throughout the track. You'll be surprised at how quickly you'll learn to do something this complex.

Let's get started learning enough Ruby to build Tic Tac Toe.

View Intro To Tic Tac Toe in Ruby on Learn.co and start learning to code for free.

Unlock your future in tech
Learn to code.

Learn about Flatiron School's Mission

With a new take on education that falls somewhere between self-taught prodigy and four-year computer science degree, the Flatiron School promises to turn students with little programming experience into developers.

In the six months since the Manhattan coding school was acquired by WeWork, it has spawned locations in Washington, D.C., Brooklyn, and London. Now, WeWork is opening a fourth Flatiron School location, this time in Houston.

Adam Enbar, Flatiron School's cofounder, believes now is the time to grow. "How the world is changing has impacted working and learning in very similar ways. We think education fundamentally is about one thing: enabling people to pursue a better life."

Learn. Love. Code.
Students come to Flatiron School to change their lives. Join our driven community of career-changers and master the skills you need to become a software engineer or a data scientist.
Find Us