# Intro to tic tac toe

## 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](https://en.wikipedia.org/wiki/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](https://camo.githubusercontent.com/41dea01402ebcece57a671bcadaa1bde4aa6ab2d/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f636f6d6d6f6e732f7468756d622f312f31622f5469632d7461632d746f652d67616d652d312e7376672f39353870782d5469632d7461632d746f652d67616d652d312e7376672e706e67)](https://camo.githubusercontent.com/41dea01402ebcece57a671bcadaa1bde4aa6ab2d/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f636f6d6d6f6e732f7468756d622f312f31622f5469632d7461632d746f652d67616d652d312e7376672f39353870782d5469632d7461632d746f652d67616d652d312e7376672e706e67)

## 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](https://camo.githubusercontent.com/8b523c403e8a1d101ace5b7b8625ed78d70d39c3/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f732f71337979757175737a6768356734792f323031352d30392d3239253230617425323031302e3435253230414d2e706e67)](https://camo.githubusercontent.com/8b523c403e8a1d101ace5b7b8625ed78d70d39c3/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f732f71337979757175737a6768356734792f323031352d30392d3239253230617425323031302e3435253230414d2e706e67)

[![CLI Tic Tac Toe](https://camo.githubusercontent.com/ed9bcadda9bf5ad3cac3aa27670197be699f7892/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f732f373169736b646937367379797168622f323031352d30392d3239253230617425323031302e3436253230414d2e706e67)](https://camo.githubusercontent.com/ed9bcadda9bf5ad3cac3aa27670197be699f7892/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f732f373169736b646937367379797168622f323031352d30392d3239253230617425323031302e3436253230414d2e706e67)

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. \<iframe width="753" height="380" src="<https://www.youtube.com/embed/NHWjlCaIrQo?rel=0&showinfo=0>" frameborder="0" allowfullscreen>\</iframe>

## 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](https://learn.co/lessons/intro-to-tic-tac-toe-rb) on Learn.co and start learning to code for free.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://certil-remy.gitbook.io/learn/intro-to-tic-tac-toe.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
