Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: questions about Dots game project  (Read 9540 times)

0 Members and 1 Guest are viewing this topic.

omarking05

  • Newbie
  • *
  • Posts: 11
    • View Profile
questions about Dots game project
« 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: questions about Dots game project
« Reply #1 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. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

omarking05

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: questions about Dots game project
« Reply #2 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 :))

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: questions about Dots game project
« Reply #3 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:


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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

omarking05

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: questions about Dots game project
« Reply #4 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
AW: questions about Dots game project
« Reply #5 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? ???
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

gostron

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: questions about Dots game project
« Reply #6 on: May 15, 2013, 07:33:25 am »
I'd add brackets to avoid ambiguity, but it's the idea, ya

omarking05

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: questions about Dots game project
« Reply #7 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 ^^

gostron

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: questions about Dots game project
« Reply #8 on: May 16, 2013, 03:08:26 pm »
when do you set false r1_key, r2_key, r3_key and r4_key ?

omarking05

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: questions about Dots game project
« Reply #9 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 !!

omarking05

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: questions about Dots game project
« Reply #10 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 .

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: questions about Dots game project
« Reply #11 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
      ....
    }
  }


omarking05

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: questions about Dots game project
« Reply #12 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;
}

omarking05

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: questions about Dots game project
« Reply #13 on: May 17, 2013, 07:28:42 pm »
Also thanks for your advices , and any help to improve my code i will be grateful :)

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: questions about Dots game project
« Reply #14 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

 

anything