TicTacToe question

Started by
24 comments, last by Cleric Kain 19 years, 1 month ago
#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.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Advertisement
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.
The G'Bro GameDev Society! -The Southeastern US GameDev Gathering Group
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]
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?
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.
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;}
Why do you have all those ifs in there? They aren't needed.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
srry i forgot
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 = Oint 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])
The G'Bro GameDev Society! -The Southeastern US GameDev Gathering Group
if i read the C++ Grand Cru book can i make a tic-tac-toe game then? i was wondering because i just started reading in it.

This topic is closed to new replies.

Advertisement