Sign in to follow this  
jordi_0071

TicTacToe question

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
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

#include <iostream>

enum Cell {Empty=' ', X='X', O='O'};

typedef Cell Board[9];

void drawLine(Cell a, Cell b, Cell c)
{
std::cout << " | | | |\n"
<< " | "<<static_cast<char>(a)
<< " | " <<static_cast<char>(b)
<< " | " <<static_cast<char>(c)
<< " |\n"
<< " |_____|_____|_____|" << std::endl;
}

void draw(const Board &board)
{
std::cout << " _________________ " << std::endl;

for(int c = 0; c < 3; ++c)
drawLine(board[c*3+0],board[c*3+1],board[c*3+2]);
}

Cell testDirection(const Board &board, int cell, int offset)
{
Cell first(board[cell]);

for(int c = 1; c<3; ++c)
if(board[cell+offset*c] != first)
return Empty;

return first;
}

Cell findWinner(const Board &board)
{
Cell result;

for(int cell = 0; cell < 3; ++cell)
{
if(Empty != (result = testDirection(board, cell, 3))) return result;
if(Empty != (result = testDirection(board, cell*3, 1))) return result;
}

if(Empty != (result = testDirection(board, 0, 4))) return result;
if(Empty != (result = testDirection(board, 2, 2))) return result;

return Empty;
}

bool boardFilled(const Board &board)
{
for(int cell = 0; cell < 9; ++cell)
if(board[cell] == Empty)
return false;

return true;
}

void clearBoard(Board &board)
{
for(int cell = 0; cell < 9; ++cell)
board[cell] = Empty;
}

bool setBoard(Board &board, Cell player, char position)
{
if(position < '1' || position > position > '9')
return false; // Not a valid position

int index = position-'1'+6;
if(position > '6') index = position-'7';
else if(position > '3') index = position-'4'+3;

if(board[index] != Empty)
return false; // Already something there.

board[index] = player;
return true;
}

int main()
{
Board board;
Cell player = X;

clearBoard(board);

while(true)
{
draw(board);

do
{
std::cout << static_cast<char>(player) << ", enter your move (1-9): ";
std::cout.flush();
std::cin.sync();
}
while(!setBoard(board, player, std::cin.get()));

Cell winner(findWinner(board));
if(Empty != winner)
{
std::cout << static_cast<char>(winner) << " wins!" << std::endl;
clearBoard(board);
}
else if(boardFilled(board))
{
std::cout << "Draw!" << std::endl;
clearBoard(board);
}

if(player == X)
player = O;
else player = X;
}

// Never reached. . .
return 0;
}




Edit: I hate tab characters.

Share this post


Link to post
Share on other sites
Quote:
Original post by The Forgotten Mindset
Quote:
Original post by jordi_0071
but how do i make a code so the 1 goes away so you can typ a other number?


I am unable to test it, but I think that is whay gotoxy() does. Take a look at my previous post.


Or just do what smart_idiot and I showed you, and completely redraw the board each time it changes. I believe smart_idiot just gave you the code to an entire tic-tac-toe game.[grin]

Share this post


Link to post
Share on other sites
Quote:
Original post by jordi_0071
but i wanna do it on my one way:P how do i make a function so the 1 goes away and you can type a new number?


By doing a loop. Look at my first post:

while (no one has won yet) {

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

HandleInput() might look something like this:

void HandleInput() {

char Response;
cout << "Please enter a number: ";
cin >> Response;

// do whatever you need to with Response
}

As you can see from the pseudocode, this function would be called each time the loop is executed. Just try it out yourself and see what I mean if you don't understand.

Share this post


Link to post
Share on other sites
does anyone wants to continue this project? so i know how to make on witch swich case and stuff? here is the code:


#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 posX, posO;

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 >> posX;

switch(posX)
{
case 1:

if(posX == 1)
gotoxy(6,4);
cout << "X";

break;

case 2:
if(posX == 2)
gotoxy(12,4);
cout << "X";
break;

case 3:
if(posX == 3)
gotoxy(18,4);
cout << "X";
break;

case 4:
if(posX == 4)
gotoxy(6,7);
cout << "X";
break;

case 5:
if(posX == 5)
gotoxy(12,7);
cout << "X";
break;

case 6:
if(posX == 6)
gotoxy(18,7);
cout << "X";
break;

case 7:
if(posX == 7)
gotoxy(6,10);
cout<< "X";
break;

case 8:
if(posX == 8)
gotoxy(12,10);
cout << "X";
break;

case 9:
if(posX == 9)
gotoxy(18,10);
cout << "X";
break;
default:

return 0;
}





gotoxy(22,22);
system("pause");

return 0;

}

Share this post


Link to post
Share on other sites
Mabey this will inspire you...

C++ Knowledge requirements:
-Multidimensional Arrays
-Nested Loops


int main()
{
int input = 0;

// This represents the board in memory
// 0 = empty space
// 1 = X
// 2 = O
int board_map[3][3] = {
0, 0, 0,
0, 0, 0,
0, 0, 0
};

int player_mark = 1; // player uses an X

// the following numbers have not been calibrated with the board
// , therefore you should tweak them accordingly.

#define LEFT_PADDING 5 // distance from left of console
#define TOP_PADDING 5 // distance fromt top of console
#define SPACE_SPACING 3 // distance between spaces (for Xs and Os)

int cur_x_pos = 0;
int cur_y_pos = 0;

gotoxy(LEFT_SPACING, TOP_PADDING);

while(input != -1)
{
// clear the screen blank
system("cls");

// Draw the board lines
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;


// Draw each row and column of the map
// onto the screen
for( int row = 0; row < 3; row++ )
{
for( int col = 0; col < 3; col++ )
{
// draw the X or O or empty
switch( board_map[row][col] )
{
case 1:
cout<< "X";
break;
case 2:
cout<< "O";
break;
}
// update the x cursor (move it to the next space)
cur_x_pos += SPACE_SPACING;
} // end draw column

// update the y cursor (move it to the next row)
cur_y_pos += SPACE_SPACING;
} // end draw row

cout<< "Do you want to continue? -1 = quit, 1 = continue";
cin>> input;

if( input != -1 )
{
int map_x, map_y;
cout<< "What X position do you want to put your X?";
cin>> map_x;
cout<< "What Y position do you want to put your X?";
cin>> map_y;

board_map[map_y][map_x] = player_mark;
}
} // end game loop

// game is done
return 0;
}






I haven't tested this, I was just typing at the edge of my seat. But hopefully you see where I'm going on this. ;)

If this doesn't make sense at first glance, just look at it step by step, especially in the drawing loops.

(Wow, I can't believe I wrote a program in a post [smile])

Share this post


Link to post
Share on other sites
I did a search on the C++ Grand Cru book, but I couldn't find an english description of it :/

So I don't know if it will teach you how to make a tic tac toe game, but if it is like all other C++ books, then it will give you a firm grasp on C++ so that you will be able to program anything (in theory [lol])

Share this post


Link to post
Share on other sites
I get it! You want to draw the different pieces in the board, but without changing anything else. You want everything to stay in place, including the prompt for a new move and then the X or O appears in the board. You can use gotoxy() to do it for real, but you can "emulate" it, so to speak, by using system( "cls" ) to clear the screen and then redraw the board and prompt. I would really reccomend that you use this method along with an array because it makes everything so much simpler. You can use the array to check if anyone has won yet or if a move entered has already been taken. You can also eliminate any switch statements. This should draw the board the way I described. If I'm way off then just ignore me.
#include<iostream>

using std::cout;
using std::cin;
using std::endl;

// function to draw the board
void draw_board( char [] );

// function to get the player's move ( and modify the appropriate array element
void get_pos( char [], int );

int main()
{
// declare an array to keep track of the board
char board_array[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

// an integer to keep track of the turn
int turn = 1;

// implement your own function to determine if the game is over ( won or lost yet ) and keep track of the turn
while ( turn <= 9 /*&& gameOver() != true */ ) {

// draw the board
draw_board( board_array );

// get the input from the player and change the board
get_pos( board_array, turn );

// go to the next turn
turn++;
}

cout << endl;

return 0;
}

void draw_board( char board[] )
{
//clear the screen
system( "cls" );

cout << " _________________ \n"
<< " | | | |\n"
<< " | " << board[7] << " | " << board[8] << " | " << board[9] << " |\n"
<< " |_____|_____|_____|\n"
<< " | | | |\n"
<< " | " << board[4] << " | " << board[5] << " | " << board[6] << " |\n"
<< " |_____|_____|_____|\n"
<< " | | | |\n"
<< " | " << board[1] << " | " << board[2] << " | " << board[3] << " |\n"
<< " |_____|_____|_____|\n" << endl;
}

void get_pos( char board[], int t )
{
static int pos;

cout << "Make your move: ";
cin >> pos;

// rework this to change depending on which turn it is ie odd turns are X, even are O
board[pos] = 'X';
}





[Edited by - skulldrudgery on March 28, 2005 10:26:20 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by jordi_0071
how do i make a function so the 1 goes away and you can type a new number?



where x and y are the position of the prompt
gotoxy(x,y)
putc(' ')

and then when you do your get player move just gotoxy(x,y) the same position

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this