Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 23rd, 2013, 3:20 AM   #1
Adak
Hobby Coder
 
Join Date: May 2006
Posts: 1,181
Rep Power: 12 Adak will become famous soon enough
Easy 2D game board display (Console)

This shows two ways to move a game piece, on a 2D board

*the simple way, where the entire board is displayed after every move (which can cause scrolling and flickering of the screen).

and

*the better way, where only the piece squares involved, are reprinted.

This is C, for Windows, with a simple console chessboard example.

//simple chessboard display and movement 

#include <stdio.h>
#include <windows.h>

#define ROWS 8
#define COLS 8
#define WIDTH 3
#define HEIGHT 3
#define Empty 32

#pragma warn(disable: 2237)

typedef struct move {
   int row;
   int col;
}Move;

typedef struct COORD {
   int x;
   int y;
}Point;


void Gotoxy(int x, int y); 
void makeMove(char board[ROWS][COLS], Move from, Move to);
void printBoard(char board[ROWS][COLS]);
void printMove(char board[ROWS][COLS], Move from, Move to); 


int main(void) {
      
   char board[8][8]=
   {"RNBQKBNR",        //uppercase pieces are White
    "PPPPPPPP",
    "        ",
    "        ",
    "        ",
    "        ",
    "pppppppp",        //lowercase pieces are Black
    "rnbqkbnr"};
   
   Move from,to;       //sqr's involved in the move 
   
   printBoard(board);
   Sleep(4000);        //reading delay of 5 seconds 

   printf("I move first, with pawn to e4\n");
   from.row=1; from.col=4;
   to.row=3; to.col=4;
   
   printMove(board,from,to);
   makeMove(board,from,to);
   
   //printBoard(board);
   Sleep(4000);

   printf("And my opponent chooses the Sicilian Defense, with Knight to c6\n");
   from.row=7; from.col=1;
   to.row=5; to.col=2;
   printMove(board,from,to);
   makeMove(board,from,to);
   //printBoard(board);
   Sleep(5000);
   
   printf("Just as I thought he would. Now to spring my trap! <suppressed evil grin> \n");

   printf("\n");
   return 0;
}
//moves the cursor in the console window
void Gotoxy(int x, int y) {
   COORD point;
   point.X = x;
   point.Y = y;
   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), point);
}
//makes the moves
void makeMove(char board[ROWS][COLS], Move from, Move to) {
   board[to.row][to.col]=board[from.row][from.col];
   board[from.row][from.col]=Empty;
}
//shows the current board status by only printing the updated board
void printMove(char board[ROWS][COLS], Move from, Move to) {
   int ylookup[]={18,16,14,12,10,8,6,4};
   int xlookup[]={4,6,8,10,12,14,16,18};
   
   /* use this to set up xlookup and ylookup easily
      when it prints a perfect diagonal from white's
      to black's side of the board, it's OK.
   *//*
   for(int i=0;i<8;i++) {
      Gotoxy(xlookup[i],ylookup[i]);
      printf("%d",i);
   } 
   getchar();
   */
     
   int x=xlookup[from.col]; 
   int y=ylookup[from.row];
   
   Gotoxy(x,y);
   putchar(Empty);
   x=xlookup[to.col];
   y=ylookup[to.row];
   Gotoxy(x,y);
   putchar(board[from.row][from.col]);
   Gotoxy(3,22);    //this is "home" always return here
}
//shows the current board status by simply printing the entire board anew
void printBoard(char board[ROWS][COLS])	{
   int r, c;
   Gotoxy(3,2);  //keeps only one board visible, no scrolling
   printf("       Black \n");    
   printf("   -----------------");
   
   for(r=7;r>-1;r--)	{    //print the rows in descending order
      printf("\n   ");    //for White's side on the bottom
      for(c=0;c<8;c++) {  //of the screen.
         printf("|%c",board[r][c]);
      }
      printf("|\n   -----------------");
   }
   printf("\n          White\n");
   Gotoxy(3,22);   //this is "home"
}

The x and y lookup tables above, make any 2D display, a LOT easier to design and code.
Adak is offline   Reply With Quote
Old Mar 23rd, 2013, 8:29 PM   #2
Adak
Hobby Coder
 
Join Date: May 2006
Posts: 1,181
Rep Power: 12 Adak will become famous soon enough
Re: Easy 2D game board display (Console)

Improved version of the above:
#include <stdio.h>
#include <windows.h>

#define ROWS 8
#define COLS 8
#define Empty 32
#define MoveMargin 26

#pragma warn(disable: 2237)

typedef struct move {
   int row;
   int col;
}Move;

typedef struct COORD {
   int x;
   int y;
}Point;

void Gotoxy(int x,int y); 
void makeMove(char board[ROWS][COLS],Move from,Move to);
void printBoard(char board[ROWS][COLS]);
void printMove(char board[ROWS][COLS],Move from,Move to,int ply,int moveNum); 


int main(void) {
   int moveNum=0,ply=0;   
   char board[ROWS][COLS]=
   {"RNBQKBNR",        //uppercase pieces are White
    "PPPPPPPP",
    "        ",
    "        ",
    "        ",
    "        ",
    "pppppppp",        //lowercase pieces are Black
    "rnbqkbnr"};
   
   Move from,to;       //squars involved in the move 
   
   printBoard(board);  //print entire board.  
   Sleep(4000);        //reading delay of 4 seconds 

   printf("White whispers:\n     I move first, with pawn to e4\n");
   from.row=1; from.col=4;
   to.row=3; to.col=4;
   ++ply; ++moveNum;
   
   printMove(board,from,to,ply,moveNum); //only print from and to squares
   makeMove(board,from,to);
   
   Sleep(5000);

   printf("White whispers:\n     And my opponent chooses the Sicilian Defense, with Knight to c6\n");
   from.row=7; from.col=1;
   to.row=5; to.col=2;
   ++ply; 

   printMove(board,from,to,ply,moveNum);
   makeMove(board,from,to);
   
   Sleep(7000);
   
   printf("White Whispers:\n     Just as I thought he would. Now to spring my Queen side trap!      \n");
   Sleep(7000);
   Gotoxy(3,21);
   printf("Black Whispers:\n     Wonder if White knows that I can read his whispers? <grin>           \n");
   Sleep(7000);
   return 0;
}
//moves the cursor in the console window
void Gotoxy(int x, int y) {
   COORD point;
   point.X = x;
   point.Y = y;
   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), point);
}
//makes the moves, call only after printMove
void makeMove(char board[ROWS][COLS], Move from, Move to) {
   board[to.row][to.col]=board[from.row][from.col];
   board[from.row][from.col]=Empty;

}
//prints the from and to squares on the board
void printMove(char board[ROWS][COLS], Move from, Move to, int ply, int moveNum) {
   int ylookup[]={17,15,13,11,9,7,5,3};
   int xlookup[]={4,6,8,10,12,14,16,18}; 
   
   /* use this to set up xlookup and ylookup easily.
      When it prints a perfect diagonal from white's
      to black's side of the board, it's OK.
   *//*
   for(int i=0;i<8;i++) {
      Gotoxy(xlookup[i],ylookup[i]);
      printf("%d",i);
   } 
   getchar();
   */
     
   int x=xlookup[from.col]; 
   int y=ylookup[from.row];
   int column;

   Gotoxy(x,y);
   putchar(Empty);
   x=xlookup[to.col];
   y=ylookup[to.row];
   Gotoxy(x,y);
   putchar(board[from.row][from.col]);
   
   
   if(ply%2==1)
      column=MoveMargin;
   else
      column=MoveMargin+10;

   Gotoxy(column,moveNum+3);
   printf("%d. %c%c%d",moveNum,board[from.row][from.col],to.col+'a',to.row+1); 
   
   Gotoxy(3,21);    //this is "home" always return here

}
//shows the current board status by simply printing the entire board anew
void printBoard(char board[ROWS][COLS])	{
	int r, c, row=ROWS;
   char *hEdge={"-----------------"}; //horizontal edge of board 196 ascii
    
	Gotoxy(26,2); printf("White     Black");    

   Gotoxy(3,2);   //keeps only one board visible, no scrolling
   printf("%s",hEdge);
   
	for(r=7;r>-1;r--)	{         //print the rows in descending order
		printf("\n %d ",row--);    //for White's side on the bottom
		for(c=0;c<8;c++) {         //of the screen.
			printf("%c%c",179,board[r][c]);
		}
      printf("%c \n   %s",179,hEdge);
	}
   printf("\n    a b c d e f g h\n");
   Gotoxy(3,21);         //back to "home"
}

The focus shouldn't be on the chess display, (although this one is better), but on the way the xlookup[] and ylookup[] tables work, in printMove().
Adak is offline   Reply With Quote
Old Mar 23rd, 2013, 8:41 PM   #3
Smjprogrammer
Learning Very Basic Stuff
 
Smjprogrammer's Avatar
 
Join Date: Jan 2010
Location: United States
Posts: 978
Rep Power: 8 Smjprogrammer is on a distinguished road
Re: Easy 2D game board display (Console)

I wasn’t able to get either one to work. What gives?
__________________
HP Laptop / AMD A4 - 3305 APU with Radeon HD Graphics 1.90 Ghz
Windows 10 64-Bit
Visual Studio Community 2015
Recommended Books - C++ All-In-One For Dummies / Webster's Computer Dictionary
Smjprogrammer is offline   Reply With Quote
Old Mar 23rd, 2013, 9:18 PM   #4
Adak
Hobby Coder
 
Join Date: May 2006
Posts: 1,181
Rep Power: 12 Adak will become famous soon enough
Re: Easy 2D game board display (Console)

You need to use:

1) A Windows PC

2) A C compiler. I have no idea what a C++ compiler might do with it, but probably choke on it.

3) You may need to click on Project Options -> compiler - then check the "enable Microsoft Extensions, box, so Sleep() will compile/link.


What errors are you seeing?

chessboardSimpleConsole.PNG
Adak is offline   Reply With Quote
Old Mar 23rd, 2013, 10:12 PM   #5
Adak
Hobby Coder
 
Join Date: May 2006
Posts: 1,181
Rep Power: 12 Adak will become famous soon enough
Re: Easy 2D game board display (Console)

If you save the source code file with a dot c extension, then most C++/C compiler IDE's will compile it with the C compiler, instead of using the cpp compiler.

So sourcefileName.c, not sourcefileName.cpp.
Adak is offline   Reply With Quote
Old Mar 24th, 2013, 6:31 AM   #6
Smjprogrammer
Learning Very Basic Stuff
 
Smjprogrammer's Avatar
 
Join Date: Jan 2010
Location: United States
Posts: 978
Rep Power: 8 Smjprogrammer is on a distinguished road
Re: Easy 2D game board display (Console)

1>------ Build started: Project: C_C++, Configuration: Debug Win32 ------
1> C_C++.cpp
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(9): warning C4068: unknown pragma
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(16): error C2371: 'COORD' : redefinition; different basic types
1> c:\program files (x86)\microsoft sdks\windows\v7.0a\include\wincon.h(38) : see declaration of 'COORD'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(30): error C2117: 'board' : array bounds overflow
1> c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(29) : see declaration of 'board'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(30): error C2117: 'board' : array bounds overflow
1> c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(29) : see declaration of 'board'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(30): error C2078: too many initializers
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(31): error C2117: 'board' : array bounds overflow
1> c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(29) : see declaration of 'board'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(31): error C2117: 'board' : array bounds overflow
1> c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(29) : see declaration of 'board'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(32): error C2117: 'board' : array bounds overflow
1> c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(29) : see declaration of 'board'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(32): error C2117: 'board' : array bounds overflow
1> c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(29) : see declaration of 'board'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(33): error C2117: 'board' : array bounds overflow
1> c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(29) : see declaration of 'board'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(33): error C2117: 'board' : array bounds overflow
1> c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(29) : see declaration of 'board'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(34): error C2117: 'board' : array bounds overflow
1> c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(29) : see declaration of 'board'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(34): error C2117: 'board' : array bounds overflow
1> c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(29) : see declaration of 'board'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(35): error C2117: 'board' : array bounds overflow
1> c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(29) : see declaration of 'board'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(35): error C2117: 'board' : array bounds overflow
1> c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(29) : see declaration of 'board'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(36): error C2117: 'board' : array bounds overflow
1> c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(29) : see declaration of 'board'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(36): error C2117: 'board' : array bounds overflow
1> c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(29) : see declaration of 'board'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(37): error C2117: 'board' : array bounds overflow
1> c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(29) : see declaration of 'board'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(37): error C2117: 'board' : array bounds overflow
1> c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(29) : see declaration of 'board'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(74): error C2039: 'X' : is not a member of 'COORD'
1> c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(16) : see declaration of 'COORD'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(75): error C2039: 'Y' : is not a member of 'COORD'
1> c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(16) : see declaration of 'COORD'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c_c++\c_c++\c_c++.cpp(76): error C2664: 'SetConsoleCursorPosition' : cannot convert parameter 2 from 'COORD' to 'COORD'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
__________________
HP Laptop / AMD A4 - 3305 APU with Radeon HD Graphics 1.90 Ghz
Windows 10 64-Bit
Visual Studio Community 2015
Recommended Books - C++ All-In-One For Dummies / Webster's Computer Dictionary
Smjprogrammer is offline   Reply With Quote
Old Mar 24th, 2013, 7:19 AM   #7
Adak
Hobby Coder
 
Join Date: May 2006
Posts: 1,181
Rep Power: 12 Adak will become famous soon enough
Re: Easy 2D game board display (Console)

What is the filename extension to your source file? Looks like it's dot cpp, and that won't work. It has to be dot.c to even hope to get the C compiler used on it.

Add the word "short" to x and y, in this declaration of COORD

typedef struct COORD {
   short int x;
   short int y;
}Point;

Would give x and y a more correct data type.

You can REM out the #pragma entirely: /* #pragma ... etc. */, you won't need it, apparently.

You should put this in it's place,
#pragma warn(disable:2117) /* array bounds overflow */

That is being given because the program filled up all 8 chars in the board array rows - so it gives you one for every sub array (row). There is no room for the end of string char. But those chars are pieces in a game - and will never be treated like strings.

Last edited by Adak; Mar 24th, 2013 at 7:43 AM.
Adak is offline   Reply With Quote
Old Mar 24th, 2013, 7:55 AM   #8
Adak
Hobby Coder
 
Join Date: May 2006
Posts: 1,181
Rep Power: 12 Adak will become famous soon enough
Re: Easy 2D game board display (Console)

COORD is declared in a windows file, so my program doesn't need this code:

/*
typedef struct COORD {  You can delete this declaration for COORD
   short int x;
   short int y;
}

COORD Point;               //and this declaration for Point
*/

Both declarations are above main().

Last edited by Adak; Mar 24th, 2013 at 8:23 AM.
Adak is offline   Reply With Quote
Old Mar 24th, 2013, 8:25 AM   #9
Ancient Dragon
Achieved Level 70

 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 4,345
Rep Power: 10 Ancient Dragon will become famous soon enoughAncient Dragon will become famous soon enough
Re: Easy 2D game board display (Console)

I compiled it with Visual Studio 2012 and had no problems at all. I also compiled it with Code::Blocks and MinGW and got only one warning that the pragma was unknown.
__________________
PFO's FAQ is here
Forum Rules

There is no cow level (Diablo III)
If you never push yourself you will never improve (Diablo III)
Ancient Dragon is offline   Reply With Quote
Old Mar 24th, 2013, 8:44 AM   #10
Smjprogrammer
Learning Very Basic Stuff
 
Smjprogrammer's Avatar
 
Join Date: Jan 2010
Location: United States
Posts: 978
Rep Power: 8 Smjprogrammer is on a distinguished road
Re: Easy 2D game board display (Console)

I have never been able to get Visual C++ 2012 to work my computer claims it is some kind of compatibility issue. On the other side of things I got it to run on 2010, and now I need a BFH.
Attached Images
File Type: jpg Untitled.jpg (213.0 KB, 10 views)
__________________
HP Laptop / AMD A4 - 3305 APU with Radeon HD Graphics 1.90 Ghz
Windows 10 64-Bit
Visual Studio Community 2015
Recommended Books - C++ All-In-One For Dummies / Webster's Computer Dictionary
Smjprogrammer is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
The Black Art of Video Game Console Design lostcauz Book Reviews 1 Nov 5th, 2015 11:33 AM
Working on a easy coded game need a fellow coder to work with me! DoJo_Master Existing Project Development 34 Feb 5th, 2013 9:21 PM
Text Adventure game leveling system (Potentialy 2d game!) ArithanSoftware Existing Project Development 2 Aug 18th, 2011 10:16 AM
Video Game Console Course lostcauz Coder's Corner Lounge 9 Sep 29th, 2006 11:09 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:32 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC