Sign in to follow this  

First game well on its way! looking for some pointers...

This topic is 4813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ive been reading these boards for about 2 weeks now. I stumbled accross this site while looking for some C++ tutorials, and now im hooked. Im here reading the boards every day. This will be my first of what i hope will become MANY posts on these forums. Well anyway, ive been working on this here and there for a few days now, and its almost ready. I decided that i wanted to tackle something a bit more complex than tic-tac-toe for my first game, so i decided to go with checkers instead! Now i have it almost ready, its playable, but i havent written the AI for the single player vs the computer option yet. But the 2 player option works just fine...(or should anyway :P ) Anyway, heres the code, let me know what you all think! checkers.cpp
#include <iostream.h>
#include <stdlib.h>
#include "checkers.h"

void New_Game(int num_players);
void Update_Board(Player player1,Player player2);
void DisplayBoard();
void Single_Player(Player player1,Player player2);
void Multi_Player(Player player1,Player player2);
bool VictoryCheck(int VictoryP1, int VictoryP2);

int board[8][8];


int main()
{
	int menu_choice;
	
	do
	{
		cout<<"Welcome to Alima's Checkers game!\n";
		cout<<"1 -- New 1 Player Game\n";
		cout<<"2 -- New 2 Player Game\n";
		cout<<"3 -- Quit\n";
		cout<<endl<<"Enter your choice: ";
		cin>>menu_choice;

		switch(menu_choice)
		{
			case 1:
				New_Game(1);
				break;
			case 2:
				New_Game(2);
				break;
			default:
				cout<<"Thanks for playing!\n";
				break;
		}
	}
	while(menu_choice<3);

	return(0);
}

void New_Game(int num_players)
{

	Player player1;
	Player player2;

	player1.setup(1);
	player2.setup(2);

	for (int y=0;y<8;y++)
	{
		for (int x=0;x<8;x++)
			board[x][y]=0;
	}

	if (num_players==1)
		Single_Player(player1,player2);
	else
		Multi_Player(player1,player2);
}

void Update_Board(Player player1,Player player2)
{

	for (int x=0;x<8;x++)
	{
		for (int y=0;y<8;y++)
		{
			board[x][y]=0;
		}
	}

	for (int z=0;z<12;z++)
	{
		board[player1.pieces[z].x_pos][player1.pieces[z].y_pos]=1;
		board[player2.pieces[z].x_pos][player2.pieces[z].y_pos]=2;
	}
}

void DisplayBoard()
{
	system("cls");

	cout<<"  1 2 3 4 5 6 7 8 X\n";

	for (int y=0;y<8;y++)
	{
		cout<<(y+1)<<"|";
		for (int x=0;x<8;x++)
		{
			if(board[x][y]==1)
				cout<<"X|";
			else if(board[x][y]==2)
				cout<<"O|";
			else
				cout<<" |";
		}
		cout<<"\n";
	}
	cout<<"Y\n";
}

void Single_Player(Player player1,Player player2)
{
	cout<<"Under Construction,,,\n\n";
}

void Multi_Player(Player player1,Player player2)
{
	bool victory=false;

	int VictoryP2, VictoryP1;

	int current_player=1;
	int jump=12;

	do
	{
		VictoryP1=0;
		VictoryP2=0;
		
		Update_Board(player1,player2);
		DisplayBoard();
		
		if(current_player==1)
		{
			jump=player1.Player_Turn(player2.pieces,1);
			if (jump<12){player2.pieces[jump].x_pos=8;player2.pieces[jump].y_pos=8;}
			current_player++;
			jump=12;
		}
		else
		{
			jump=player2.Player_Turn(player1.pieces,2);
			if (jump<12){player1.pieces[jump].x_pos=8;player1.pieces[jump].y_pos=8;}
			current_player--;
			jump=12;
		}

		for (int c=0;c<12;c++)
		{
			if ((player1.pieces[c].x_pos==8) && (player1.pieces[c].x_pos==8))
				VictoryP2++;
			else if ((player2.pieces[c].x_pos==8) && (player2.pieces[c].x_pos==8))
				VictoryP1++;
		}
		
		victory=VictoryCheck(VictoryP1, VictoryP2);
			
	}
	while(!victory);
}

bool VictoryCheck(int VictoryP1, int VictoryP2)
{
	if (VictoryP1==12)
	{
		system("cls");
		cout<<"Player1 Wins!!!\n";
		cin.get();
		return (true);
	}
	else if (VictoryP2==12)
	{
		system("cls");
		cout<<"Player2 Wins!!!\n";
		cin.get();
		return (true);
	}

	return (false);
}

