-
Connect 4!
Heres a Connect 4 game I made last night.. I got everything working, exept a few things are behaving kinda weird. First off, whenever you're asked for input, it always prints the first players name, and symbol, and never the second players. But yet the right symbol gets printed to the board.
Another, is after you try to place a piece into a full column, it tells you it's full, then it asks for your input again. But, you have to input twice for it to accept your new move; the first one you input is ignored. I thought I'd be able to fix this by putting cin.ignore(); after it but it didn't.
I believe the offending code is in getInput and setInput. Maybe a fresh set of eyes will get these bugs out.
And I know the winning conditions aren't there, I didn't put those in for sake of simplicity.
Code:
include <fstream.h>
#include <windows.h>
//#include <conio.h>
//#include <time.h>
#include <string.h>
//#define int short;
//#define NULL 0;
int i, ii, move, moves;
int maxmoves = 6*7;
char blankchar = ' ';
char board[6][7];
char name1[10];
char player1 = '$';
char name2[10];
char player2 = '#';
char playerup = player1;
int init()
{
cout << "\t Welcome to Connect 4";
cout << "\n\t ["<< player1 <<"] Player 1, Enter your name now->";
cin.getline(name1, 15);
cout << "\t ["<< player2 <<"] Player 2, Enter your name now->";;
cin.getline(name2, 15);
cin.ignore();
for (i = 0; i < 7; i++)
{
for (ii = 0; ii < 6; ii++)
{
board[ii][i] = blankchar;
}
}
//char filename[40];
//time_t seconds;
//seconds = time(NULL);
//strcat(filename, name1);
//strcat(filename, name2);
//strcat(filename, seconds);
//ofstream sendout(filename, ios::nocreate);
return 0;
}
int getInput(char player)
{
if (player = player1)
{
cout << "\t[" << player1 << "]" << name1 << " make your move->";
cout << player << " player- " << playerup << " playerup- " << player1 << " player1- " << player2 << " -player2";
cin>>move;
cin.ignore();
while (move > 7)
{
cout << "\t Invalid move, please restate->";
cin>>move;
cin.ignore();
return move;
}
return move;
} else {
cout << "\t[" << player2 << "]" << name2 << " make your move->";
cin>>move;
cin.ignore();
return move;
}
}
int setInput(int move, char player)
{
int i = 5;
while (i >= 0)
{
if (board[i][move-1] == blankchar)
{
board[i][move-1] = player;
return 0;
} else {
i--;
if (i == -1)
{
cout << "\tThat column is full!\n";
cin.ignore();
getInput(playerup);
return 1;
}
}
}
}
void printBoard()
{
cout << "\n ";
for (USHORT n = 1; n <= 7; n++)
{
cout << " [" << n << "] ";
}
cout << "\n";
ii = 0;
for (i = 0; i < 7; i++)
{
cout << " | " << board[ii][i];
if (i == 6)
{
cout << " |";
cout << "\n";
cout << " +-----+-----+-----+-----+-----+-----+-----+";
cout << "\n";
if (ii != 5)
{
ii++;
i = -1;
}
}
}
}
int main ()
{
init();
system("CLS");
printBoard();
do
{
if (moves <= maxmoves)
{
getInput(playerup);
setInput(move, playerup);
moves++;
system("CLS");
printBoard();
if (playerup == player2)
{
playerup = player1;
} else {
playerup = player2;
}
} else {
cout << "\n\tNo winner this time, thanks for playing\n\n\n\t";;
//_getch();
return 0;
}
} while (1); //TODO: Winning conditions
if (playerup == player1)
{
cout << "\n\tCongratulations " << name2 << ", you've won!\n\n\n\t";;
//_getch();
return 0;
} else {
cout << "\n\tCongratulations " << name1 << ", you've won!\n\n\n\t";
//_getch();
return 0;
}
}
-
Cool Game
Its a Cool Game,Something I should study a bit.
But when i won versus...myself i dint won it just kept on
running
:confused:
-
Yeah, I didn't put the winning conditions in; I will once I finish with these bugs.. Then I'll post it here
-
Ahh, here we go.. both problems fixed. Now I need a good algorithm for winning. I think it was covered on an old topic, so I'll search that.
Code:
#include <fstream.h>
#include <windows.h>
//#include <conio.h>
//#include <time.h>
#include <string.h>
//#define int short;
//#define NULL 0;
short int i, ii, move, moves, mm;
const short maxmoves = 6*7;
const char blankchar = ' ';
char board[6][7];
char name1[10];
const char player1 = '$';
char name2[10];
const char player2 = '#';
char playerup = player1;
int init()
{
cout << "\t Welcome to Connect 4";
cout << "\n\t ["<< player1 <<"] Player 1, Enter your name now->";
cin.getline(name1, 15);
cout << "\t ["<< player2 <<"] Player 2, Enter your name now->";;
cin.getline(name2, 15);
cin.ignore();
for (i = 0; i < 7; i++)
{
for (ii = 0; ii < 6; ii++)
{
board[ii][i] = blankchar;
}
}
//char filename[40];
//time_t seconds;
//seconds = time(NULL);
//strcat(filename, name1);
//strcat(filename, name2);
//strcat(filename, seconds);
//ofstream sendout(filename, ios::nocreate);
return 0;
}
int getInput(char player)
{
if (playerup == player1)
{
cout << "\t [" << player1 << "]" << name1 << " make your move->";
//cout << player << " player- " << playerup << " playerup- " << player1 << " player1- " << player2 << " -player2";
cin>>move;
cin.ignore();
while (move > 7)
{
cout << "\t Invalid move, please restate->";
cin>>move;
cin.ignore();
}
return move;
} else {
cout << "\t [" << playerup << "]" << name2 << " make your move->";
cin>>move;
cin.ignore();
while (move > 7)
{
cout << "\t Invalid move, please restate->";
cin>>move;
cin.ignore();
return move;
}
return move;
}
}
int setInput(int move, char player)
{
int i = 5;
while (i >= 0)
{
if (board[i][move-1] == blankchar)
{
board[i][move-1] = player;
return 0;
} else {
i--;
if (i == -1)
{
cout << "\t That column is full!";
cin.ignore();
return 1;
}
}
}
}
void printBoard()
{
cout << "\n\t ";
for (USHORT n = 1; n <= 7; n++)
{
cout << " [" << n << "] ";
}
cout << "\n";
ii = 0;
mm = 0;
for (i = 0; i < 7; i++)
{
if (i == 0)
{
cout << "\t";
}
cout << " " << char(186) << " " << board[ii][i];
if (i == 6)
{
cout << " " << char(186);
cout << "\n";
if (mm != 5)
{
cout << "\t ";
cout << char(204) << char(205) << char(205) << char(205) << char(205) << char(205) << char(206);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(206);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(206);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(206);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(206);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(206);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(185);
} else {
cout << "\t ";
cout << char(200) << char(205) << char(205) << char(205) << char(205) << char(205) << char(202);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(202);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(202);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(202);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(202);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(202);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(188);
}
mm++;
cout << "\n";
if (ii != 5)
{
ii++;
i = -1;
}
}
}
}
int main ()
{
init();
system("CLS");
printBoard();
do
{
if (moves <= maxmoves)
{
getInput(playerup);
while (setInput(move, playerup))
{
system("CLS");
printBoard();
getInput(playerup);
}
moves++;
system("CLS");
printBoard();
if (playerup == player2)
{
playerup = player1;
} else {
playerup = player2;
}
} else {
cout << "\n\tNo winner this time, thanks for playing\n\n\n\t";;
//_getch();
return 0;
}
} while (1); //TODO: Winning conditions
if (playerup == player1)
{
cout << "\n\tCongratulations " << name2 << ", you've won!\n\n\n\t";;
//_getch();
return 0;
} else {
cout << "\n\tCongratulations " << name1 << ", you've won!\n\n\n\t";
//_getch();
return 0;
}
}
-
Great work!
Good work Dual :)
Its more easier to play now,you cleaned up :D
So,whats the next step?
-
Next step? Well I'm done now.. I didn't notice any flaws.. eg. all the winning conditions work.
I would have liked to do a log but I got better things to do.
Here it is!
Code:
#include <fstream.h>
#include <windows.h>
//#include <conio.h>
//#include <time.h>
#include <string.h>
//#define int short;
//#define NULL 0;
short int i, ii, iii, iiii, move, moves, mm, store;
const short maxmoves = 6*7;
const char blankchar = ' ';
char board[6][7];
char name1[10];
const char player1 = '$';
char name2[10];
const char player2 = '#';
char playerup = player1;
int init()
{
cout << "\t Welcome to Connect 4\n";
cout << "\t Contact: [email protected]\n";
cout << "\n\t ["<< player1 <<"] Player 1, Enter your name now->";
cin.getline(name1, 15);
cout << "\t ["<< player2 <<"] Player 2, Enter your name now->";;
cin.getline(name2, 15);
cin.ignore();
for (i = 0; i < 7; i++)
{
for (ii = 0; ii < 6; ii++)
{
board[ii][i] = blankchar;
}
}
//Log feature not yet implemented.
//char filename[40];
//time_t seconds;
//seconds = time(NULL);
//strcat(filename, name1);
//strcat(filename, name2);
//strcat(filename, seconds);
//ofstream sendout(filename, ios::nocreate);
return 0;
}
int getInput(char player)
{
if (playerup == player1)
{
cout << "\t [" << player1 << "]" << name1 << " make your move->";
cin>>move;
cin.ignore();
while (move > 7)
{
cout << "\t Invalid move, please restate->";
cin>>move;
cin.ignore();
}
return move;
} else {
cout << "\t [" << playerup << "]" << name2 << " make your move->";
cin>>move;
cin.ignore();
while (move > 7)
{
cout << "\t Invalid move, please restate->";
cin>>move;
cin.ignore();
}
return move;
}
}
int setInput(int move, char player)
{
int i = 5;
while (i >= 0)
{
if (board[i][move-1] == blankchar)
{
board[i][move-1] = player;
return 0;
} else {
i--;
if (i == -1)
{
cout << "\t That column is full!";
cin.ignore();
return 1;
}
}
}
}
void printBoard()
{
cout << "\n\t ";
for (USHORT n = 1; n <= 7; n++)
{
cout << " [" << n << "] ";
}
cout << "\n";
ii = 0;
mm = 0;
for (i = 0; i < 7; i++)
{
if (i == 0)
{
cout << "\t";
}
cout << " " << char(186) << " " << board[ii][i];
if (i == 6)
{
cout << " " << char(186);
cout << "\n";
if (mm != 5)
{
cout << "\t ";
cout << char(204) << char(205) << char(205) << char(205) << char(205) << char(205) << char(206);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(206);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(206);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(206);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(206);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(206);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(185);
} else {
cout << "\t ";
cout << char(200) << char(205) << char(205) << char(205) << char(205) << char(205) << char(202);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(202);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(202);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(202);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(202);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(202);
cout << char(205) << char(205) << char(205) << char(205) << char(205) << char(188);
}
mm++;
cout << "\n";
if (ii != 5)
{
ii++;
i = -1;
}
}
}
}
int checkWin(char player)
{
store = 0;
for (i = 0; i<4; i++)
{
//cout << "i" << i //Debugging;
if ((board[store][i] == board[store][i+1]) &&
(board[store][i+1] == board[store][i+2]) &&
(board[store][i+2] == board[store][i+3]) &&
(board[store][i+3] != blankchar))
{
return 1;
} else {
if (i == 3) // Is this the last loop through?;
{ // if not, i is never reset, the loop ends, and 0 returns;
if (store != 5) //Is this the last column?;
{
store++;
i = -1; //-1 instead of 0 because it's incremented at the end of the loop;
}
}
}
}
store = 0;
for (i = 0; i<3; i++)
{
//cout << "i" << i; //Debugging;
if ((board[i][store] == board[i+1][store]) &&
(board[i+1][store] == board[i+2][store]) &&
(board[i+2][store] == board[i+3][store]) &&
(board[i+3][store] != blankchar))
{
return 1;
} else {
if (i == 2) // Is this the last loop through?;
{ // if not, i is never reset, the loop ends, and 0 returns;
if (store != 6) //Is this the last column?;
{
store++;
i = -1; //-1 instead of 0 because it's incremented at the end of the loop;
}
}
}
}
for (i=0; i<5; i++)
{
for (ii=0; ii<4; ii++)
{
if ((board[i][ii] == board[i+1][ii+1])
&& (board[i][ii] == board[i+2][ii+2])
&& (board[i][ii] == board[i+3][ii+3])
&& (board[i+3][ii+3] != blankchar))
{
return 1;
}
}
}
for (i=6; i>3; i--)
{
for (ii=0; ii<4; ii++)
{
if ((board[i][ii] == board[i-1][ii+1])
&& (board[i][ii] == board[i-2][ii+2])
&& (board[i][ii] == board[i-3][ii+3])
&& (board[i-3][ii+3] != blankchar))
{
return 1;
}
}
}
return 0;
}
int main ()
{
init();
system("CLS");
printBoard();
do
{
if (moves <= maxmoves)
{
getInput(playerup);
while (setInput(move, playerup))
{
system("CLS");
printBoard();
getInput(playerup);
}
moves++;
system("CLS");
printBoard();
if (playerup == player2)
{
playerup = player1;
} else {
playerup = player2;
}
} else {
cout << "\n\tNo winner this time, thanks for playing\n\n\n\t ";;
system("PAUSE");
return 0;
}
} while (!checkWin(playerup)); //TODO: Winning conditions
if (playerup == player1)
{
cout << "\n\t Congratulations " << name2 << ", you've won!\n\n\n\t ";;
system("PAUSE");
return 0;
} else {
cout << "\n\t Congratulations " << name1 << ", you've won!\n\n\n\t ";
system("PAUSE");
return 0;
}
}