okay , i did it but for 1 square till now but i encountered a prob , I ( cout ) the score but it keep increasing and increasing
here is my code :
#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
#include <SFML/Graphics/Vertex.hpp>
using namespace std;
using namespace sf;
const int SIZE=4;
void Set_Position(CircleShape circles[][SIZE])
{
int pos_x=50,pos_y=50;
for ( int i=0;i<SIZE;i++)
for (int j=0;j<SIZE;j++)
{
circles[i][j].setPosition(pos_x,pos_y);
circles[i][j].setRadius(5);
pos_x+=100;
if (j==SIZE-1)
{
pos_x=50;
pos_y+=100;
}
}
}
int main ()
{
Vector2i screenDimensions(400,400);
sf::RenderWindow Window;
Window.create(sf::VideoMode(screenDimensions.x,screenDimensions.y),"My First Sfml Game");
CircleShape circles[SIZE][SIZE];
Set_Position(circles);
//Lines
//Y
RectangleShape r1;
r1.setSize(Vector2f(10,100));
r1.setFillColor(Color::Blue);
r1.setPosition(50,55);
RectangleShape r1_copy;
r1_copy.setSize(Vector2f(10,100));
r1_copy.setFillColor(Color::Yellow);
r1_copy.setPosition(50,55);
RectangleShape r2;
r2.setSize(Vector2f(10,100));
r2.setFillColor(Color::Blue);
r2.setPosition(150,55);
RectangleShape r2_copy;
r2_copy.setSize(Vector2f(10,100));
r2_copy.setFillColor(Color::Yellow);
r2_copy.setPosition(150,55);
//X
RectangleShape r3;
r3.setSize(Vector2f(100,10));
r3.setFillColor(Color::Blue);
r3.setPosition(50,50);
RectangleShape r3_copy;
r3_copy.setSize(Vector2f(100,10));
r3_copy.setFillColor(Color::Yellow);
r3_copy.setPosition(55,50);
RectangleShape r4;
r4.setSize(Vector2f(100,10));
r4.setFillColor(Color::Blue);
r4.setPosition(50,150);
RectangleShape r4_copy;
r4_copy.setSize(Vector2f(100,10));
r4_copy.setFillColor(Color::Yellow);
r4_copy.setPosition(55,150);
bool r1_key=false,r2_key=false,r3_key=false,r4_key=false;
bool turn=true;
int mouse_x,mouse_y,player1_score=0,player2_score=0;
while (Window. isOpen())
{
sf::Event event;
while (Window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
Window.close();
break;
}
if (event.type==Event::MouseMoved)
{
mouse_x=event.mouseMove.x;
mouse_y=event.mouseMove.y;
}
if (event.type==Event::MouseButtonPressed)
{
if (event.key.code== Mouse::Left && r1_copy.getGlobalBounds().contains(mouse_x,mouse_y))
{
r1_key=true;
turn=false;
}
else if (event.key.code== Mouse::Left && r2_copy.getGlobalBounds().contains(mouse_x,mouse_y))
{
r2_key=true;
turn=false;
}
else if (event.key.code== Mouse::Left && r3_copy.getGlobalBounds().contains(mouse_x,mouse_y))
{
r3_key=true;
turn=false;
}
else if (event.key.code== Mouse::Left && r4_copy.getGlobalBounds().contains(mouse_x,mouse_y))
{
r4_key=true;
turn=false;
}
}
}
if (r1_key && r2_key && r3_key && r4_key)
if (turn)
player1_score++;
else if (!turn)
player2_score++;
cout<<player1_score<<" "<<player2_score<<endl;
Window.draw(r1_copy);
Window.draw(r2_copy);
Window.draw(r3_copy);
Window.draw(r4_copy);
if (r1_key)
{
Window.draw(r1);
turn=true;
}
if (r2_key)
{
Window.draw(r2);
turn=true;
}
if (r3_key)
{
Window.draw(r3);
turn=true;
}
if (r4_key)
{
Window.draw(r4);
turn=true;
}
//Drawing Dots
for (int i=0;i<SIZE;i++)
for (int j=0;j<SIZE;j++)
Window.draw(circles[i][j]);
Window.display();
Window.clear();
}
return 0;
}
Also here ( for more comfortable ) : http://www.ahmed-aly.com/f.jsp?i=XJtDLZVa
thanks for your time eXpl0it3r ^^
Here is the code after Editing it i made an array of lines initializing it with 48 divided into , 24 lines Colored with yellow that will user click on , and other 24 lines colored with red that will be drawn after user click .
so its the same problems plus it breaks when i try to draw the red line !!
Code :
#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
#include <algorithm>
#include <SFML/Graphics/Vertex.hpp>
using namespace std;
using namespace sf;
//Consts
const int Num_Rects = 48 ;
const int Num_Yellow = 24 , Num_Red = 48 ;
const Color Rect_Colors [Num_Rects] ;
bool Key_Flag[Num_Red] = {false};
void Intialize_Colors ( RectangleShape Rect_Colors[] )
{
for (int i=0 ; i<Num_Yellow ; i++)
Rect_Colors[i].setFillColor(Color::Yellow);
for (int i=Num_Yellow ; i<Num_Red ; i++)
Rect_Colors[i].setFillColor(Color::Red);
}
void Intialize_Position ( RectangleShape rects[] )
{
const Vector2f Rect_Positions [Num_Rects] = {
Vector2f(55,50) , Vector2f(155,50) , Vector2f(255,50)
, Vector2f(50,55) , Vector2f(150,55) , Vector2f(250,55) , Vector2f(350,55)
, Vector2f(55,150) , Vector2f(155,150) , Vector2f(255,150)
, Vector2f(50,155) , Vector2f(150,155) , Vector2f(250,155) , Vector2f(350,155)
, Vector2f(55,250) , Vector2f(155,250) , Vector2f(255,250)
, Vector2f(50,255) , Vector2f(150,255) , Vector2f(250,255) , Vector2f(350,255)
, Vector2f(55,350) , Vector2f(155,350) , Vector2f(255,350)
, Vector2f(55,50) , Vector2f(155,50) , Vector2f(255,50)
, Vector2f(50,55) , Vector2f(150,55) , Vector2f(250,55) , Vector2f(350,55)
, Vector2f(55,150) , Vector2f(155,150) , Vector2f(255,150)
, Vector2f(50,155) , Vector2f(150,155) , Vector2f(250,155) , Vector2f(350,155)
, Vector2f(55,250) , Vector2f(155,250) , Vector2f(255,250)
, Vector2f(50,255) , Vector2f(150,255) , Vector2f(250,255) , Vector2f(350,255)
, Vector2f(55,350) , Vector2f(155,350) , Vector2f(255,350)
};
for (int i=0 ; i<Num_Rects ; i++)
rects[i].setPosition(Rect_Positions[i]);
}
void Intialize_Size ( RectangleShape rects[] )
{
const Vector2f Lines_Size [Num_Rects] = {
Vector2f(100,10) , Vector2f(100,10) , Vector2f(100,10)
, Vector2f(10,100) , Vector2f(10,100) , Vector2f(10,100) , Vector2f(10,100)
, Vector2f(100,10) , Vector2f(100,10) , Vector2f(100,10)
, Vector2f(10,100) , Vector2f(10,100) , Vector2f(10,100) , Vector2f(10,100)
, Vector2f(100,10) , Vector2f(100,10) , Vector2f(100,10)
, Vector2f(10,100) , Vector2f(10,100) , Vector2f(10,100) , Vector2f(10,100)
, Vector2f(100,10) , Vector2f(100,10) , Vector2f(100,10)
, Vector2f(100,10) , Vector2f(100,10) , Vector2f(100,10)
, Vector2f(10,100) , Vector2f(10,100) , Vector2f(10,100) , Vector2f(10,100)
, Vector2f(100,10) , Vector2f(100,10) , Vector2f(100,10)
, Vector2f(10,100) , Vector2f(10,100) , Vector2f(10,100) , Vector2f(10,100)
, Vector2f(100,10) , Vector2f(100,10) , Vector2f(100,10)
, Vector2f(10,100) , Vector2f(10,100) , Vector2f(10,100) , Vector2f(10,100)
, Vector2f(100,10) , Vector2f(100,10) , Vector2f(100,10)
};
for (int i=0 ; i<Num_Rects ; i++)
rects[i].setSize(Lines_Size[i]);
}
void Set_Position(CircleShape circles[][Num_Rects])
{
int pos_x=50,pos_y=50;
for ( int i=0;i<Num_Rects;i++)
for (int j=0;j<Num_Rects;j++)
{
circles[i][j].setPosition(pos_x,pos_y);
circles[i][j].setRadius(5);
pos_x+=100;
if (j==Num_Rects-1)
{
pos_x=50;
pos_y+=100;
}
}
}
int main ()
{
bool r1_key=false,r2_key=false,r3_key=false,r4_key=false;
bool turn=true;
int player1_score=0,player2_score=0;
Vector2i screenDimensions(400,400);
sf::RenderWindow Window;
Window.create(sf::VideoMode(screenDimensions.x,screenDimensions.y),"The Fuckin DOTS Game");
CircleShape circles[Num_Rects][Num_Rects];
Set_Position(circles);
//Lines
RectangleShape rects [Num_Rects];
//INTIALIZING colors , positions and Sizes
Intialize_Colors(rects);
Intialize_Position(rects);
Intialize_Size(rects);
while (Window. isOpen())
{
sf::Event event;
while (Window.pollEvent(event))
{
if ( (event.type==Event::KeyPressed && event.key.code==Keyboard::Escape) || event.type == Event::Closed)
Window.close();
if (event.type==Event::MouseButtonPressed && event.mouseButton.button==sf::Mouse::Left)
{
for (int i=0 ; i<Num_Rects ; i++)
{
if (rects[i].getGlobalBounds().contains(event.mouseButton.x,event.mouseButton.y))
{
Key_Flag[i]=true;
turn=false;
Window.draw(rects[23+i]);
}
}
}
}
for (int i=0 ; i<Num_Rects ; i++)
{
if (Key_Flag[i] && Key_Flag[i+3] && Key_Flag[i+7] && Key_Flag[i+4])
if (turn)
player1_score++;
else if (!turn)
player2_score++;
}
//Drawing Lines
for (int i=0 ; i<Num_Rects/2 ; i++)
Window.draw(rects[i]);
//Drawing Dots
for (int i=0;i<Num_Rects;i++)
for (int j=0;j<Num_Rects;j++)
Window.draw(circles[i][j]);
Window.display();
Window.clear();
}
return 0;
}
Thanks Dr i really appreciate your time and help :)) .
although your reply was very helpful but im really confused , and got more questions
First
i think im rolling in circle without or like im taking a lot of time in this project although its easy except some bugs ,and im kinda approaching dead line so i need to build a working game as quickly as i can and then improve it if there is a time ,so i hope if there is any simple solution for this instead of lost into hard codes and reaching deadline .
but i can't deny the benefits i gained and gaining here ,and im making really use of it.
Second
questions about code (in comments) :
enum GameState {NOT_PLAYING, PLAYER_ONE_TURN, PLAYER_TWO_TURN};
...
GameState gameState;
while(window.isOpen()){
...
if (event.type==Event::MouseButtonPressed && event.mouseButton.button==sf::Mouse::Left){
if (gameState==NOT_PLAYING){
start the game ...
drawing grid and Displaying texts and menu right?
}
else {
detect which line we have clicked in
i will do that with 2 functions :
void GridSetHLine(Grid& grid, int x, int y, bool b);
void GridSetVLine(Grid& grid, int x, int y, bool b);
right?
then update the lines
Draw the line with another color that shows to the user that he has clicked on right?
numLoops= calculate all the loops we've just closed
if (gameState==PLAYER_ONE_TURN) playerOneScore += numLoops;
else if (gameState==PLAYER_TWO_TURN) playerTwoScore += numLoops;
detect if the grid is full, and if so go into GAME_FINISHED mode and show who won
}
}
...
draw everything
to draw anything i will set bool variable and using if and else i will draw it
}
so am i getting it right till now?
for the other code :
i got that Grid is kinda grid of bool beside the grid it drawn to the user ,and we use it to determine which line the user has clicked on.
void GridSetEmpty(Grid& grid);
so this function set all the grid to False till user click on any line it comes the turn of this two functions :
bool GridHasHLine(Grid& grid, int x, int y);
bool GridHasVLine(Grid& grid, int x, int y);
im really confusing at this ,but i think its a mouse position (x,y) ,but i can't understand why checking if the mouse position is in the range of the grid ? ,and also returning x and y as here :
else {
return grid.hlines[x][y];
}
confusing me cuz it cant be mouse position or it will be returning a huge integer for a grid.
any way this 2 functions will return some values that this two functions will take :
void GridSetHLine(Grid& grid, int x, int y, bool b);
void GridSetVLine(Grid& grid, int x, int y, bool b);
so this 2 functions will set the clicked line to True till it comes to the logic of GAME_FINISHED mode
so i hope i get most of it right :D ,and thanks for your time agian Dr :)