SFML community forums

Help => Graphics => Topic started by: omarking05 on May 14, 2013, 09:22:58 am

Title: questions about Dots game project
Post by: omarking05 on May 14, 2013, 09:22:58 am
Hey ,
im doing a dots game project and i have a lot of questions about using arrays and some logic of the game

1- how can i make an array of circles ? - to use it in drawing dots on the window -
i tried to use vertex array but i didn't find any solution !

rest of questions about the Dots project so anyone can Give me a help please don't stop it ^^

2- how can i do a multiplayer game logic ? in simple way

3- how can i write the logic code of making the score increases one if the one of the 2 users complete a full square ?

this is how i want my project to be like if you didn't understand because my bad English :D
http://www.addictinggames.com/puzzle-games/dots.jsp

thanks in advance
Title: Re: questions about Dots game project
Post by: eXpl0it3r on May 14, 2013, 09:56:54 am
1- how can i make an array of circles ? - to use it in drawing dots on the window -
I don't suggest to use an array of circles but use a vector of circles, e.g. std::vector<sf::CircleShape>

2- how can i do a multiplayer game logic ? in simple way
The most basic way for multiplayer, is to make it turn based and let the people share the same mouse. So you could simply display somewhere which player should now be using the mouse.
Later on you could always dive into network coding.

For an AI, you'll have to figure out an algorithm on how to chose what line next etc.

3- how can i write the logic code of making the score increases one if the one of the 2 users complete a full square ?
Have a variable somewhere for each player and whenever they make a squar increase it by one.

Btw you should keep in mind that programming is quite a hard thing and C++ is a very complex language. If you have problems with basic logical thing (i.e. increasing a variable or using arrays/vectors), then you're knowledge might not be ready for such a challenge and I'd suggest to learn more of the basics first. :)
Title: Re: questions about Dots game project
Post by: omarking05 on May 14, 2013, 10:14:02 am
thanks for reply ^^ .

in fact , yes maybe be my knowledge isn't enough for that project i reached till STL in C++ till now but what can i do its a first year college project and i have to do it :D.

the problem in Multiplayer issue that i use mouse instead of keyboard and i don't know what line would any of the 2 players choose ! , so i can't increase the score due to clicking on special texture or thing cuz i don't know what this thing would be !

and to be clear i want to make user share mouse and also take turns between User1 and User2 .

thanks again :))
Title: Re: questions about Dots game project
Post by: eXpl0it3r on May 14, 2013, 10:33:50 am
the problem in Multiplayer issue that i use mouse instead of keyboard and i don't know what line would any of the 2 players choose ! , so i can't increase the score due to clicking on special texture or thing cuz i don't know what this thing would be !
Well one part of programming is, to figure out the logic behind an idea.

I'd probably come up with something like:
(http://i.imgur.com/MDx2z2y.png)

The red areas are the areas you need to check whether the mouse is in or not. This check will be some basic math, so you should definitely figure that out on your own.
If the mouse is in that area and a click happens you can add a line between those to dots and depending on the player turn, increase the correct score variable.
Title: Re: questions about Dots game project
Post by: omarking05 on May 15, 2013, 03:35:05 am
I actually did that but i still can't find the logic that add 1 to one of 2 players score , that is the thing that i miss
Title: AW: questions about Dots game project
Post by: eXpl0it3r on May 15, 2013, 07:13:36 am
I don't understand where's the problem then... :-
if(closed_rectangle())
    if(turn==1)
        ++player1_score;
    else if(turn==2)
        ++player2_score;
Something like that? ???
Title: Re: questions about Dots game project
Post by: gostron on May 15, 2013, 07:33:25 am
I'd add brackets to avoid ambiguity, but it's the idea, ya
Title: Re: questions about Dots game project
Post by: omarking05 on May 16, 2013, 01:41:22 am
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 ^^
Title: Re: questions about Dots game project
Post by: gostron on May 16, 2013, 03:08:26 pm
when do you set false r1_key, r2_key, r3_key and r4_key ?
Title: Re: questions about Dots game project
Post by: omarking05 on May 16, 2013, 04:34:15 pm
aha , it is True all the game I see .
but if i set to False the lines would gone !!
Title: Re: questions about Dots game project
Post by: omarking05 on May 17, 2013, 03:31:33 am
Any help !

I set all the Keys (  r1_key, r2_key, r3_key and r4_key ) to false till the player click on the any of the 4 lines then it set one of the keys to true as in the code :

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;
                                }
                        }

and if all Keys are true so it is a full closed square , and i initialized bool variable turn where :
Turn = true >> First player turn
Turn = false >> second player turn
So if its closed square check if its player 1 turn or not as here :

if (r1_key && r2_key && r3_key && r4_key)
                        if (turn)
                                player1_score++;
                        else if (!turn)
                                player2_score++;

and here is a problem that makes the score is always increasing without stopping that i connected the drawing of the lines with the Turn variable to make it True again to let the player play .
as here :

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;
                }

Im really confused with a big pressure on , so any help i will be grateful .
Title: Re: questions about Dots game project
Post by: eigenbom on May 17, 2013, 04:37:04 am
Code: [Select]
--snip--
event.key.code==Mouse::Left
--snip--

This is wrong, check out the tutorials on handling events. You could also simplify your code by using arrays, e.g.,

Code: [Select]
const int NUM_RECTS = 4;
const sf::Color RECT_COLOURS[NUM_RECTS] = {sf::Color::Yellow, ....};
const sf::Vector2f RECT_POSITIONS[NUM_RECTS] = {sf::Vector2f(50,55), ....};
RectangleShape rectangles[NUM_RECTS];
for(int i=0;i<NUM_RECTS;i++){
  rectangles[i].setSize(Vector2f(10,100));
  rectangles[i].setFillColor(RECT_COLOURS[i]);
  rectangles[i].setPosition(RECT_POSITIONS[i]);
}

...

if (event.type==Event::MouseButtonPressed && event.mouseButton.button==sf::Mouse::Left)
{
  for(int i=0;i<NUM_RECTS;i++){
    if (rectangles[i].getGlobalBounds().contains(event.mouseButton.x,event.mouseButton.y)){
      // user clicked in i'th rectangle
      ....
    }
  }

Title: Re: questions about Dots game project
Post by: omarking05 on May 17, 2013, 07:23:08 pm
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;
}
Title: Re: questions about Dots game project
Post by: omarking05 on May 17, 2013, 07:28:42 pm
Also thanks for your advices , and any help to improve my code i will be grateful :)
Title: Re: questions about Dots game project
Post by: eigenbom on May 18, 2013, 03:52:39 am
There's a lot of stuff you can do to make this program clearer.

You should clean up your loop. You shouldn't do any drawing while processing window events, for example. (But you can combine events and the game update code for simplicity). It should look something like this:

Code: [Select]
while(window.isOpen()){
  // process window events..
  // update game state..
  window.clear();
  // do drawing..
  window.display();
}

You can make your game state clearer. Here's how I would do it...

Code: [Select]
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 ...
    }   
    else {
      detect which line we have clicked in ...
      then update the lines
      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
}

Also you should program the grid in a simple way with a typedef and functions for modifying or checking the grid. (This can be cleaner using OO, but I'll leave that as an exercise ;)):

Code: [Select]
//
// Here's a grid. It is 3x3.
// But the variable data is actually the lines in-between (of which there are 12)
// So we need two 2d-arrays, one for the horizontal lines and one for the vertical lines.
// +---+---+
// |   |   |
// +---+---+
// |   |   |
// +---+---+
//
const int GRID_WIDTH = 3;
const int GRID_HEIGHT = 3;
struct Grid {
  bool hlines[GRID_WIDTH-1][GRID_HEIGHT]; // 2 across, 3 down
  bool vlines[GRID_WIDTH][GRID_HEIGHT-1]; // 3 across, 2 down
};

// Grid modification and access functions
void GridSetEmpty(Grid& grid);
bool GridHasHLine(Grid& grid, int x, int y);
bool GridHasVLine(Grid& grid, int x, int y);
void GridSetHLine(Grid& grid, int x, int y, bool b);
void GridSetVLine(Grid& grid, int x, int y, bool b);

// Functions to detect mouse clicks
// This one will return true is mouseX, mouseY is in a horizontal line. And it will set the x,y parameters with the correct values if it is.
bool GridHLineAtMouse(Gird& grid, int mouseX, int mouseY, int& x, int& y);

// Similarly...
bool GridVLineAtMouse(Gird& grid, int mouseX, int mouseY, int& x, int& y);

main(){
  Grid grid; // The main grid (no global variables please!)
 

}

// function definitions way below main function
// or even better, in another file

void GridSetEmpty(Grid& grid){
  // set hlines
  for(int i=0;i<GRID_WIDTH-1;i++){
    for(int j=0;j<GRID_HEIGHT;j++){
      grid.hlines[i][j] = false;
    }
  }
  // set vlines empty
  // ...
}

bool GridHasHLine(Grid& grid, int x, int y){
  // Check x,y is in range
  if (y<0 ||y>=GRID_HEIGHT || x<0 || x>GRID_WIDTH){
    // something is wrong, indices are out of bounds
    std::cerr << "Error!";
  }
  else {
    return grid.hlines[x][y];
  } 
}

etc ...

HTH
Title: Re: questions about Dots game project
Post by: omarking05 on May 18, 2013, 07:34:51 am
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 :)
Title: Re: questions about Dots game project
Post by: eigenbom on May 18, 2013, 09:55:24 am
Remember to break up your event handling code from your drawing code. So..

Code: [Select]
enum GameState {NOT_PLAYING, PLAYER_ONE_TURN, PLAYER_TWO_TURN};
...
GameState gameState;
while(window.isOpen()){
  // EVENT LOOP
  poll event loop {
    if (event.type==Event::MouseButtonPressed && event.mouseButton.button==sf::Mouse::Left){
      if (gameState==NOT_PLAYING){
        // change the game state
        gameState = PLAYER_ONE_TURN;
      }
      else ...   
    }
  }

  // DRAWING
  window.clear();
  if (gameState==NOT_PLAYING){
    // draw the menu, and a message that says "click to start"
  }
  else {
     drawTheGrid();
     if (gameState==PLAYER_ONE_TURN){
        // draw the text "player one's turn"
     }
     else if (gameState==PLAYER_TWO_TURN){
        // draw the text "player two's turn"
     }
  }
}


As for detecting the mouse click... it would go something like this...

Code: [Select]
if (event.type==Event::MouseButtonPressed && event.mouseButton.button==sf::Mouse::Left){
  if (gameState==NOT_PLAYING){
    ...
  }   
  else {
    // detect which line we have clicked in
    int hx, hy, vx, vy;
    bool clickedOnHLine = GridHLineAtMouse(grid, event.mouse.x, event.mouse.y, hx, hy);
    bool clickedOnVLine = GridVLineAtMouse(grid, event.mouse.x, event.mouse.y, vx, vy);
    if (clickedOnHLine){
      // we clicked on horizontal line with index (hx,hy)
      bool isSet = GridHasHLine(grid, hx, hy);
      if (isSet){
         // then line is already set, so don't do anything
      }
      else {
         // line is not set, so we set it and update the score etc..
         GridSetHLine(grid,hx,hy,true);
         updateScore()...
      }     
    }
    else if (clickedOnVLine){
      // we clicked on vertical line at (vx,vy)
      ...
    }
}

Where the function GridHLineAtMouse takes a mouse coordinate and finds the index (hx,hy) of a horizontal line under the cursor (if any).

NOTE that in bool GridHasHLine(Grid& grid, int x, int y), the x,y refer to indices of the horizontal line array and NOT mouse coordinates.

The basic idea of any program is to split it up into understandable chunks or modules (like the Grid data structure and it's functions). And unless you spend time doing that you're going to end up with a huge mess of a program that is impossible to debug or understand. GL!
Title: Re: questions about Dots game project
Post by: omarking05 on May 19, 2013, 01:38:16 pm
sorry Dr im kinda confused with all of this functions , i think the problems are getting bigger and bigger .
yes I know that i should break up event from drawing but what if i need to mix them like if he closed the square set BOOL = true and if BOOL == true , then Draw(something) and Do event like score++ !!
the score here will getting bigger and bigger cuz BOOL will always be true !

what i understood from your reply :

if (gameState==NOT_PLAYING){
        // change the game state
        gameState = PLAYER_ONE_TURN;
      }

it will change game state to PLAYER_ONE_TURN and else it will Deal with the logic inside game

and here :
if (gameState==NOT_PLAYING){
    // draw the menu, and a message that says "click to start"
  }
  else {
     drawTheGrid();
     if (gameState==PLAYER_ONE_TURN){
        // draw the text "player one's turn"
     }
     else if (gameState==PLAYER_TWO_TURN){
        // draw the text "player two's turn"
     }

If it was player1 turn can I add 1 to his score HERE if he closed the square ?

what i don't understand bout bool GridHasHLine(Grid& grid, int x, int y) Function is why we are checking if there is a line or not !? , if the user himself MUST click on the line or nothing would happen ?!

in bool clickedOnHLine = GridHLineAtMouse(grid, event.mouse.x, event.mouse.y, hx, hy);

hx an hy are the position of the line ,right ?

i don't understand this function :(
 bool isSet = GridHasHLine(grid, hx, hy);
      if (isSet){
         // then line is already set, so don't do anything
      }
      else {
         // line is not set, so we set it and update the score etc..
         GridSetHLine(grid,hx,hy,true);
         updateScore()...
      }

Thanks for your time Dr.
Title: Re: questions about Dots game project
Post by: eigenbom on May 20, 2013, 01:03:12 am
I couldn't really understand your reply. Sorry. I'll try to answer what I think you were asking..

Quote
yes I know that i should break up event from drawing but what if i need to mix them like if he closed the square set BOOL = true and if BOOL == true , then Draw(something) and Do event like score++ !! the score here will getting bigger and bigger cuz BOOL will always be true !

Huh? When a player clicks the mouse, your game receives the mouse click event from SFML. You then take that event and update the game state (for example, score++). But this will only ever happen once for each mouse click. So the state of your game is updated only when SFML receives a mouse event.

Sometimes there will be no events at all, and so the code will just skip the events and re-draw the game.

Code: [Select]
main loop {
  event loop {
     handle events
     on mouse click -> detect what the player clicked in -> and update game state (score++ of whatever)
  }

  draw game
}

Quote
If it was player1 turn can I add 1 to his score HERE if he closed the square ?
Yep!

Do you understand the concept of a 2-D array? If not then you need to go learn some more about programming in general.

Code: [Select]

// In this code we check the coordinates of the mouse click event
// In this part we are ONLY checking if the mouse was clicked inside
// the rectangle corresponding to a horizontal line segment (whether
// or not the actual LINE exists or not)

// These variables will store the index into the 2D array
int hx, hy;

// This detects whether mouse.x,mouse.y is in the horizontal segment
// IF IT IS: return true, and set the variables hx,hy to the index INTO the horizontal line bool array
bool clickedOnHLine = GridHLineAtMouse(grid, event.mouse.x, event.mouse.y, hx, hy);
if (clickedOnHLine){
 // Ok, now we have clicked on a segment but we don't know if there is a line drawn there or not
 // So this line of code checks the horizontal line in the Grid to see if the line is there
 bool isSet = GridHasHLine(grid, hx, hy);
 if (isSet){
  // If the line is there, then a player has just clicked on an existing horizontal line
  // So we don't do anything
 }
 else {
  // Now we know that a horizontal line is not drawn at the place the player clicked
  // So we need to draw the line there, BUT
  // we don't draw here, so instead we just set a true flag in the horizontal line array (in the Grid)
  GridSetHLine(grid,hx,hy,true);

  // In this part of the code we will need an algorithm to detect whether the horizontal
  // line, that was just added, closed a loop or not. And if it did, then we increment the player's
  // score.
  updateScore()...
 }
}

I highly recommend making the program with just 9 dots while you work all these bugs out. Whenever you come across a problem: work through the program on paper. In other words, sit down with a pen and paper, write out the values of all the variables, then simulate one loop of the code in your head, updating the values of the variables on the paper.

That's all I can help with, as I've got to get back to my other work. Good luck.






Title: Re: questions about Dots game project
Post by: omarking05 on May 20, 2013, 01:30:56 pm
Okay ,I really appreciated thanks Dr ^^
Title: Re: questions about Dots game project
Post by: hiyogt on October 13, 2019, 10:40:22 pm
Hey ,
im doing a dots game project and i have a lot of questions about using arrays and some logic of the game

1- how can i make an array of circles ? - to use it in drawing dots on the window -
i tried to use vertex array but i didn't find any solution !

rest of questions about the Dots project so anyone can Give me a help please don't stop it ^^

2- how can i do a multiplayer game logic ? in simple way

3- how can i write the logic code of making the score increases one if the one of the 2 users complete a full square ?

this is how i want my project to be like if you didn't understand because my bad English :D
http://www.addictinggames.com/puzzle-games/dots.jsp

thanks in advance

Addicting games version is dead as flash is going the way of the dodo. Here is a modern html5 dots and boxes game (https://gametable.org/games/dots-and-boxes/) with a pretty good ai.
https://gametable.org/games/dots-and-boxes/ (https://gametable.org/games/dots-and-boxes/)