Hi, i've been learning c++ for a month or so now. It's been going well but i've just hit a snag while trying to make a tic tac toe game. In my array when I input a value into the left column it will also change the value in the right column but one higher. In other works modifying gameArray[2][0] will change gameArray[1][2]. Puzzled as to why this is happening.
I've also been making a connect 4 game and the same thing has happened when inputting a value into the leftmost column the rightmost column one row higher will also change.
//Tic Tac Toe Game
//V1.0 Player 1 vs PLayer 2
#include <iostream>
/*Function for printing out the board*/
void boardPrint(char gameArray[2][2])
{
std::cout << std::endl; //to keep the new board away from anything else on the screen
for(int iii = 0; iii<3; iii++)
{
for(int jjj=0; jjj<3; jjj++)
{
if(jjj!=2)
std::cout << " " << gameArray[iii][jjj] << "\t|"; // | used to give structure to grid
else
std::cout << " " << gameArray[iii][jjj] << std::endl;
}
if(iii!=2)
std::cout << "-------------------------" << std::endl;
}
std::cout << std::endl;
}
void takeTurn(char playerSymbol, int playerNumber ,char (&gameArray)[2][2]) //Reference used so the array can be changed by the function
{
std::cout << "Player " << playerNumber << "s turn.\n";
std::cout << "Row number: ";
int rowNumber;
std::cin >> rowNumber;
std::cout << "Column number: ";
int columnNumber;
std::cin >> columnNumber;
gameArray[rowNumber - 1][columnNumber - 1] = playerSymbol;
}
/*sets all elements of array to 0*/
void clearBoard(char(&gameArray)[2][2])
{
for(int iii=0; iii < 3; iii++)
{
for(int jjj=0; jjj < 3; jjj++)
{
gameArray[iii][jjj]= 0;
}
}
}
int main()
{
bool game = true;
char gameArray[2][2];
clearBoard(gameArray);
std::cout << "Welcome to the game of tic tac toe.\n\nInstructions:\nPlayer 1 will start and will be X.\n"
<< "To make a move you must enter the row number and then the column number of the \nspace you want to play each followed by the return key.\n\n";
while(game)
{
for(int iii=1; iii<=9; iii++) //There will be 9 turns taken
{
if(iii%2==1)
{
takeTurn('X',1,gameArray);
boardPrint(gameArray);
}
else
{
takeTurn('O',2,gameArray);
boardPrint(gameArray);
}
}
}
return 0;
}
Any useful tips on how to improve my coding will also be vastly appreciated.
Thanks
:)
Your program attempts to access elements of the array at out-of-bounds array indexes multiple times. This results in undefined behavior (http://en.wikipedia.org/wiki/Undefined_behavior) and could be the cause of the problem you mentioned in your post.
The bounds of an array is its size (the number of elements it contains) minus one, so the right operand of the less than operator (<) in the conditions of the preceding for loops should be two.
The bounds of an array is its size (the number of elements it contains) minus one, so the right operand of the less than operator (<) in the conditions of the preceding for loops should be two.
Would it not be 2 if I was using the operator (<=)? If i change the loop values from 3 to 2 in the boardClear function then it wont clear the board fully.
Would it not be 2 if I was using the operator (<=)? If i change the loop values from 3 to 2 in the boardClear function then it wont clear the board fully.
concerning:
char gameArray[2][2];
This is an array with 4 elements. The valid indexes for gameArray are: