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;
}
}