checkers.h
#include <iostream.h>

struct piece
{
	int x_pos;
	int y_pos;
//	bool active=1;
//	bool king=0;
};

class Player
{
public:
	piece pieces[12];

	void setup(int player_num);
	int Player_Turn(piece sPlayer2[12], int player_num);
	void Comp_Turn();

	Player();
	~Player();

private:
	bool CheckPiece(int xOld, int yOld, int *CurrentPiece);
	bool CheckDest(int PlayerNum, int CurrentPiece, int xNew, int yNew, piece sPlayer2[12], int *DeadPiece);
	int JumpCheck(int xNew, int yNew, piece sPlayer2[12]);
};

Player::Player()
{
}

Player::~Player()
{
}

void Player::setup(int player_num)
{
	if (player_num==1)
	{
		pieces[0].x_pos=0;
		pieces[0].y_pos=0;

		pieces[1].x_pos=2;
		pieces[1].y_pos=0;

		pieces[2].x_pos=4;
		pieces[2].y_pos=0;

		pieces[3].x_pos=6;
		pieces[3].y_pos=0;

		pieces[4].x_pos=1;
		pieces[4].y_pos=1;

		pieces[5].x_pos=3;
		pieces[5].y_pos=1;

		pieces[6].x_pos=5;
		pieces[6].y_pos=1;

		pieces[7].x_pos=7;
		pieces[7].y_pos=1;

		pieces[8].x_pos=0;
		pieces[8].y_pos=2;

		pieces[9].x_pos=2;
		pieces[9].y_pos=2;

		pieces[10].x_pos=4;
		pieces[10].y_pos=2;

		pieces[11].x_pos=6;
		pieces[11].y_pos=2;
	}
	else
	{
		pieces[0].x_pos=1;
		pieces[0].y_pos=5;

		pieces[1].x_pos=3;
		pieces[1].y_pos=5;

		pieces[2].x_pos=5;
		pieces[2].y_pos=5;

		pieces[3].x_pos=7;
		pieces[3].y_pos=5;

		pieces[4].x_pos=0;
		pieces[4].y_pos=6;

		pieces[5].x_pos=2;
		pieces[5].y_pos=6;

		pieces[6].x_pos=4;
		pieces[6].y_pos=6;

		pieces[7].x_pos=6;
		pieces[7].y_pos=6;

		pieces[8].x_pos=1;
		pieces[8].y_pos=7;

		pieces[9].x_pos=3;
		pieces[9].y_pos=7;

		pieces[10].x_pos=5;
		pieces[10].y_pos=7;

		pieces[11].x_pos=7;
		pieces[11].y_pos=7;
	}

}

int Player::Player_Turn(piece sPlayer2[12], int player_num)
{
	int xOld;
	int yOld;
	int xNew;
	int yNew;

	int CurrentPiece=0;
	int DeadPiece=12;

	bool valid=false;


	do
	{
		do
		{
			cout<<"Player"<<player_num<<", Enter the X and Y coord for the piece you wish to move: ";
			cin>>xOld>>yOld;
			
			xOld--;
			yOld--;
			valid=CheckPiece(xOld, yOld,&CurrentPiece);
		}
		while(!valid);
		
		valid=false;

		cout<<"Player"<<player_num<<", Enter the X and Y coord for where you want to move thie piece: ";
		cin>>xNew>>yNew;
		
		xNew--;
		yNew--;

		valid=CheckDest(player_num, CurrentPiece, xNew, yNew, sPlayer2, &DeadPiece);
		if (!valid)
			cout<<"You cannot move your piece there!\n";
	}
	while(!valid);

	if (DeadPiece<12)
		return (DeadPiece);

	return 12;
}

void Player::Comp_Turn()
{
	int xOld;
	int yOld;
	int xNew;
	int yNew;
}

bool Player::CheckPiece(int xOld, int yOld, int *CurrentPiece)
{
	for (int z=0;z<12;z++)
	{
		if ((pieces[z].x_pos==xOld)&&(pieces[z].y_pos==yOld))
		{
			*CurrentPiece=z;
			return (true);
		}
	}
	return (false);
}

bool Player::CheckDest(int PlayerNum, int CurrentPiece, int xNew, int yNew, piece sPlayer2[12], int *DeadPiece)
{
	int Jump=12;

	for (int z=0;z<12;z++)
	{
		if ((xNew==sPlayer2[z].x_pos) && (yNew==sPlayer2[z].y_pos))
			return (false);
	}

	switch(PlayerNum)
	{
	case 1:
		if (((pieces[CurrentPiece].x_pos+1==xNew)||(pieces[CurrentPiece].x_pos-1==xNew))&&(pieces[CurrentPiece].y_pos+1==yNew))
		{
			pieces[CurrentPiece].x_pos=xNew;
			pieces[CurrentPiece].y_pos=yNew;
			return (true);
		}
		else if ((pieces[CurrentPiece].x_pos+2==xNew)||(pieces[CurrentPiece].x_pos-2==xNew)&&(pieces[CurrentPiece].y_pos+2==yNew))
		{
			Jump=JumpCheck(xNew, yNew-1, sPlayer2);
			if (Jump==12)
			{
				return (false);
			}
			else
			{
				pieces[CurrentPiece].x_pos=xNew;
				pieces[CurrentPiece].y_pos=yNew;
				*DeadPiece=Jump;
				return (true);
			}
		}
		break;
	case 2:
		if (((pieces[CurrentPiece].x_pos+1==xNew)||(pieces[CurrentPiece].x_pos-1==xNew))&&(pieces[CurrentPiece].y_pos-1==yNew))
		{
			pieces[CurrentPiece].x_pos=xNew;
			pieces[CurrentPiece].y_pos=yNew;
			return (true);
		}
		else if ((pieces[CurrentPiece].x_pos+2==xNew)||(pieces[CurrentPiece].x_pos-2==xNew)&&(pieces[CurrentPiece].y_pos-2==yNew))
		{
			Jump=JumpCheck(xNew, yNew+1, sPlayer2);
			if (Jump==12)
				return (false);
			else
			{
				pieces[CurrentPiece].x_pos=xNew;
				pieces[CurrentPiece].y_pos=yNew;
				*DeadPiece=Jump;
				return (true);
			}
		}
		break;
	default:
		break;
	}
	return (false);
}

int Player::JumpCheck(int xNew, int yNew, piece sPlayer2[12])
{
	for (int z=0;z<12;z++)
	{
		if ((sPlayer2[z].x_pos==xNew-1)||(sPlayer2[z].x_pos==xNew+1))
		{
			if (sPlayer2[z].y_pos==yNew)
				return z;
		}
	}
	return (NULL);
}

please let me know what yall think. thanks in advance and i look forward to being part of the GameDev community! Alima

Share this post


Link to post
Share on other sites
Congrats on your first game! There is one little error that i found playing it for a few minutes. i was able to move my game piece from 2 4 to 4 5. so basically this happens:

from 2 4:

1 2 3 4 5 6 7 8 X
1| | |X| |X| |X| |
2| | | |X| |X| |X|
3|X| |X| |X| |X| |
4| |X| | | | | | |
5| | | | | | | | |
6| | |O|O| |O| |O|
7| | |O| |O| |O| |
8| |O| |O| |O| |O|
Y

to:

1 2 3 4 5 6 7 8 X
1| | |X| |X| |X| |
2| | | |X| |X| |X|
3|X| |X| |X| |X| |
4| | | | | | | | |
5| | | |X| | | | |
6| | |O|O| |O| |O|
7| | |O| |O| |O| |
8| |O| |O| |O| |O|
Y

and as you can see i did it earlier as well from the O that's at 3 6. So cool nonetheless! Can't wait to the ai!

Share this post


Link to post
Share on other sites
if you put [ code ] and [ /code ] around the your ASCII diagram, it'll look much better [smile]
edit: without the spaces

edit2 (demo):

Congrats on your first game! There is one little error that i found playing it for a few minutes.
i was able to move my game piece from 2 4 to 4 5. so basically this happens:

from 2 4:

1 2 3 4 5 6 7 8 X
1| | |X| |X| |X| |
2| | | |X| |X| |X|
3|X| |X| |X| |X| |
4| |X| | | | | | |
5| | | | | | | | |
6| | |O|O| |O| |O|
7| | |O| |O| |O| |
8| |O| |O| |O| |O|
Y

to:

1 2 3 4 5 6 7 8 X
1| | |X| |X| |X| |
2| | | |X| |X| |X|
3|X| |X| |X| |X| |
4| | | | | | | | |
5| | | |X| | | | |
6| | |O|O| |O| |O|
7| | |O| |O| |O| |
8| |O| |O| |O| |O|
Y

and as you can see i did it earlier as well from the O that's at 3 6.
So cool nonetheless! Can't wait to the ai!

Share this post


Link to post
Share on other sites

This topic is 4813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this