Reputation: 71
So I want to print a tic-tac-toe board but can't really work out how to do it without getting crazy results
#include <iostream>
int main()
{
const int ROWS = 3;
const int COLUMNS = 3;
char board[ROWS][COLUMNS] =
{ {'X', 'O', 'X'},
{' ', 'O', 'O'},
{'X', 'X', 'O'} };
for (int i = 0; i < COLUMNS; i++)
{
for (int j = 0; j < ROWS; j++)
{
std::cout << board[0][j];
}
std::cout << "\n";
}
}
Upvotes: 0
Views: 24
Reputation: 2208
The printout never iterates the rows of the board. Also, you have mixed up the rows and the columns, let the outer loop (the first one) iterate on ROWS and the inner on COLUMNS.
Change
for (int i = 0; i < COLUMNS; i++) {
for (int j = 0; j < ROWS; j++) {
std::cout << board[0][j];
}
std::cout << "\n";
}
to
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
std::cout << board[i][j];
}
std::cout << "\n";
}
Upvotes: 1
Reputation: 36
It seems you've used wrong first index, 0
instead of i
! So replace your line
std::cout << board[0][j];
with
std::cout << board[i][j];
and it works fine!
P.S. In 2003 I've written my graduate thesis abut "Game Theory", and one of the most important examples was tic-tac-toe game, that I've implemented in Visual Basic 97 with animations, audio and non-losing strategy for the computer player. You may also see my home video about tic-tac-toe with an Ghost player: https://youtu.be/1kL-1lqAt6Y
Upvotes: 0
Reputation:
You are printing only the first row again and again i.e. std::cout << board[0][j];
It should be std::cout << board[i][j];
instead.
Upvotes: 0