Thread: Jump moves in Checkers Game

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    1

    Jump moves in Checkers Game

    Hi,

    I'm having a bit of trouble with getting jump moves and especially multiple jump moves working. Any help would be great! excuse the code, it may be a little all over the place and I could have totally the wrong method! I've also cut out a stringtokenizer class which I know works fine.

    Thanks in advance,
    Ronan

    Code:
    #include <iostream>
    #include <string>
    #include <stdexcept>
    #include <ctype.h>
    #include  <math.h>
    
    
    using namespace std;
    
    // Token types
    const int NONE   = 0;
    const int WORD   = 1;
    const int NUMBER = 2;
    
    
    string b_w = " Black";	//who's move is it
    int black_pieces_left = 12;
    int white_pieces_left = 12;
    
    
    class Board {
    public:
    	Board();
    	void print_board();
    	void reset_space(int, int, bool);
    	void jumped_space(int);
    	char squares[32];
    	char spaces[32];
    private:
    	
    };
    
    Board::Board() {
    	
    	
    	for (int i=0; i<13; i++) {
    		squares[i] = 'W';
    	}
    	
    	for (i=12; i<20; i++) {
    		squares[i] = ' ';
    	}
    	
    	for (i=20; i<32; i++) {
    		squares[i] = 'B';
    	}
    
    	for (i=0; i<32; i++) {
    		spaces[i] = ' ';
    	}
    	
    	squares[16] = 'W';
    }
    
    
    void Board::print_board() {
    
    	cout << endl << endl;
    
    	cout << "\t\t +---+---+---+---+---+---+---+---+ " << endl;
    	cout << "\t\t |///|" << spaces[0] << squares[0] << " |///|" << spaces[1] << squares[1] << " |///|" << spaces[2] << squares[2] << " |///|" << spaces[3] << squares[3] << " | " << endl;
    	cout << "\t\t |///| 1 |///| 2 |///| 3 |///| 4 | " << endl;
    	cout << "\t\t +---|---|---|---|---|---|---|---+ " << endl;
    	cout << "\t\t |" << spaces[4] << squares[4] << " |///|" << spaces[5] << squares[5] << " |///|" << spaces[6] << squares[6] << " |///|" << spaces[7] << squares[7] << " |///|" << endl;
    	cout << "\t\t | 5 |///| 6 |///| 7 |///| 8 |///| " << endl;
    	cout << "\t\t +---|---|---|---|---|---|---|---+ " << endl;
    	cout << "\t\t |///|" << spaces[8] << squares[8] << " |///|" << spaces[9] << squares[9] << " |///|" << spaces[10] << squares[10] << " |///|" << spaces[11] << squares[11] << " | " << endl;
    	cout << "\t\t |///| 9 |///| 10|///| 11|///| 12| " << endl;	
    	cout << "\t\t +---|---|---|---|---|---|---|---+ " << endl;
    	cout << "\t\t |" << spaces[12] << squares[12] << " |///|" << spaces[13] << squares[13] << " |///|" << spaces[14] << squares[14] << " |///|" << spaces[15] << squares[15] << " |///| " << "\tBlack Remaining: " << black_pieces_left << endl;
    	cout << "\t\t | 13|///| 14|///| 15|///| 16|///| " << endl;
    	cout << "\t\t +---|---|---|---|---|---|---|---+ " << endl;
    	cout << "\t\t |///|" << spaces[16] << squares[16] << " |///|" << spaces[17] << squares[17] << " |///|" << spaces[18] << squares[18] << " |///|" << spaces[19] << squares[19] << " | " << "\tWhite Remaining: " << white_pieces_left << endl;
    	cout << "\t\t |///| 17|///| 18|///| 19|///| 20| " << endl;
    	cout << "\t\t +---|---|---|---|---|---|---|---+ " << endl;
    	cout << "\t\t |" << spaces[20] << squares[20] << " |///|" << spaces[21] << squares[21] << " |///|" << spaces[22] << squares[22] << " |///|" << spaces[23] << squares[23] << " |///| " << endl;
    	cout << "\t\t | 21|///| 22|///| 23|///| 24|///| " << endl;
    	cout << "\t\t +---|---|---|---|---|---|---|---+ " << endl;
    	cout << "\t\t |///|" << spaces[24] << squares[24] << " |///|" << spaces[25] << squares[25] << " |///|" << spaces[26] << squares[26] << " |///|" << spaces[27] << squares[27] << " | " << endl;
    	cout << "\t\t |///| 25|///| 26|///| 27|///| 28| " << endl;
    	cout << "\t\t +---|---|---|---|---|---|---|---+ " << endl;
    	cout << "\t\t |" << spaces[28] << squares[28] << " |///|" << spaces[29] << squares[29] << " |///|" << spaces[30] << squares[30] << " |///|" << spaces[31] << squares[31] << " |///| " << endl;
    	cout << "\t\t | 29|///| 30|///| 31|///| 32|///| " << endl;
    	cout << "\t\t +---+---+---+---+---+---+---+---+ " << endl;
    
    	cout << endl << endl;
    
    }
    
    void Board::reset_space(int start, int end, bool king) {
    	
    	char tmp1, tmp2;
    	
    	//King Me
    	if (king == true)
    	{
    		spaces[start] = ' ';
    		spaces[end] = 'K';
    		
    		tmp1 = squares[start];
    		squares[start] = ' ';
    		squares[end] = tmp1;
    	}
    
    	else 
    	{
    		//Regular Moves Pieces
    		tmp1 = squares[start];
    		squares[start] = ' ';
    		squares[end] = tmp1;
    
    		tmp2 = spaces[start];
    		spaces[start] = ' ';
    		spaces[end] = tmp2;
    	}
    }
    
    void Board::jumped_space(int del) {
    	cout << "in jumped_space" << endl;
    	cout << "space to del: " << del << endl;
    
    	squares[del] = ' ';
    	spaces[del] = ' ';
    
    	if (b_w[1] == 'B')
    		white_pieces_left--;
    	else
    		black_pieces_left--;
    }
    
    
    class GamePlay {
    public:
    	GamePlay();
    	Board b;
    	void check_move(int, int);
    	bool legal();
    	bool game_over(string);
    	bool king_me();
    private:
    	bool reg_move();
    	bool jump_legal();
    	int jump_move();
    	int moves_GP[4];
    	int row1, row2;
    	int col1, col2;
    
    };
    
    GamePlay::GamePlay() {
    	
    	row1 = 0, row2 = 0;
    	col1 = 0, col2 = 0;
    }
    
    //Initilasing 
    void GamePlay::check_move(int a, int b) {
    
    	moves_GP[0] = a, moves_GP[1] = b;
    
    	//Switch Statement
    	if (moves_GP[0] >= 1 && moves_GP[0] <= 4) 
    		row1 = 1;
    	else if (moves_GP[0] >= 5 && moves_GP[0] <= 8) 
    		row1 = 2;
    	else if (moves_GP[0] >= 9 && moves_GP[0] <= 12) 
    		row1 = 3;
    	else if (moves_GP[0] >= 13 && moves_GP[0] <= 16) 
    		row1 = 4;
    	else if (moves_GP[0] >= 17 && moves_GP[0] <= 20) 
    		row1 = 5;
    	else if (moves_GP[0] >= 21 && moves_GP[0] <= 24) 
    		row1 = 6;
    	else if (moves_GP[0] >= 25 && moves_GP[0] <= 28) 
    		row1 = 7;
    	else if (moves_GP[0] >= 29 && moves_GP[0] <= 32) 
    		row1 = 8;
    		
    
    	if (moves_GP[1] >= 1 && moves_GP[1] <= 4) 
    		row2 = 1;
    	else if (moves_GP[1] >= 5 && moves_GP[1] <= 8) 
    		row2 = 2;
    	else if (moves_GP[1] >= 9 && moves_GP[1] <= 12) 
    		row2 = 3;
    	else if (moves_GP[1] >= 13 && moves_GP[1] <= 16) 
    		row2 = 4;
    	else if (moves_GP[1] >= 17 && moves_GP[1] <= 20) 
    		row2 = 5;
    	else if (moves_GP[1] >= 21 && moves_GP[1] <= 24) 
    		row2 = 6;
    	else if (moves_GP[1] >= 25 && moves_GP[1] <= 28) 
    		row2 = 7;
    	else if (moves_GP[1] >= 29 && moves_GP[1] <= 32) 
    		row2 = 8;
    
    	if (moves_GP[0] == 5 || moves_GP[0] == 13 || moves_GP[0] == 21 || moves_GP[0] == 29) 
    		col1 = 1;
    	else if (moves_GP[0] == 1 || moves_GP[0] == 9 || moves_GP[0] == 17 || moves_GP[0] == 25) 
    		col1 = 2;
    	else if (moves_GP[0] == 6 || moves_GP[0] == 14 || moves_GP[0] == 22 || moves_GP[0] == 30) 
    		col1 = 3;
    	else if (moves_GP[0] == 2 || moves_GP[0] == 10 || moves_GP[0] == 18 || moves_GP[0] == 26) 
    		col1 = 4;
    	else if (moves_GP[0] == 7 || moves_GP[0] == 15 || moves_GP[0] == 23 || moves_GP[0] == 31) 
    		col1 = 5;
    	else if (moves_GP[0] == 3 || moves_GP[0] == 11 || moves_GP[0] == 19 || moves_GP[0] == 27) 
    		col1 = 6;
    	else if (moves_GP[0] == 8 || moves_GP[0] == 16 || moves_GP[0] == 24 || moves_GP[0] == 32) 
    		col1 = 7;
    	else if (moves_GP[0] == 4 || moves_GP[0] == 12 || moves_GP[0] == 20 || moves_GP[0] == 28) 
    		col1 = 8;
    
    	if (moves_GP[1] == 5 || moves_GP[1] == 13 || moves_GP[1] == 21 || moves_GP[1] == 29) 
    		col2 = 1;
    	else if (moves_GP[1] == 1 || moves_GP[1] == 9 || moves_GP[1] == 17 || moves_GP[1] == 25) 
    		col2 = 2;
    	else if (moves_GP[1] == 6 || moves_GP[1] == 14 || moves_GP[1] == 22 || moves_GP[1] == 30) 
    		col2 = 3;
    	else if (moves_GP[1] == 2 || moves_GP[1] == 10 || moves_GP[1] == 18 || moves_GP[1] == 26) 
    		col2 = 4;
    	else if (moves_GP[1] == 7 || moves_GP[1] == 15 || moves_GP[1] == 23 || moves_GP[1] == 31) 
    		col2 = 5;
    	else if (moves_GP[1] == 3 || moves_GP[1] == 11 || moves_GP[1] == 19 || moves_GP[1] == 27) 
    		col2 = 6;
    	else if (moves_GP[1] == 8 || moves_GP[1] == 16 || moves_GP[1] == 24 || moves_GP[1] == 32) 
    		col2 = 7;
    	else if (moves_GP[1] == 4 || moves_GP[1] == 12 || moves_GP[1] == 20 || moves_GP[1] == 28) 
    		col2 = 8;
    
    }
    
    
    
    //Checking a number of parameters to see if spaces
    //used in the move are legal
    bool GamePlay::legal() {
    
    	//Check number for validity
    	if(moves_GP[0] <= 32 && moves_GP[0] > 0 && moves_GP[1] <= 32 && moves_GP[1] > 0) 
    	{	
    		//Check if space selected is empty
    		if(b.squares[moves_GP[0]-1] != ' ') 
    		{	
    			//Check if own piece is in space selected
    			if (b.squares[moves_GP[0]-1] == b_w[1]) 
    			{
    				//Space not already occupied
    				if (b.squares[moves_GP[1]-1] == ' ') 
    				{
    					//Piece Moving Right Direction
    					if ((b_w[1] == 'B' && row2 < row1) || (b_w[1] == 'W' && row1 < row2)) 
    					{
    						if (reg_move() == true)
    							return true;
    						else
    							return false;
    					}
    
    					//King Piece Moving
    					else if (b.spaces[moves_GP[0]-1] == 'K')
    					{
    						if (reg_move() == true)
    							return true;
    						else
    							return false;
    					}
    
    					else 
    					{
    						cout << "Pieces may only move forward" << endl;
    						return false;
    					}
    				}
    
    				else 
    				{
    					cout << "That space is already occupied" << endl;
    					return false;
    				}
    			}
    
    			else 
    			{
    				cout << "You may only move your own pieces" << endl;
    				return false;
    			}
    		}
    
    		else 
    		{
    			cout << "There is no piece in the space selected" << endl;
    			return false;
    		}
    	}
    
    	else 
    	{
    		cout << "Move entered is outside the board or you have not entered enough parameters" << endl;
    		return false;
    	}
    
    }
    
    
    bool GamePlay::reg_move() {
    
    	cout << "Space To Jump: " << jump_move() << endl;
    
    	if (abs(col1 - col2) == 1 && abs(row1 - row2) == 1) 
    	{	
    		return true;
    	}
    
    	else if (jump_legal() == true)
    	{
    		return true;
    	}
    
    	else
    	{
    		cout << "You have tried to move too many spaces" << endl;
    		return false;
    	}
    }
    
    
    bool GamePlay::jump_legal() {
    
    	
    	if (b.squares[jump_move()] == ' ')
    	{
    		cout << "You cannot jump over empty spaces" << endl;
    		return false;
    	}
    
    	else 
    	{
    		if (b.squares[jump_move()] == b_w[1])
    		{
    			cout << "You cannot jump over your own piece" << endl;
    			return false;
    		}
    
    		else
    		{
    			b.jumped_space(jump_move());
    			return true;
    			
    		}
    	}
    }
    
    
    int GamePlay::jump_move() {
    
    	int jumped = 99;
    
    	if (moves_GP[1] > moves_GP[0])
    		jumped = ((moves_GP[1] - moves_GP[0]) / 2) + moves_GP[0];
    
    	else if (moves_GP[0] > moves_GP[1])
    		jumped = ((moves_GP[0] - moves_GP[1]) / 2) + moves_GP[1] - 1;
    
    	return jumped;
    }
    
    
    
    //Check if the moved piece needs to be knighted
    bool GamePlay::king_me() {
    
    	if(b_w[1] == 'B' && row2 == 1) 
    		return true;
    	else if (b_w[1] == 'W' && row2 == 8) 
    		return true;
    	else
    		return false;
    }
    
    bool GamePlay::game_over(string s) {
    	//		no legal move available, no pieces left;
    	//Needs to be finished
    
    	
    
    
    	if (b_w[1] == 'B' && (black_pieces_left == 0 || s == "pass")) {
    		cout << endl << endl << "WHITE WINS!!" << endl << endl;
    		return true;
    	}
    
    	else if (b_w[1] == 'W' && (white_pieces_left == 0 || s == "pass")) {
    		cout << endl << endl << "BLACK WINS!!" << endl << endl;
    		return true;
    	}
    
    	else if (s == "False")
    		return false;
    
    	else
    		return false;
    }
    
    
    
    
    
    int main() {
    
    	int i,j;
    	int moves[4];
    	string resign = "False";
    	string record = "\nRecord Of Moves For Game:\n";
    	
    	//Board b;
    	GamePlay g;
    	//b.setup();
    	
    	do {
    		
    		g.b.print_board();
    		
    		j=0;
    		for (i = 0; i < 4; i++) {
    			moves[i] = 0;
    		}
    		
    		cout << b_w << "'s go: ";
    
    		string t="";
    		getline(cin,t);
    		StringTokenizer st(t);
    		cin.ignore();
    
    		/*cout << b_w << "'s go: ";
    
    		string t;
    		getline(cin,t);
    		cin.ignore();
    			
    		StringTokenizer st(t);
    		*/
    
    		while (!st.end()) {
    			
    			if (st.token_type() == NUMBER) {
    				moves[j] = st.last_number();
    				j++;
    			}
    
    			else if (st.token_type() == WORD) {
    				resign = st.last_word();
    			}
    
    			
    			st.next();			
    		}
    		
    		//***QUIT***
    		if (moves[0] == 99)
    			break;
    	
    
    		g.check_move(moves[0], moves[1]);
    			
    		if (resign != "pass")
    		{
    			if (g.legal() == true) 
    			{
    
    				record = record + b_w + ": " + t + "\n";
    
    				//Checking if King
    				if(b_w[1] == 'B') 
    					g.b.reset_space(moves[0]-1,moves[1]-1,g.king_me());
    				else if (b_w[1] == 'W') 
    					g.b.reset_space(moves[0]-1,moves[1]-1,g.king_me());
    					
    				//Regular Pieces
    				else
    					g.b.reset_space(moves[0]-1,moves[1]-1,g.king_me());
    
    				//Switch Player
    				if (b_w[1] == 'B')
    					b_w = " White";
    				else if (b_w[1] == 'W')
    					b_w = " Black";
    			}
    		}
    
    	} while (g.game_over(resign) == false);
    
    	cout << record << endl;
    
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You're not going to find many people willing to debug 500+ lines of incomplete code
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Debug the code one line at a time. There are two very possible programs. One is logic error (you left something essential out of the design) or syntax (you typed in the wrong syntax).

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Need book to program game into multiplayer...
    By edomingox in forum Game Programming
    Replies: 3
    Last Post: 10-02-2008, 09:26 AM
  3. C# - Building a SCUMM-like game.. questions
    By Iyouboushi in forum Game Programming
    Replies: 0
    Last Post: 05-24-2008, 10:54 PM
  4. Try my game
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 09-15-2004, 11:58 AM
  5. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM