checkers

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

63 Replies - 3698 Views - Last Post: 05 May 2018 - 07:40 PM Rate Topic: -----

#1 pbivens85   User is offline

  • D.I.C Addict

Reputation: -13
  • View blog
  • Posts: 507
  • Joined: 22-October 13

checkers

Posted 17 March 2018 - 02:15 PM

I have decided to work on a checkers game using c++ and text based input. here is the code for the initial checkers placement.
#include <iostream>

using namespace std;

char board[8][8];

int main()
{
	board[0][1] = 'b';
	board[0][3] = 'b';
	board[0][5] = 'b';
	board[0][7] = 'b';

	board[1][0] = 'b';
	board[1][2] = 'b';
	board[1][4] = 'b';
	board[1][6] = 'b';

	board[2][1] = 'b';
	board[2][3] = 'b';
	board[2][5] = 'b';
	board[2][7] = 'b';

	board[5][0] = 'w';
	board[5][2] = 'w';
	board[5][4] = 'w';
	board[5][6] = 'w';

	board[6][1] = 'w';
	board[6][3] = 'w';
	board[6][5] = 'w';
	board[6][7] = 'w';

	board[7][0] = 'w';
	board[7][2] = 'w';
	board[7][4] = 'w';
	board[7][6] = 'w';

	for (int i = 0; i < 8; i++)
	{
		for (int j = 0; j < 8; j++)
		{
			cout << board[i][j];
		}
		cout << endl;
	}

	cout << endl;

	system("pause");
	return 0;
}


however I am unsure of how to implement the player and board interaction, I am working on the menu I need.

Is This A Good Question/Topic? 0
  • +

Replies To: checkers

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5339
  • View blog
  • Posts: 14,234
  • Joined: 18-April 07

Re: checkers

Posted 17 March 2018 - 02:32 PM

Well you can start by asking the user to enter in a set of coordinates of the checker to move and then a set to where you want to place the checker. Those will then need to be verified (to see if it is their piece and a legal move) and then you can set the new place on the board with the piece and leave the old spot empty.

How you specify the coordinates is up to you. Most people have trouble thinking of the board in all numbers like checker at 3,2 but might better remember C2 (The first row being A, second being B third being C and then column 2.) Then you have move checker C2 to E4 as a sign of a jump to capture. Remember to use your functions here. Asking for a source and destination for a checker might itself be its own function.

:)
Was This Post Helpful? 0
  • +
  • -

#3 pbivens85   User is offline

  • D.I.C Addict

Reputation: -13
  • View blog
  • Posts: 507
  • Joined: 22-October 13

Re: checkers

Posted 17 March 2018 - 02:35 PM

you know I got this project from your posting and e book, thanks
Was This Post Helpful? 2
  • +
  • -

#4 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5339
  • View blog
  • Posts: 14,234
  • Joined: 18-April 07

Re: checkers

Posted 17 March 2018 - 02:38 PM

Ah well, good to see someone is finding it useful and that it is helping you. The fact you have some basic code already and asking questions means you are well on your way to great learning. :)
Was This Post Helpful? 0
  • +
  • -

#5 pbivens85   User is offline

  • D.I.C Addict

Reputation: -13
  • View blog
  • Posts: 507
  • Joined: 22-October 13

Re: checkers

Posted 17 March 2018 - 02:42 PM

thanks for all the encourement :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#6 pbivens85   User is offline

  • D.I.C Addict

Reputation: -13
  • View blog
  • Posts: 507
  • Joined: 22-October 13

Re: checkers

Posted 18 March 2018 - 07:49 PM

I have simple question. how do you convert from char to enum.
here is the code I am having trouble with
	piece = (check)row_one;


Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14771
  • View blog
  • Posts: 59,047
  • Joined: 12-June 08

Re: checkers

Posted 18 March 2018 - 09:14 PM

Which is the enum in that line of code?
Was This Post Helpful? 0
  • +
  • -

#8 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7380
  • View blog
  • Posts: 15,311
  • Joined: 16-October 07

Re: checkers

Posted 19 March 2018 - 05:19 AM

Given what you have, I'd start with:
#include <iostream>

// global?  No.
// char board[8][8];
const int ROWS=8, COLS=8;

const char CELL_BLACK = 'b';
const char CELL_WHITE = 'w';
const char CELL_EMPTY = ' ';

typedef char Board[ROWS][COLS];

