5 Replies - 1725 Views - Last Post: 18 March 2013 - 03:10 AM Rate Topic: -----

#1 Vzen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-March 13

Connect Four program - Symbol does not display correctly

Posted 17 March 2013 - 09:22 AM

Hi guys,

I've been working on a simple connect four c++ program, where two players play against each other (no A.I.)
It is the first time i've written a c++ program using matrixes.
For some reason whenever I run my program it does not seem to show the symbols i assigned for both players, nor does it stop after it is supposed to reach four-in-a-row
I've experimented with two different display functions, neither seem to be working
#include<iostream>
#include "lvp\matrix.h"
#include<conio.h>


using namespace std;

bool check(int a, int B)/>/>/>;
matrix<char>grid(6,7);
void display(/*matrix<char> &m*/);
int gravitydrop(int b, char player);



const char BRICK=177;
const int rowsize=6;
char player=1;

int main()
{

	bool win=false;
	int holdc=0; //drop value hold2
	int holdinput; //hold
	int numPlaced=0;
	int count=0;

	
	system("cls");
	
	for(int a =0;a <= 5; a++){		//fill place with whitespace
		for(int b = 0; b<=6; b++)	//
			grid[a][b] = ' ';		//
	}	
	cout<<"Instructions: There are two players in a game of connect four."<<endl;
	cout<<"First to get four of their pieces in a linear line next to each other wins!"<<endl;
	display();
	_getch();


		
	while (!win)
	{
		if (holdc!=-1)
		{
				if (player==1)
				{
					cout<<"Player one, where do you wish to place your piece?"<<endl;
					player=2;
				}
				else
				{
					cout<<"Player two, where do you wish to place your piece?"<<endl;
					player=1;
				}
		}
		while(true)
		{
			if (numPlaced==42) break;
			cin>>holdinput;
			holdinput--;
			if (holdinput<=6 && holdinput>=0)break;
			else 
				cout<<"Please enter a valid number between 1 and 7"<<endl;
		}
	}
	if(numPlaced==42)
		_getch;
		
	holdc=gravitydrop(holdinput,player);

	if (holdc==-1)
		cout<<"column full. please choose another number."<<endl;
	else
	{
		win = check(holdc,holdinput);
		numPlaced++;
		system("cls");
		display();
	}

	if(numPlaced==42)
	{
		cout<<"It's a tie"<<endl;
		_getch;
		return 0;
	}
	if (player==1)
		cout<<"game won by player 1";

	else 
		cout<<"game won by player 2";

	_getch;		
	return 0;
}
/*void display(matrix<char> &m)
{
	for(int r=0; r<m.numrows(); r++)
	{
		//horizontal line
		for(int i=0; i<m.numcols()*2+1; i++)
			cout<<BRICK;
		cout<<endl;
		//lines containing actual elements
		for(int c=0; c<m.numcols(); c++)
		{
			cout<<BRICK<<m[r][c];
		}
		cout<<BRICK<<endl;
	}
	//horizontal line
	for(int i=0; i<m.numcols()*2+1; i++)
			cout<<BRICK;
	cout<<endl;
	//grid numbering
	for(int c=0; c<m.numcols(); c++)
	{
		cout<<" "<<c;
	}
	cout<<endl;

}*/
int gravitydrop(int b, char player)
{
	if(b >=0 && b<= 6)
	{
		if(grid[0][b] == ' ')
		{
			int i;
			for(i = 0;grid[i][b] == ' ';i++)
				if(i == 5)
					{
						grid[i][b] = player;
						return i;
					}
			i--;
			grid[i][b] =player;
			return i;
		}
		else
			return -1;
	
	}
	else
		return -1;
	

}
bool check(int a, int B)/>/>/>
{
	int vertcheck=1;
	int horcheck=1;
	int dia1check=1; // (\)
	int dia2check=1; // (/)
	int v;
	int h;

	for (v = a +1;grid[v][b]==player && v <= 5;v++,vertcheck++);  //checks to see if the player is same for four in a row or greater.
	for(v = a -1;grid[v][b] == player && v >= 0;v--,vertcheck++);
	if(vertcheck >= 4)
		return true;

	for(h = b -1;grid[a][h] == player && h>= 0;h--,horcheck++); //checks "4" 4-in-a-column
	for(h = b +1;grid[a][h] == player && h<= 6;h++,horcheck++);
    if(horcheck>= 4) 
		return true;

	for(v = a -1, h= b +1;grid[v][h] == player && v>=0 && h <= 6; dia2check++, v --, h++); //checks for (/)
    for(v = a +1, h= b -1;grid[v][h] == player && v<=5 && h>=0; dia2check++, v ++, h--);
    if(dia2check >= 4) 
		return true;

	for(v = a -1, h= b -1;grid[v][h] == player && v>=0 && h >=0; dia1check++,v --,h --); //checks for (\)
    for(v = a +1, h = b+1;grid[v][h] == player && v<=5 && h<=6;dia1check ++, v ++, h++);
    if(dia1check >= 4) 
		return true;
	
	return false;
}

void display(){
	cout<<" 1   2   3   4   5   6   7\n";
	for(int a = 0; a<= 5; a++)
	{
		for(int b =0; b <= 6; b++) cout<<char(218)<<char(196)<<char(191)<<" ";
		cout<<endl;
		for(int b =0; b <= 6; b++) cout<<char(179)<<grid[a][b]<<char(179)<<" ";
		cout<<endl;
		for(int b =0; b <= 6; b++) cout<<char(192)<<char(196)<<char(217)<<" ";
		cout<<endl;
	}
}



It would be a great help if someone could kindly point out why my pieces aren't displaying nor adding up correctly.

Is This A Good Question/Topic? 0
  • +

Replies To: Connect Four program - Symbol does not display correctly

#2 Vzen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-March 13

Re: Connect Four program - Symbol does not display correctly

Posted 17 March 2013 - 12:08 PM

So I have a project in which I have to use matrix to make a connect-four game.

For some reason, whenever I use
char grid [6][7];



to set my matrix, my program seems to run from beginning to end without a problem

However, whenever I try to use

matrix<char>grid(6,7,' ')


it couts correctly at first without any input, but when i try to choose an area to mark, it never displays it.
Could someone tell me the difference between the two and potentially how to alter my code to accommodate matrix?

Sorry for my bad explanation, I'm rather new at this.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,235
  • Joined: 23-August 08

Re: Connect Four program - Symbol does not display correctly

Posted 17 March 2013 - 12:16 PM

Merged member's two Connect Four topics
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3177
  • View blog
  • Posts: 9,632
  • Joined: 05-May 12

Re: Connect Four program - Symbol does not display correctly

Posted 17 March 2013 - 07:11 PM

It is a little hard to help you without seeing the header and implementation for the matrix class that you are using. Could you post the source code for the .h and .cpp files?
Was This Post Helpful? 0
  • +
  • -

#5 Vzen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-March 13

Re: Connect Four program - Symbol does not display correctly

Posted 17 March 2013 - 07:17 PM

View PostSkydiver, on 17 March 2013 - 07:11 PM, said:

It is a little hard to help you without seeing the header and implementation for the matrix class that you are using. Could you post the source code for the .h and .cpp files?


It does not permit me to upload either .h or .cpp files.Any other way to provide them?
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,235
  • Joined: 23-August 08

Re: Connect Four program - Symbol does not display correctly

Posted 18 March 2013 - 03:10 AM

Copy and paste them into code tags?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1