Code:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <string>
using namespace std;
// standard Connect Four board size
const int NROWS = 6;
const int NCOLS = 7;
//globalvariable declarations
int board [NROWS][NCOLS] = {0, 0}; // make everything equal 0
// NOTE: The 8th row is the row that contains the height of the column
// I see what you are trying to do above.
// A better way is just to check to see if their is a piece in the
// topmost row of the column.
// Since the 8th row is messing you up, I've removed it.
int currentPlayer = 1;
int playerMove = 1; // which player's move it is.
int victory; // who has won (0 if no one)
// prototype declarations
void displayBoard ();
int inputNewMove ();
int checkForWin ();
void gameOver ();
void logo();
void playername(string& name1, string&name2);
void rules();
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
int main ()
{
string name1, name2;
logo();
playername(name1, name2);
rules();
displayBoard ();
for (int i = 0 ; i < 42 ; i++)
{
if ( currentPlayer == 1 ) {
cout<<name1 <<"'s turn"<<endl;
currentPlayer++;
currentPlayer=currentPlayer%2;
}
else {
cout<<name2 <<"'s turn"<<endl;
currentPlayer++;
currentPlayer=currentPlayer%2;
}
inputNewMove ();
displayBoard ();
victory = checkForWin ();
if (victory > 0)
{
gameOver ();
return 0;
}
}
}
//----------------------------------------------------------------------------
void displayBoard ()
{
// suggestion: use a lookup table
const char LUT[] = " 12";
cout << " 1 2 3 4 5 6 7" << endl;
cout << " --------------";
for (int boardRow = 0 ; boardRow<NROWS ; boardRow++)// cycle through rows
{
cout << endl; // go to the next line
cout << (boardRow + 1) << "|";
for (int boardColumn = 0 ; boardColumn<NCOLS ; boardColumn++) // cycle through columns
{
cout << LUT[ board[boardRow][boardColumn] ] << ' ';
/*
if (board [boardRow][boardColumn] == 0) // empty
{
cout << " ";
}
if (board [boardRow][boardColumn] == 1) // player 1's piece
{
cout << "1 ";
}
if (board [boardRow][boardColumn] == 2) // player 2's piece
{
cout << "2 ";
}
*/
}
}
cout << endl << endl;
return;
}
//----------------------------------------------------------------------------
bool isColumnFull( int col )
{
return board[0][col] != 0;
}
//----------------------------------------------------------------------------
int inputNewMove()
{
int x;
int height;
for (;;)
{
cout << "Column? ";
cin >> x;
if ((x <= 7) && (x >= 1) && !isColumnFull( x-1 ))
{ //(board[8][x-1] != 8))
break;
}
else
{
cout << "Choice must be between 1 and 7, and column cannot be full." << endl;
}
}
// brilliant! however, since board[7] no longer exists...
// board[8][x-1]++; // adds one to the HEIGHT OF THE COLUMN VALUE (ROW 8)
// height = (8 - (board[8][x-1])); // determines point in array to fall (simulates the falling piece until it stops)
// search for the next available space
for (height=NROWS-1; board[height][x-1] != 0; height--);
board [height][x-1] = playerMove; // sets array coordinate to the number of the player
//switch move
if (playerMove == 1)
playerMove++;
else
playerMove--;
return 0;
}
//----------------------------------------------------------------------------
int checkForWin ()
{
//test for VERTICAL victory
for (int i=0; i<NCOLS-3; i++) // columns
{
for (int j=0; j<NROWS; j++) // rows
{
if (board[j][i] == 1 && board[j][i+1] == 1 && board[j][i+2] == 1 && board[j][i+3] == 1)
{
return 1;
}
if (board[j][i] == 2 && board[j][i+1] == 2 && board[j][i+2] == 2 && board[j][i+3] == 2)
{
return 2;
}
}
}
//test HORIZONTAL victory
for (int i=0; i<NCOLS; i++) // columns
{
for (int j=0; j<NROWS-3; j++) // rows
{
if (board[j][i] == 1 && board[j+1][i] == 1 && board[j+1][i] == 1 && board[j+1][i] == 1)
{
return 1;
}
if (board[j][i] == 2 && board[j+1][i] == 2 && board[j+2][i] == 2 && board[j+3][i] == 2)
{
return 2;
}
}
}
// don't forget to test for diagonal victory
// (combine what was done for your vertical and horizontal tests)
return 0;
}
//----------------------------------------------------------------------------
void gameOver()
{
cout << endl << "Game Over! Player " << victory << "has won!\n" ;
getchar();
}
void logo()
{
cout<<"------------Welcome to Connect 4 ----------------"<<endl;
cout<<" ************Connect*************"<<endl;
cout<<" **********************************"<<endl;
cout<<" *****************4******************"<<endl;
cout<<" *****************4*4******************"<<endl;
cout<<" ****************4**4******************"<<endl;
cout<<" **************4444444***************"<<endl;
cout<<" *****************4****************"<<endl;
cout<<" ****************4***************"<<endl;
cout<<"\n\n"<<endl;
}
void playername(string& name1, string&name2)
{
cout<<"What is player 1's name : ";
cin>>name1;
cout<<"What is player 2's name : ";
cin>>name2;
cout<<"Hi "<<name1<<", "<<name2<<endl;
}
void rules()
{
cout<<"The following is the rules of the Connect 4 : \n";
cout<<"1. Each player in his turn drops one of his checkers down any of the slots \n in the top of the grid. \n";
cout<<"2. The play alternates until one of the players gets four checkers of his \n colour in a row. The four in a row can be horizontal, vertical, or diagonal.\n";
cout<<"3. The first player to get four in a row wins.\n";
cout<<"4. If the board is filled with pieces and neither player has 4 in a row, \n then the game is a draw.\n";
}
in the main()
Code:
int main ()
{
string name1, name2;
logo();
playername(name1, name2);
rules();
displayBoard ();
for (int i = 0 ; i < 42 ; i++)
{
if ( currentPlayer == 1 ) {
cout<<name1 <<"'s turn"<<endl;
currentPlayer++;
currentPlayer=currentPlayer%2;
}
else {
cout<<name2 <<"'s turn"<<endl;
currentPlayer++;
currentPlayer=currentPlayer%2;
}
inputNewMove ();
displayBoard ();
victory = checkForWin ();
if (victory > 0)
{
gameOver ();
return 0;
}
}
}
in the for loop, why i change 42 instead of 15, the program can't check the win diagonal???
also, when player 1 wins , how can his name displayed???
like this image
http://i1128.photobucket.com/albums/...g?t=1302880048
thx!!!!!!