void initBoard(Board);
void showBoard(Board);

int main() {
    Board board;
    initBoard(board);
    showBoard(board);
    return 0;
}



However, that's still just C with classes. If you're using C++, you should actually use classes and other tools.

So:
class Board {
public:
    enum Cell { CELL_BLACK, CELL_WHITE, CELL_EMPTY, CELL_INVALID };
    static const int ROWS=8, COLS=8;
    Board();
    void print(std::ostream &) const;
private:
    Cell cells[ROWS][COLS];
    int turn;
    // probably other things
};

std::ostream &operator<<(std::ostream &, const Board &);


int main() {
    Board board;
    std::cout << board << std::endl;

    return 0;
}



Now, perhaps more completely:
#include <iostream>

class Board {
public:
    enum Cell { CELL_BLACK, CELL_WHITE, CELL_EMPTY, CELL_INVALID };
    static const int ROWS=8, COLS=8;
    struct Pos {
        const int row, col;
        Pos(int row, int col);
    };
    Board();
    void print(std::ostream &) const;
    bool move(const Pos &from, const Pos &to);
    Cell at(const Pos &) const;
private:
    Cell cells[ROWS][COLS];
    int turn;
};

std::ostream &operator<<(std::ostream &, const Board &);



A move would check for various things. If the from position contains a valid piece. If the to position is valid for that piece and empty, etc. Once all the checks are passed, the to position is set to the piece, the from position is now empty.

Upon further consideration, you'll probably want two more enum values:
enum Cell { CELL_BLACK, CELL_WHITE, CELL_EMPTY, CELL_BLACK_ROYAL, CELL_WHITE_ROYAL, CELL_INVALID };



Good luck.
Was This Post Helpful? 0
  • +
  • -

#9 pbivens85   User is offline

  • D.I.C Addict

Reputation: -13
  • View blog
  • Posts: 507
  • Joined: 22-October 13

Re: checkers

Posted 19 March 2018 - 01:43 PM

thanks for all the code, but I want to do this on my own , I am trying to learn code.
Was This Post Helpful? 0
  • +
  • -

#10 pbivens85   User is offline

  • D.I.C Addict

Reputation: -13
  • View blog
  • Posts: 507
  • Joined: 22-October 13

Re: checkers

Posted 20 March 2018 - 06:22 PM

well I have made good progress on my game. can I get some feedback on my player interactivity. please don't give me code.
#include <iostream>

using namespace std;

char board[8][8];
enum check {A,B,C,D,E,F,G,H} piece;
char dir = NULL;

char row_one;
int col_one;
char row_two;
int col_two;

void move();
void quit();

int main()
{
	board[0][1] = 'b';
	board[0][3] = 'b';
	board[0][5] = 'b';
	board[0][7] = 'b';

	board[1][0] = 'b';
	board[1][2] = 'b';
	board[1][4] = 'b';
	board[1][6] = 'b';

	board[2][1] = 'b';
	board[2][3] = 'b';
	board[2][5] = 'b';
	board[2][7] = 'b';

	board[5][0] = 'w';
	board[5][2] = 'w';
	board[5][4] = 'w';
	board[5][6] = 'w';

	board[6][1] = 'w';
	board[6][3] = 'w';
	board[6][5] = 'w';
	board[6][7] = 'w';

	board[7][0] = 'w';
	board[7][2] = 'w';
	board[7][4] = 'w';
	board[7][6] = 'w';

	for (int i = 0; i < 8; i++)
	{
		for (int j = 0; j < 8; j++)
		{
			cout << board[i][j];
		}
		cout << endl;
	}
	while (dir != 'q')
	{
		move();

		piece = (check)row_one;

		switch (piece)
		{
		case 65:
			board[0][col_one] = ' ';
			cout << A << endl;
			break;
		case 66:
			board[1][col_one] = ' ';
			break;
		case 67:
			board[2][col_one] = ' ';
			break;
		case 68:
			board[3][col_one] = ' ';
			break;
		case 69:
			board[4][col_one] = ' ';
			break;
		case 70:
			board[5][col_one] = ' ';
			break;
		case 71:
			board[6][col_one] = ' ';
			break;
		case 72:
			board[7][col_one] = ' ';
			break;
		}

		piece = (check)row_two;

		switch (piece)
		{
		case 65:
			board[0][col_two] = 'w';
			break;
		case 66:
			board[1][col_two] = 'w';
			break;
		case 67:
			board[2][col_two] = 'w';
			break;
		case 68:
			board[3][col_two] = 'w';
			break;
		case 69:
			board[4][col_two] = 'w';
			break;
		case 70:
			board[5][col_two] = 'w';
			break;
		case 71:
			board[6][col_two] = 'w';
			break;
		case 72:
			board[7][col_two] = 'w';
			break;
		}

		for (int i = 0; i < 8; i++)
		{
			for (int j = 0; j < 8; j++)
			{
				cout << board[i][j];
			}
			cout << endl;
		}
		quit();
	}
	system("pause");
	return 0;
}

void move()
{
	cout << "Move White Man From: ";
	cin >> row_one >> col_one;
	cout << "Move White Man To: ";
	cin >> row_two >> col_two;
}

void quit()
{
	cout << "q to quit: ";
	cout << "c to continue: ";
	cin >> dir;
}


Was This Post Helpful? 0
  • +
  • -

#11 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7380
  • View blog
  • Posts: 15,311
  • Joined: 16-October 07

Re: checkers

Posted 21 March 2018 - 02:48 AM

Hmm... I haven't given you code, but recommended structure. Alas, the largest fail of structure is everything in a global context, which you seem to insist on doing.

Last attempt:
#include <iostream>

using namespace std;

// no magic numbers!
// no globals
// char board[8][8];
// char dir = NULL; // this is meaningless
// char row_one;
// int col_one;
// char row_two;
// int col_two;

// enum fine, global bad
// enum check { A, B, C, D, E, F, G, H } piece;
enum check { A, B, C, D, E, F, G, H };

// define board size
const int ROWS = 8, COLS = 8;

// enum fine, global bad
// enum check { A, B, C, D, E, F, G, H } piece;
enum check { A, B, C, D, E, F, G, H };

// gather up the variables you feel you need to share everywhere
struct Game {
    char board[ROWS][COLS];
    int turn;
};

// define move, even if it is an odd design
struct Move {
    // if this is supposed to be check, make it check
    // char row_one;
    check row_one;
    int col_one;
    check row_two;
    int col_two;
};

// return that move
// void move();
Move getMove();

// return result
// void quit();
bool askUserDone();

// don't do every bloody thing in main
void initGame(Game &);
void showBoard(Game &);
void makeMove(Game &, Move &);

int main() {
    Game game;
    initGame(game);
    showBoard(game);

    bool done = false;
    while (done) {
        Move move = getMove();
        makeMove(game, move);
        showBoard(game);

        done = askUserDone();
    }
    system("pause");
    return 0;
}



Perhaps:
struct BoardPos {
    int row, col;
};

void getUserMove(BoardPos &from, BoardPos &to);
void makeMove(Game &, BoardPos &from, BoardPos &to);



This is not code, this is structure. You still have to do the work.

If you want people to simply guide you down the path you've myopically chosen to walk without criticizing it or attempting to offer better choices, you will be disappointed. If you insist on a path that any decent programmer would view as spaghetti code and obstinately stick to that path, the voices offering well meaning advise will go silent.

I'm not trying to be cruel here. I am happy you wish to write your very own game. It is great that you want to write you own code. Taking advice, or looking at someone else's code, does not invalidate this goal. You learn by example. If you reject all examples as not yours, I'm afraid learning will be impossible.
Was This Post Helpful? 2
  • +
  • -

#12 pbivens85   User is offline

  • D.I.C Addict

Reputation: -13
  • View blog
  • Posts: 507
  • Joined: 22-October 13

Re: checkers

Posted 21 March 2018 - 02:43 PM

I am trying to get better at coding, why is globals bad? thanks for all the help baavgai.

This post has been edited by pbivens85: 21 March 2018 - 03:01 PM

Was This Post Helpful? 0
  • +
  • -

#13 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7380
  • View blog
  • Posts: 15,311
  • Joined: 16-October 07

Re: checkers

Posted 21 March 2018 - 03:29 PM

View Postpbivens85, on 21 March 2018 - 04:43 PM, said:

I am trying to get better at coding

Excellent!

View Postpbivens85, on 21 March 2018 - 04:43 PM, said:

but I don't want to copy someone else's code

To get better at coding, you need to be able to read someone else's code. Both good and bad. Looking at good code will help you see good practices in action. Looking at bad code will hopefully get you thinking "that's not good, how can I fix it."

