TicTacToe question

Started by
24 comments, last by Cleric Kain 19 years ago
hey i was borred so i was making a TicTacToe game but i've a problem! this is my code:

#include <iostream>
using namespace std;

void xpos();
void ypos();

int main()
{
	int pos;

	cout << endl;
	cout << endl;
	cout << "    _________________ " << endl;
	cout << "   |     |     |     |" << endl;
	cout << "   |     |     |     |" << endl;
	cout << "   |_____|_____|_____|" << endl;
	cout << "   |     |     |     |" << endl;
	cout << "   |     |     |     |" << endl;
	cout << "   |_____|_____|_____|" << endl;
	cout << "   |     |     |     |" << endl;
	cout << "   |     |     |     |" << endl;
	cout << "   |_____|_____|_____|" << endl;
	cout << endl;
	cout << endl;

	cout << "enter a number to place the X(1-9): ";

	cin  >> pos;

	switch (pos)
	{
	case 1:
	if ( pos == 1)
	
	break;

	default:
		break;
	}

	system("pause");
	return 0;

}
how do i make a case that if you typed 1 it makes a X in colume 1? and delete the "1" that you typed so you can typ a new letter? and must i use arrays?
Advertisement
I believe you can use the gotoxy() function to place characters anywhere on the screen in a console window.

http://www.programmershelp.co.uk/cgotoxy.php

Quote:From http://www.programmershelp.co.uk/cgotoxy.php

#include <conio.h>
#include <stdio.h>


int main(void)
{
int i;
printf("This is the first line.\n");
gotoxy(10,4);
printf("This is the second line.\n");
gotoxy(20,8);
printf("And this is line 3.\n");

return 0;

}


With this method, you would not need arrays, since you have random access to place any char onto any x,y location in the console window.

Hope it helps.

-For some reason that code didn't work in my compiler :(
your results may vary
The G'Bro GameDev Society! -The Southeastern US GameDev Gathering Group
You need to put the drawing code inside a loop. For example, here's some pseudocode:

while (no one has won yet) {

DrawBoard();
HandleInput();
CheckForWinner();
}

So it would just keep redoing this main loop until somebody wins. And yes, you'll need to use arrays to store the board, rather than just explicitly drawing it. The code for drawing the array may be something like this:

int x = 0, y = 0;
for (y < NumRows; ++y) {

cout << "-- -- --\n";

for (x < NumColumns; ++x) {

cout << "|";
char Pos = Board[x][y];
if (Pos == 'X') cout << "X";
else if (Pos == 'O') cout << "O";
else cout << " ";
}

cout << "|\n";
}

cout << "-- -- --\n\n";

That's just some pseudoish code, it may or may not be totally correct. Just typed it out really quickly. You'd have to mess around with it to get the table looking right. [smile]

Anyway, hope this helps.
you mean you didn't see anything or something else?
and i get this error: error C2065: 'gotoxy' : undeclared identifier

i'm using visual C++ 6.0
Are you including the correct headers? It appears from that article on it you need conio.h and stdio.h. If you are, then your compiler most likely doesn't support it. I know I for one have never heard of it.
i got it the compiler doesn't know a header so i had to make a one gotoxy funtion:

#include <windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;



void gotoxy(int x,int y)
{
COORD coord;
coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}


int main()
{
int pos;

cout << endl;
cout << endl;
cout << " _________________ " << endl;
cout << " | | | |" << endl;
cout << " | | | |" << endl;
cout << " |_____|_____|_____|" << endl;
cout << " | | | |" << endl;
cout << " | | | |" << endl;
cout << " |_____|_____|_____|" << endl;
cout << " | | | |" << endl;
cout << " | | | |" << endl;
cout << " |_____|_____|_____|" << endl;
cout << endl;
cout << endl;

cout << "enter a number to place the X(1-9): ";

cin >> pos;
gotoxy(10,4);
cout << pos;

switch (pos)
{
case 1:
if ( pos == 1)

break;

default:
break;
}

system("pause");
return 0;

}
BTW: You said your using VC++ 6.0. I highly recommend checking out this. It's the optimizing C++ compiler that ships with Visual Studio .NET 2003 Pro, and it's totally free! I'm fairly sure you can "plug" it in to the VC++ 6.0 IDE, and have it use it instead of the native compiler.
Quote:Original post by The Forgotten Mindset
-For some reason that code didn't work in my compiler :(
your results may vary


Ah, unless you are using an old compiler, I think you must use conio2.h.

http://molhanec.net/conio_doc.html

VC++ 6.0 doesn't come with it (at least on my installation), so you may need to download it from somewhere.

And when you get this working, you can simply place an X or an O inside of a space just by finding the xy coordinates that you want to use.

// all numbers are arbitraryint xcoord = 33;int ycoord = 12;gotoxy(ycoord,xcoord)_putch('X');xcoord += 5;_putch('O');


hope it helps [smile]

(And yes, I did quote myself [lol])
The G'Bro GameDev Society! -The Southeastern US GameDev Gathering Group
but how do i make a code so the 1 goes away so you can typ a other number?

This topic is closed to new replies.

Advertisement