TicTacToe question

Started by
24 comments, last by Cleric Kain 19 years, 1 month ago
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])
The G'Bro GameDev Society! -The Southeastern US GameDev Gathering Group
Advertisement
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 boardvoid draw_board( char [] );// function to get the player's move ( and modify the appropriate array elementvoid 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]
skulldrudgery--A tricky bit of toil
i got one error: error C2660: 'get_pos' : function does not take 1 parameters
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
I fixed the error. The function get_pos() needs two arguments, and I only put one. Instead of

get_pos( board_array );

I should have put:
get_pos( board_array, turn );
skulldrudgery--A tricky bit of toil
You are all making it way harder then this needs 2 be for such a simple version of TTT. First off using _gotoxy is not the best way to do this, and using one way just because "you wanna do it ur way" when there is a much better way is just plain dumb imo. Just make an array set 2 1-9 then build a board around it and when a person makes a move just replace their choice with a -1 or -2 etc then when u rewrite the board use a switch with case: -1 cout an X case 2: a Y default is just the 1-9 so they can see what the numbers r. See what I am saying? As for the input clearing just put it in a while loop like:

while(nobody has won)
{ -draw board with clearscrean
-get input
-do what i said above
}

rinse repeat. I would show you my TT game I made but I rebuilt it with an ascii board grr I just saved over old version on my floppy like 15 minutes ago =/

This topic is closed to new replies.

Advertisement