Hi guys, I am trying to make the Tic Tac Toe Class game but I am encountering an error with the initialization of my array for the game board. Instead of initializing everything to blank spaces, random characters end up in there and I don't know what is causing this problem.
Here is my code beginning with the header file:
#ifndef TTT_H
#define TTT__H
class TTT
{
public:
TTT();
~TTT();
void drawBoard(char tttBoard[9]);
void makeMoveX(char tttBoard[9]);
void makeMoveO(char tttBoard[9]);
void checkWinnerX(char tttBoard[9], char z, int& win);
void checkWinnerO(char tttBoard[9], char u, int& win);
private:
char tttBoard[9];
int round;
int win;
};
#endif
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "ttt.h"
using namespace std;
//constructor
TTT::TTT()
{
round = win = 0;
tttBoard[0] = ' ';
tttBoard[1] = ' ';
tttBoard[2] = ' ';
tttBoard[3] = ' ';
tttBoard[4] = ' ';
tttBoard[5] = ' ';
tttBoard[6] = ' ';
tttBoard[7] = ' ';
tttBoard[8] = ' ';
}
//destructor
TTT::~TTT()
{
cout<<"Destructor called";
}
void TTT::drawBoard(char tttBoard[9])
{
cout<<"\n +---+---+---+";
cout<<"\n | "<<tttBoard[0]<<" | "<<tttBoard[1]<<" | "<<tttBoard[2]<<" | ";
cout<<"\n +---+---+---+";
cout<<"\n | "<<tttBoard[3]<<" | "<<tttBoard[4]<<" | "<<tttBoard[5]<<" | ";
cout<<"\n +---+---+---+";
cout<<"\n | "<<tttBoard[6]<<" | "<<tttBoard[7]<<" | "<<tttBoard[8]<<" | ";
cout<<"\n +---+---+---+";
}
void TTT::makeMoveX(char tttBoard[9])
{
int s;
bool repeat = false;
cout<<"\nPlayer x, enter the square number you wish to play: ";
do {
cin>>s;
if ((s>0)&&(s<=9))
{
if ((!(tttBoard[s-1]=='X'))&&(!(tttBoard[s-1]=='O')))
{
tttBoard[s-1]='X';
round++;
repeat = true;
}
else
{
cout<<"\n Enter an empty square number ";
}
}
else
{
cout<<"\n Enter a valid square number(1-9) : ";
}
} while (repeat == false);
}
void TTT::makeMoveO(char tttBoard[9])
{
int v;
bool repeat = false;
cout<<"\nPlayer O, enter the square number you wish to play: ";
do {
cin>>v;
if ((v>0)&&(v<=9))
{
if ((!(tttBoard[v-1]=='O'))&&(!(tttBoard[v-1]=='X')))
{
tttBoard[v-1]='O';
round++;
repeat = true;
}
else
{
cout<<"\n Enter an empty square number ";
}
}
else
{
cout<<"\n Enter a valid square number(1-9) : ";
}
} while (repeat == false);
}
void TTT::checkWinnerX(char tttBoard[9], char z, int& win)
{
for (int i=0; i<7; i+=3)
{
if (((tttBoard[i]==z)&&(tttBoard[i+1]==z))&&((tttBoard[i+1]==z)&&(tttBoard[i+2]==z)))
win = 1;
}
for (int j=0; j<3; j++)
{
if (((tttBoard[j]==z)&&(tttBoard[j+3]==z))&&((tttBoard[j+3]==z)&&(tttBoard[j+6]==z)))
win = 1;
}
if ((tttBoard[0]==z)&&(tttBoard[4]==z)&&(tttBoard[8]==z))
win=1;
if ((tttBoard[2]==z)&&(tttBoard[4]==z)&&(tttBoard[6]==z))
win=1;
}
void TTT::checkWinnerO(char tttBoard[9], char u, int& win)
{
for (int i=0; i<7; i+=3)
{
if (((tttBoard[i]==u)&&(tttBoard[i+1]==u))&&((tttBoard[i+1]==u)&&(tttBoard[i+2]==u)))
win = 2;
}
for (int j=0; j<3; j++)
{
if (((tttBoard[j]==u)&&(tttBoard[j+3]==u))&&((tttBoard[j+3]==u)&&(tttBoard[j+6]==u)))
win = 2;
}
if ((tttBoard[0]==u)&&(tttBoard[4]==u)&&(tttBoard[8]==u))
win=2;
if ((tttBoard[2]==u)&&(tttBoard[4]==u)&&(tttBoard[6]==u))
win=2;
}
If anyone could help me I would really really appreciate it, thank you