#include<iostream>
#include<stdio.h>
#include<string>
#include<stdlib.h>
#include<time.h>
using namespace std;

void printBoard();
void fillBoard();
void checkWin();
string board [9][9];
bool gameOver = false;


int main()
{
    fillBoard();
    printBoard();

}//end of main
void printBoard()
{
    cout<<board[0][0]<<" | "<<board[0][1]<<" | "<<board[0][2]<<"   "<<board[0][3]<<" | "<<board[0][4]<<" | "<<board[0][5]<<"  "<<board[0][6]<<"| "<<board[0][7]<<" | "<<board[0][8]<<endl<<"---------   ---------   ---------\n";//1st line
    cout<<board[1][0]<<" | "<<board[1][1]<<" | "<<board[1][2]<<"   "<<board[1][3]<<" | "<<board[1][4]<<" | "<<board[1][5]<<"  "<<board[1][6]<<"| "<<board[1][7]<<" | "<<board[1][8]<<endl<<"---------   ---------   ---------\n";//2nd line
    cout<<board[2][0]<<" | "<<board[2][1]<<" | "<<board[2][2]<<"   "<<board[2][3]<<" | "<<board[2][4]<<" | "<<board[2][5]<<"    "<<board[2][6]<<"| "<<board[2][7]<<" | "<<board[2][8]<<endl<<endl<<endl;//3rd line
    cout<<board[3][0]<<" | "<<board[3][1]<<" | "<<board[3][2]<<"   "<<board[3][3]<<" | "<<board[3][4]<<" | "<<board[3][5]<<"    "<<board[3][6]<<"| "<<board[3][7]<<" | "<<board[3][8]<<endl<<"---------   ---------   ---------\n";//4th line
    cout<<board[4][0]<<" | "<<board[4][1]<<" | "<<board[4][2]<<"   "<<board[4][3]<<" | "<<board[4][4]<<" | "<<board[4][5]<<"    "<<board[4][6]<<"| "<<board[4][7]<<" | "<<board[4][8]<<endl<<"---------   ---------   ---------\n";//5th line
    cout<<board[5][0]<<" | "<<board[5][1]<<" | "<<board[5][2]<<"   "<<board[5][3]<<" | "<<board[5][4]<<" | "<<board[5][5]<<"    "<<board[5][6]<<"| "<<board[5][7]<<" | "<<board[5][8]<<endl<<endl<<endl;//6th line
    cout<<board[6][0]<<" | "<<board[6][1]<<" | "<<board[6][2]<<"   "<<board[6][3]<<" | "<<board[6][4]<<" | "<<board[6][5]<<"    "<<board[6][6]<<"| "<<board[6][7]<<" | "<<board[6][8]<<endl<<"---------   ---------   ---------\n";//7th line
    cout<<board[7][0]<<" | "<<board[7][1]<<" | "<<board[7][2]<<"   "<<board[7][3]<<" | "<<board[7][4]<<" | "<<board[7][5]<<"    "<<board[7][6]<<"| "<<board[7][7]<<" | "<<board[7][8]<<endl<<"---------   ---------   ---------\n";//8th line
    cout<<board[8][0]<<" | "<<board[8][1]<<" | "<<board[8][2]<<"   "<<board[8][3]<<" | "<<board[8][4]<<" | "<<board[8][5]<<"    "<<board[8][6]<<"| "<<board[8][7]<<" | "<<board[8][8]<<endl<<endl<<endl;//9th line
}//end of printBoard()
void fillBoard()
{

    for (int i=0; i<9; i++)
    {
        for (int j=0; j<9; j++)
            {
                board [i][j] =('0'-48)+(9*i + j+1);
            }
    }
}//end of fill Board()

we originally use char board. but char wont let us go past single digit

forcing us to use "string"
the program output this:

☺ | ☻ | ♥   ♦ | ♣ | ♠  | |
---------   ---------   ---------

 | ♫ | ☼  ►| ◄ | ↕
---------   ---------   ---------
‼ | ¶ | §   ▬ | ↨ | ↑    ↓| → | ←


∟ | ↔ | ▲   ▼ |   | !    "| # | $
---------   ---------   ---------
% | & | '   ( | ) | *    +| , | -
---------   ---------   ---------
. | / | 0   1 | 2 | 3    4| 5 | 6


7 | 8 | 9   : | ; | <    =| > | ?
---------   ---------   ---------
@ | A | B   C | D | E    F| G | H
---------   ---------   ---------
I | J | K   L | M | N    O| P | Q


Press any key to continue . . .

suppose to look at this:

1 | 2 | 3   4 | 5 | 6    7| 8 | 9
---------   ---------   ---------
10| 11| 12  13| 14| 15  16| 17| 18
---------   ---------   ---------
19| 20| 21  22| 23| 24  25| 26| 27


28| 29| 30  31| 32| 33  34| 35| 36
---------   ---------   ---------
37| 38| 39  40| 41| 42  43| 44| 45
---------   ---------   ---------
46| 47| 48  49| 50| 51  52| 53| 54


55| 56| 57  58| 59| 60  61| 62| 63
---------   ---------   ---------
64| 65| 66  67| 68| 69  70| 71| 72
---------   ---------   ---------
73| 74| 75  76| 77| 78  79| 80| 81


Press any key to continue . . .

Our teacher told us to use "stringstream"

Can someone explain to me the usage of "stringstream"?

in depth will be better

Recommended Answers

All 6 Replies

Before messing around with tictactoe, you should make some examples for yourself to make sure you understand the basics. Create a string and output it. Does it look correct?

Stringstream works just like "cin" (an input stream). Here is a demo:
http://programmingexamples.net/index.php?title=CPP/StringStream

David

I know the basic, we made a tic tac toe in class

this is a project, in order to win, you have to 3 small tic tac toe board, form a straight line.

i am stuck on the printBoard() and fillBoard() part not the gameplay part

Before messing around with a printBoard() function, you should make some examples for yourself to make sure you understand the basics. Create a string and output it. Does it look correct?

You need a function that converts integers to a string. itoa, sprintf, or stringstreams are your best bet. itoa is non-standard, so you may have to write your own. This is C++. Stringstreams will do you just fine.

This won't work...

board [i][j] =('0'-48)+(9*i + j+1);

Too many types here (char, int, string) used incorrectly together.

i know how to output a string correctly

i just need help on the fillBoard() funtion

we try int, to output 1~81

but int can't contain 'X' or 'O'

so we're back to string

Outputting isn't the problem. Converting 75 to "75" is the problem. It's harder than converting 7 to "7". You're only using a single char with that. Can't do that here.

Options...

1. Do it with a stringstream.
2. Write your own itoa-style function that does it by creating a string, isolating digits and converting to characters, and concatenating.
3. sprintf (Stringstreams C style).

You appear to be trying to do the second option, but you are not isolating digits with the / and % operator.

Stringstreams is easiest.

commented: answear my question +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.