Tic Tac Toe Troubles!

Hello all!

I am having trouble with my Tic Tac Toe console game. I have the user enter which row/column they want to place their piece, although on coordinate (1, 3) it places a piece there, but also at coordinate (2, 1). This also happens vice versa and at (2, 3) and (3, 1). Here is the code (it is no where near complete, I just need to resolve this issue before I can move forward).

main.cpp

#include "GameSystem.h"

#include <iostream>

using namespace std;

int main()
{
GameSystem gamesystem;
gamesystem.playGame();

return 0;
}


GameSystem.h

#ifndef GAMESYSTEM_H
#define GAMESYSTEM_H

#include <iostream>

using namespace std;


class GameSystem
{
public:
GameSystem();

void instructions();
void updateBoard();
void playGame();
int chooseSpot();
char processMove();
int resetInput();

private:
char _boardArr[2][2];
bool _xTurn;
bool _oTurn;

bool _isDone;
int _row;
int _column;

};

#endif // GAMESYSTEM_H


GameSystem.cpp

#include "GameSystem.h"

#include <iostream>
#include <cstdlib>
#include <conio.h>



using namespace std;

GameSystem::GameSystem()
{
_isDone = false;
_xTurn = true;

_boardArr[0][0] = ' ';
_boardArr[0][1] = ' ';
_boardArr[0][2] = ' ';

_boardArr[1][0] = ' ';
_boardArr[1][1] = ' ';
_boardArr[1][2] = ' ';

_boardArr[2][0] = ' ';
_boardArr[2][1] = ' ';
_boardArr[2][2] = ' ';
}

void GameSystem::instructions()
{
cout << "\t\tWelcome to Tic Tac Toe!\n\n";
cout << "X goes first, then O" << endl;
cout << "Choose at which place to put the piece by entering the row and column" << endl;
cout << "Try to get three in a row (diagonally, horizontally, or vertically)" << endl;
}

void GameSystem::updateBoard()
{
system("CLS");

cout << " |-1-|-2-|-3-|\n"
<< "1-|-" << _boardArr[0][0] << "-|-" << _boardArr[0][1] << "-|-" << _boardArr[0][2] << "-|" << endl
<< "2-|-" << _boardArr[1][0] << "-|-" << _boardArr[1][1] << "-|-" << _boardArr[1][2] << "-|" << endl
<< "3-|-" << _boardArr[2][0] << "-|-" << _boardArr[2][1] << "-|-" << _boardArr[2][2] << "-|" << endl;
}

//Main function that plays the game
void GameSystem::playGame()
{
while(_isDone == false){
updateBoard();
chooseSpot();
processMove();
}
}

//Chooses spot
int GameSystem::chooseSpot()
{
resetInput();

if(_xTurn == true){
cout << "Player X's turn\n" << endl;

cout << "Choose row: ";
cin >> _row;
if(_row > 3 || _row < 1){
cout << "INVALID INPUT!" << endl;
cin >> _row;
}

cout << "Choose column: ";
cin >> _column;
if(_column > 3 || _column < 1){
cout << "INVALID INPUT!" << endl;
cin >> _column;
}

return _row;
return _column;
}

if(_oTurn == true){
cout << "Player O's turn\n" << endl;
}
}

char GameSystem::processMove()
{
if(_xTurn){
_boardArr[_row - 1][_column - 1] = 'X';
return _boardArr[_row - 1][_column - 1];
}

if(_oTurn){
_boardArr[_row - 1][_column - 1] = 'O';
return _boardArr[_row - 1][_column - 1];
}
}

int GameSystem::resetInput()
{
_row = 0;
_column = 0;

return _row;
return _column;
}



Sorry if the code is a little messy or not that straightforward :P (I'm sure there are worse).

Thanks everyone in advance!
You have an out of bounds problem:

char _boardArr[2][2]; // Only indexes 0/1 are allowed

change the board to

char _boardArr[3][3]; // Only indexes 0/1/2 are allowed
Thanks so much! Sorry for the lack-of-tabbing-over: it wouldn't let me post the code like it should be :P. Again, thanks for the help - I didn't know one line of code could fix this issue.

:D
it wouldn't let me post the code like it should be
Use code tags: [code]Your code[/code]
Read this: http://www.cplusplus.com/articles/z13hAqkS/
Topic archived. No new replies allowed.