lundi 23 mars 2015

My program stops in the middle of its operation because it is unable to assign an element of a 2 dimensional array?


I was trying to make a tic tac toe program that allows two users to play tic tac toe. The program allows the users to decide, if they want to play, what symbols to represent themselves by, and which user will go first; however, the program does not allow the users to enter onto the playing board.


I ran the debugger tool in Codeblocks and found that the problem lies in the Input() function of my program. Below is where the input function is called in my program:



while (checkWin() != true && checkTie() != true)
{
input();
checkWin();
checkTie();
}


Here are the contents of the input() function that are important, the lines I believe are causing the problem are also indicated:



if (playerTurn == '1' && notFilled(in))
{
if (in == 1)
{
board[0][0] = player1Sym; //I think this line may be causing a problem.
}
else if (in == 2)
{
board[0][2] = player1Sym; //and this one
}
else if (in == 3)
{
board[0][4] = player1Sym; //and this one
}
else if (in == 4)
{
board[2][0] = player1Sym; //and this one
}
else if (in == 5)
{
board[2][2] = player1Sym; //and this one
}
else if (in == 6)
{
board[2][4] = player1Sym; //and this one
}
else if (in == 7)
{
board[4][0] = player1Sym; //and this one
}
else if (in == 8)
{
board[4][2] = player1Sym; //and this one
}
else if (in == 9)
{
board[4][4] = player1Sym; //and this one
}

playerTurn = '2';
}


If anyone was wondering, there is another else if statement in case playerTurn is equal to '2' that is basically a duplicate of the first if statement except for a few details. Thus, it is not necessary to post these lines.


Please help me to find the problem in this code.


UPDATE: Here is the code requested by sray that declaresd the 2d array...



char board[5][5] = {'1', '|', '2', '|', '3', '-', '-', '-', '-', '-', '4', '|', '5', '|', '6', '-', '-', '-', '-', '-', '7', '|', '8', '|', '9'};


UPDATE:


Here is what the tic tac board printed out looks like:



1|2|3
-----
4|5|6
-----
7|8|9


The board is 5 elements wide and 5 elements long. Since each field is located at every other indices (keeping in mind that for every 3 fields,there are 5 dashes) and an array starts at 0, each field is located on an equal indices.




Aucun commentaire:

Enregistrer un commentaire