Tic-Tac-Toe Problem <Can anyone help<?.

T

tomakated

How do I get rid of the dashes next to the 3, 6, and the 9<?.

Also the last row of "------" and the "|" on the last line so that the
board will look like this<?.

| |
| |
1| 2| 3
--------------------
| |
| |
4| 5| 6
--------------------
| |
| |
7| 8| 9

and not this


| |
| |
1| 2| 3|
--------------------
| |
| |
4| 5| 6|
--------------------
| |
| |
7| 8| 9|
--------------------
| |

Can anyone Help<?>

#include <iostream>
#include <iomanip>

void tttboard (int,int) ;
void printboard (int,int) ;
void lineofdashes() ;
void linewithnumbers(int, int) ;
void linewithoutnumbers();

const int Boxwidth = 6;
const int Boxheight = 4;

int main()
{
tttboard (1,9) ;
}

void tttboard (int start, int numberofcells)
{
int first;

first = start;
while (first<=numberofcells)
{
printboard (first, numberofcells);
first = first+3;
}
linewithoutnumbers();

}

void printweek (int first, int numberofcells)
{
int i;

linewithoutnumbers ();
linewithoutnumbers ();
linewithnumbers (first, numberofcells);
for (i=0; i<Boxheight-3; i++)
lineofdashes ();
}

void lineofdashes()
{
int i;
for (i=0; i<3*Boxwidth + 2; i++)
cout << "-";
cout << endl;
}
void linewithoutnumbers()
{
int i;
for (i=0; i<2; i++)
cout << setw(Boxwidth+1) << "|";
cout << endl;
}

void linewithnumbers (int first, int numberofcells)
{
int i, cellnumber;

cellnumber = first;
for (i=0; i<3; i++)
{
if ((cellnumber >0) && (cellnumber <= numberofcells))
cout << setw(Boxwidth) << boxnumber << "|";
else cout << setw(Boxwidth) << boxnumber << endl;
cellnumber ++;
}
cout << endl;
}

"calendar.cpp" 72 lines, 1189 characters
$ calendar.out
| |
| |
1| 2| 3|
 
M

Mike Wahler

tomakated said:
How do I get rid of the dashes next to the 3, 6, and the 9<?.


void linewithnumbers (int first, int numberofcells)
{
int i, cellnumber;

cellnumber = first;
for (i=0; i<3; i++)
{
if ((cellnumber >0) && (cellnumber <= numberofcells))

if ((cellnumber >0) &&
(cellnumber <= numberofcells) &&
(cellnumber % 3) /* not evenly divisible by 3 */
)
cout << setw(Boxwidth) << boxnumber << "|";
else cout << setw(Boxwidth) << boxnumber << endl;
cellnumber ++;
}
cout << endl;
}

-Mike
 
M

Mike Wahler

tomakated said:
Thanks, it works but it makes a space after that line before the
dashes"------"

I don't know what you mean. If your program is still not
behaving the way you want, post the exact code of it and tell us
what it does, and what you want instead.

-Mike
 
K

Karl Heinz Buchegger

tomakated said:
Thanks, it works but it makes a space after that line before the
dashes"------"

Study you function:
How often and when do you output endl ?
 
T

tomakated

I need to get rid of the space under the numbers
Then the line after 7 8 and 9 I need to get rid of the line of
dashes"-----" and the line of "|"

It's looks like this.
| |
| |
1| 2| 3

--------------------
| |
| |
4| 5| 6

--------------------
| |
| |
7| 8| 9

--------------------
| |


But I need this......
| |
| |
1| 2| 3
--------------------
| |
| |
4| 5| 6
--------------------
| |
| |
7| 8| 9





#include <iostream>
#include <iomanip>

void calendar (int,int) ;
void printweek (int,int) ;
void lineofdashes() ;
void linewithnumbers(int, int) ;
void linewithoutnumbers();

const int Boxwidth = 6;
const int Boxheight = 4;

int main()
{
calendar (1,9) ;
}

void calendar (int startday, int daysinmonth)
{
int first;

first = startday;
while (first<=daysinmonth)
{
printweek (first, daysinmonth);
first=first+3;
}
linewithoutnumbers();
}

void printweek (int first, int daysinmonth)
{
int i;

linewithoutnumbers ();
linewithoutnumbers ();
linewithnumbers (first, daysinmonth);
for (i=0; i<Boxheight-3; i++)
lineofdashes ();
}

void lineofdashes()
{
int i;
for (i=0; i<3*Boxwidth + 5; i++)
cout << "-";
cout << endl;
}
void linewithoutnumbers()
{
int i;
for (i=0; i<3; i++)
cout << setw(Boxwidth+1) << "|";
cout << endl;
}

void linewithnumbers (int first, int numberofcells)
{
int i, cellnumber;

cellnumber = first;
for (i=0; i<3; i++)
{
if (((cellnumber >0) && (cellnumber <= numberofcells) &&
(cellnumber%3)))
cout << setw(Boxwidth) << cellnumber << "|";
else cout << setw(Boxwidth) << cellnumber << endl;
cellnumber ++;
}
cout << endl;

"calendar.cpp" 71 lines, 1160 characters
$ calendar.cpp
calendar.cpp: execute permission denied
$ calendar.out
$ calendar.out
 
R

rossum

How do I get rid of the dashes next to the 3, 6, and the 9<?.

Also the last row of "------" and the "|" on the last line so that the
board will look like this<?.

| |
| |
1| 2| 3
--------------------
| |
| |
4| 5| 6
--------------------
| |
| |
7| 8| 9

and not this


| |
| |
1| 2| 3|

If you are posting code here then it is best to post *compilble* code,
and the best way to do that is to cut and paste from your editor into
your usenet post. I don't think that you did that here.

#include <iostream>
#include <iomanip>

using std::cout;
using std::endl;
using std::setw;

void tttboard (int,int) ;
void printboard (int,int) ;
[Error]This function is not defined anywhere, and would be better
called printrow() anyway.
void lineofdashes() ;
void linewithnumbers(int, int) ;
void linewithoutnumbers();

const int Boxwidth = 6;
const int Boxheight = 4;

int main()
{
tttboard (1,9) ;
}

void tttboard (int start, int numberofcells)
{
int first;

first = start;
while (first<=numberofcells)
{
printboard (first, numberofcells);
first = first+3;
}
linewithoutnumbers();
[Question]What does this line do? Is it really what you want?
}

void printweek (int first, int numberofcells)
void printboard (int first, int numberofcells)
[Error]See above.
{
int i;

linewithoutnumbers ();
linewithoutnumbers ();
linewithnumbers (first, numberofcells);
for (i=0; i<Boxheight-3; i++)
for (i = 0; i < Boxheight - 3; ++i)
[Style]++i is never slower and sometimes faster than i++. Where you
could use either it is better to get into the habit of using ++i.
lineofdashes ();
}
[Error]Every time you call printboard() you unconditionally call
lineofdashes(). So you will get as many lines of dashes as you are
getting rows, which is why you are getting the extra line of dashes at
the bottom of your grid.

You need to think of a way to call printboard() (=printrow()) three
times whie only calling lineofdashes() twice.
void lineofdashes()
{
int i;
for (i=0; i<3*Boxwidth + 2; i++)
cout << "-";
cout << endl;
}
void linewithoutnumbers()
{
int i;
for (i=0; i<2; i++)
cout << setw(Boxwidth+1) << "|";
cout << endl;
}

void linewithnumbers (int first, int numberofcells)
{
int i, cellnumber;

cellnumber = first;
for (i=0; i<3; i++)
{
if (i != 0) { cout << '|'; }
[Solution]The first cell on a row does not start with a '|', the other
cells do.
if ((cellnumber >0) && (cellnumber <= numberofcells))
cout << setw(Boxwidth) << boxnumber << "|";
cout << setw(Boxwidth) << cellnumber;
[Error]boxnumber is not declared anywhere. You don't want a '|' after
the last cell, so don't put one in.
else cout << setw(Boxwidth) << boxnumber << endl;
else cout << setw(Boxwidth) << cellnumber << endl;
[Error]Again, boxnumber not declared.
cellnumber ++;
}
cout << endl;
}

"calendar.cpp" 72 lines, 1189 characters
$ calendar.out
| |
| |
1| 2| 3|

Hope that helps.

rossum
 
M

Mike Wahler

tomakated said:
I need to get rid of the space under the numbers
Then the line after 7 8 and 9 I need to get rid of the line of
dashes"-----" and the line of "|"

It's looks like this.
| |
| |
1| 2| 3

How many newline characters are you writing after the '3'?
(By 'newline', I mean '\n' or 'endl'.)

How many newline characters are you writing after the '6'?

How many newline characters are you writing after the '6'?
--------------------

After you print the '9', simply stop printing anything else.
| |


But I need this......
| |
| |
1| 2| 3

Try to notice patterns in the above. E.g. 1, 2 and 3 followed
by a line of hyphens; 4, 5, and 6 followed by a line of hyphens.
But 7, 8, and 9 are *not* followed by a line of hyphens. This
means that the same statement or routine should not be used
to print the "7,8,9" row as for the "123" and "456" rows.

I'll let you try to fix it first, then if you're still stuck, I'll
point out where in the code things go awry.

-Mike
 
T

tomakated

Ready to quit<!!!!>
I got rid of the last line of "|" and now I only have the last line of
"------" to get rid of. but that isn't the problem anymore I need to get
in the cells how to put an 'x' or 'o' as a mark<!!!!.

so if anyone can help me out that would be great
I need to go from this........
| |
| |
1| 2| 3

--------------------
| |
| |
4| 5| 6

--------------------
| |
| |
7| 8| 9

--------------------

To this..........


| |
O | X | O
1| 2| 3
--------------------
| |
O | X | O
4| 5| 6
--------------------
| |
X | O | X
7| 8| 9





#include <iostream>
#include <iomanip>

// prototypes

bool won(const char []);
void makecomputermove(char [], int);
void makeusermove(char [], int);
void initialize(char b[]);
void winningmessage(int player);
void tiemessage();
void tttboard (int, int);
void printboard (int, int);
void lineofdashes();
void linewithnumbers (int, int);
void linewithoutnumbers();

// constants
// refer to these by name only. do not use 0 or 1 to mean User or
Machine


const int User = 1;
const int Machine = 0;
const char Blank = ' ';
const int Boxheight = 4;
const int Boxwidth = 6;

int main()
{
int movenumber =0, // for the 9 moves
player; // 0 for the machine, 1 for the user
bool done = false;
char board[9]; // for the tic tac toe board

initialize(board);
instructions();
player = whogoesfirst();
while ((movenumber<9) && (!done))
{
if (player == User) makeusermove(board,movenumber);
else makecomputermove(board,movenumber);
done = won(board);
if (!done) player = 1 - player;
movenumber++;
}
if (!done) tiemessage();
else winningmessage(player);

tttboard (1,9);
}

void initialize(char b[])
{ // initializes the board to Blanks
}


void instructions()
{
cout <<"Welcome to the game of Tic-Tac-Toe. Are you ready to
take on the Computer<?.\n";
cout <<"If you are I wish you luck but first lets go over the
rules.\n";
cout <<"The object of the game is to get three in a row any
way.\n";
cout <<"You pick a cell and then type that number in to make
your move.\n";
cout <<"Then who ever gets three in a row wins. Else there is a
tie.\n";
cout <<"But you won't have to to worry my computer will beat
you.\n";
cout <<"Now lets get started if you dare<!!!.\n";
boardwithnumbers( const char b[]);
cout <<"Again I wish you luck, pick your moves wisely.\n";
whogoesfirst();

}

int whogoesfirst()
{ char answer;

cout <<"Would you like to go first<?. (y or n): \n";
cin >> answer;
if (answer == 'y') || (answer == 'Y')
{
makeusermove();
}
if (answer == 'n') || (answer == 'N')
{
makecomputermove();
}
else cout <<"You didn't enter y or n please re-enter: \n"
cin >> answer;
}


void tttboard (int start, int numberofcells)
{
int first;

first = start;
while (first <= numberofcells)
{
printboard (first, numberofcells);
first = first+3;
}
}

void lineofdashes()
{
int i;
for (i=0; i<3*Boxwidth +1; i++)
cout << "-";
cout << endl;
}

void linewithoutnumbers ()
{
int i;
for (i=0; i<2; i++)
cout << setw(Boxwidth+1) << "|";
cout << endl;
}
void linewithnumbers (int first, int numberofcells)
{
int i, cellnumber;

cellnumber = first;
for (i=0; i<3; i++)
{
if (((cellnumber >0) && (cellnumber <= numberofcells) &&
(cellnumber%3)))
{
cout << setw(Boxwidth) << cellnumber << "|";
}
else cout << setw(Boxwidth) << cellnumber << "\n";

cellnumber ++;
}
}


bool won(const char b[])
{
// returns true if the board is a winning position
// and false if not.

int i;
for (i=0;i<7;i=i+3)
if ((b==b[i+1]) && (b==b[i+2]) && (b!=Blank))
return(true);
for (i=0;i<3;i++)
if ((b==b[i+3]) && (b==b[i+6]) && (b!=Blank))
return(true);
if ((b[0]==b[4]) && (b[4]==b[8]) && (b[0]!=Blank)) return(true);
if ((b[2]==b[4]) && (b[4]==b[6]) && (b[2]!=Blank)) return(true);
return(false);
}



void makeusermove(char b[], int move)
{
char mark;
int cell;

if (move % 2 == 0) mark = 'X';
else mark = 'O';
cout << "\n\nIt's your move.\n";
cout << "Choose a cell in which to put your " << mark << ".\n";
boardwithnumbers(b);
cout << "Cell number? : ";
cin >> cell;
while ((cell<1) || (cell >9) || (b[cell-1]!=Blank))
{
if ((cell<1) || (cell>9))
cout << "\tYou have chosen an invalid cell
number.\n";
else if (b[cell-1]!=Blank)
cout << "\tThat cell has already been taken.\n";
cout << "Re-enter cell number: ";
cin >> cell;
}
b[cell-1]=mark;
}


void boardwithnumbers( const char b[])
{
int i;

linewithoutnumbers ();
linewithoutnumbers ();
linewithnumbers (first, numberofcells);
lineofdashes ();
}


void makecomputermove(char b[], int move)
{ int i;
char mark, theirmark;
int randmoves[9] ={4,0,2,6,8,1,3,5,7};

if (move % 2 == 0) {mark = 'X'; theirmark = 'O';}
else {mark = 'O'; theirmark = 'X';}
for (i=0; i<9; i++)
{ if (b == Blank)
{
b = mark;
if (won(b))
{ cout << "I put an " << mark << " in cell "
<< i+1 << " and win.\n";
printboard(b);
return;
}
else b = Blank;
}
}

for (i=0; i<9; i++)
{ if (b == Blank)
{
b = theirmark;
if (won(b))
{ cout << "I put an " << mark << " in cell "
<< i+1 << ".\n";
b=mark;
return;
}
b = Blank;
}
}

// feel free to change this part if you choose

i=0;
while (b[randmoves]!=Blank)
i++;
b[randmoves]=mark;
cout << "I put an " << mark << " in cell " << randmoves+1 <<
".\n";
}



void printboard(const char b[], int first, int numberofcells)
{
int i;

linewithoutnumbers ();
linewithoutnumbers ();
linewithnumbers (first, numberofcells);
lineofdashes ();

}


void tiemessage()
{
cout <<"Bad news you didn't win" << endl;
cout <<"The computer also didn't win, it's a tie" << endl;
cout <<"Don't worry next time I will win!!!" << endl;
}


void winningmessage(int player)
{
cout <<"You have won the game! The computer is no match for you"
<< endl";
}
 
T

tomakated

But for you guys that already halped me out thanks a million. Had no idea
how to do it.
 

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

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top