Tic Tac Toe Board


Ad

Advertisements

T

Thrillhouse

How is the board stored? If it's a two-dimensional array then it's as
simple as nested for loops.
 
J

Jon Bell

How who I go about printing a Tic Tac Toe Board that is 3 x 3<?.

How about this?

cout << " | | \n"
<< "---+---+---\n"
<< " | | \n"
<< "---+---+---\n"
<< " | | \n";
 
Ad

Advertisements

J

Jonathan Turkanis

Jon Bell said:
How about this?

cout << " | | \n"
<< "---+---+---\n"
<< " | | \n"
<< "---+---+---\n"
<< " | | \n";

To generalize this a bit, if Tic Tac Toe boards are represented as instances of
vector< vector<char> >, you can do something like this:

#include <iostream>
#include <vector>
#include <boost/assign.hpp>
#include <boost/iostreams/format_lite.hpp>

int main()
{
using namespace std;
using namespace boost::io;
using namespace boost::assign;

typedef vector<char> row;
typedef vector<row> board;

board b =
list_of( list_of('X')(' ')('O') )
( list_of('O')('O')('X') )
( list_of('X')(' ')('X') );

std::cout << punctuate<row>(" ", " | ", " ")
<< punctuate< vector<_> >("", "\n---+---+---\n", "")
<< b;

}

Output (view with fixed-width font):

X | | O
---+---+---
O | O | X
---+---+---
X | | X

Here I'm using the new Boost Assign library to initialize the board, and my
Format Lite library to format it. (See
http://home.comcast.net/~jturkanis/format_lite/)

Of course, this might be using a sledge hammer to kill a mosquito -- or, to use
a phrase I am trying to popularize -- using a daisy-cutter to cut a daisy.

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top