Jump to content
  • Advertisement
Sign in to follow this  
jordi_0071

TicTacToe question

This topic is 4806 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;

}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 arbitrary
int 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])

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!