CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2007
    Location
    France
    Posts
    329

    Help with chess game

    Im trying to make a chess game from scratch ( without AI ) and after a lot of
    googling and searching this site i couldnt find answers to some of my questions.

    1. I created three checkerboards with three different technics:
    - one with if() and else() statements
    - one with switches
    - one with 2D array.

    Now is there a most prefered way to go of these three ways? I dont wanna lose a lot of time by choosing one just to find out later that i should have chosen the other one.

    2. That must be a stupid question but i prefer to clarify it for me: is there a technic to change something on the board after it was drawn or i have to redraw it after every move (talking about consol program)?

    Thanks in advance!

  2. #2
    Join Date
    Mar 2009
    Location
    Granada, Spain
    Posts
    40

    Re: Help with chess game

    Quote Originally Posted by MasterDucky View Post
    Im trying to make a chess game from scratch ( without AI ) and after a lot of
    googling and searching this site i couldnt find answers to some of my questions.

    1. I created three checkerboards with three different technics:
    - one with if() and else() statements
    - one with switches
    - one with 2D array.

    Now is there a most prefered way to go of these three ways? I dont wanna lose a lot of time by choosing one just to find out later that i should have chosen the other one.
    I don't understant it. What is the checkerboard?

    Quote Originally Posted by MasterDucky
    2. That must be a stupid question but i prefer to clarify it for me: is there a technic to change something on the board after it was drawn or i have to redraw it after every move (talking about consol program)?
    There is no other way, you will have to redraw it.

  3. #3
    Join Date
    Jun 2007
    Location
    MA-USA
    Posts
    247

    Re: Help with chess game

    1: To render grid of alternating types i would just use a loop and check if(loop % 2 == 0)
    On a 32 bit system i think you can optimize by using if(loop & 1 == 0) instead of the mod 2.
    This really doesnt matter because we only need to draw it once anyway...
    Which leads us into part 2...

    2: There is no need to redraw the entire window just to change one character to another.
    What you want to do is move the caret to the desired location and change only that character.

    MSDN Refs:
    GetStandardHandle
    GetConsoleCaretPosition
    SetConsoleCaretPosition

  4. #4
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Help with chess game

    Is it Chess or Checkers that you're making?

    The array is the best way to go to create a board image. You draw the board by looping through the array and printing each element, then for each piece playing the game, you move the cursor (or caret as bitshifter suggested) to that piece's location and draw it's image (or character in console terms).
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  5. #5
    Join Date
    Dec 2007
    Location
    France
    Posts
    329

    Re: Help with chess game

    HTML Code:
     Is it Chess or Checkers that you're making?
    Its chess and i meant chessboard. Im sorry i thought it was the same.

    My only "problem" with the arrray method is that id like to do a bigger chessboard than just a few character wide and this is what i got as amount of code so far with only a two characters per square wide board.
    But maybe im on a wrong way to create my 2D array board?
    Its working perfectly well but just too small for me.

    With the switch() technic i created a 12 char wide board with less code than the array[][] method. So imagine the size of the code below 6x times larger.

    But then there is the problem to move the pieces where the array[][] version must be simpler as you stated.


    @bitshifter420

    That sounds like a fourth method to draw a chesboard. Interesting.

    So you would create only one square and than redraw it 64 times if i understand well.
    Probably this is what i should do with the array[][] method?

    Anyway good news that i dont have to redraw it every time. I should use SetConsoleCursorPosition(h, Pos) function i presume.


    Code:
    char board[18][26] = {{0,0,65,0,0,66,0,0,67,0,0,68,0,0,69,0,0,70,0,0,71,0,0,72,0,0}, //LETTERS
    {0,218,196,196,194,196,196,194,196,196,194,196,196,194,196,196,194,196,196,194,196,196,194,196,196,191},
    {49,179,82,107,179,75,116,179,66,115,179,81,6,179,75,5,179,66,115,179,75,116,179,82,107,179},
    {0,195,196,196,197,196,196,197,196,196,197,196,196,197,196,196,197,196,196,197,196,196,197,196,196,180},
    {50,179,80,110,179,80,110,179,80,110,179,80,110,179,80,110,179,80,110,179,80,110,179,80,110,179},
    {0,195,196,196,197,196,196,197,196,196,197,196,196,197,196,196,197,196,196,197,196,196,197,196,196,180},
    {51,179,0,0,179,0,0,179,0,0,179,0,0,179,0,0,179,0,0,179,0,0,179,0,0,179},
    {0,195,196,196,197,196,196,197,196,196,197,196,196,197,196,196,197,196,196,197,196,196,197,196,196,180},
    {52,179,0,0,179,0,0,179,0,0,179,0,0,179,0,0,179,0,0,179,0,0,179,0,0,179},
    {0,195,196,196,197,196,196,197,196,196,197,196,196,197,196,196,197,196,196,197,196,196,197,196,196,180},
    {53,179,0,0,179,0,0,179,0,0,179,0,0,179,0,0,179,0,0,179,0,0,179,0,0,179},
    {0,195,196,196,197,196,196,197,196,196,197,196,196,197,196,196,197,196,196,197,196,196,197,196,196,180},
    {54,179,0,0,179,0,0,179,0,0,179,0,0,179,0,0,179,0,0,179,0,0,179,0,0,179},
    {0,195,196,196,197,196,196,197,196,196,197,196,196,197,196,196,197,196,196,197,196,196,197,196,196,180},
    {55,179,80,110,179,80,110,179,80,110,179,80,110,179,80,110,179,80,110,179,80,110,179,80,110,179},
    {0,195,196,196,197,196,196,197,196,196,197,196,196,197,196,196,197,196,196,197,196,196,197,196,196,180},
    {56,179,82,107,179,75,116,179,66,115,179,81,6,179,75,5,179,66,115,179,75,116,179,82,107,179},
    {0,192,196,196,193,196,196,193,196,196,193,196,196,193,196,196,193,196,196,193,196,196,193,196,196,217}};
    Last edited by MasterDucky; March 23rd, 2009 at 12:12 AM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured