Hi all
I'm new to this site and hoping someone can assist with this problem. I've done all the code for the Tic Tac Toe game but having problems placing the 'o's & 'x's. This is the part of code I'm stuck on. Any help is greatly appreciated.

char PLAYER_TURN()
{
       int i;
       int x,y;
       board[9];
       drawX(x,y);
      do
       {
       displayBoard();

       for (i = 0; i<9;i++);
       while (board[i] ==! -1)
       {
       i++;
       }
       board[i]=0;
       }
  return 0;
  }

Recommended Answers

All 7 Replies

So, uh... what's board's type? Int? Char? (I'd go with char) And you could probably address it a little more logically as a 2-dimensional array.

Hello Drowzee

Yep it is a char board. If you like I could post the whole code if that would help?

By all means, post it inside code /code tags (Put square brackets, [ and ], around the code and /code, and paste your code between 'em...)

Here is my code - apologies if I haven't posted it correctly!! I'm having problems displaying the 'O's & 'X's.

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
char board[9];
int TURN = 0;
int MOVE;
//functions
void instructions();
int displayBoard();
void hline (int x, int y, int col);
void vline (int x, int y, int col);
void playgame();
char PLAYER_TURN();
char CPU_TURN();
void drawX(int x, int y);
void drawO(int x, int y);
int checkWinner();


int main()
{
clrscr();
char key;  //define a character called key
  do
    {


cout << "\n\n\n\n\n\n\t\t\t\tTIC TAC TOE\n\n\n\t\t";
cout << "Think You Can Win a Game of Tic Tac Toe?\n\n\n\t\t";
cout << "Please choose one of the following options...\n\n\n";
cout << "\n\tView Instructions    Press '1'";
cout << "\n\tStart New Game       Press '2'";
cout << "\n\tExit Program         Press '3'";

do
      {
      key= getch();//input choice
      }
    while (key!='1' && key!='2' && key!='3');

if (key=='1')
{
instructions();
}

if (key=='2')
{
playgame();
}
}

while (key!='3');
}






void instructions()
{
clrscr();
cout << " \n\tPlayer 1 always is always X";
cout << "\n\n\tThe object of the game is to get 3 matching symbols in a row\n\n\tThe grid is numbered as shown below....";
cout << "Press a key to continue...";
gotoxy(20,15);
cout << "0 | 1 | 2\n";
gotoxy(20,16);
cout << "---------\n";
gotoxy(20,17);
cout << "3 | 4 | 5\n";
gotoxy(20,18);
cout << "---------\n";
gotoxy(20,19);
cout << "6 | 7 | 8";
getch();
clrscr();
}

void playgame()
 {
  int i;
  for (i=0;i<10;i++) board[i]= -1;
  PLAYER_TURN();
  CPU_TURN();
  PLAYER_TURN();
  CPU_TURN();
  PLAYER_TURN();
  checkWinner();
  CPU_TURN();
  checkWinner();
  PLAYER_TURN();
  checkWinner();
  CPU_TURN();
  checkWinner();
  PLAYER_TURN();
  checkWinner();

}




int displayBoard()
{
int i;
clrscr();
hline(5,10,17);
hline(5,16,17);
vline(10,5,17);
vline(16,5,17);
for (i = 0; i<9;i++);

 {
 if (i == 0)
 {
  drawX(5,5);
 }
 else if (i == 1)
 {
 drawX (11,5);
 }
 else if (i == 2)
 {
 drawX (17,5);
 }
 else if (i == 3)
 {
 drawX (5,11);
 }
 else if (i == 4)
 {
 drawX (11,11);
 }
 else if (i == 5)
 {
 drawX (17,11);
 }
 else if (i == 6)
 {
 drawX (5,17);
 }
 else if (i == 7)
 {
 drawX (11,17);
 }
 else if (i == 8)
 {
 drawX (17,17);
 }
 }
 {
 if (i == 0)
 {
 drawO (5,5);
 }
 else if (i == 1)
 {
 drawO (11,5);
 }
 else if (i == 2)
 {
 drawO (17,5);
 }
 else if (i == 3)
 {
 drawO (5,11);
 }
 else if (i == 4)
 {
 drawO (11,11);
 }
 else if (i == 5)
 {
 drawO (17,11);
 }
 else if (i == 6)
 {
 drawO (5,17);
 }
 else if (i == 7)
 {
 drawO (11,17);
 }
 else if (i == 8)
 {
 drawO (17,17);
 }
 return 0;
 }
 }


void hline (int x, int y, int col)
{
int i;
textcolor (col);
for (i=0;i<17;i++)
{
gotoxy(i+x,y);
cprintf("%c",219);
}
}


void vline (int x, int y, int col)
{
int i;
textcolor (col);
for (i=0;i<17;i++)
{
gotoxy(x,i+y);
cprintf("%c",219);
}
}



void drawX (int x, int y)
{
gotoxy(x,y+1);
cprintf(" X X");
gotoxy(x,y+2);
cprintf("  X ");
gotoxy(x,y+3);
cprintf(" X X");

}



void drawO (int x, int y)
{
gotoxy(x,y+1);
cprintf("  O ");
gotoxy(x,y+2);
cprintf(" O O");
gotoxy(x,y+3);
cprintf("  O ");

}






char PLAYER_TURN()
{
       int i;
       int x,y;

       board[9];
       drawX(x,y);
       displayBoard();
       {
       for (i = 0; i<10;i++);
       {
       while(board[i] ==! -1);
       }
      checkWinner();
      }

      return 0;
       }




char CPU_TURN()
{
        int i;
        int x,y;

        board[9];
        drawO(x,y);

        displayBoard();

        for (i = 0; i<10;i++);
      {
       board[i] == -1;
      }
      checkWinner();
      }






int checkWinner()
{

    while(TURN < 10)
    {

    if (board[0]== 'X' && board[4]== 'X' && board[8]== 'X') {cout<<"Player 1 is the winner";break;}
    if (board[6]== 'X' && board[4]== 'X' && board[2]== 'X') {cout<<"Player 1 is the winner";break;}
    if (board[0]== 'X' && board[3]== 'X' && board[6]== 'X') {cout<<"Player 1 is the winner";break;}
    if (board[1]== 'X' && board[4]== 'X' && board[7]== 'X') {cout<<"Player 1 is the winner";break;}
    if (board[2]== 'X' && board[5]== 'X' && board[8]== 'X') {cout<<"Player 1 is the winner";break;}
    if (board[0]== 'X' && board[1]== 'X' && board[2]== 'X') {cout<<"Player 1 is the winner";break;}
    if (board[3]== 'X' && board[4]== 'X' && board[5]== 'X') {cout<<"Player 1 is the winner";break;}
    if (board[6]== 'X' && board[7]== 'X' && board[8]== 'X') {cout<<"Player 1 is the winner";break;}

    // Check for Player 2...

    if (board[0]== 'O' && board[4]== 'O' && board[8]== 'O') {cout<<"Player 2 is the winner";break;}
    if (board[6]== 'O' && board[4]== 'O' && board[2]== 'O') {cout<<"Player 2 is the winner";break;}
    if (board[0]== 'O' && board[3]== 'O' && board[6]== 'O') {cout<<"Player 2 is the winner";break;}
    if (board[1]== 'O' && board[4]== 'O' && board[7]== 'O') {cout<<"Player 2 is the winner";break;}
    if (board[2]== 'O' && board[5]== 'O' && board[8]== 'O') {cout<<"Player 2 is the winner";break;}
    if (board[0]== 'O' && board[1]== 'O' && board[2]== 'O') {cout<<"Player 2 is the winner";break;}
    if (board[3]== 'O' && board[4]== 'O' && board[5]== 'O') {cout<<"Player 2 is the winner";break;}
    if (board[6]== 'O' && board[7]== 'O' && board[8]== 'O') {cout<<"Player 2 is the winner";break;}

 }

}

I'm a bit busy at work, so I don't have time to run through this completely.
So, what's the problem? The X's and O's don't show up at all?

Oh, and you may find this helpful to try:

char board[3][3];

;

That creates a 3x3 grid that you can use in a more reasonable fashion. I think it's x, then y, but the origin would most logically be in the upper left corner, like this:

board[0][0] | board[0][1] | board[0][2]
-------------------------------------
board[1][0] | board[1][1] | board[1][2]
-------------------------------------
board[2][0] | board[2][1] | board[2][2]

With the horizontal and vertical bars just for the purpose of illustration, not as code.


I think your code is a bit too complex, personally. I don't know why I feel that way, but my intuition (admittedly poor) just tells me that you're overdefining things.

assuming EX and OH are defined somewhere as non-zero ints then something like this can keep your checking routines simple.

int MainWindow::IsWinner()
{
    // this holds the patterns we will check for
    static int pattern[8][3]=
    {
        0,1,2,
        3,4,5,
        6,7,8,
        0,3,6,
        1,4,7,
        2,5,8,
        0,4,8,
        2,4,6
    };
    // this loop does the checking
    for( int i = 0; i < 8; ++i)
    {
        if( (GameGrid_[pattern[i][0]] == EX) &&
            (GameGrid_[pattern[i][1]] == EX) &&
            (GameGrid_[pattern[i][2]] == EX))
            return EX;
        if( (GameGrid_[pattern[i][0]] == OH) &&
            (GameGrid_[pattern[i][1]] == OH) &&
            (GameGrid_[pattern[i][2]] == OH))
            return OH;
    }
    // no winner so return 0
    return 0;
}

this assumes GameGrid_ is defined as a an array of 9 ints.
Similarly your drawing routines can be simplified but ill leave you to work that out on your own.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.