DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Tic Tac Toe (http://www.daniweb.com/forums/thread201553.html)

PrincessT Jul 3rd, 2009 8:25 pm
Tic Tac Toe
 
I'm a beginner at c++ and I have to do a game of tic tac toe and I'm having problems with asking the player to play. Please help.

*****************************************************

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

///function prototype
void inttableArray(int [][3], int );
void displayArray (int [][3], int );
void inputArrayFunction(int [][3], int );
void getMove(int [][3], int );
void getComputerMove(int [][3], int );
char Check(int [][3], int );


int main()
{
        char end;
       
       
        end= ' ';

        int tableOne[3][3]; //array of 3 row and 5 columns


        cout<<"This is the game of Tic Tac Toe.\n";
        cout<<"You will be playing against the computer.\n";


       
        inttableArray(tableOne, 3);

        displayArray(tableOne, 3);

        getMove(tableOne, 3);

        end = Check(tableOne, 3);

        getComputerMove(tableOne, 3);

        inputArrayFunction(tableOne, 3);

        if(end== ' ');
       
       
  if(end=='X') cout<<"You won!";
  else cout<<"I won!!!!";
  displayArray(tableOne, 3);


       
        return 1;

}

//**************************************************
void inttableArray(int TempArray[][3], int size )
{
        int row, column;

        for ( row = 0; row <3; row ++)
                for (column = 0; column < size; column ++)
                        TempArray[row][column] = ' ';
}

//**************************************************
void getMove(int TTempArray[][3], int Tsize)
{
        int x =0;
        int        y=0;
        int n=0;


  cout<<" Please make your move: ";
  cin>>n;

  if(n<1||n>9)
          cout<<"Invalid move"<<endl;
  else
          if(TTempArray[x][y]!= ' ')
        {
                cout<<"Invalid move, try again.\n";
                getMove(TTempArray, 3);
        }
        else TTempArray[x][y] = 'X';

       


}

//**************************************************

void getComputerMove(int STempArray[][3], int Ssize)
{
        int x;
        int        y;
        int n;


  cout<<"Player 2"<<endl;
  cout<<" Please make your move: ";
  cin>>n;

  if(n<1||n>9)
          cout<<"Invalid move"<<endl;
  else
          if(n=' ';n<=9);
        if(STempArray[y][x]!= ' ')
        {
                cout<<"Invalid move, try again.\n";
                getMove(STempArray, 3);
        }
        else STempArray[y][x] = 'Y';
}

//**************************************************
void displayArray (int DTempArray[][3], int Dsize)
{
        int x=0;
        int y=0;
       
               
        for ( x = 0; x < 3; x ++)
        {
                printf(" %c | %c | %c ",DTempArray[x][0],
            DTempArray[x][1], DTempArray [x][2]);
    if(x!=2) printf("\n---|---|---\n");
  }
  printf("\n");

       
}


//**************************************************
void inputArrayFunction(int inputArray[][3], int inputsize)
{
        int a, b;
        int x = 1;

        printf("\n---|---|---\n");

        for ( a = 0 ;a < 3; a++)
                for (b = 0; b < inputsize; b++)
                        if ( b == 2 )
                                inputArray[a][b] = 10;
                        else
                                inputArray[a][b] = b;
}
//**************************************************

char Check(int TinputArray[][3], int Tinputsize )
{
  int i;
  int row;
  int tableOne[3][3];
 
  for ( row = 0; row <3; row ++)
    if(tableOne[row][0]==tableOne[row][1] &&
      tableOne[row][0]==tableOne[row][2]) return tableOne[row][0];

  for(i=0; i<3; i++)
    if(tableOne[0][row]==tableOne[1][row] &&
      tableOne[0][row]==tableOne[2][row]) return tableOne[0][row];

 
  if(tableOne[0][0]==tableOne[1][1] &&
    tableOne[1][1]==tableOne[2][2])
      return tableOne[0][0];

  if(tableOne[0][2]==tableOne[1][1] &&
    tableOne[1][1]==tableOne[2][0])
      return tableOne[0][2];

  return ' ';
}

u8sand Jul 3rd, 2009 10:24 pm
Re: Tic Tac Toe
 
I didn't really look at anything accept for the main function. Wouldn't it make sense to have a loop? It looks like your only calling the human to move once, then the computer the move once. Then the game ends... Shouldn't you loop until someone wins or something? And also please be more specific as to what your error is.

daviddoria Jul 3rd, 2009 10:24 pm
Re: Tic Tac Toe
 
It doesn't sound like the problem has anything to do with tic-tac-toe. Can you please make a much, much smaller demonstration of your problem along with input, expected output, and current (incorrect) output.

shadwickman Jul 4th, 2009 3:21 am
Re: Tic Tac Toe
 
And as a side note, you shouldn't be using
stdio.h
or
stdlib.h
as those aren't standard C++ headers. Use
cstdio
and
cstdlib
which are the standard C++ headers.

And as u8sand said, you're only executing things through once. Loop until the game is over, then ask to if the player wants to play again, and if so, re-loop it all.

kangarooblood Jul 4th, 2009 7:45 am
Re: Tic Tac Toe
 
You could use a much easier type of game, here's a link from where you could get the code: http://brom8305.blogspot.com/2009/06...oe-script.html

try it out!


All times are GMT -4. The time now is 3:54 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC