dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2018
    Posts
    4

    Simple console chess game

    Hello, I am trying to create a simple console chess game by using a class JustABoard with an 8x8 array of the board and class Board. (However with valid moves I use a [64] bit board and then convert from a position on the [64] item board to the [8][8] board). I also have a base class Piece with derived classes of each piece which each has its member function legalMove of each respective piece.

    It is a work in progress but currently when I run it, it displays the board as it should and moves the pieces to the user desired square on the board. The problem is that any piece can move anywhere at the moment because the play() function is unaware of the legal moves of each piece. For the sake of testing I have only tried to implement the legal moves for the white pawn by trying to call currentPosition() and legalMoveP and have failed to do so.

    To further elaborate I do have the code for currentPosition() and legal moves of each piece written down. It is simply a matter of implementing it to the game which I am unable of doing.


    Do ignore some of the comments as some are just instructions to myself on what I have left to do/add to the code.

    Main.cpp
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include "Piece.h"
    #include <string>
    #include "board.h"
    
    void printBoard();
    
    //Use monospaced font to view board as uppercase letters for white and lowercase for black
    void printBoard()
    {
    
    	std::cout << "\t\t\t\t\t\t\t Chess Game\n" << std::endl;
    
    	std::cout << "This is a representation of the board.\n" << std::endl;
    	std::cout << "    0 1 2 3 4 5 6 7" << std::endl;
    	std::cout << "   ________________" << std::endl;
    	std::cout << "0 | r n b q k b n r " << std::endl;
    	std::cout << "1 | p p p p p p p p " << std::endl;
    	std::cout << "2 | - - - - - - - - " << std::endl;
    	std::cout << "3 | - - - - - - - - " << std::endl;
    	std::cout << "4 | - - - - - - - - " << std::endl;
    	std::cout << "5 | - - - - - - - - " << std::endl;
    	std::cout << "6 | P P P P P P P P " << std::endl;
    	std::cout << "7 | R N B Q K B N R \n" << std::endl;
    }
    
    int main()
    {
    	Board theGame;
    
    	printBoard();
    	theGame.play();
    
    
    	return 0;
    }
    board.h
    Code:
    #ifndef BOARD_H
    #define BOARD_H 
    #include <string>
    #include "Piece.h"
    
    class JustABoard
    {
    public:
    	std::string static board[8][8];
    };
    
    class Board {
    private:
    	JustABoard currentBoard;
    public:
    	void static getUserMove(std::string); //Coordinates of source, coordinates of destination
    	void displayBoard(); // the current state of the game
    	void play();
    };
    
    #endif
    Piece.h
    Code:
    //kingCheck(), and legelMove() calculations based on AlphaBetaChess.java by Logic Crazy Chess
    //https://onedrive.live.com/?authkey=%21AIKDmEOnXEbMfHk&cid=D4629BC8D856F7D5&id=D4629BC8D856F7D5%21179&parId=D4629BC8D856F7D5%21176&o=OneUp
    
    #ifndef PIECE_H
    #define PIECE_H
    #include <string>
    
    //abstract/base class
    class Piece {
    public:
    	bool static kingSafe(); //Check if king is safe
    	static std::string currentPosition(); //Current Horizontal and Vertical position of piece
    };
    
    class Pawn : public Piece // derive class Pawn from class Piece 
    {
    public:
    	bool isWhite(); //Determine which direction pawn can move in based on if it is White or Black
    	static std::string legalMoveP(int); //Retrieve legal moves for pawn
    
    };
    
    class Rook : public Piece
    {
    public:
    	static std::string legalMoveR(int);
    };
    
    class Bishop : public Piece
    {
    public:
    	static std::string legalMoveB(int);
    };
    
    class Knight : public Piece
    {
    public:
    	static std::string legalMoveN(int);
    };
    
    class Queen : public Piece
    {
    public:
    	static std::string legalMoveQ(int);
    };
    
    class King : public Piece
    {
    public:
    	static std::string legalMoveK(int);
    };
    
    #endif
    Definitions.cpp
    Code:
    #include "stdafx.h"
    #include <string>
    #include <iostream>
    #include "board.h"
    #include "Piece.h"
    #include <ctype.h>
    // Create legal moves for black pieces ------------------------------------------------------------------------------------
    // Call currentPosition() and legalMoves() so program knows where pieces are and how they can move ------------------------------
    // Do kingCheck for black -------------------------------------------------------------------------------------------------
    // Change variable names----------------------------------------------------------------------------------------------------
    // Remove using namespace std-----------------------------------------------------------------------------------------------
    
    using namespace std;
    
    int kingPosWhite = 0;
    int kingPosBlack = 0;
    int row;
    int col;
    int r;
    int c;
    JustABoard CurrentGame;
    
    
    string JustABoard::board[8][8]
    {
    	// To convert from a position on the [64] item board to a [8][8] board
    	//row = (byte)(index / 8); (horizontal)
    	//col = (byte)(index % 8); (vertical)
    
    	{ "r","n","b","q","k","b","n","r" },
    	{ "p","p","p","p","p","p","p","p" },
    	{ "-","-","-","-","-","-","-","-" },
    	{ "-","-","-","-","-","-","-","-" },
    	{ "-","-","-","-","-","-","-","-" },
    	{ "-","-","-","-","-","-","-","-" },
    	{ "P","P","P","P","P","P","P","P" },
    	{ "R","N","B","Q","K","B","N","R" }
    };
    
    
    void Board::displayBoard(){ 
    	getUserMove("");
    	int counter = 0;
    	int i = 0;
    	for (int j = 0; j < 9; j++) {
    		if (j == 8) {
    			i++;
    			j = 0;
    			cout << endl;
    		}
    		
    		cout << currentBoard.board[i][j] << " ";
    		if (i == 7 && j == 7) {
    			cout << endl; // setw
    			break;
    		}
    	}
    
    	
    };
    
    void Board::getUserMove(string move)
    {
    	//to                    from
    	//move receives x1,y1,x2,y2(0,1,2,3) 
    	//r1,c1,r2,c2,piece captured
    	CurrentGame.board[r][c] = CurrentGame.board[row][col];
    	//from = "-"
    	CurrentGame.board[row][col] = "-";
    }
    
    void Board::play()
    {
    	Piece k;
    	Pawn p;
    	int i = 0;
    
    	cout << "If you wish to exit at any time, type 9 into both the row and column input.\n" << endl;
    
    	do
    	{
    		cout << "\nEnter the Row of the piece you would like to move: " << endl;
    		cin >> row;
    		cout << "\nEnter the column of the piece you would like to move: " << endl;
    		cin >> col;
    		cout << "\nEnter the Row of the position you would like to move to: " << endl;
    		cin >> r;
    		cout << "\nEnter the column of the position you would like to move to: " << endl;
    		cin >> c;
    		cout << endl;
    
    		if (currentBoard.board[row][col] == "P")
    		{
    			k.currentPosition();
    			p.legalMoveP(i);
    			
    
    			displayBoard();
    			//Check pawn legal moves
    		}
    		
    		/*else if (currentBoard.board[row][col] == "R")
    		{
    			
    		}
    		else if (currentBoard.board[row][col] == "N")
    		{
    			
    		}
    
    		else if ((row == 9) && (col == 9))
    		{
    			break;
    		}*/
    
    	} while ((!k.kingSafe()) || ((row == 9) && (col == 9)));
    }
    
    
    string Piece::currentPosition()
    {
    	Pawn P, p;
    	Rook R, r;
    	Bishop B, b;
    	Knight N, n;
    	Queen Q, q;
    	King K, k;
    
    	string position;
    
    	for (int i = 0; i < 64; i++)
    	{
    		if (CurrentGame.board[i / 8][i % 8] == "P")
    		{
    			position += P.legalMoveP(i);
    		}
    		else if (CurrentGame.board[i / 8][i % 8] == "R")
    		{
    			position += R.legalMoveR(i);
    
    		}
    		else if (CurrentGame.board[i / 8][i % 8] == "N")
    		{
    			position += N.legalMoveN(i);
    
    		}
    		else if (CurrentGame.board[i / 8][i % 8] == "B")
    		{
    			position += B.legalMoveB(i);
    
    		}
    		else if (CurrentGame.board[i / 8][i % 8] == "Q")
    		{
    			position += Q.legalMoveQ(i);
    
    		}
    		else if (CurrentGame.board[i / 8][i % 8] == "K")
    		{
    			position += K.legalMoveK(i);
    
    		}
    		else if (CurrentGame.board[i / 8][i % 8] == "p")
    		{
    			position += p.legalMoveP(i);
    		}
    		else if (CurrentGame.board[i / 8][i % 8] == "r")
    		{
    			position += r.legalMoveR(i);
    
    		}
    		else if (CurrentGame.board[i / 8][i % 8] == "n")
    		{
    			position += n.legalMoveN(i);
    
    		}
    		else if (CurrentGame.board[i / 8][i % 8] == "b")
    		{
    			position += b.legalMoveB(i);
    
    		}
    		else if (CurrentGame.board[i / 8][i % 8] == "q")
    		{
    			position += q.legalMoveQ(i);
    
    		}
    		else if (CurrentGame.board[i / 8][i % 8] == "k")
    		{
    			position += k.legalMoveK(i);
    
    		}
    	}
    	return position;
    }
    
    
    bool Piece::kingSafe() //do for white
    {
    	int temp = 1;
    
    	//Bishop/ Queen
    	for (int i = -1; i <= 1; i += 2) {
    		for (int j = -1; j <= 1; j += 2) {
    			try {
    				while (CurrentGame.board[kingPosWhite / 8 + temp * i][kingPosWhite % 8 + temp * j] == "-")
    				{
    					temp++;
    				}
    				if ((CurrentGame.board[kingPosWhite / 8 + temp * i][kingPosWhite % 8 + temp * j] == "b") || (CurrentGame.board[kingPosWhite / 8 + temp * i][kingPosWhite % 8 + temp * j] == "q"))
    				{
    					return false;
    				}
    
    			}
    			catch (exception e) {}
    			temp = 1;
    		}
    	}
    
    	//rook/queen
    	for (int i = -1; i <= 1; i += 2) {
    		try {
    			while (CurrentGame.board[kingPosWhite / 8][kingPosWhite % 8 + temp * i] == "-")
    			{
    				temp++;
    			}
    			if ((CurrentGame.board[kingPosWhite / 8][kingPosWhite % 8 + temp * i] == "r") || (CurrentGame.board[kingPosWhite / 8][kingPosWhite % 8 + temp * i] == "q"))
    			{
    				return false;
    			}
    		}
    		catch (exception e) {}
    		temp = 1;
    
    		try {
    			while (CurrentGame.board[kingPosWhite / 8 + temp * i][kingPosWhite % 8] == "-")
    			{
    				temp++;
    			}
    			if ((CurrentGame.board[kingPosWhite / 8 + temp * i][kingPosWhite % 8] == "r") || (CurrentGame.board[kingPosWhite / 8 + temp * i][kingPosWhite % 8] == "q"))
    			{
    				return false;
    			}
    
    		}
    		catch (exception e) {}
    		temp = 1;
    
    	}
    
    	//Knight
    	for (int i = -1; i <= 1; i += 2) {
    		for (int j = -1; j <= 1; j += 2) {
    			try {
    				if (CurrentGame.board[kingPosWhite / 8 + i][kingPosWhite % 8 + j * 2] == "n")
    				{
    					return false;
    				}
    
    			}
    			catch (exception e) {}
    			try {
    				if (CurrentGame.board[kingPosWhite / 8 + i * 2][kingPosWhite % 8 + j] == "n")
    				{
    					return false;
    				}
    
    			}
    			catch (exception e) {}
    		}
    	}
    	//Pawn
    	if (kingPosWhite >= 16) {
    		try {
    			if (CurrentGame.board[kingPosWhite / 8 - 1][kingPosWhite % 8 - 1] == "p")
    			{
    				return false;
    			}
    		}
    		catch (exception e) {}
    		try {
    			if (CurrentGame.board[kingPosWhite / 8 - 1][kingPosWhite % 8 + 1] == "p")
    			{
    				return false;
    			}
    		}
    		catch (exception e) {}
    
    		//king
    		for (int i = -1; i <= 1; i++) {
    			for (int j = -1; j <= 1; j++) {
    				if (i != 0 || j != 0) {
    					try {
    						if (CurrentGame.board[kingPosWhite / 8 + i][kingPosWhite % 8 + j] == "k")
    						{
    							return false;
    						}
    					}
    					catch (exception e) {}
    				}
    			}
    		}
    	}
    	return true;
    }
    
    bool Pawn::isWhite() // figure out better way of making all control paths return value
    {
    	int black = 0;
    	int white = 0;
    	for (int i = 0; i < 64; i++)
    	{
    		if (islower(CurrentGame.board[i / 8][i % 8].at(0)) && (CurrentGame.board[i / 8][i % 8] == "p"))
    		{
    			black = 1;
    		}
    		else (isupper(CurrentGame.board[i / 8][i % 8].at(0)) && (CurrentGame.board[i / 8][i % 8] == "P"));
    		{
    			white = 1;
    		}
    	}
    
    	if (black == 1)
    	{
    		return false;
    	}
    	else
    	{
    		return true;
    	}
    }
    
    string Pawn::legalMoveP(int i) 
    {
    	string list;
    	string oldPiece;
    	int r = i / 8; //Horizontal --
    	int c = i % 8; //Vertical ||
    	
    	for (int j = -1; j <= 1; j += 2) {
    		try {// capture
    			if (islower(CurrentGame.board[r - 1][c + j].at(0)))
    			{
    				oldPiece = CurrentGame.board[r - 1][c + j];
    				CurrentGame.board[r][c] = "-";
    				CurrentGame.board[r - 1][c + j] = "P";
    				if (Piece::kingSafe())
    				{
    					list = list + to_string(r) + to_string(c) + to_string(r - 1) + to_string(c + j) + oldPiece;
    				}
    				CurrentGame.board[r][c] = "P";
    				CurrentGame.board[r - 1][c + j] = oldPiece;
    			}
    
    		}
    		catch (exception e) {}
    
    		try {// move one up
    			if (CurrentGame.board[r - 1][c] == "-")
    			{
    				oldPiece = CurrentGame.board[r - 1][c];
    				CurrentGame.board[r][c] = "-";
    				CurrentGame.board[r - 1][c] = "P";
    				if (Piece::kingSafe())
    				{
    					list = list + to_string(r) + to_string(c) + to_string(r - 1) + to_string(c) + oldPiece;
    				}
    				CurrentGame.board[r][c] = "P";
    				CurrentGame.board[r - 1][c] = oldPiece;
    			}
    
    		}
    		catch (exception e) {}
    
    		try {// move 2 up
    			if ((CurrentGame.board[r - 1][c] == "-") && (CurrentGame.board[r - 2][c] == "-"))
    			{
    				oldPiece = CurrentGame.board[r - 2][c];
    				CurrentGame.board[r][c] = "-";
    				CurrentGame.board[r - 2][c] = "P";
    				if (Piece::kingSafe())
    				{
    					list = list + to_string(r) + to_string(c) + to_string(r - 2) + to_string(c) + oldPiece;
    				}
    				CurrentGame.board[r][c] = "P";
    				CurrentGame.board[r - 2][c] = oldPiece;
    			}
    
    		}
    		catch (exception e) {}
    	}
    	return list;
    }
    
    string Rook::legalMoveR(int i)
    {
    	string oldPiece;
    	string list;
    	int r = i / 8; //Horizontal --
    	int c = i % 8; //Vertical ||
    	int temp = 1;
    	for (int j = -1; j <= 1; j += 2) {
    		try {
    			while (CurrentGame.board[r][c + temp * j] == "-")
    			{
    				oldPiece = CurrentGame.board[r][c + temp * j];
    				CurrentGame.board[r][c] == "-";
    				CurrentGame.board[r][c + temp * j] = "R";
    				if (Piece::kingSafe()) {
    					list = list + to_string(r) + to_string(c) + to_string(r) + to_string(c + temp * j) + oldPiece;
    				}
    				CurrentGame.board[r][c] = "R";
    				CurrentGame.board[r][c + temp * j] = oldPiece;
    				temp++;
    			}
    			if (islower(CurrentGame.board[r][c + temp * j].at(0)))
    			{
    				oldPiece = CurrentGame.board[r][c + temp * j];
    				CurrentGame.board[r][c] = "-";
    				CurrentGame.board[r][c + temp * j] = "R";
    				if (Piece::kingSafe())
    				{
    					list = list + to_string(r) + to_string(c) + to_string(r) + to_string(c + temp * j) + oldPiece;
    				}
    				CurrentGame.board[r][c] = "R";
    				CurrentGame.board[r][c + temp * j] = oldPiece;
    			}
    		}
    		catch (exception e) {}
    		temp = 1;
    		try {
    			while (CurrentGame.board[r + temp * j][c] == "-")
    			{
    				oldPiece = CurrentGame.board[r + temp * j][c];
    				CurrentGame.board[r][c] == "-";
    				CurrentGame.board[r + temp * j][c] = "R";
    				if (Piece::kingSafe()) {
    					list = list + to_string(r) + to_string(c) + to_string(r + temp * j) + to_string(c) + oldPiece;
    				}
    				CurrentGame.board[r][c] = "R";
    				CurrentGame.board[r + temp * j][c] = oldPiece;
    				temp++;
    			}
    			if (islower(CurrentGame.board[r + temp * j][c].at(0)))
    			{
    				oldPiece = CurrentGame.board[r + temp * j][c];
    				CurrentGame.board[r][c] = "-";
    				CurrentGame.board[r + temp * j][c] = "R";
    				if (Piece::kingSafe())
    				{
    					list = list + to_string(r) + to_string(c) + to_string(r + temp * j) + to_string(c) + oldPiece;
    				}
    				CurrentGame.board[r][c] = "R";
    				CurrentGame.board[r + temp * j][c] = oldPiece;
    			}
    		}
    		catch (exception e) {}
    	}
    	return list;
    }
    
    string Bishop::legalMoveB(int i)
    {
    	string oldPiece;
    	string list;
    	int r = i / 8; //Horizontal --
    	int c = i % 8; //Vertical ||
    	int temp = 1;
    
    	for (int j = -1; j <= 1; j += 2)
    	{
    		for (int k = -1; k <= 1; k += 2)
    		{
    			try {
    				while (CurrentGame.board[r + temp * j][c + temp * k] == "-")
    				{
    					oldPiece = CurrentGame.board[r + temp * j][c + temp * k];
    					CurrentGame.board[r][c] == "-";
    					CurrentGame.board[r + temp * j][c + temp * k] = "B";
    					if (Piece::kingSafe()) {
    						list = list + to_string(r) + to_string(c) + to_string(r + temp * j) + to_string(c + temp * k) + oldPiece;
    					}
    					CurrentGame.board[r][c] = "B";
    					CurrentGame.board[r + temp * j][c + temp * k] = oldPiece;
    					temp++;
    				}
    				if (islower(CurrentGame.board[r + temp * j][c + temp * k].at(0)))
    				{
    					oldPiece = CurrentGame.board[r + temp * j][c + temp * k];
    					CurrentGame.board[r][c] = "-";
    					CurrentGame.board[r + temp * j][c + temp * k] = "B";
    					if (Piece::kingSafe())
    					{
    						list = list + to_string(r) + to_string(c) + to_string(r + temp * j) + to_string(c + temp * k) + oldPiece;
    					}
    					CurrentGame.board[r][c] = "B";
    					CurrentGame.board[r + temp * j][c + temp * k] = oldPiece;
    				}
    			}
    			catch (exception e) {}
    			temp = 1;
    		}
    	}
    	return list;
    }
    
    string Knight::legalMoveN(int i)
    {
    	string oldPiece;
    	string list;
    	int r = i / 8; //Horizontal --
    	int c = i % 8; //Vertical ||
    
    	for (int j = -1; j <= 1; j += 2)
    	{
    		for (int k = -1; k <= 1; k += 2)
    		{
    			try {
    				if (islower(CurrentGame.board[r + j][c + k * 2].at(0)) || ((CurrentGame.board[r + j][c + k * 2] == "-")))
    				{
    					oldPiece = CurrentGame.board[r + j][c + k * 2];
    					CurrentGame.board[r][c] = "-";
    					if (Piece::kingSafe())
    					{
    						list = list + to_string(r) + to_string(c) + to_string(r + j) + to_string(c + k * 2) + oldPiece;
    					}
    					CurrentGame.board[r][c] = "N";
    					CurrentGame.board[r + j][c + k * 2] = oldPiece;
    				}
    
    			}
    			catch (exception e) {}
    			try {
    				if (islower(CurrentGame.board[r + j * 2][c + k].at(0)) || ((CurrentGame.board[r + j * 2][c + k] == "-")))
    				{
    					oldPiece = CurrentGame.board[r + j * 2][c + k];
    					CurrentGame.board[r][c] = "-";
    					if (Piece::kingSafe())
    					{
    						list = list + to_string(r) + to_string(c) + to_string(r + j * 2) + to_string(c + k) + oldPiece;
    					}
    					CurrentGame.board[r][c] = "N";
    					CurrentGame.board[r + j * 2][c + k] = oldPiece;
    				}
    
    			}
    			catch (exception e) {}
    		}
    
    	}
    	return list;
    }
    
    string Queen::legalMoveQ(int i)
    {
    	string oldPiece;
    	string list;
    	int r = i / 8; //Horizontal --
    	int c = i % 8; //Vertical ||
    	int temp = 1;
    
    	for (int j = -1; j <= 1; j++) {
    		for (int k = -1; k <= 1; k++) {
    			if (j != 0 || k != 0) {
    				try {
    					while (CurrentGame.board[r + temp * j][c + temp * k] == "-")
    					{
    						oldPiece = CurrentGame.board[r + temp * j][c + temp * k];
    						CurrentGame.board[r][c] == " ";
    						CurrentGame.board[r + temp * j][c + temp * k] = "Q";
    						if (Piece::kingSafe()) {
    							list = list + to_string(r) + to_string(c) + to_string(r + temp * j) + to_string(c + temp * k) + oldPiece;
    						}
    						CurrentGame.board[r][c] = "Q";
    						CurrentGame.board[r + temp * j][c + temp * k] = oldPiece;
    						temp++;
    					}
    					if (islower(CurrentGame.board[r + temp * j][c + temp * k].at(0)))
    					{
    						oldPiece = CurrentGame.board[r + temp * j][c + temp * k];
    						CurrentGame.board[r][c] = "-";
    						CurrentGame.board[r + temp * j][c + temp * k] = "Q";
    						if (Piece::kingSafe())
    						{
    							list = list + to_string(r) + to_string(c) + to_string(r + temp * j) + to_string(c + temp * k) + oldPiece;
    						}
    						CurrentGame.board[r][c] = "Q";
    						CurrentGame.board[r + temp * j][c + temp * k] = oldPiece;
    					}
    				}
    				catch (exception e) {}
    				temp = 1;
    			}
    		}
    	}
    	return list;
    }
    
    string King::legalMoveK(int i)
    {
    	string oldPiece;
    	string list;
    	int r = i / 8; //Horizontal --
    	int c = i % 8; //Vertical ||
    	for (int j = 0; j < 9; j++) {
    		if (j != 4) {
    			try {
    				if (islower(CurrentGame.board[r - 1 + j / 3][c - 1 + j % 3].at(0)) || (CurrentGame.board[r - 1 + j / 3][c - 1 + j % 3] == "-"))
    				{
    					oldPiece = CurrentGame.board[r - 1 + j / 3][c - 1 + j % 3];
    					CurrentGame.board[r][c] = "-";
    					CurrentGame.board[r - 1 + j / 3][c - 1 + j % 3] = "K";
    					int kingTemp = kingPosWhite;
    					kingPosWhite = i + (j / 3) * 8 + j % 3 - 9;
    					if (Piece::kingSafe())
    					{
    						list = list + to_string(r) + to_string(c) + to_string(r - 1 + j / 3) + to_string(c - 1 + j % 3) + oldPiece;
    					}
    					CurrentGame.board[r][c] = "K";
    					CurrentGame.board[r - 1 + j / 3][c - 1 + j % 3] = oldPiece;
    					kingPosWhite = kingTemp;
    				}
    			}
    			catch (exception e) {}
    		}
    	}
    	return list;
    }

    Thank you for your help.

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,210

    Re: Simple console chess game

    Code:
    string Pawn::legalMoveP(int i)
    This function returns a value of type string and also updates CurrentGame.board. It is used as

    Code:
    k.currentPosition();
    p.legalMoveP(i);
    displayBoard();
    There is no checking on the return value of .legalMoveP() - so how does the calling routine know if the move is legal or illegal? Also, if legalMoveP() is just supposed to check for a legal Pawn move, why does it alter CurrentGame.board? Why not a function

    Code:
    bool Pawn::IsLegal(int i)
    which just returns true or false if the passed position is legal or not?

    Also, you are passing i as an argument to .legalMoveP(). However, i is always 0. Don't you need to pass something like r * 8 + c ?
    Last edited by 2kaud; April 22nd, 2018 at 06:04 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.1)

  3. #3
    Join Date
    Apr 2018
    Posts
    4

    Re: Simple console chess game

    Since my post did not get accepted until 12-15 hours later I did not get the chance to add that my program crashes after the user input their to and from integer values. After some testing I figured out it has to do with
    Code:
     currentPosition()
    and
    Code:
    legalMoveP
    returning string values, because if I change them to return int values my program does not crash. I am not sure though how to make the change permanent as the coordinate numbers returned are not concatenated if they are int, they are all instead added up.
    Last edited by Aroom; April 22nd, 2018 at 06:49 AM.

  4. #4
    Join Date
    Apr 2018
    Posts
    4

    Re: Simple console chess game

    @2kaud Thank you for your reply.
    I have fixed passing an empty i as an argument to legalMoveP() by creating a function that converts the moves from an [8][[8] coordinate to a 64 item array one.
    I don't however understand what you mean by altering the CurrentGame.board? The legalMoveP() has to check if there is another piece in the way of Pawn or if it is empty to allow the Pawn to move, and if there is another piece to successfully capture the piece and remove it should the user wish to do so.

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,210

    Re: Simple console chess game

    I don't however understand what you mean by altering the CurrentGame.board?
    This is an example of what I meant.

    Code:
    CurrentGame.board[r][c] = "-";
    CurrentGame.board[r - 1][c] = "P";
    but this was based on understanding that .legalMoveP() was just a check for a legal move - and did not actually undertake the move. From your description, .legalMoveP() checks and performs the move.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.1)

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)