Help with A Chess game

This is a discussion on Help with A Chess game within the C++ Programming forums, part of the General Programming Boards category; OK this is the code i typed typed into vs2003, it worked on another computer but not my current one. ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    9

    Help with A Chess game

    OK this is the code i typed typed into vs2003, it worked on another computer but not my current one. I get the following error:
    Run-Time Check Failure #3 - The variable 'b' is being used without being defined.
    and it is defined. It "compiles" fine, but when i run or debug i get that .

    any ideas?
    thank you
    Code:
    // some loser chess game
    
    #include "iostream"
    #include "math.h"
    
    using namespace std;
    
    int fun(int x, int y)// najes 1d array 2d array
    {
    	return (8*y+x);
    }
    
    
    
    void naming(int p)// bind a number to each piece(positive white, negitive black)
    {
    	
    	if (p == 1)
    	cout << "  P";
    	if (p == 2)
    	cout << "  R";
    	if (p == 3)
    	cout << "  K";
    	if (p == 4)
    	cout << "  B";
    	if (p == 5)
    	cout << "  V";
    	if (p == 6)
    	cout << "  Q";
    	if (p == 0)
    	cout << "  -";
    	if (p == -1)
    	cout << "  p";
    	if (p == -2)
    	cout << "  r";
    	if (p == -3)
    	cout << "  k";
    	if (p == -4)
    	cout << "  b";
    	if (p == -5)
    	cout << "  v";
    	if (p == -6)
    	cout << "  q";
    }
    
    void drawchessboard(int chess[]) // draw the chess board
    {
    	cout << endl << endl << endl << "   1  2  3  4  5  6  7  8"  << endl;
    	int b;
    	for(int x = 0; x < 8 ;x++)
    	{
    		cout << "		" << endl << endl << x+1;
    		for(int y = 0; y < 8; y++)
    		{
    			b = fun(y,x);
    			naming(chess[b]);
    		}
    	}
    }
    
     
    
    bool game(int chess[]) // set up the win/lose function
    {
    	int b;
    	int w;
    	int p;
    	for( p = 0; p < 64; p++)
    	{
    		
    		if (chess[p] == 5)
    		w = 1;
    		if (chess[p] == -5)
    		b = 1;
    		if (b == 1 && w == 1)
    			return true;
    	}
    	
    	return false;
    	if (b == 1 && w ==0)
    	cout << "black wins";
    	else
    		cout << "White Wins";
    
    }
    
    
    void execute(int sx, int dx, int sy, int dy, int chess[]) // give moove function
    {
    	chess[fun(dx,dy)] = chess [fun(sx,sy)];
    	chess[fun(sx, sy)] = 0;
    }
    
    bool legalmove(int sx, int dx, int sy, int dy, int st, int dt) // check to se if it is a legal moove
    
    { 
    	if (abs(dx-sx) == 0 && abs(dy-sy) == 0 )  // cant moove to current position
    		return false;
    	else if (abs(st) == 2 && (abs(sy-dy)==0 || abs(sx-dx)==0)) // if rook and moving forward,back,left,or right then it can moove
    		return true;
    	else if (abs(st) == 4 && abs(dx-sx) == abs(sy-dy))// checks to maek sure bishop is mooving diagonally
    		return true;
    	else if (abs(st) == 5 && abs(dx-sx) <= 1 && abs(dy-sy) <= 1)
    		return true;
    	else if (abs(st) == 6 && ((abs(sy-dy)==0) || abs(sx-dx)==0) || (abs(dx-sx) == abs(sy-dy)))
    		return true;
    	else if (abs(st) == 3 && (abs(sy-dy) == 1 && abs(sx-dx) == 2) || (abs(sy-dy) ==2 && abs(sx-dx) == 1))
    		return true;
    	else if ((st) == 1 && (dy-sy) == 1 && dt == 0 && (dx-sx) == 0)
    		return true;
    	else if ((st) == 1 && (dy-sy) == 2 && dt == 0 && (dx-sx) == 0 && sy == 1)
    		return true;
    	else if ((st) == 1 && abs(dx-sx) == 1 && (dy-sy) == 1 && dt < 0)
    		return true;
    	else if ((st) == -1 && (dy-sy) == -1 && dt == 0 && (dx-sx) == 0)
    		return true;
    	else if ((st) == -1 && (dy-sy) == -2 && dt == 0 && (dx-sx) == 0 && sy == 6)
    		return true;
    	else if ((st) == -1 && abs(dx-sx) == 1 && (dy-sy) == -1 && dt > 0)
    		return true;
    	// kyles mom
    		return false;
    }
    
    bool inbounds(int sx, int dx, int sy, int dy)
    {
    	if (sx > -1 && sx < 8 && dx > -1 && dx < 8 && sy > -1 && sy < 8 && dy > -1 && dy < 8)
    	return true;
    	else
    	return false;
    }
    
    bool turn(int color, int st)
    {
    	if(color == 0 && st > 0)
    	return true;
    	if(color == 1 && st < 0)
    	return true;
    	else
    	return false;
    }
    
    // MAIN _________________
    int main()
    {
    	int chess[64] = {2, 3, 4, 5, 6, 4, 3, 2, 1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,
    						-1, -1, -1, -1, -1, -1, -1, -1, -2, -3, -4, -6, -5, -4, -3, -2};
    
    int sx, sy, dx, dy, col, st, dt;
    
    int color =  0;
    
    while  (game(chess))
    	{
    		drawchessboard(chess);
    		if (color)
    			
    			cout << endl << endl << "it is black's turn";
    			else cout << endl << endl << "it is white's turn";
    	
    		
    		
    		
    		cout << endl << endl << "please type in the x and y coordinates of the selected piece and the destination of the piece in that order." << endl;
    		cin >> sx >> sy >> dx >> dy;
            sx--;sy--;dx--;dy--;
    
    		if (inbounds(sx, dx, sy, dy))
    		{
    			st = chess[fun(sx, sy)];
    			dt = chess[fun(dx, dy)];
    			if ((st * dt <= 0) && legalmove(sx, dx, sy, dy, st, dt) && turn(color, st))
    			{
    
    				execute(sx, dx, sy, dy, chess);	
    				color =!color;
    				
    			}
    			else
    				cout << "illegal move";
    		}
    			
    	}
    	cin.get();
    	return 0;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Well, there are only two bs, one in drawchessboard() and one in game().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    9
    the proglem is in game on line 75 9if (b == 1 && w == 1))

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    I dont know about your error but i do know whats in red will never be printed.
    Can you see why?
    Code:
    bool game(int chess[]) // set up the win/lose function
    {
    	int b;
    	int w;
    	int p;
    	for( p = 0; p < 64; p++)
    	{
    		
    		if (chess[p] == 5)
    		w = 1;
    		if (chess[p] == -5)
    		b = 1;
    		if (b == 1 && w == 1)
    			return true;
    	}
    	
    	return false;
    	if (b == 1 && w ==0)
    	cout << "black wins";
    	else
    		cout << "White Wins";
    }
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    9
    no, can you help me? can anyone else compile this?

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    9
    i have been trying things for days now, can someone please help me?

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    in your game function, initilize b and w to something, 0 I guess should be fine.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    The red code will never be printed because there is a return statement just before it.

    I don't see how you could get a runtime error on code that never gets executed. Is it the other b, maybe? Post your whole code again if you've modified it.

    And take defcon's advise and initialize your variables.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    9
    I fixed it, apperently i had to define b and w outside the function, but shouldnt local varieables be able to be used inside that if statement?

    Code:
    // some loser chess game
    
    #include "iostream"
    #include "math.h"
    #include "stdafx.h"
    
    using namespace std;
    
    int fun(int x, int y)// najes 1d array 2d array
    {
    	return (8*y+x);
    }
    
    
    
    void naming(int p)// bind a number to each piece(positive white, negitive black)
    {
    	
    	if (p == 1)
    	cout << "  P";
    	if (p == 2)
    	cout << "  R";
    	if (p == 3)
    	cout << "  K";
    	if (p == 4)
    	cout << "  B";
    	if (p == 5)
    	cout << "  V";
    	if (p == 6)
    	cout << "  Q";
    	if (p == 0)
    	cout << "  -";
    	if (p == -1)
    	cout << "  p";
    	if (p == -2)
    	cout << "  r";
    	if (p == -3)
    	cout << "  k";
    	if (p == -4)
    	cout << "  b";
    	if (p == -5)
    	cout << "  v";
    	if (p == -6)
    	cout << "  q";
    }
    
    void drawchessboard(int chess[]) // draw the chess board
    {
    	cout << endl << endl << endl << "   1  2  3  4  5  6  7  8"  << endl;
    	int q;
    	for(int x = 0; x < 8 ;x++)
    	{
    		cout << "		" << endl << endl << x+1;
    		for(int y = 0; y < 8; y++)
    		{
    			q = fun(y,x);
    			naming(chess[q]);
    		}
    	}
    }
    
     
    
    bool game(int chess[]) // set up the win/lose function
    {
    	int b;
    	int w;
    	int p;
    	b = 1;
    	w = 1;
    	for( p = 0; p < 64; p++)
    	{
    		
    		if (chess[p] == 5)
    		//w = 1;
    		if (chess[p] == -5)
    		//b = 1;
    		if (b == 1 && w == 1)
    			return true;
    	}
    	
    	return false;
    	if (b == 1 && w ==0)
    	cout << "black wins";
    	else
    		cout << "White Wins";
    
    }
    
    
    void execute(int sx, int dx, int sy, int dy, int chess[]) // give moove function
    {
    	chess[fun(dx,dy)] = chess [fun(sx,sy)];
    	chess[fun(sx, sy)] = 0;
    }
    
    bool legalmove(int sx, int dx, int sy, int dy, int st, int dt) // check to se if it is a legal moove
    
    { 
    	if (abs(dx-sx) == 0 && abs(dy-sy) == 0 )  // cant moove to current position
    		return false;
    	else if (abs(st) == 2 && (abs(sy-dy)==0 || abs(sx-dx)==0)) // if rook and moving forward,back,left,or right then it can moove
    		return true;
    	else if (abs(st) == 4 && abs(dx-sx) == abs(sy-dy))// checks to maek sure bishop is mooving diagonally
    		return true;
    	else if (abs(st) == 5 && abs(dx-sx) <= 1 && abs(dy-sy) <= 1)
    		return true;
    	else if (abs(st) == 6 && ((abs(sy-dy)==0) || abs(sx-dx)==0) || (abs(dx-sx) == abs(sy-dy)))
    		return true;
    	else if (abs(st) == 3 && (abs(sy-dy) == 1 && abs(sx-dx) == 2) || (abs(sy-dy) ==2 && abs(sx-dx) == 1))
    		return true;
    	else if ((st) == 1 && (dy-sy) == 1 && dt == 0 && (dx-sx) == 0)
    		return true;
    	else if ((st) == 1 && (dy-sy) == 2 && dt == 0 && (dx-sx) == 0 && sy == 1)
    		return true;
    	else if ((st) == 1 && abs(dx-sx) == 1 && (dy-sy) == 1 && dt < 0)
    		return true;
    	else if ((st) == -1 && (dy-sy) == -1 && dt == 0 && (dx-sx) == 0)
    		return true;
    	else if ((st) == -1 && (dy-sy) == -2 && dt == 0 && (dx-sx) == 0 && sy == 6)
    		return true;
    	else if ((st) == -1 && abs(dx-sx) == 1 && (dy-sy) == -1 && dt > 0)
    		return true;
    	// kyles mom
    		return false;
    }
    
    bool inbounds(int sx, int dx, int sy, int dy)
    {
    	if (sx > -1 && sx < 8 && dx > -1 && dx < 8 && sy > -1 && sy < 8 && dy > -1 && dy < 8)
    	return true;
    	else
    	return false;
    }
    
    bool turn(int color, int st)
    {
    	if(color == 0 && st > 0)
    	return true;
    	if(color == 1 && st < 0)
    	return true;
    	else
    	return false;
    }
    
    // MAIN _________________
    int main()
    {
    	int chess[64] = {2, 3, 4, 5, 6, 4, 3, 2, 1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,
    						-1, -1, -1, -1, -1, -1, -1, -1, -2, -3, -4, -6, -5, -4, -3, -2};
    
    int sx, sy, dx, dy, col, st, dt;
    
    int color =  0;
    
    while  (game(chess))
    	{
    		drawchessboard(chess);
    		if (color)
    			
    			cout << endl << endl << "it is black's turn";
    			else cout << endl << endl << "it is white's turn";
    	
    		
    		
    		
    		cout << endl << endl << "please type in the x and y coordinates of the selected piece and the destination of the piece in that order." << endl;
    		cin >> sx >> sy >> dx >> dy;
            sx--;sy--;dx--;dy--;
    
    		if (inbounds(sx, dx, sy, dy))
    		{
    			st = chess[fun(sx, sy)];
    			dt = chess[fun(dx, dy)];
    			if ((st * dt <= 0) && legalmove(sx, dx, sy, dy, st, dt) && turn(color, st))
    			{
    
    				execute(sx, dx, sy, dy, chess);	
    				color =!color;
    				
    			}
    			else
    				cout << "illegal move";
    		}
    			
    	}
    	cin.get();
    	return 0;
    }

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    9
    wait, that doesnt work, the way i fixed was uncommented the definitions. So this only works when b and w is definied twice?
    *cough*vug*cough* I am using latest version of vs2003

  11. #11
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    The lines of code below the return statement in your game function will still never be used, regardless of how you initialize the variables. The program will never print "black wins" or "White Wins". Think of it like this:
    Code:
    int main()
    {
        int a;
        cin >> a;
        cout << "You typed in ";
        return 0;
        cout << a << endl;
    }
    You'd never make a program like this because you'd never get to the bolded line. The same goes for that function. You may not be getting a compiler error anymore, but the function will still not work the way you want it to.
    There is a difference between tedious and difficult.

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. c++ chess game help
    By jdm71488 in forum C++ Programming
    Replies: 6
    Last Post: 04-26-2004, 09:46 AM
  3. 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
  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