Thread: Board game in C

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    27

    Board game in C

    First of all i would like to apologize for being too vague in my prior requests for help regarding a randomly generated game board. I will try to be as precise as possible. Even then I am sorry if my question still ends up sounding vague because I have just started C and am not exactly sure how to explain myself better. So here goes,

    1.) I assigned the values to elements as follows: Ry=0/Rr=1/Cy=2/Cr=3

    2.) I then used time.h and the srand function along with two for loops to randomly fill an array with values from 0-3 and used if statements to print out the board as shown below.
    the result is something like this:

    ....1....2....3

    1 Ry Rr Cy

    2 Cy Rr Ry

    3 Rr Cr Rr

    In this game the objective is to get to all position of the board. A user may move from one element of the array ARRAY[1][1] to ARRAY[1][2] because the shape component (R) of the array is the same. The user may also move from ARRAY[1][1] to ARRAY[2][1] since the colour component (y) is the same. However since the board is randomly generated, there might not be a solution to the board(all possible paths that could lead the user to getting from one place on the board to every place.

    I am having a very hard time finding a way to check if there is a solution to the board. Someone even suggested that I use 8 'for' loops and i just don't know how that is possible. This is all I myself know about this game and I have been trying to logically put things into place. So I have four questions and I will try to be as clear as possible.

    1.) As i move from one position of the element to the other, is there a way to display an asterisk on the board to indicate my current position?

    2.) How should I try to check if there is a solution to the board? Below is a link to the actual game:

    clickmazes

    This should be even more helpful then my words to help get a picture of the actual game.

    Sorry for the long read. I have had three questions closed already and i have tried to learn from my mistakes. I know this isn't exactly easy to do for the helpers out there but I would appreciate any kind of help as I have no other options to look for help.

  2. #2
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    You can solve it using recursion. It's similar to towers of Hanoi. But honestly if you're asking how to display an asterisk on the board, you're not ready to solve this thing with recursion.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    27
    Quote Originally Posted by javaeyes View Post
    You can solve it using recursion. It's similar to towers of Hanoi. But honestly if you're asking how to display an asterisk on the board, you're not ready to solve this thing with recursion.
    How exactly am i supposed to display an asterisk on he board though?
    I mean if it was as simple as printing the asterisk on the board then i would have obviously been able to do it but i am printing the array using a for loop and i am finding it very hard to display an asterisk at my position on the board as i make my way through the board.

    Just a thought but would a pointer help in this case?
    would i be able to point to the element of the array and then append it by printing that element along with the pointer?
    I am trying to somehow make this into a function so that i could call it every time a make a move, hence shifting the pointer from one position to the other.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    As for drawing the asterisk:
    There is no way in standard C for you to draw one instance of the board and just move the asterisk around. To do something like that in the console/terminal, you would need a library like ncurses, or you would need to do some GUI programming. That's way too much work for a project like this, and probably not acceptable by the professor/TA. The "easy" solution is to draw a whole new board for every move you make. It's a lot of output, but will probably be useful for debugging anyway. As you print the board, your loop variables will iterate through the rows/columns of your board. When the loop variables match the row/column you are at, print an asterisk. You may choose to print this asterisk before the regular info for that grid location, after it, or in place of it, up to you. Just make sure the margins between grid locations have enough space for the asterisk and regular info if you want both to show up.

    As for checking for a solution, you need to keep track of which squares have been visited. If all squares are marked visited, then you have a solution. If the particular path you're trying did not lead to a solution, remember to clear the visited flag as you backtrack to try another route.

  5. #5
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    Show the code you have. What is your goal? To have a playable game, or to solve any given board programatically.
    I mean one simple way to do it is this: You already have a 3 x 3 array of the board. so create a 3 x 3 array of currentposition. set it to 1 if that's the position and 0 if it's not. When you loop to draw your board you have an if statement and you see if it's at the currentposition, if it is: print an asterisk.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You have to distinguish between the mechanics/logic of the game and the presentation of the game. The presentation could involve an ncurses interface, a GUI, a web interface, it could be done with a special 3D projector, etc. etc. This is totally and completely irrelevant to the logic of the game. The presentation requires a working logic engine/model, but that engine does not require any presentation to work. In other words, you must first determine how you are going to model the logic of the game, without thinking about it's presentation.

    For example, part of the model underlying a tic tac toe game might be a 3x3 matrix. Also, there are two players, who alternately input a move into the model, a position in the matrix. Part of the logic of the model is that after each move, it must be determined if there are three X's or O's lined up in the matrix, or if that is no longer possible, in which case the game is over.

    That does not require any presentation (except, of course, without presentation, no one can play the game). I mention this because it is not clear whether the purpose of your assignment is:

    A) to produce a playable game,
    B) to determine whether a randomly generated board is solvable,
    C) both A and B.

    If it is B, then the only presentation you have to do is draw the board once, and state whether it can be solved or not. You don't have to input moves, drag an asterisk around, etc. This is important because implementing gameplay is an unnecessary distraction WRT whether the board is solvable or not. Ie, if the task is B, you are wasting your time thinking about things (A) that do not matter.

    If the the task is C, you have to decide whether to tackle A first, or B first. Choose one, and forget about the other until the first part is done.

    Make sense?
    Last edited by MK27; 03-26-2012 at 05:55 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    27
    Quote Originally Posted by MK27 View Post
    You have to distinguish between the mechanics/logic of the game and the presentation of the game. The presentation could involve an ncurses interface, a GUI, a web interface, it could be done with a special 3D projector, etc. etc. This is totally and completely irrelevant to the logic of the game. The presentation requires a working logic engine/model, but that engine does not require any presentation to work. In other words, you must first determine how you are going to model the logic of the game, without thinking about it's presentation.

    For example, part of the model underlying a tic tac toe game might be a 3x3 matrix. Also, there are two players, who alternately input a move into the model, a position in the matrix. Part of the logic of the model is that after each move, it must be determined if there are three X's or O's lined up in the matrix, or if that is no longer possible, in which case the game is over.

    That does not require any presentation (except, of course, without presentation, no one can play the game). I mention this because it is not clear whether the purpose of your assignment is:

    A) to produce a playable game,
    B) to determine whether a randomly generated board is solvable,
    C) both A and B.

    If it is B, then the only presentation you have to do is draw the board once, and state whether it can be solved or not. You don't have to input moves, drag an asterisk around, etc. This is important because implementing gameplay is an unnecessary distraction WRT whether the board is solvable or not. Ie, if the task is B, you are wasting your time thinking about things (A) that do not matter.

    If the the task is C, you have to decide whether to tackle A first, or B first. Choose one, and forget about the other until the first part is done.

    Make sense?
    It makes a lot of sense but the fact of the matter is that i am panicking right now because other than the printing of the array(which took me like a week to figure out) i simply dont know how to find all possible solutions to the board right now which is my main goal. I mean, it is a randomly generated board! How can there be a way of checking if i can go to every position?

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sssk9797 View Post
    How can there be a way of checking if i can go to every position?
    If you've been given it as an assignment then there must be a way, lol.

    Think about the logic. It seems to me that the only way a board could not be solvable is if there is a position which cannot be moved to or from. Eg (I'm presuming diagonal moves are okay):

    Code:
    Ry Rr Cy
    
    Cy Cy Ry
    
    Rr Cy Rr
    The bottom left corner can't be connected to any other position, so the board is unsolvable. Meaning the simplest way to determine that is to go thru each position one at a time and evaluate it in relation to the surrounding positions.

    That's not a promise, but it makes sense to me at a glance . It's probably more complicated if you cannot move back to a position you have already been to...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    I like the fact you're trying to challenge yourself, but you have bitten off more than you can chew. You're still learning loops and arrays, and this calls for recursion, which is degrees of magnitude more complex. Don't try to do too much. Slow down and do some planning. It's not the end of the world if you need to withdraw or take another course. Lord knows there are a few Ws on my transcript. Relax, do your best. You break down a big problem into smaller ones and tackle 'em one by one.

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    27
    Quote Originally Posted by MK27 View Post
    If you've been given it as an assignment then there must be a way, lol.

    Think about the logic. It seems to me that the only way a board could not be solvable is if there is a position which cannot be moved to or from. Eg (I'm presuming diagonal moves are okay):

    Code:
    Ry Rr Cy
    
    Cy Cy Ry
    
    Rr Cy Rr
    The bottom left corner can't be connected to any other position, so the board is unsolvable. Meaning the simplest way to determine that is to go thru each position one at a time and evaluate it in relation to the surrounding positions.

    That's not a promise, but it makes sense to me at a glance . It's probably more complicated if you cannot move back to a position you have already been to...
    Probably is lol.
    There are a couple of things that i forgot to mention (sorry)
    1. diagonal moves aren't allowed.
    2. It is possible to go from, lets say 11 to 31. Which means as long as the move is within the row or column, it is possible. (I think this further complicates things.)

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    27
    Quote Originally Posted by javaeyes View Post
    I like the fact you're trying to challenge yourself, but you have bitten off more than you can chew. You're still learning loops and arrays, and this calls for recursion, which is degrees of magnitude more complex. Don't try to do too much. Slow down and do some planning. It's not the end of the world if you need to withdraw or take another course. Lord knows there are a few Ws on my transcript. Relax, do your best. You break down a big problem into smaller ones and tackle 'em one by one.
    This does seem quite complicated and i dont understand how someone can be asked to do so much with only 2.5 months of experience. Thanks for the support though!

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    javaeyes is mistaken that it needs recursion. It just needs a stack of your previous positions so that you can backtrack and a way to not make the same move again (that can just be to test moves in a certain order: e.g., up, right, down, left.

    I'm not sure if MK27 is correct. Obviously there may be a move from position A to position B (and therefore from B to A) without there being a way to get to either of them from somewhere else.

    The moves wrapping around doesn't really complicate things much. But overall it does seem a little tough for 2.5 months. Can you list your last few assignments so we can see what kind of things you know?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by oogabooga View Post
    Obviously there may be a move from position A to position B (and therefore from B to A) without there being a way to get to either of them from somewhere else.
    Good point. This would hopefully become apparent sometime during implementation or testing .
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Mar 2012
    Posts
    27
    Quote Originally Posted by oogabooga View Post
    javaeyes is mistaken that it needs recursion. It just needs a stack of your previous positions so that you can backtrack and a way to not make the same move again (that can just be to test moves in a certain order: e.g., up, right, down, left.


    I'm not sure if MK27 is correct. Obviously there may be a move from position A to position B (and therefore from B to A) without there being a way to get to either of them from somewhere else.

    The moves wrapping around doesn't really complicate things much. But overall it does seem a little tough for 2.5 months. Can you list your last few assignments so we can see what kind of things you know?
    Some of the assignments that we have had so far involve reading a file and printing it out on the screen, using loops to scan and print an array, creating tables for different values of input by applying functions, and printing out hello world!(first assignment).

  15. #15
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    Hmm. Maybe I'm not understanding the rules of the game. It just seems like this made for recursion. I guess what I had in mind was something like this:
    Code:
    getPossibleMoves( n , i , j)
    {
    if (n==9) return 1; //Solvable
    //determine legal moves, both literally according to game rules (no diagonals), and on a per board basis (same color)
    for each legal i,j couple
      {
       getPossibleMoves(n+1, i , j);
      }
    return 0; //Not solvable
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Board Game
    By Tiago in forum C Programming
    Replies: 4
    Last Post: 04-10-2010, 09:33 AM
  2. Tilting board game
    By vonxi in forum Game Programming
    Replies: 6
    Last Post: 02-18-2010, 01:06 PM
  3. Replies: 1
    Last Post: 09-04-2007, 05:31 AM
  4. Game Programming Board improvements
    By JoshG in forum Game Programming
    Replies: 8
    Last Post: 10-17-2002, 03:32 PM
  5. Board Game
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 08-17-2001, 12:29 PM