I am using c++ to create a Connect 4 program, and now I am making graphics. I am
ID: 3575787 • Letter: I
Question
I am using c++ to create a Connect 4 program, and now I am making graphics. I am trying to get it such that when a player presses at a point on the screen, a token falls from that point to the farthest available spot. I have already figured out how to get the y and x coordinates for the start and end point. However, I do know how to animate it using GLUT/ openGL. I have a drawCircle() function to physically draw the token, but can anyone show me how to make the animation from point A to point B at a constant speed?
Explanation / Answer
#include "std_lib_facilities.h"
int main()
{
vector<char>row1;
vector<char>row2;
vector<char>row3;
vector<char>row4;
vector<char>row5;
vector<char>row6;
vector<char>row7;
int move1;
int win=0;
int i;
int> int twoscore=0;
char again='x';
for(int j=0;j<7;++j)
{
row1.push_back('O');
row1[0]='1';
row2.push_back('O');
row2[0]='2';
row3.push_back('O');
row3[0]='3';
row4.push_back('O');
row4[0]='4';
row5.push_back('O');
row5[0]='5';
row6.push_back('O');
row6[0]='6';
row7.push_back('O');
row7[0]='7';
}
while(again=='X'||again=='x')
{
cout<<"Player 1: "<<onescore<<" Player 2: "<<twoscore;
for(int j=0;j<7;++j)
{
if(j==0)
cout<<" "<<row1[j]<<" "<<row2[j]<<" "<<row3[j]<<" "<<row4[j]<<" "<<row5[j]<<" "<<row6[j]<<" "<<row7[j]<<" --------------- ";
if(j>0&&i<7)
cout<<" | "<<row1[j]<<" "<<row2[j]<<" "<<row3[j]<<" "<<row4[j]<<" "<<row5[j]<<" "<<row6[j]<<" "<<row7[j]<<" | ";
if(j==6)
cout<<"........ | | ";
}
while(win==0)
{
/*
if(win==0)
{
cout<<"Player 1: ";
cin>>move1;
cout<<" ";
while(move1<1||move1>7||(move1==1&&(row1[1]=='1'||row1[1]=='2'))||(move1==2&&(row2[1]=='1'||row2[1]=='2'))||(move1==3&&(row3[1]=='1'||row3[1]=='2'))||(move1==4&&(row4[1]=='1'||row4[1]=='2'))||(move1==5&&(row5[1]=='1'||row5[1]=='2'))||(move1==6&&(row6[1]=='1'||row6[1]=='2'))||(move1==7&&(row7[1]=='1'||row7[1]=='2')))
{
cout<<" Sorry you can't move there! Player 1: ";
cin>>move1;
}
if(move1==1)
{
i=6;
while(row1[i]=='1'||row1[i]=='2')
{
i--;
}
row1[i]='1';
}
else if(move1==2)
{
i=6;
while(row2[i]=='1'||row2[i]=='2')
{
i--;
}
row2[i]='1';
}
else if(move1==3)
{
i=6;
while(row3[i]=='1'||row3[i]=='2')
{
i--;
}
row3[i]='1';
}
else if(move1==4)
{
i=6;
while(row4[i]=='1'||row4[i]=='2')
{
i--;
}
row4[i]='1';
}
else if(move1==5)
{
i=6;
while(row5[i]=='1'||row5[i]=='2')
{
i--;
}
row5[i]='1';
}
else if(move1==6)
{
i=6;
while(row6[i]=='1'||row6[i]=='2')
{
i--;
}
row6[i]='1';
}
else if(move1==7)
{
i=6;
while(row7[i]=='1'||row7[i]=='2')
{
i--;
}
row7[i]='1';
}
}
/*
for(int j=0;j<7;++j)
{
if(j==0)
cout<<" "<<row1[j]<<" "<<row2[j]<<" "<<row3[j]<<" "<<row4[j]<<" "<<row5[j]<<" "<<row6[j]<<" "<<row7[j]<<" --------------- ";
if(j>0&&i<7)
cout<<" | "<<row1[j]<<" "<<row2[j]<<" "<<row3[j]<<" "<<row4[j]<<" "<<row5[j]<<" "<<row6[j]<<" "<<row7[j]<<" | ";
if(j==6)
cout<<" .............. | | ";
}
for(int j=6;j>0;--j)
{
if((row1[j]=='1'&&row2[j]=='1'&&row3[j]=='1'&&row4[j]=='1')||(row5[j]=='1'&&row2[j]=='1'&&row3[j]=='1'&&row4[j]=='1')||(row5[j]=='1'&&row6[j]=='1'&&row3[j]=='1'&&row4[j]=='1')||(row5[j]=='1'&&row6[j]=='1'&&row7[j]=='1'&&row4[j]=='1'))
win=1; //for horizontal
}
for(int j=6;j>2;--j)
{
if((row1[j]=='1'&&row1[j-1]=='1'&&row1[j-2]=='1'&&row1[j-3]=='1')||(row2[j]=='1'&&row2[j-1]=='1'&&row2[j-2]=='1'&&row2[j-3]=='1')||(row3[j]=='1'&&row3[j-1]=='1'&&row3[j-2]=='1'&&row3[j-3]=='1')||(row4[j]=='1'&&row4[j-1]=='1'&&row4[j-2]=='1'&&row4[j-3]=='1')||(row5[j]=='1'&&row5[j-1]=='1'&&row5[j-2]=='1'&&row5[j-3]=='1')||(row6[j]=='1'&&row6[j-1]=='1'&&row6[j-2]=='1'&&row6[j-3]=='1')||(row7[j]=='1'&&row7[j-1]=='1'&&row7[j-2]=='1'&&row7[j-3]=='1'))
win=1; //for the vertical
if((row1[j]=='1'&&row2[j-1]=='1'&&row3[j-2]=='1'&&row4[j-3]=='1')||(row2[j]=='1'&&row3[j-1]=='1'&&row4[j-2]=='1'&&row5[j-3]=='1')||(row3[j]=='1'&&row4[j-1]=='1'&&row5[j-2]=='1'&&row6[j-3]=='1')||(row4[j]=='1'&&row5[j-1]=='1'&&row6[j-2]=='1'&&row7[j-3]=='1'))
win=1; //for the diagonally up right
if((row7[j]=='1'&&row6[j-1]=='1'&&row5[j-2]=='1'&&row4[j-3]=='1')||(row6[j]=='1'&&row5[j-1]=='1'&&row4[j-2]=='1'&&row3[j-3]=='1')||(row5[j]=='1'&&row4[j-1]=='1'&&row3[j-2]=='1'&&row2[j-3]=='1')||(row4[j]=='1'&&row3[j-1]=='1'&&row2[j-2]=='1'&&row1[j-3]=='1'))
win=1; //for the diagonally up left
}
/*
if(win==0)
{
cout<<"Player 2: ";
cin>>move1;
cout<<" ";
while(move1<1||move1>7||(move1==1&&(row1[1]=='1'||row1[1]=='2'))||(move1==2&&(row2[1]=='1'||row2[1]=='2'))||(move1==3&&(row3[1]=='1'||row3[1]=='2'))||(move1==4&&(row4[1]=='1'||row4[1]=='2'))||(move1==5&&(row5[1]=='1'||row5[1]=='2'))||(move1==6&&(row6[1]=='1'||row6[1]=='2'))||(move1==7&&(row7[1]=='1'||row7[1]=='2')))
{
cout<<"Sorry you can't move there! Player 2: ";
cin>>move1;
}
if(move1==1)
{
i=6;
while(row1[i]=='1'||row1[i]=='2')
{
i--;
}
row1[i]='2';
}
else if(move1==2)
{
i=6;
while(row2[i]=='1'||row2[i]=='2')
{
i--;
}
row2[i]='2';
}
else if(move1==3)
{
i=6;
while(row3[i]=='1'||row3[i]=='2')
{
i--;
}
row3[i]='2';
}
else if(move1==4)
{
i=6;
while(row4[i]=='1'||row4[i]=='2')
{
i--;
}
row4[i]='2';
}
else if(move1==5)
{
i=6;
while(row5[i]=='1'||row5[i]=='2')
{
i--;
}
row5[i]='2';
}
else if(move1==6)
{
i=6;
while(row6[i]=='1'||row6[i]=='2')
{
i--;
}
row6[i]='2';
}
else if(move1==7)
{
if(row7[1]=='1'||row7[1]=='2')
cout<<"No";
else
{
i=6;
while(row7[i]=='1'||row7[i]=='2')
{
i--;
}
row7[i]='2';
}
}
}
/
for(int j=0;j<7;++j)
{
if(j==0)
cout<<" "<<row1[j]<<" "<<row2[j]<<" "<<row3[j]<<" "<<row4[j]<<" "<<row5[j]<<" "<<row6[j]<<" "<<row7[j]<<" --------------- ";
if(j>0&&i<7)
cout<<" | "<<row1[j]<<" "<<row2[j]<<" "<<row3[j]<<" "<<row4[j]<<" "<<row5[j]<<" "<<row6[j]<<" "<<row7[j]<<" | ";
if(i==6)
cout<<" ...... | | ";
}
for(int j=6;j>0;--j)
{
if((row1[j]=='2'&&row2[j]=='2'&&row3[j]=='2'&&row4[j]=='2')||(row5[j]=='2'&&row2[j]=='2'&&row3[j]=='2'&&row4[j]=='2')||(row5[j]=='2'&&row6[j]=='2'&&row3[j]=='2'&&row4[j]=='2')||(row5[j]=='2'&&row6[j]=='2'&&row7[j]=='2'&&row4[j]=='2'))
win=2; //for the horizontal
}
for(int j=6;j>2;--j)
{
if((row1[j]=='2'&&row1[j-1]=='2'&&row1[j-2]=='2'&&row1[j-3]=='2')||(row2[j]=='2'&&row2[j-1]=='2'&&row2[j-2]=='2'&&row2[j-3]=='2')||(row3[j]=='2'&&row3[j-1]=='2'&&row3[j-2]=='2'&&row3[j-3]=='2')||(row4[j]=='2'&&row4[j-1]=='2'&&row4[j-2]=='2'&&row4[j-3]=='2')||(row5[j]=='2'&&row5[j-1]=='2'&&row5[j-2]=='2'&&row5[j-3]=='2')||(row6[j]=='2'&&row6[j-1]=='2'&&row6[j-2]=='2'&&row6[j-3]=='2')||(row7[j]=='2'&&row7[j-1]=='2'&&row7[j-2]=='2'&&row7[j-3]=='2'))
win=2; //for the vertical
if((row1[j]=='2'&&row2[j-1]=='2'&&row3[j-2]=='2'&&row4[j-3]=='2')||(row2[j]=='2'&&row3[j-1]=='2'&&row4[j-2]=='2'&&row5[j-3]=='2')||(row3[j]=='2'&&row4[j-1]=='2'&&row5[j-2]=='2'&&row6[j-3]=='2')||(row4[j]=='2'&&row5[j-1]=='2'&&row6[j-2]=='2'&&row7[j-3]=='2'))
win=2; //for the diagonally up right
if((row7[j]=='2'&&row6[j-1]=='2'&&row5[j-2]=='2'&&row4[j-3]=='2')||(row6[j]=='2'&&row5[j-1]=='2'&&row4[j-2]=='2'&&row3[j-3]=='2')||(row5[j]=='2'&&row4[j-1]=='2'&&row3[j-2]=='2'&&row2[j-3]=='2')||(row4[j]=='2'&&row3[j-1]=='2'&&row2[j-2]=='2'&&row1[j-3]=='2'))
win=2; //for the diagonally up left
}
/*
}
if(win==2)
{
++twoscore;
cout<<"Player 2 Wins!!! Play again? (Y/N): ";
}
else if(win==1)
{
++onescore;
cout<<"Player 1 Wins!!! Play again? (Y/N): ";
}
cin>>again;
while(again!='Y'&&again!='y'&&again!='N'&&again!='n')
{
cout<<"Play again? (Y/N): ";
cin>>again;
}
cout<<" ";
win=0;
for(int j=1;j<7;++j)
{
row1[j]='O';
row2[j]='O';
row3[j]='O';
row4[j]='O';
row5[j]='O';
row6[j]='O';
row7[j]='O';
}
}
cout<<"Thanking you please come again! ";
keep_window_open();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.