1
General / Re: first program, help making grid
« on: January 23, 2015, 07:41:55 am »
Without having closely read your code, I think a problem is your use of the modulo ( % ) function. I'd read up on it, but basically what it does is return the remainder of the left side divided by the right side. So anything modulo 2 can return either 0 or 1.
Edit: seems I've misjudged the purpose of them in the code. Sorry, viewing from a mobile device.
Edit2: So yeah, I sat down and took a look at it. The problem is that your
You initialize it at (5, 5), and this never changes. Thus, the math using it
can only work once.
You add cellPos.x to cellSize.x, but for the third square in that row, you're doing the same calculation and thus getting the same coordinates.
That said, there are probably better ways to accomplish what you're trying to do, such as, say, setting the position of your desired square to (your grid[][] position * cellSize).
Example:
Edit: seems I've misjudged the purpose of them in the code. Sorry, viewing from a mobile device.
Edit2: So yeah, I sat down and took a look at it. The problem is that your
Code: [Select]
sf::Vector2f cellPos
is declared and initialized inside that for loop, and that it isn't actually changed.You initialize it at (5, 5), and this never changes. Thus, the math using it
Code: [Select]
grid[i][j].setPosition(cellPos.x + cellSize.x, cellPos.y + cellSize.y);
can only work once.
You add cellPos.x to cellSize.x, but for the third square in that row, you're doing the same calculation and thus getting the same coordinates.
That said, there are probably better ways to accomplish what you're trying to do, such as, say, setting the position of your desired square to (your grid[][] position * cellSize).
Example:
#include <SFML/Graphics.hpp>
int main()
{
int columns = 3;
int rows = 3;
sf::RenderWindow window(sf::VideoMode(800, 600), "TicTacToe");
sf::RectangleShape grid[columns][rows];
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
sf::Vector2f cellSize(200.0f, 200.0f);
for(int i=0;i<columns;i++){
for(int j=0;j<rows;j++){
grid[i][j].setSize(cellSize);
grid[i][j].setOutlineColor(sf::Color::Blue);
grid[i][j].setOutlineThickness(5.0f);
grid[i][j].setPosition(i*cellSize.x + 5.0f, j*cellSize.y + 5.0f);
window.draw(grid[i][j]);
}
}
window.display();
}
return 0;
}
int main()
{
int columns = 3;
int rows = 3;
sf::RenderWindow window(sf::VideoMode(800, 600), "TicTacToe");
sf::RectangleShape grid[columns][rows];
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
sf::Vector2f cellSize(200.0f, 200.0f);
for(int i=0;i<columns;i++){
for(int j=0;j<rows;j++){
grid[i][j].setSize(cellSize);
grid[i][j].setOutlineColor(sf::Color::Blue);
grid[i][j].setOutlineThickness(5.0f);
grid[i][j].setPosition(i*cellSize.x + 5.0f, j*cellSize.y + 5.0f);
window.draw(grid[i][j]);
}
}
window.display();
}
return 0;
}