My connect 4 game compiles fine, You can enter the names, then after it prints the table it crashes..
Can anyone see why/point something out I did wrong?
Code:
#include <iostream.h>
#include <conio.h>
#include <windows.h>
typedef unsigned short int USHORT;
char board[7][6];
char playername1[25];
char playername2[25];
char playerup = '$';
const char player1 = '$';
const char player2 = '%';
const char blankchar = ' ';
USHORT i, ii, iii, iiii, x, y, moves, move, row;
const USHORT maxmoves = 7*6;
int checkWin(char player);
int printBoard();
int getInput(char player);
void init();
int setInput(char player, USHORT move);
void init()
{
for (USHORT i = 0; i<6; i++)
{
for (USHORT ii = 0; i<7; i++)
{
board[ii][i] = blankchar;
}
}
cout << "\tWelcome to Connect 4!\n";
cout << "[$] Player 1, enter your name now->";
cin.ignore();
cin.getline(playername1, 25, '\n');
cout << "[*] Player 2; now yours->";
cin.getline(playername2, 25, '\n');
}
int printBoard()
{
cout << "\n ";
for (i = 1; i<7; i++)
{
cout << " [" << i << "] ";
}
cout << "\n";
USHORT ii = 6;
for (USHORT i = 0; i<7; i++)
{
cout << " | " << board[i][ii];
if (i == 6)
{
cout << "\n";
cout << " +-----+-----+-----+-----+-----+-----+ ";
ii--;
if (ii >= 0)
{
i = 0;
}
cout << "\n";
}
}
return 0;
}
int getInput(char player)
{
if (player == player1)
{
cout << "\t" << playername1 << ", make your move->";
cin>>move;
return 1;
} else {
cout << "\t" << playername2 << ", make your move->";
cin>>move;
return 2;
}
}
int setInput(char player, USHORT move)
{
USHORT i = 0;
while (i < 6)
{
if (board[move][i] != ' ')
{
i++;
if (i == 5)
{
cout << "That column is full!";
getInput(player);
return 0;
}
} else {
board[move][i] = player;
if (player == player1)
{
return 1;
} else {
return 2;
}
}
}
}
int checkWin(char player)
{
USHORT row = 0;
for (i = 0; i<4; i++)
{
for (USHORT ii = 1; ii<5; ii++)
{
for (USHORT iii = 2; iii<6; iii++)
{
for (USHORT iiii = 3; iiii<7; iiii++)
{
if ((board[row][i] == board[row][ii]) && (board[row][ii] == board[row][iii]) && (board[row][iii] == board[row][iiii]))
{
if (player == player1)
{
//horizontal win;
return 1;
} else {
//horizontal win;
return 2;
}
}
if (iiii == 6)
{
if (row < 6)
{
row++;
}
i = 0;
ii = i++;
iii = ii++;
iiii = iii++;
}
}
}
}
}
USHORT column = 0;
USHORT &c = column;
for (i = 0; i<3; i++)
{
for (USHORT ii = 1; ii<4; ii++)
{
for (USHORT iii = 2; iii<5; iii++)
{
for (USHORT iiii = 3; iiii<6; iiii++)
{
if ((board[i][c] == board[ii][c]) && (board[ii][c] == board[iii][c]) && (board[iii][c] == board[iiii][c]))
{
if (player == player1)
{
//vertical win;
return 1;
} else {
//vertical win;
return 2;
}
}
if (iiii == 5)
{
if (c < 6)
{
c++;
}
i = 0;
ii = i++;
iii = ii++;
iiii = iii++;
}
}
}
}
}
return 0;
}
int main ()
{
init();
system("CLS");
printBoard();
do
{
if (moves < maxmoves)
{
getInput(playerup);
while (setInput(playerup, move))
{
cout << "\tThat move has already been made!\n\n";
getInput(playerup);
}
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");
_getch();
return 0;
}
} while (!checkWin(playerup));
if (playerup == player1)
{
cout << "\n\tCongratulations " << playername2 << ", you've won!\n\n\n\t";
//system("PAUSE");
_getch();
return 0;
} else {
cout << "\n\tCongratulations " << playername1 << ", you've won!\n\n\n\t";
//system("PAUSE");
_getch();
return 0;
}
}
I'm aware that not all the winning conditions are done yet.