Advertisement

Some ideas for a tic tac toe

Started by November 30, 2013 03:55 PM
12 comments, last by bratiefanut 11 years, 2 months ago

Hi,

I am creating a Tic tac toe game in c++ and I must store my moves in some way. I tried to store Xs and Os in a matrix ( int matrix[3][3]). 1 if there was placed a X, -1 if there is an O and 0 if wasn't placed anything. But this method was a bit ineficient and got some problem with it atm. Do you know a more efficient method to store Xs n Os table?

"Don't gain the world and lose your soul. Wisdom is better than silver or gold." - Bob Marley

Your 3x3 int matrix should work just fine.

What specifically are you having trouble with? Some more detail on which parts were inefficient/not working would be helpful.

Hello to all my stalkers.

Advertisement

When I tried to acces matrix[3][2] and matrix[3][3], I've got an error such as accesing invalid memory..

"Don't gain the world and lose your soul. Wisdom is better than silver or gold." - Bob Marley

When I tried to acces matrix[3][2] and matrix[3][3], I've got an error such as accesing invalid memory..

Programmers start counting from zero, not 1.

To create a 3x3 matrix, we use [3][3], but to access the FIRST value, we use [0][0].

To get the 3rd column, 3rd row, we use [2][2].

Hello to all my stalkers.

When I tried to acces matrix[3][2] and matrix[3][3], I've got an error such as accesing invalid memory..

Programmers start counting from zero, not 1.

To create a 3x3 matrix, we use [3][3], but to access the FIRST value, we use [0][0].

To get the 3rd column, 3rd row, we use [2][2].

Modifying now..

Edit: Now its working and the matrix stores the values fine. Thankssmile.png . But it should work fine if I count from 1 too because the matrix is [3][3], right?

"Don't gain the world and lose your soul. Wisdom is better than silver or gold." - Bob Marley

When I tried to acces matrix[3][2] and matrix[3][3], I've got an error such as accesing invalid memory..

Programmers start counting from zero, not 1.

To create a 3x3 matrix, we use [3][3], but to access the FIRST value, we use [0][0].

To get the 3rd column, 3rd row, we use [2][2].

Modifying now..

Edit: Now its working and the matrix stores the values fine. Thankssmile.png . But it should work fine if I count from 1 too because the matrix is [3][3], right?

It is an inherent property of arrays (in all languages that I know of anyway) that indexing starts at 0, so if you have a 3x3 matrix, the last element in the matrix is [2][2].

Yo dawg, don't even trip.

Advertisement

When I tried to acces matrix[3][2] and matrix[3][3], I've got an error such as accesing invalid memory..

Programmers start counting from zero, not 1.

To create a 3x3 matrix, we use [3][3], but to access the FIRST value, we use [0][0].

To get the 3rd column, 3rd row, we use [2][2].

Modifying now..

Edit: Now its working and the matrix stores the values fine. Thankssmile.png . But it should work fine if I count from 1 too because the matrix is [3][3], right?

It is an inherent property of arrays (in all languages that I know of anyway) that indexing starts at 0, so if you have a 3x3 matrix, the last element in the matrix is [2][2].

Thank you bothsmile.png

"Don't gain the world and lose your soul. Wisdom is better than silver or gold." - Bob Marley

There are languages that index arrays defined for N elements in [1...N] (matlab comes to mind) or even [0...N] (Blitz Basic).

There is a good reason for the typical choice of [0...N-1], but it goes somewhat deep. You need to remember that the end result of the code is meant to run on actual hardware. In this case you want to access memory, but memory itself doesn't know the concept of an "array". It knows what data it holds and what index it's at. The most important thing is that an array is actually some index to the memory, and more precisely it's the index of the first member of the array. To access this, there is the typical array[j] syntax, which pretty much means "give me the j:th element of array 'array' " which is the same as finding the bit of memory that's at array+j. Since array is an index in the global memory and j is the index inside that array, we can just sum them to find the location of j in the memory.

Now, it (hopefully) makes sense to choose that the first element of "array" is at memory address "array", not at "array+1", so the first element should be at array[0] instead of array[1], since it gets translated to an actual memory address. Why it ends at N-1 instead of N is because we still want to have N elements - and if we count from 0 and go forwards until we have N elements, we reach element N-1.

This is, of course, only a chosen convention but it implies other nice things, like for(int i = 0; i<N; i++) /*operate on array; */ instead of for(int i = 1; i<N+1; i++) /*operate on array*/; the length of the for loop can instantly be seen from the ending condition instead of having to remember to take one out - it's a simple thing, but would cause (I believe) even more headache, especially for beginners or less enthusiastic programmers.

I know Lua uses one as the index. When I did my Tic-Tac-Toe game, I used an enum class of X, O, and Empty or so.

I started counting from 0 and it works now. The problem I am facing now is that I render when when the square in the XnO table is empty and when is the turn of player 1 but when is the turn of player 2 what was rendered befor disapears.. The problem is that Xs are rendered only when is player1 turn and Os only when is player2 turn. Maybe i should add a new condition?


//getting input 
if (mousex >= 140 && mousex <= 250 && mousey <= 200 && mousey >= 115 && hge->Input_GetKey() == HGEK_LBUTTON && table1 == true)
{
table1x = 190; table1y = 150; table1 = false; matrix[0][0] = 1; turn++;
}
if (mousex >= 250 && mousex <= 375 && mousey <= 200 && mousey >= 115 && hge->Input_GetKey() == HGEK_LBUTTON && table2 == true)
{
table2x = 315; table2y = 150; table2 = false; matrix[0][1] = 1; turn++;
}
.........
//rendering

if (table1 == false && player1 == true)
x_spr->Render(table1x, table1y);
else if (table1 == false && player2 == true)
o_spr->Render(table1x, table1y);
 
if (table2 == false && player1 == true)
x_spr->Render(table2x, table2y);
else if (table2 == false && player2 == true)
o_spr->Render(table2x, table2y);

"Don't gain the world and lose your soul. Wisdom is better than silver or gold." - Bob Marley

This topic is closed to new replies.

Advertisement