Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    6
    Thanked 0 Times in 0 Posts

    c++ replay implementation of my problem

    hello everyone,
    i am trying to implement save,load and replay in my chess game made in c++ console.
    i am done with save and load but stuck on replay.
    i want every turn to be stored in file and when player asks to replay it should show all turns in order they executed
    for this i want my read function to act in this way:
    -read first 64 characters(total number of components of chess arrary 8*8) and bring them to board array
    -then start from 64teh character to 128th character to show next state of game
    -then from 128th to next 64 characters and so on

    my problem is that is there any function in c++ that allows me to read from file from one point to other point (provided the points are defined by me )..

    ask any questions to clarify more..and plz help

  • #2
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    What you can do is create a binary file that stores states of the table. I will give you a small concept of how you can make things work.

    1) How to think of your board?
    It is a square. It is basically a two-dimensional array (RC-notation is important!):
    Code:
      0 1 2 3 4 5 6 7
    0
    1
    2
    3
    4
    5
    6
    7
    2) Who starts first?
    Again, we are working with a file, correct? A binary file at that. As such, the first bit of your file should be something that tells you who starts. Perhaps 1 means its black's turn, 0 is white turn first. Really, you don't NEED this bit if you can follow #3 (below).

    3) How to represent each move?
    Well notice how the board has numbers 0 to 7. We can represent that with 3 bits (and octal)! 000 is 0, 111 is 7!

    So let's say its black's move. Let's say black moves his 1-5 pawn forward one. We can represent it as such:
    001 101 002 101
    This can mean that the piece that location 1-5 has moved to location 2-5.

    Obviously after his move, it's white's move. We don't KNOW that it's white's move, but his piece is just moved. Here's an example of how the binary file will look if only three moves were made:
    110 011 101 011 // white moved pawn at 6-3 to 5-3
    001 101 010 101 // black moved pawn at 1-5 to 2-5
    101 011 100 011 // white moved pawn at 5-3 up 4-3
    ...
    Of course, in reality the file will look like this:
    Code:
    110011101011001101010101101011100011...
    4) What if one piece is supposed to devour another piece?
    Well, your program should be the that controls this; if say black's piece lands on a location that white's piece is currently on, that piece should be removed. Of course, the binary file doesn't need to tell you anything about that.

    5) But how do I know when it is over?
    Again, your program should control this. It should probably keep track of where the king has moved, and/or if the king has been 'devoured'.


    I hope this has helped you.

    P.S. There is no "write bit" method, so you may just want to use a char and ignore the MSB and the bit after the MSB. If you do it like this, it should be easier and the file should come out like this:
    Code:
     // take the bit and do: ((byte&0x38) >> 3) = row; (byte&0x07) = col
    00110011 00101011  // white moved pawn at 6-3 to 5-3
    00001101 00010101  // black moved pawn at 1-5 to 2-5
    00101011 00100011  // white moved pawn at 5-3 up 4-3
    Code:
    001100110010101100001101000101010010101100100011
    If you really wanted to reduce file size, you can probably create writeBit and readBit methods. Though, that may be more complicated for you.

    PPS if you are using a one-dimensional array, it isn't too much different. You would probably be able to represent the whole board with just 5 bits if you did (00000 is equal to idx 0, 11111 is idx 63).

    PPS look up istream & ostream references.
    Last edited by Apothem; 12-19-2011 at 10:04 AM.

  • #3
    New Coder
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    6
    Thanked 0 Times in 0 Posts
    i really appreciate you posted a long description but unfortunately my problem got unaddressed..
    actually i am not concerned about memory and row column phenomenon u r telling i m done with that ...
    i think u r explaining how to store moves in an array(chess board) but i want whole of array to be stored(it will be printed every time ,so that movement may be detected by comparing two arrays before and after player turn)

    i was asking for a function that allow me to start reading from a file(from a point defined by me) and same way end reading on a point described by me..


    2nd Question:is there a function that clears(empties ) the file ?

  • #4
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    By opening a file for outputting you are essentially clearing the file.

    I'm not completely sure how my explanation does not follow through what you are asking help for. What do you think storing the moves of a board really does? It's a compact version of storing a board. What each move does is enable you to follow through the Command Pattern. You want to detect the movements that occur between two boards after each turn, correct? What I have outlined for you does exactly this:
    1. Start out with a board with all pieces in appropriate places.
    2. Read first two bytes. Determine initial move location. This determines who is playing first. Move piece at specified location to its new location.
    -- If your program is logging this correctly, where the piece moves follows through the chess rules.
    3. Read next two bytes. It is obvious the other person's turn. Move their piece at the specified location to its new location.
    4. Read next two bytes. It is now the first player's turn again. Move their piece at the specified location to its new location.
    .
    .
    .
    k. Read next two bytes. It is now <x> player's turn. Move their piece at the specified location to its new location. Is there a collision? If so, the piece originally at the new location is removed and replaced by the piece to be moved.
    .
    .
    .
    n. Read next two bytes. It is now <y> player's turn. Move their piece at the specified location to its new location. Is there a collision? Is the colliding piece a king? If so, player <y> wins! (stop reading bytes)

    Here's an example (assume only three pieces remains in board; two kings and a pawn):
    Code:
    Let k = black king, K = white king, and p = black pawn
         BLACK
      0 1 2 3 4 5 6 7
    0
    1   K
    2           k
    3
    4   p
    5
    6
    7
         WHITE
    00 100 001
    00 101 001
    = black pawn moves forward
    piece = board[r][c];
    board[r][c] = null;
    board[new_r][new_c] = piece;
    Code:
    Let k = black king, K = white king, and p = black pawn
         BLACK
      0 1 2 3 4 5 6 7
    0
    1   K
    2           k
    3
    4
    5   p
    6
    7
         WHITE
    00 001 001
    00 001 000
    = White King moves left
    piece = board[r][c];
    board[r][c] = null;
    board[new_r][new_c] = piece;
    Code:
    Let k = black king, K = white king, and p = black pawn
         BLACK
      0 1 2 3 4 5 6 7
    0
    1 K
    2           k
    3
    4
    5   p
    6
    7
         WHITE
    ... and so on. What essentially happens is that you're storing the state of the board.

    I don't know how your program works. I don't have your code, nor do I request to see it. I am giving you an outline to solve a problem. If you do not understand how to integrate what I have outlined to your program, please tell me what exactly it is that is giving you a mental block.
    Last edited by Apothem; 12-19-2011 at 08:53 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •