Hi, im a newbie in programming with only couple of months experience.
This game is a connect 4 game but works basically like a tic tac toe because insted of a piece dropping at the bottom of the board, it would stay in the selected area like tic tac toe

The problems are that when the program asks for the input, it does not show X or O. I think i messed with "turn" and my function for board but i dont know how to fix it. Another thing is that the "Scores" for the players dont show up and I want my board to have labels from 0-4 for row and 0-4 as well as for column but im lost on how to do it xD. I havent wrote the part to see whos the winner yet and if a draw happens

heres the code

#include <iostream>
using namespace std;
#define X -1
#define O 1

void Logo();
void Board1(int board1[5][5]);

int main()
{
	int player;
	int score1=0;
	int score2=0;
	int row;
	int col;
	int board1[5][5]={(0,0,0,0,0),
					  (0,0,0,0,0),
					  (0,0,0,0,0),
					  (0,0,0,0,0),
					  (0,0,0,0,0),
					  (0,0,0,0,0)};

		Logo();
	cout<<"Welcome to dash to four"<<endl;

			
	   for (int turn=1; turn <= 25 ; turn++) 
			{
				cout<<score1<<endl;
				cout<<score2<<endl;

					Board1(board1);

					if (turn%2==0)
						player=O;
						else
						player=X;

				do
				{
					cout<<"Player "<<(player==X ? "X" : "O")<<"Please enter a input of Row then Colmn: ";
					cin>>row>>col;
				}
				while(row<0 || row>24 || col<0 || col>24 || board1[row][col] != 0 );

        board1[row][col]=player;
				}
					if (board1[0][0]+board1[0][1]+board1[0][2]+board1[0][3]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[0][1]+board1[0][2]+board1[0][3]+board1[0][4]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[1][0]+board1[1][1]+board1[1][2]+board1[1][3]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[1][1]+board1[1][2]+board1[1][3]+board1[1][4]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[2][0]+board1[2][1]+board1[2][2]+board1[2][3]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[2][1]+board1[2][2]+board1[2][3]+board1[2][4]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[3][0]+board1[3][1]+board1[3][2]+board1[3][3]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[3][1]+board1[3][2]+board1[3][3]+board1[3][4]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[4][0]+board1[4][1]+board1[4][2]+board1[4][3]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[4][1]+board1[4][2]+board1[4][3]+board1[4][4]==-4)
						cout<<"Player 1 Sores!";
						score1++;

					if (board1[0][0]+board1[1][0]+board1[2][0]+board1[3][0]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[1][0]+board1[2][0]+board1[3][0]+board1[4][0]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[0][1]+board1[1][1]+board1[2][1]+board1[3][1]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[1][1]+board1[2][1]+board1[3][1]+board1[4][1]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[0][2]+board1[1][2]+board1[2][2]+board1[3][2]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[1][2]+board1[2][2]+board1[3][2]+board1[4][2]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[0][3]+board1[1][3]+board1[2][3]+board1[3][3]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[1][3]+board1[2][3]+board1[3][3]+board1[4][3]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[0][4]+board1[1][4]+board1[2][4]+board1[3][4]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[1][4]+board1[2][4]+board1[3][4]+board1[4][4]==-4)
						cout<<"Player 1 Sores!";
						score1++;

					if (board1[0][0]+board1[1][1]+board1[2][2]+board1[3][3]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[1][1]+board1[2][2]+board1[3][3]+board1[4][4]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[0][1]+board1[1][2]+board1[2][3]+board1[3][4]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[1][0]+board1[2][1]+board1[3][2]+board1[4][3]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[4][0]+board1[3][1]+board1[2][2]+board1[1][3]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[3][1]+board1[2][2]+board1[1][3]+board1[0][4]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[4][1]+board1[3][2]+board1[2][3]+board1[1][4]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[3][0]+board1[2][1]+board1[1][2]+board1[0][3]==-4)
						cout<<"Player 1 Sores!";
						score1++;
					if (board1[0][0]+board1[0][1]+board1[0][2]+board1[0][3]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[0][1]+board1[0][2]+board1[0][3]+board1[0][4]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[1][0]+board1[1][1]+board1[1][2]+board1[1][3]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[1][1]+board1[1][2]+board1[1][3]+board1[1][4]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[2][0]+board1[2][1]+board1[2][2]+board1[2][3]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[2][1]+board1[2][2]+board1[2][3]+board1[2][4]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[3][0]+board1[3][1]+board1[3][2]+board1[3][3]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[3][1]+board1[3][2]+board1[3][3]+board1[3][4]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[4][0]+board1[4][1]+board1[4][2]+board1[4][3]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[4][1]+board1[4][2]+board1[4][3]+board1[4][4]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;

					if (board1[0][0]+board1[1][0]+board1[2][0]+board1[3][0]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[1][0]+board1[2][0]+board1[3][0]+board1[4][0]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[0][1]+board1[1][1]+board1[2][1]+board1[3][1]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[1][1]+board1[2][1]+board1[3][1]+board1[4][1]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[0][2]+board1[1][2]+board1[2][2]+board1[3][2]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[1][2]+board1[2][2]+board1[3][2]+board1[4][2]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[0][3]+board1[1][3]+board1[2][3]+board1[3][3]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[1][3]+board1[2][3]+board1[3][3]+board1[4][3]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[0][4]+board1[1][4]+board1[2][4]+board1[3][4]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[1][4]+board1[2][4]+board1[3][4]+board1[4][4]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;

					if (board1[0][0]+board1[1][1]+board1[2][2]+board1[3][3]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[1][1]+board1[2][2]+board1[3][3]+board1[4][4]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[0][1]+board1[1][2]+board1[2][3]+board1[3][4]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[1][0]+board1[2][1]+board1[3][2]+board1[4][3]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[4][0]+board1[3][1]+board1[2][2]+board1[1][3]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[3][1]+board1[2][2]+board1[1][3]+board1[0][4]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[4][1]+board1[3][2]+board1[2][3]+board1[1][4]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					if (board1[3][0]+board1[2][1]+board1[1][2]+board1[0][3]==4)
						cout<<"Player2 Scores!"<<endl;
						score2++;
					
			return 0;
}

void Logo()
{
	cout<<"    ******************************"<<endl;
	cout<<"   ************DASH TO*************"<<endl;
	cout<<"  **********************************"<<endl;
	cout<<" *****************4******************"<<endl;
	cout<<"*****************4*4******************"<<endl;
	cout<<"****************4**4******************"<<endl;
	cout<<" **************444444****************"<<endl;
	cout<<"  *****************4****************"<<endl;
	cout<<"   ****************4***************"<<endl;
	cout<<"    ******************************"<<endl;
}
void Board1(int board1[5][5])
{
	cout << endl;
   
   for (int row=0; row<5; row++)
   {
      for (int col=0; col<5; col++)
      {
         cout << "| ";
         if (board1[row][col] == 0) cout << " ";
         else if (board1[5][5] == -1) cout << "X";
         else if (board1[5][5] == 1) cout << "O";
      }
      cout << "\n-------------" << endl;
   }
}

Really need help on fixing this thing asap

Recommended Answers

All 8 Replies

First off -- You know about loops, so what is that monstrosity of a list of
stuff deciding who has won. Why not use a loop.

The problem is actually in the last two else if statements.
board1[5][5] which does two things. (A) there is no board[5][5] the
array is ranged 0-4 + 0-4 so that is a random UNKNOWN memory location. (B) use didn't you mean [row][col].

(C) it is pain ugly.

const char PUnit[]="X 0"; 
for(int row=0;row<5;row++)
{
  for(int col=0;col<5;col++)
     std::cout<<PUnit[board1[row][col]];
  std::cout<<std::endl;
}

Note that the idea in this code, effectively transform -1/0/1 into characters. You might want a if guard on the values BUT it is possible that is only in debug mode as you shouldn't be able to get out of the -1,0,1 range on board1.

p.s Sorry but there are other errors with your code. BUT how to test code is also part of the process of learning to program. (and is easily transferable to any other language -- most likely a more important skill than how to write C++. )

Member Avatar for jencas

What the heck is X?

Replace

if (turn%2==0)
						player=O;
						else
						player=X;

by

player = turn % 2;

and

cout<<"Player "<<(player==X ? "X" : "O")<<"Please enter a input of Row then Colmn: ";

by

cout<<"Player "<<(player==0 ? "X" : "O")<<"Please enter a input of Row then Colmn: ";

and it should work (didn't test it!)

i dont know what to say...so...

void Board1(int board1[5][5])
{
	cout << endl;
   
   for (int row=0; row<5; row++)
   {
      for (int col=0; col<5; col++)
      {
         cout << "| ";
         if (board1[row][col] == 0) cout << " ";
         else if (board1[row][col] == -1) cout << "X";
         else if (board1[row][col] == 1) cout << "O";
      }
      cout << "\n-------------" << endl;
   }
}

X is....

#define X -1
#define O 1

rite!!....

commented: well spotted (#define) -- pure evil. +1

ok so i reviewed my program and saw a lot and i mean a lot of mistakes xD. so i fixed them and now...

my problem now is i cant label the rows as 0 1 2 3 4
i can only label the columns xD
and i have no idea on how to check if a tie happens

i know its ugly, ive just started to learn to program last 2 months and just 2 days of class per week

I encountered another problem

i labeled my columns already (not good though) but when i ran the program and if someone wins, say player X, it still asks for input though someone already won and i still cant figure out properly how to check for a tie AND I BADLY NEED THIS THING TO WORK :(

#include <iostream>
using namespace std;
#define X -1
#define O 1

void Logo();
void Board1(int board1[5][5]);

int main()
{
	int player;
	int row;
	int col;
	int turn;
	int board1[5][5]={(0,0,0,0,0),
					  (0,0,0,0,0),
					  (0,0,0,0,0),
					  (0,0,0,0,0),
					  (0,0,0,0,0),
					  (0,0,0,0,0)};

		Logo();
	cout<<"Welcome to dash to four"<<endl;

			
	   for (turn=1; turn<=25 ; turn++) 
			{

					Board1(board1);

					if (turn%2==0)
						player=O;
						else
						player=X;

				do
				{
					cout<<"Player "<<(player==X ? "X" : "O")<<" Please input of Row then Column: ";
					cin>>row>>col;
				}
				while(row<0 || row>24 || col<0 || col>24 || board1[row][col] != 0 );

				board1[row][col]=player;
				

					if (board1[0][0]+board1[0][1]+board1[0][2]+board1[0][3]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[0][1]+board1[0][2]+board1[0][3]+board1[0][4]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[1][0]+board1[1][1]+board1[1][2]+board1[1][3]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[1][1]+board1[1][2]+board1[1][3]+board1[1][4]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[2][0]+board1[2][1]+board1[2][2]+board1[2][3]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[2][1]+board1[2][2]+board1[2][3]+board1[2][4]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[3][0]+board1[3][1]+board1[3][2]+board1[3][3]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[3][1]+board1[3][2]+board1[3][3]+board1[3][4]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[4][0]+board1[4][1]+board1[4][2]+board1[4][3]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[4][1]+board1[4][2]+board1[4][3]+board1[4][4]==-4)
						cout<<"Player X Wins!"<<endl;

					if (board1[0][0]+board1[1][0]+board1[2][0]+board1[3][0]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[1][0]+board1[2][0]+board1[3][0]+board1[4][0]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[0][1]+board1[1][1]+board1[2][1]+board1[3][1]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[1][1]+board1[2][1]+board1[3][1]+board1[4][1]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[0][2]+board1[1][2]+board1[2][2]+board1[3][2]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[1][2]+board1[2][2]+board1[3][2]+board1[4][2]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[0][3]+board1[1][3]+board1[2][3]+board1[3][3]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[1][3]+board1[2][3]+board1[3][3]+board1[4][3]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[0][4]+board1[1][4]+board1[2][4]+board1[3][4]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[1][4]+board1[2][4]+board1[3][4]+board1[4][4]==-4)
						cout<<"Player X Wins!"<<endl;

					if (board1[0][0]+board1[1][1]+board1[2][2]+board1[3][3]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[1][1]+board1[2][2]+board1[3][3]+board1[4][4]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[0][1]+board1[1][2]+board1[2][3]+board1[3][4]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[1][0]+board1[2][1]+board1[3][2]+board1[4][3]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[4][0]+board1[3][1]+board1[2][2]+board1[1][3]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[3][1]+board1[2][2]+board1[1][3]+board1[0][4]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[4][1]+board1[3][2]+board1[2][3]+board1[1][4]==-4)
						cout<<"Player X Wins!"<<endl;
					if (board1[3][0]+board1[2][1]+board1[1][2]+board1[0][3]==-4)
						cout<<"Player X Wins!"<<endl;


					if (board1[0][0]+board1[0][1]+board1[0][2]+board1[0][3]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[0][1]+board1[0][2]+board1[0][3]+board1[0][4]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[1][0]+board1[1][1]+board1[1][2]+board1[1][3]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[1][1]+board1[1][2]+board1[1][3]+board1[1][4]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[2][0]+board1[2][1]+board1[2][2]+board1[2][3]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[2][1]+board1[2][2]+board1[2][3]+board1[2][4]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[3][0]+board1[3][1]+board1[3][2]+board1[3][3]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[3][1]+board1[3][2]+board1[3][3]+board1[3][4]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[4][0]+board1[4][1]+board1[4][2]+board1[4][3]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[4][1]+board1[4][2]+board1[4][3]+board1[4][4]==4)
						cout<<"Player O Wins!"<<endl;

					if (board1[0][0]+board1[1][0]+board1[2][0]+board1[3][0]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[1][0]+board1[2][0]+board1[3][0]+board1[4][0]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[0][1]+board1[1][1]+board1[2][1]+board1[3][1]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[1][1]+board1[2][1]+board1[3][1]+board1[4][1]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[0][2]+board1[1][2]+board1[2][2]+board1[3][2]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[1][2]+board1[2][2]+board1[3][2]+board1[4][2]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[0][3]+board1[1][3]+board1[2][3]+board1[3][3]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[1][3]+board1[2][3]+board1[3][3]+board1[4][3]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[0][4]+board1[1][4]+board1[2][4]+board1[3][4]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[1][4]+board1[2][4]+board1[3][4]+board1[4][4]==4)
						cout<<"Player O Wins!"<<endl;

					if (board1[0][0]+board1[1][1]+board1[2][2]+board1[3][3]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[1][1]+board1[2][2]+board1[3][3]+board1[4][4]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[0][1]+board1[1][2]+board1[2][3]+board1[3][4]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[1][0]+board1[2][1]+board1[3][2]+board1[4][3]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[4][0]+board1[3][1]+board1[2][2]+board1[1][3]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[3][1]+board1[2][2]+board1[1][3]+board1[0][4]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[4][1]+board1[3][2]+board1[2][3]+board1[1][4]==4)
						cout<<"Player O Wins!"<<endl;
					if (board1[3][0]+board1[2][1]+board1[1][2]+board1[0][3]==4)
						cout<<"Player O Wins!"<<endl;
					if (turn==26)
						cout<<"Draw!"<<endl;
					system("cls");
			}		
		
			return 0;
}

void Logo()
{
	cout<<"    ******************************"<<endl;
	cout<<"   ************DASH TO*************"<<endl;
	cout<<"  **********************************"<<endl;
	cout<<" *****************4******************"<<endl;
	cout<<"*****************4*4******************"<<endl;
	cout<<"****************4**4******************"<<endl;
	cout<<" **************444444****************"<<endl;
	cout<<"  *****************4****************"<<endl;
	cout<<"   ****************4***************"<<endl;
	cout<<"    ******************************"<<endl;
}
void Board1(int board1[5][5])
{	
	int counter=0;
	cout<<endl;
	cout<<"  0   1   2   3   4"<<endl;
	cout<<"---------------------"<<endl;
	for (int row=0; row<5; row++)
   {
      for (int col=0; col<5; col++)
      {
         cout<<"|  ";
         if (board1[row][col]==0) cout<<" ";
         else if (board1[row][col]==-1) cout<<"X";
         else if (board1[row][col]==1) cout<<"O";
      }
      cout<<"\n---------------------"<<counter<<endl;
	  counter++;
	 
   }
}

Let me see now. But sorry, a set of things were pointed out
(A) have you removed the #define ?
(B) have you improved the stupid do I win section
(C) Have you made ANY changes above the simplest 5=row, 5=col,
like actually improve the loop

Answers on a postcard please ..... Beyond that I am prepared to look at you ugly code for a total of the amount of time required to make your changes.

try to use if..else if..else..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.