Blindly copying code will teach you nothing. Understanding extant code and considering how you might manipulate it to your purposes will teach you a great deal. If you want to get better, aim for the latter.

View Postpbivens85, on 21 March 2018 - 04:43 PM, said:

why is globals bad?

Good question.

Programming is, among over things, an exercise in controlling complexity. The more moving parts, the more complex. Global variables can be changed in any part of the program, so controlling and pin pointing where change happens to the variable can involve any part of the program. The fewer "mutable" elements in play at any given time, the easier it is to see what's going on.

A quick, simple, example:
bool done = false;

void work();
void quit();

int main() {
    while(!done) {
        work();
        quit();
    }
    return 0;
}

void work() {
    // do some stuff
    // and...
    done = true;
}

void quit() {
    char x;
    cout << "q to quit: ";
    cout << "c to continue: ";
    cin >> x;
    if (x=='q') { done = true; }
}



Now, clearly, if some part of your program changes done and you didn't plan on it, finding where that happened can be hard. However, you can get the effect you want with less exposure, with:
void work();
bool quit();

int main() {
    bool done = false;
    while(!done) {
        work();
        done = quit();
    }
    return 0;
}

void work() {
    // can't change done here
}

bool quit() {
    char x;
    cout << "q to quit: ";
    cout << "c to continue: ";
    cin >> x;
    return (x=='q');
}



I found a good write up here: http://www.nickhodge...ting-Scope.aspx

Also, from the inventor of the language:

Quote

Maybe "just one little global variable" isn't too unmanageable, but that style leads to code that is useless except to its original programmer
-- Stroustrup, Bjarne. The C++ Programming Language. pp. 467.


There is a concept in programming of "best practices." While a programming language offers you an entire lexicon to implement your vision with, it's as easy to write Twilight as Dracula. Time tested techniques help make coding more elegant and maintainable.

e.g. Effective C++ by Scott Meyers is respected tome on these. At the same time, you'll find programmers who will disagree with some or much of such books. The "best" part is, as always, somewhat subjective. However, it is a good starting point.
Was This Post Helpful? 3
  • +
  • -

#14 pbivens85   User is offline

  • D.I.C Addict

Reputation: -13
  • View blog
  • Posts: 507
  • Joined: 22-October 13

Re: checkers

Posted 21 March 2018 - 03:34 PM

cool thanks I will rework my code, I am going to implement classes and oop techniques. this may take a while
Was This Post Helpful? 0
  • +
  • -

#15 pbivens85   User is offline

  • D.I.C Addict

Reputation: -13
  • View blog
  • Posts: 507
  • Joined: 22-October 13

Re: checkers

Posted 22 March 2018 - 02:26 PM

well here is my reworked code, I am using classes and oop techniques
#include <iostream>

using namespace std;

char board[8][8];

class checkers
{
public:
	enum check{A,B,C,D,E,F,G,H}piece;
	char dir;
	void move();
	void quit();
	void init_board();
	void move_piece();
private:
	char row_one;
	int col_one;
	char row_two;
	int col_two;
};

void checkers::move()
{
	cout << "Move White Man From: ";
	cin >> row_one >> col_one;
	cout << "Move White Man To: ";
	cin >> row_two >> col_two;
}

void checkers::quit()
{
	cout << "q to quit: " << endl;
	cout << "c to continue: " << endl;
	cin >> dir;
}

void checkers::init_board()
{
	board[0][1] = 'b';
	board[0][3] = 'b';
	board[0][5] = 'b';
	board[0][7] = 'b';

	board[1][0] = 'b';
	board[1][2] = 'b';
	board[1][4] = 'b';
	board[1][6] = 'b';

	board[2][1] = 'b';
	board[2][3] = 'b';
	board[2][5] = 'b';
	board[2][7] = 'b';

	board[5][0] = 'w';
	board[5][2] = 'w';
	board[5][4] = 'w';
	board[5][6] = 'w';

	board[6][1] = 'w';
	board[6][3] = 'w';
	board[6][5] = 'w';
	board[6][7] = 'w';

	board[7][0] = 'w';
	board[7][2] = 'w';
	board[7][4] = 'w';
	board[7][6] = 'w';

	for (int i = 0; i < 8; i++)
	{
		for (int j = 0; j < 8; j++)
		{
			cout << board[i][j];
		}
		cout << endl;
	}

}

int main()
{
	checkers check;
	check.init_board();

	system("pause");
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »