473,657 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 linewithoutnumb ers();

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<=numbero fcells)
{
printboard (first, numberofcells);
first = first+3;
}
linewithoutnumb ers();

}

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

linewithoutnumb ers ();
linewithoutnumb ers ();
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 linewithoutnumb ers()
{
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.c pp" 72 lines, 1189 characters
$ calendar.out
| |
| |
1| 2| 3|
--------------------
| |
| |
4| 5| 6|
--------------------
| |
| |
7| 8| 9|
--------------------
| |
Jul 22 '05 #1
9 1479

"tomakated" <to*******@comc ast.net> wrote in message
news:e1******** *************** *******@localho st.talkaboutpro gramming.com...
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
Jul 22 '05 #2
Thanks, it works but it makes a space after that line before the
dashes"------"

Jul 22 '05 #3
"tomakated" <to*******@comc ast.net> wrote in message
news:6d******** *************** *******@localho st.talkaboutpro gramming.com...
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
Jul 22 '05 #4
tomakated wrote:

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 ?

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #5
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 linewithoutnumb ers();

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<=daysinm onth)
{
printweek (first, daysinmonth);
first=first+3;
}
linewithoutnumb ers();
}

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

linewithoutnumb ers ();
linewithoutnumb ers ();
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 linewithoutnumb ers()
{
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.c pp" 71 lines, 1160 characters
$ calendar.cpp
calendar.cpp: execute permission denied
$ calendar.out
$ calendar.out
Jul 22 '05 #6
On Sun, 12 Dec 2004 15:31:06 -0500, "tomakated"
<to*******@comc ast.net> wrote:
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<?>

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 linewithoutnumb ers();

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<=numbero fcells)
{
printboard (first, numberofcells);
first = first+3;
}
linewithoutnumb ers(); [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;

linewithoutnumb ers ();
linewithoutnumb ers ();
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 linewithoutnumb ers()
{
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.cp p" 72 lines, 1189 characters
$ calendar.out
| |
| |
1| 2| 3|
--------------------
| |
| |
4| 5| 6|
--------------------
| |
| |
7| 8| 9|
--------------------
| |


Hope that helps.

rossum

--

The ultimate truth is that there is no Ultimate Truth
Jul 22 '05 #7

"tomakated" <to*******@comc ast.net> wrote in message
news:6c******** *************** *******@localho st.talkaboutpro gramming.com...
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'.)

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

--------------------
| |
| |
7| 8| 9
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
--------------------
| |
| |
4| 5| 6
--------------------
| |
| |
7| 8| 9


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
Jul 22 '05 #8
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 makecomputermov e(char [], int);
void makeusermove(ch ar [], 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 linewithoutnumb ers();

// 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(boar d);
instructions();
player = whogoesfirst();
while ((movenumber<9) && (!done))
{
if (player == User) makeusermove(bo ard,movenumber) ;
else makecomputermov e(board,movenum ber);
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";
boardwithnumber s( 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')
{
makecomputermov e();
}
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 linewithoutnumb ers ()
{
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[i]==b[i+1]) && (b[i]==b[i+2]) && (b[i]!=Blank))
return(true);
for (i=0;i<3;i++)
if ((b[i]==b[i+3]) && (b[i]==b[i+6]) && (b[i]!=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(ch ar 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";
boardwithnumber s(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 boardwithnumber s( const char b[])
{
int i;

linewithoutnumb ers ();
linewithoutnumb ers ();
linewithnumbers (first, numberofcells);
lineofdashes ();
}
void makecomputermov e(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[i] == Blank)
{
b[i] = mark;
if (won(b))
{ cout << "I put an " << mark << " in cell "
<< i+1 << " and win.\n";
printboard(b);
return;
}
else b[i] = Blank;
}
}

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

// feel free to change this part if you choose

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

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

linewithoutnumb ers ();
linewithoutnumb ers ();
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";
}

Jul 22 '05 #9
But for you guys that already halped me out thanks a million. Had no idea
how to do it.

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2983
by: lawrence | last post by:
I'm trying to read up on the rfc's that govern form inputs. Much of what I'm reading is stuff I didn't know before and some of it is alarming. This one left with me questions: http://www.ietf.org/rfc/rfc1867.txt Is this (below) addressed to me as a web designer, or is this addressed to the makers of web browsers? Identifying the type of file being uploaded seems way outside of my scope as a PHP coder. Am I
8
2189
by: Dgates | last post by:
Has anyone typed up an index for the O'Reilly book "C# and VB.NET Conversion?" I'm just learning C#, and often using this little book to see which VB.NET terms translate directly to some term in C#. However, it's a real hassle that the book has no index, just a table of contents. For example, as early as page 8, the book teaches that C#'s "using" statement is the equivalent of VB.NET's "imports" statement. However, that concept...
0
2006
by: chandniashar | last post by:
This is my perl program for tictactoe. Can anyone help me run the program as a cgi script? #!/usr/bin/perl # Description: This program implements the game of Tic Tac Toe # The grid looks like the following. # # +---+---+---+ # | 0 | 1 | 2 | # |---+---+---| # | 3 | 4 | 5 | # |---+---+---|
9
6504
by: wizardRahl | last post by:
Hello, I'm attempting to write a TicTacToe program for class and need some help with arrays. We have to write a program that will allow two users to play tic-tac-toe. The program needs to have turns alternately from player X and player O. I have defined the board as char board. The real problem is that in my loop that I have set up it's not giving me a chance to input each player's positions. I get a compilation error saying: "Error E2377...
7
2200
by: Warren Hoskins | last post by:
Old title: Homework Due 2-20-07 can"t understand why this will not compile. I've been working on tis all week end. Need Help desperately
4
2080
by: doc | last post by:
Anyone tell me about patServer please. Does anyone know where I can find instructions on how to use patServer - spent ages on the net trying to find a 'how to'. Specifically, where to put each of the files in one of their examples (say, tictactoe) and whether I need to use CLI to start patServer - if so how to do that. Adapting it for my purposes seems fairly straight forward but I just need to get an example running.
1
1568
by: racshah | last post by:
I have a tictactoe script with 2 users playing. I need a perl script in which one user plays with the computer. Can anyone help me with it???
0
933
by: Sean McIlroy | last post by:
""" AUTHOR: Sean McIlroy LANGUAGE: Python 2.5 OVERVIEW: instances of "tictactoeplayer" play optimal tictactoe SPECIALIZED TYPES: player={ex,oh}; empty={blank}; cell=player+empty; board= TYPE RELATIONS: bool(c)==True for any c in cell """ ex, oh, blank = 'X', '0', ' '
10
2490
oedipus
by: oedipus | last post by:
I am currently learning java at school and thus far we have oly dealt with the text-based compiler JGrasp; therefore; my attempt to code Tictactoe or "X and O's" game is written as a text based program. I have made use of a 2 dimensional integer array to represent the 3x3 grid. Each block therefore has a co-ordinate.eg. grid.Initially all blocks in the grid are equal to zero. The computer uses ones(1) and the player uses twos(2).The user is...
0
8402
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8315
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8829
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8734
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8608
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5633
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4164
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1962
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.