Thread: chess board

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    10

    chess board

    hi I need to make a chess board and I'm pretty stuck
    m gives the thickness of the square and n th size

    Code:
    #include <stdio.h>
    void main() {
    	int i,n,column,m,show,full_rows,k,b,flag=0,flag_b=0;
    	do {
    		printf("please enter frame size and width<frame size > 2*width>:");
    		scanf("%d",&n);
    		scanf("%d",&m);
    	}
    	while (n<=2*m||m<0); /*gives the conditions for the input of the program*/
    	for (i=0,k=0;k<23;i++,k++) {
    		for (column=0,b=0;b<79;column++,b++){
    			if (column==n){
    				column=0;  /*restarts the building of the square*/
    				flag+=1;
    			}
    			if (i==n)
    				if ((n+1)%2)
    					flag_b+=1;
    				else 
    					flag_b=0;
    			
    				show=m>column;
    				show=show||m+column>=n; /*this are the conditions to create a squre with the thicknes(m) in the columns that is needed*/
    				if(show)
    					printf("*");
    				else {                                            
    					full_rows=m>i;
    					full_rows=full_rows||m+i>=n;                         
    					if (full_rows)               /*the condition to create thickness(m) in the rows that is needed*/
    						printf("*");
    					else
    						printf(" ");            /*creates a blank in the spots that do not fullfill the conditions */
    				}
    
    		
    			if(b==78)
    				printf("\n");
    			i=i%n;
    		}	
    	}
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    while (n<=2*m||m<0);
    This does nothing.

    Furthermore, don't use void main; use it main.
    And finally, state your problem and what you need help with.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Chess boards are two dimensional items - they have a length and a width. You can call that a height and a width if you want.

    They have no "thickness" whatsoever.

    This is not a perfect console board, as you'll notice that the pieces (represented by their values here), are not quite in their correct row, inside the square.

    The board is basically good though, allowing the piece to be centered (the piece should be either one or three columns wide).

    If your compiler can't handle conio.h, you'll be stuck with the gotoxy(), let me know and I'll set it up for Windows SetConsoleCursorPosition() for you.

    Code:
    //this program just displays an 8 x 8 board
    
    
    #include <stdio.h>
    #include <conio.h>
    
    int left;            //left edge of board
    int right;          //right edge of board
    int width;        //width of square in cols
    int top;	      //top row
    int bottom;     //bottom row
    int row;
    int col;
    int board[8][10]; 	//the current playing board-it is 10 columns wide to allow the 
    //use of pairs of digits for representing the board in this array see page 159 in 
    //How Computers Play Chess
    int number;
    
    int main(void)  { 
      void Newboard();
      int i;
    
      left=2;               //left edge of board
      right=left+40;     //right edge of board
      width=5;            //width of square in cols
      top=2;	           //top row
      bottom=top+16; //bottom row
    
       Newboard();
       i = getchar(); ++i;
       return 0;
     }
    void Newboard() { 
      void Drawsq(char sqr, int col, int row);
      void Putpieces();
      const unsigned char bsqr = '\xB0';  	//black square
      const unsigned char wsqr = '\xB2';	//white square
      unsigned char sqr= wsqr;
      int j, color = 0;
    
      for(row=0;row<8;row++) { 
        for(col=0;col<10;col++) 
    	    board[row][col] = 0;
          if(row==0) { 
            board[row][0]=4;board[row][1]=2;board[row][2]=3;
            board[row][3]=5;board[row][4]=6;board[row][5]=3;
            board[row][6]=2;board[row][7]=4;board[row][8]=99;
            board[row][9]=99; 
          }
          if(row==1) { 
            board[row][0]=1;board[row][1]=1;board[row][2]=1;
            board[row][3]=1;board[row][4]=1;board[row][5]=1;
            board[row][6]=1;board[row][7]=1;board[row][8]=99;
            board[row][9]=99; 
          }   
          if(row==6) { 
            board[row][0]=-1;board[row][1]=-1;board[row][2]=-1;
            board[row][3]=-1;board[row][4]=-1;board[row][5]=-1;
            board[row][6]=-1;board[row][7]=-1;board[row][8]=99;
            board[row][9]=99; 
          }   
          if(row==7) { 
            board[row][0]=-4;board[row][1]=-2;board[row][2]=-3;
            board[row][3]=-5;board[row][4]=-6;board[row][5]=-3;
            board[row][6]=-2;board[row][7]=-4;board[row][8]=99;
            board[row][9]=99; 
          }
      } //end of for row (for col is not braced)
      clrscr();
      for(row=top;row<bottom;row+=2) { 
        if (color>0) {	//swap color of sqr every row
         color=0; 
         if(sqr==wsqr) 
           sqr=bsqr; 
         else 
           sqr=wsqr; 
        } 
        for(col=left;col<right;col+=5) { 
          gotoxy(col,row);
          Drawsq(sqr, col, row);
          color ++;
    	    if (color>0) { 
            color=0; 
    	      if(sqr==wsqr) 		//swap color of sqr every col too
    	        sqr=bsqr; 
    	      else 
    	        sqr=wsqr;
    	    }//end of if 
    	  } //end of for col
        color ++;
      } //end of for row
      Putpieces();
    } 
    void Drawsq(char sqr, int col, int row)  { 
      int j;
      gotoxy(col, row);  	            //top half of sqr
      for(j=0; j<5; j++)
        putch(sqr); 	                   //each square is 5 cols X 2 rows
      gotoxy(col,row+1);
      for(j=0; j<5; j++)		//lower half of sqr
        putch(sqr); 	               
       
       number++;
       gotoxy(30,20);
         printf("Been Here %d Times", number);
      }
    void Putpieces()  { 
      int j, k, midrow, midcol;
      midrow=bottom-2;
      midcol=left+2;
      for(k=0;k<8;k++) { 
        for(j=0;j<8;j++) { 
          gotoxy(midcol,midrow);
        	if(board[k][j]!=0)  
            printf("%d",board[k][j]);
        	midcol+=width;
        }
        midcol=midcol-(8*width);
        midrow-=2;
      }
    }
    If you want a much more basic type of chessboard, let me know.

    You'll notice that the board isn't "square" any more. And the height of each square is just two char's.

    Change that to 3 char's, and adjust your font until it is square. This board is perfectly square with a much smaller font size, but that's impractical with modern monitors. To change your font size on the console window, put the mouse pointer on the top bar of the console window, and right click on it. Then choose "properties" and "fonts". Save it when you get it right, as the default for the console window.

    You can also change your console window by using the Windows API.



    This is what it looks like:
    Last edited by Adak; 08-15-2010 at 11:26 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDL Chess Game problem
    By Lesshardtofind in forum Game Programming
    Replies: 0
    Last Post: 07-02-2010, 04:49 PM
  2. Bishops on the chess board, I need help
    By LDi in forum C Programming
    Replies: 4
    Last Post: 03-08-2008, 11:24 PM
  3. Constructor problem
    By rebel in forum C++ Programming
    Replies: 22
    Last Post: 01-11-2006, 06:45 AM
  4. function trouble
    By rebel in forum C++ Programming
    Replies: 4
    Last Post: 12-21-2005, 05:23 AM
  5. Pick a number....
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 39
    Last Post: 01-19-2003, 07:27 AM