c++ chess game help

This is a discussion on c++ chess game help within the C++ Programming forums, part of the General Programming Boards category; hey, i am making a SIMPLE text based chess game and am having a problem when it gets to entering ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    12

    c++ chess game help

    hey, i am making a SIMPLE text based chess game and am having a problem when it gets to entering someones more... i don't know why, but here is my code:

    Code:
    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>
    #include <conio.h>
    
    // function prototypes
    void setup_board();
    void display_board();
    void set_coords();
    
    // global variables
    char piece_moved;
    char board[10][10];
    char choice;
    char *move;
    short x, y;
    short checkmate;
    
    int main()
    {  
      setup_board();
      display_board();
    
      while(!checkmate)
      {
        cout << "\nEnter Move From: ";
        cin >> move; // ex: b2
    
        set_coords();
    
        piece_moved = board[x][y];
    
        board[x][y] = ' ';
    
        cout << "Enter Move To: ";
        cin >> move; // ex: b4
    
        set_coords();
    
        board[x][y] = piece_moved;
      
        display_board();
      }
    
      system("PAUSE");
      return 0;
    }
    
    void setup_board()
    {
      // clear boxes
      for(x=0;x<10;x++)
      {
        for(y=0;y<10;y++)
        {
          board[x][y] = ' ';
        }
      }
      // color board
      for(x=0;x<8;x++)
      {
        for(y=0;y<8;y++)
        {
          board[x][y] = '-';
        }
      }
      // upper pieces
      board[0][0] = 'P';
      board[0][1] = 'P';
      board[0][2] = 'P';
      board[0][3] = 'P';
      board[0][4] = 'P';
      board[0][5] = 'P';
      board[0][6] = 'P';
      board[0][7] = 'P';
    
      board[1][0] = 'R';
      board[1][1] = 'N';
      board[1][2] = 'B';
      board[1][3] = 'Q';
      board[1][4] = 'K';
      board[1][5] = 'B';
      board[1][6] = 'N';
      board[1][7] = 'R';
    
      // lower piece  
      board[6][0] = 'P';
      board[6][1] = 'P';
      board[6][2] = 'P';
      board[6][3] = 'P';
      board[6][4] = 'P';
      board[6][5] = 'P';
      board[6][6] = 'P';
      board[6][7] = 'P';
    
      board[7][0] = 'R';
      board[7][1] = 'N';
      board[7][2] = 'B';
      board[7][3] = 'Q';
      board[7][4] = 'K';
      board[7][5] = 'B';
      board[7][6] = 'N';
      board[7][7] = 'R';
    
      // notation help
      board[7][9] = 'A';
      board[6][9] = 'B';
      board[5][9] = 'C';
      board[4][9] = 'D';
      board[3][9] = 'E';
      board[2][9] = 'F';
      board[1][9] = 'G';
      board[0][9] = 'H';
      
      board[9][0] = '1';
      board[9][1] = '2';
      board[9][2] = '3';
      board[9][3] = '4';
      board[9][4] = '5';
      board[9][5] = '6';
      board[9][6] = '7';
      board[9][7] = '8';
    }
    
    void display_board()
    {
      for(x=0;x<10;x++)
      {
        for(y=0;y<10;y++)
        {
          cout << board[x][y];
        }
        cout << endl;
      }
    }
    
    void set_coords()
    {
      // get x value from character
      if(strchr(move,'a'))
      {
        x = 7;
      }
      else
      {
        if(strchr(move,'b'))
        {
          x = 6;
        }
        else
        {
          if(strchr(move,'c'))
          {
            x = 5;
          }
          else
          {
            if(strchr(move,'d'))
            {
              x = 4;
            }
            else
            {
              if(strchr(move,'e'))
              {
                x = 3;
              }
              else
              {
                if(strchr(move,'f'))
                {
                  x = 2;
                }
                else
                {
                  if(strchr(move,'g'))
                  {
                    x = 1;
                  }
                  else
                  {
                    if(strchr(move,'h'))
                    {
                      x = 0;
                    }
                  }
                }
              }
            }
          }
        }
      }
      
      // get y
      if(strchr(move,'1'))
      {
        y = 0;
      }
      else
      {
        if(strchr(move,'2'))
        {
          y = 1;
        }
        else
        {
          if(strchr(move,'3'))
          {
            y = 2;
          }
          else
          {
            if(strchr(move,'4'))
            {
              y = 3;
            }
            else
            {
              if(strchr(move,'5'))
              {
                y = 4;
              }
              else
              {
                if(strchr(move,'6'))
                {
                  y = 5;
                }
                else
                {
                  if(strchr(move,'7'))
                  {
                    y = 6;
                  }
                  else
                  {
                    if(strchr(move,'8'))
                    {
                      y = 7;
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    if you can help... it would help me out a lot!

    thanks,
    JDM

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >char *move;
    This just declares a pointer, no space for the actual data. You can either use new to allocate some memory, or change this to:

    char move[5];

  3. #3
    Registered User cfrost's Avatar
    Join Date
    Apr 2004
    Posts
    119

    Angry Use OOp

    You are making your life hell with such bad approach use OOP for this it would be much easier ...

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    12
    hey thanks.. that worked...

    i was introduced to programming at the beginning of this year... i am in 10th grade and our class is moving really slow... we haven't been introduced to OOP yet... i will have programming classes all next year so for now, I guess I will have to through H***...

    what would be the best solution for this anyhow?

  5. #5
    jdm
    jdm is offline
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    For starters, you might change all those nested if...else statements into a switch block. I think that would make your code quite a bit simpler and more readable. I'm pretty new to C++ too, though...
    // Visual C++ 6.0... newbie with many questions!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,744
    Quote Originally Posted by jdm
    For starters, you might change all those nested if...else statements into a switch block. I think that would make your code quite a bit simpler and more readable. I'm pretty new to C++ too, though...
    I dont think that's possible here, since each if statement deals with a different conditional.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    just to clean up your code a bit, you could get rid of alot of the curlies:
    Code:
    void set_coords()
    {
      // get x value from character
      if(strchr(move,'a'))
        x = 7;
      else if(strchr(move,'b'))
        x = 6;
      else if(strchr(move,'c'))
        x = 5;
      else if(strchr(move,'d'))
        x = 4;
      else if(strchr(move,'e'))
        x = 3;
      else if(strchr(move,'f'))
        x = 2;
      else if(strchr(move,'g'))
        x = 1;
      else if(strchr(move,'h'))
        x = 0;
    ...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New Project, text game, design stage.
    By Shamino in forum Game Programming
    Replies: 9
    Last Post: 05-23-2007, 06:39 AM
  2. Game Programmer's AIM Circle: Join Today
    By KingZoolerius66 in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 12-20-2003, 11:12 AM
  3. IDEA: chess game
    By master5001 in forum Contests Board
    Replies: 30
    Last Post: 07-05-2003, 05:26 AM
  4. Chess is the coolest game
    By Jet_Master in forum A Brief History of Cprogramming.com
    Replies: 74
    Last Post: 06-07-2002, 10:29 AM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21