Tic Tac Toe

Started by
7 comments, last by Zahlman 18 years, 1 month ago
i am busy working on a tic tac toe clone in c++ and was wondering what the best way to represent the board would be. So far I have gone with a vector however would a multidemsional vector be more appropriate?
-----------------[ serafin studios ]
Advertisement
You can use a multidimensional vector like this: board[row][column] or board[row * 10 + column] (10 is the board wide). Both can be used and there is almost no difference.
Whatever method you choose, I recommend you to encapsulate your board into its own class. When you'll change your internal implementation, the niterface won't change and it will really help you :)

Now, you can try both - a vector and a multi array (boost::multi_array<> might be a good candidate (overkill?), but you can also create a fixed array - since its size is rather well known).

Regards,

yes, a board class that initialises a board of variable dimensions passed in the constructor would be a good thing to have.
If you make it pluggable you can then have the class be the core of all kinds of boardgames, accepting different kinds of pieces and rules engines.
When I made my program Tic Tac Toe I just const rows=3 and columns=3 because thats how a board would look like. Then:
char board[rows][columns] = {{'o', 'x', 'o'},
{' ', 'x', 'x'},
{'x', 'o', 'o'}};

the ' ' is wjre u have to put the x which i think you know how to do. Hope this helps.
bool board[3][3];
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
Quote:Original post by simon10k
bool board[3][3];


That would not work as the board has three states (Empty, nought, cross).

You could have two arrays such as -
bool board_PiecePlaced[3][3];
and
bool board_NoughtOrCross[3][3];

but it's not the solution I would go for.

As has beed said, you may want to go for a class and hide the implementation in there.
sorry, I meant

char board[3][3];

=)
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
Yeah, this time, do use the plain array - because you know the sizes. And do wrap it up - because copying structs or class instances works "as you expect", even when they include arrays as members, while copying the arrays themselves doesn't. And do take advantage of the opportunity to encapsulate the access. Really encapsulate, you know.

// Bad!char board::get(int x, int y) {  return m_b[x][y];}void board::set(int x, int y, int value) {  m_b[x][y] = value;}// and logic outside for checking for wins or move legality.// Instead, do something like:bool board::place(int x, int y, int value) {  if (m_b[x][y] != ' ') { return false; } // couldn't place anything.  m_b[x][y] = value;  return true;}char board::winner() {  // implement checking logic here and return the id of the winner, or  // else the blank char if there is no win}bool board::full() {  // return whether all spaces have been occupied  // Maybe you want to fold this in to the winner() functionality instead,  // by letting some other symbol stand for "tie game" versus "game not done yet".}// But on the other hand, something like computer AI for example would go // outside the class. In fact, you might for example have an AI object which// holds on to several "scratch" boards for considering moves...

This topic is closed to new replies.

Advertisement