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

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

0 Members and 1 Guest are viewing this topic.

omarking05

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

eigenbom

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

omarking05

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

eigenbom

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







omarking05

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: questions about Dots game project
« Reply #19 on: May 20, 2013, 01:30:56 pm »
Okay ,I really appreciated thanks Dr ^^

hiyogt

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Re: questions about Dots game project
« Reply #20 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 with a pretty good ai.
https://gametable.org/games/dots-and-boxes/
« Last Edit: October 13, 2019, 10:42:15 pm by hiyogt »