0 Replies - 520 Views - Last Post: 15 November 2009 - 05:11 PM Rate Topic: -----

#1 javalurnin  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 81
  • Joined: 05-October 07

why does my program hang?

Post icon  Posted 15 November 2009 - 05:11 PM

Hi everybody!

I wrote a connect four program and it hangs as soon as it asks for single player or multi player mode. Anyone know why?

[code]// connect4.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
//#include <ncurses.h>
//#include <conio.c>
//#include <termios.h>
//#include <unistd.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>


void printBoard( int width, int height); /* prints the board before game starts */
int dropPiece( int column, char currentPlayer, int mode ); /* returns row which piece was dropped, or -1 if column is full
mode 0 - works as normal, drop a piece and updates the board
mode 1 - just updates the board array, used for trying moves in 'tryEachMove()'
mode 2 - remove top piece in give column, used for trying moves in 'tryEachMove()'
*/
int checkWin( char currentPlayer ); /* returns 1 if currentPlaye wins, 0 otherwise */
void updateBoard( int x, int y, char currentPlayer ); /* updates the board when a piece is dropped */
int checkTie( void ); /* checks if it's a tie by checking whether if the board is full, returns 1 if full, 0 if not */
void resetEverything(); /* resets arrays and variables and prepare for new game */
void colorWinningPieces( int column, int row, char currentPlayer ); /* colors winning pieces so they stand out */
//void intro( void ); /* display an intro */
void typeOnScreen( char str[] ); /* uses the sleep() function to create a 'typing effect' and print stuff on screen */
void pretendToThink( void ); /* make the comp player pretend to think... */

/* AI functions */
int compMove( char currentPlayer ); /* performs differet kinds of moves depends on situation, returns 1 if successful, 0 otherwise */
int makeWinMove( char currentPlayer, int mode ); /* returns 1 if move is made, 0 otherwise
mode 1 is used in 'makeCleverRandomMove' */
int makeBlockMove( char currentPlayer ); /* returns 1 if blocked opponent, 0 otherwise */
int makeCleverRandomMove( char currentPlayer ); /* returns 1 if successful */


char board[ 7 ][ 6 ] = { ' ' }; /* global board array for convenience */
int numPiecesInColumn[ 7 ] = { 0 }; /* used for counting how man pieces are on each column */
int end_game = 0; /* indicates whether to end current game */
char winner = 'N'; /* N stands for none */
int gameLoopCounter = 0; /* basically for used in flipping a coin */

/* note: computer player's symbol is 'X' */

void gotoxy(int x, int y)
{
char essq[100]; //string variable to hold the excape sequence
char xstr[100]; // strings to hold the x and y coordinates
char ystr[100]; //escape sequences must be built with characters

/*convert the screen coordinates to strings*/
sprintf(xstr, "%d", x);
sprintf(ystr, "%d", y);

/*build the excape sequence for vertical move */
essq[0] = '\0';
strcat(essq, "\033[");
strcat(essq, "ystr");

/*described in man terminfo as vpa=\E[%p1%dd. vertical position absolute */
strcat(essq, "d");

/*horizontal move with position absolute */
strcat(essq, "\033[");
strcat(essq, xstr);
strcat(essq, "G");
printf("%s", essq);
}


int main()
{
int move = 900; /* set it to 900 to test it later to see if the game has been restarted, see line 74 */
char move2;
char menuInput;
char response;
int randNum;
int playMode; /* 1 is single, 2 indicating two-player */
char currentPlayer;
char str[10];

/* seed */
srand( time( NULL ) );

/* display the intro */
// intro();

printBoard( 7, 6 );

printf( "\n\n" );

gotoxy( 45, 1 );
// textcolor( BLUE );
printf( "by Daniel Ho.\'@\'" );
//textcolor( LIGHTGRAY );

newGame:

/* prompt for play mode (single or two-player) */
gotoxy( 1, 16 );
printf( " " );
gotoxy( 1, 16 );
printf( "Enter play mode ('S' for single, 'T' for two-player): " );
/* for some reason, i think something was left in the input buffer after resetting a game */
if ( move != 900 )
getchar();

scanf( "%c", &response );
response = toupper( response );
if ( response == 'T' )
playMode = 2;
else
playMode = 1;

gotoxy( 45, 4 );
if ( playMode == 1 ) {
printf( "Single-player mode" );
gotoxy( 45, 5 );
printf( "------------------" );
}
else {
printf( "Two-player mode" );
gotoxy( 45, 5 );
printf( "---------------" );
}

gotoxy( 1, 16 );
printf( "Enter column # in which to drop piece in (-1 to end the game): " );

/* flip a coin and see who moves first */
gotoxy( 45, 7 );
printf( " " );
gotoxy( 45, 7 );
printf( "flipping a coin" );
// sleep( 500 );
printf( "." );
// sleep( 500 );
printf( "." );
randNum = 1 + rand() % 2;
if ( randNum == 1 )
currentPlayer = 'X';
else if ( randNum == 2 )
currentPlayer = 'O';

/* game loop */
while ( end_game != 1 ) {

gotoxy( 1, 24 );
printf( " " );
gotoxy( 1, 17 );
printf( " " );
gotoxy( 63, 16 );
printf( " " );

gotoxy( 45, 7 );
printf( " " );
gotoxy( 45, 7 );
if ( gameLoopCounter == 0 )
printf( "Player %c moves first...", currentPlayer );
else
printf( "Player %c's turn...", currentPlayer );

rePrompt:

if ( playMode == 1 ) {
if ( currentPlayer == 'X' ) {
gotoxy( 45, 7 );
printf( " " );
gotoxy( 45, 7 );
if ( gameLoopCounter == 0 )
printf( "Player %c moves first...", currentPlayer );
else
printf( "Player %c's turn...", currentPlayer );

// sleep( 500 );
pretendToThink();
compMove( currentPlayer );
if ( checkWin( currentPlayer ) == 1 ) {
winner = currentPlayer;
end_game = 1;
printf( "\nPlayer %c wins !!!\n", winner );
}
else if ( checkTie() == 1 ) {
winner = 'T'; /* indicating a tie */
end_game = 1;
printf( "Tie Game" );
}
if ( winner == 'N' ) {

/* change turns */
currentPlayer = 'O';

gotoxy( 45, 7 );
printf( "Player %c's turn...", currentPlayer );

gotoxy( 64, 16 );
scanf( "%d", &move );

move2 = move;

if ( isalpha( move2 ) != 0 ) {
goto rePrompt; /* invalid input */
}
if ( move < 0 ) {
end_game = 1; /* ends the game */
continue;
}
if( move < 1 || move > 7 ) {
goto rePrompt;
}

if ( ( dropPiece( move - 1, currentPlayer, 0 ) ) == -1 ) {
gotoxy( 1, 18 );
printf( "Sorry, the column you selected (%d) is full.\n", move );
gotoxy( 64, 16 );
continue;
}

if ( checkWin( currentPlayer ) == 1 ) {
winner = currentPlayer;
end_game = 1;
printf( "\nPlayer %c wins !!!\n", winner );
}
else if ( checkTie() == 1 ) {
winner = 'T'; /* indicating a tie */
end_game = 1;
printf( "Tie Game" );
}
currentPlayer = 'X';
}
}
/* player 'O' is moving first */
else if ( currentPlayer == 'O' ) {
gotoxy( 45, 7 );
if ( gameLoopCounter == 0 )
printf( "Player %c moves first...", currentPlayer );
else
printf( "Player %c's turn...", currentPlayer );

gotoxy( 64, 16 );
scanf( "%d", &move );

move2 = move;

if ( isalpha( move2 ) != 0 ) {
goto rePrompt; /* invalid input */
}
if ( move < 0 ) {
end_game = 1;
continue;
} /* ends the game */
if( move < 1 || move > 7 ) {
goto rePrompt;
}

if ( ( dropPiece( move - 1, currentPlayer, 0 ) ) == -1 ) {
gotoxy( 1, 18 );
printf( "Sorry, the column you selected (%d) is full.\n", move );
gotoxy( 64, 16 );
continue;
}

if ( checkWin( currentPlayer ) == 1 ) {
winner = currentPlayer;
end_game = 1;
printf( "\nPlayer %c wins !!!\n", winner );
}
else if ( checkTie() == 1 ) {
winner = 'T'; /* indicating a tie */
end_game = 1;
printf( "Tie Game" );
}
currentPlayer = 'X';
}
}
else if ( playMode == 2 ) {


gotoxy( 64, 16 );
scanf( "%d", &move );

move2 = move;

if ( isalpha( move2 ) != 0 ) {
if ( move > 58 ) {
goto rePrompt; /* invalid input */
}
}
if ( move < 0 ) {
end_game = 1; /* ends the game */
continue;
}
if( move < 1 || move > 7 ) {
goto rePrompt;
}
if ( ( dropPiece( move - 1, currentPlayer, 0 ) ) == -1 ) {
gotoxy( 1, 18 );
printf( "Sorry, the column you selected (%d) is full.\n", move );
gotoxy( 64, 16 );
continue;
}

if ( checkWin( currentPlayer ) == 1 ) {
winner = currentPlayer;
end_game = 1;
printf( "\nPlayer %c wins !!!\n", winner );
}
else if ( checkTie() == 1 ) {
winner = 'T'; /* indicating a tie */
end_game = 1;
printf( "Tie Game" );
}

/* change turns */
if ( currentPlayer == 'X' )
currentPlayer = 'O';
else if ( currentPlayer == 'O' )
currentPlayer = 'X';
}

gotoxy( 1, 18 );
printf( " " );

gameLoopCounter++;
}

/* works like flushing the input stream */
getchar();

printf( "\nWould you like to play again (Y/N)? " );
scanf( "%c", &response );
/* quit the game and say someting polite */
if ( toupper( response ) == 'Y' ) {
/* restart the game */

resetEverything();
gotoxy( 64, 16 );
printf( " " );
gotoxy( 1, 17 );
printf( " " );
gotoxy( 1, 19 );
printf( " " );
gotoxy( 1, 21 );
printf( " " );
goto newGame;
}
else
printf( "\nThank you for playin...\n\n" );

system( "PAUSE" );
return 0;
}

void printBoard( int width, int height )
{
int i;
int j;

for ( i = 0; i <= height * 2; i++ ) {
for ( j = 0; j <= width; j++ ) {
if ( i == 0 )
printf( "+ " );
else if ( i % 2 == 0 && i != 0 ) {
printf( "+" );
if ( j != width )
printf( "___" );
}
else
printf( "| " );

}
printf( "\n" );
}

for ( i = 1; i <= 7; i++ )
printf( " %d ", i );
}

int dropPiece( int column, char currentPlayer, int mode )
{
int row;

/* this function removes the top piece of 'column' if mode is 2 */
if ( mode == 2 ) {
for ( row = 0; row < 6; row++ )
if ( board[ column ][ row ] == 'X' || board[ column ][ row ] == 'O' ) {
board[ column ][ row ] = ' ';
break;
}
return row;
}

/* column is full */
if ( numPiecesInColumn[ column ] >= 6 )
return -1;

/* test which row to drop the piece to */
for ( row = 5; row > -1; row-- ) {
if ( board[ column ][ row ] != 'X' && board[ column ][ row ] != 'O' )
break;
}


/* record piece on board array */
board[ column ][ row ] = currentPlayer;

/* if mode is 1, which means that it's just 'trying' a move, quit here */
if ( mode == 1 )
return row;

/* increment pieces count */
numPiecesInColumn[ column ]++;

/* put piece on the board */
updateBoard( column, row, currentPlayer );

return row;
}

int checkTie( void )
{
int total = 0;
int col;

for ( col = 0; col < 7; col++ )
total += numPiecesInColumn[ col ];

if ( total < 42 )
return 0;
else
return 1;
}

int checkWin( char currentPlayer )
{
int col;
int row;

/* checking vertically */
for ( col = 0; col < 7; col++ )
for ( row = 0; row < 3; row++ ) {
if ( board[ col ][ row ] != ' ' )
if ( currentPlayer == board[ col ][ row ] &&
currentPlayer == board[ col ][ row + 1 ] &&
currentPlayer == board[ col ][ row + 2 ] &&
currentPlayer == board[ col ][ row + 3 ] ) {
colorWinningPieces( col, row, currentPlayer );
colorWinningPieces( col, row + 1, currentPlayer );
colorWinningPieces( col, row + 2, currentPlayer );
colorWinningPieces( col, row + 3, currentPlayer );
return 1;
}
}

/* checking horizontally */
for ( row = 0; row < 6; row++ )
for ( col = 0; col < 4; col++ ) {
if ( board[ col ][ row ] != ' ' )
if ( currentPlayer == board[ col ][ row ] &&
currentPlayer == board[ col + 1 ][ row ] &&
currentPlayer == board[ col + 2 ][ row ] &&
currentPlayer == board[ col + 3 ][ row ] ) {

colorWinningPieces( col, row, currentPlayer );
colorWinningPieces( col + 1, row, currentPlayer );
colorWinningPieces( col + 2, row, currentPlayer );
colorWinningPieces( col + 3, row, currentPlayer );
return 1;
}
}

/* checking diagonally (NE) */
for ( row = 5; row > 2; row-- )
for ( col = 0; col < 4; col++ ) {
if ( board[ col ][ row ] != ' ' )
if ( currentPlayer == board[ col ][ row ] &&
currentPlayer == board[ col + 1 ][ row - 1 ] &&
currentPlayer == board[ col + 2 ][ row - 2 ] &&
currentPlayer == board[ col + 3 ][ row - 3 ] ) {

colorWinningPieces( col, row, currentPlayer );
colorWinningPieces( col + 1, row - 1, currentPlayer );
colorWinningPieces( col + 2, row - 2, currentPlayer );
colorWinningPieces( col + 3, row - 3, currentPlayer );
return 1;
}
}

/* checking diagonally (NW) */
for ( row = 5; row > 2; row-- )
for ( col = 6; col > 2; col-- ) {
if ( board[ col ][ row ] != ' ' )
if ( currentPlayer == board[ col ][ row ] &&
currentPlayer == board[ col - 1 ][ row - 1 ] &&
currentPlayer == board[ col - 2 ][ row - 2 ] &&
currentPlayer == board[ col - 3 ][ row - 3 ] ) {

colorWinningPieces( col, row, currentPlayer );
colorWinningPieces( col - 1, row - 1, currentPlayer );
colorWinningPieces( col - 2, row - 2, currentPlayer );
colorWinningPieces( col - 3, row - 3, currentPlayer );
return 1;
}
}

/* no one wins */
return 0;
}

void updateBoard( int x, int y, char currentPlayer )
{
int xPos;
int yPos;

y += 1;
x += 1;

/* calculations relative to the layout of the board */
xPos = x * 4 - 1;
yPos = y * 2;



gotoxy( xPos, yPos );
printf( "%c", currentPlayer );
gotoxy( 1, 20 );

/* return to normal color */
//textcolor( LIGHTGRAY );
}

void resetEverything( void )
{
int i;
int j;
int randNum;

/* redraws an empty board */
gotoxy( 1, 1 );
printBoard( 7, 6 );

/* resets the board */
for ( i = 0; i < 7; i++ ) {
for ( j = 0; j < 6; j++ )
board[ i ][ j ] = ' ';
/* resets piece count */

numPiecesInColumn[ i ] = 0;
}

winner = 'N';

end_game = 0;

gameLoopCounter = 0;
}

void colorWinningPieces( int column, int row, char currentPlayer )
{
int xPos;
int yPos;

column += 1;
row += 1;

/* calculations relative to the layout of the board */
xPos = column * 4 - 1;
yPos = row * 2;

gotoxy( xPos, yPos );
// textcolor( WHITE );
printf( "%c", currentPlayer );
// textcolor( LIGHTGRAY );
gotoxy( 64, 16 );
}

int makeWinMove( char currentPlayer, int mode )
{
int row;
int col;

/* check vertically */
for ( col = 0; col < 7; col++ )
for ( row = 5; row > 2; row-- ) {
if ( board[ col ][ row ] != ' ' ) {
if ( board[ col ][ row ] == currentPlayer &&
board[ col ][ row - 1 ] == currentPlayer &&
board[ col ][ row - 2 ] == currentPlayer &&
board[ col ][ row - 3 ] != 'X' &&
board[ col ][ row - 3 ] != 'O' ) {
if ( mode == 1 )
return 1;
dropPiece( col, currentPlayer, 0 );

return 1; /* indicating success */
}
}
}

/* check horizontally */
for ( row = 0; row < 6; row++ )
for ( col = 0; col < 4; col++ ) {
if ( board[ col ][ row ] == currentPlayer &&
board[ col + 1 ][ row ] == currentPlayer &&
board[ col + 2 ][ row ] == currentPlayer &&
board[ col + 3 ][ row ] != 'X' &&
board[ col + 3 ][ row ] != 'O' &&
( ( board[ col + 3 ][ row + 1 ] == 'X' ||
board[ col + 3 ][ row + 1 ] == 'O' ) || row == 5 )) {
if ( mode == 1 )
return 1;
dropPiece( col + 3, currentPlayer, 0 );

return 1;
}
if ( board[ col ][ row ] == currentPlayer &&
board[ col + 1 ][ row ] == currentPlayer &&
board[ col + 3 ][ row ] == currentPlayer &&
board[ col + 2 ][ row ] != 'X' &&
board[ col + 2 ][ row ] != 'O' &&
( ( board[ col + 2 ][ row + 1 ] == 'X' ||
board[ col + 2 ][ row + 1 ] == 'O' ) || row == 5 )) {
if ( mode == 1 )
return 1;
dropPiece( col + 2, currentPlayer, 0 );

return 1;
}
if ( board[ col ][ row ] == currentPlayer &&
board[ col + 3 ][ row ] == currentPlayer &&
board[ col + 2 ][ row ] == currentPlayer &&
board[ col + 1 ][ row ] != 'X' &&
board[ col + 1 ][ row ] != 'O' &&
( ( board[ col + 1 ][ row + 1 ] == 'X' ||
board[ col + 1 ][ row + 1 ] == 'O' ) || row == 5 )) {
if ( mode == 1 )
return 1;
dropPiece( col + 1, currentPlayer, 0 );

return 1;
}
if ( board[ col + 3 ][ row ] == currentPlayer &&
board[ col + 1 ][ row ] == currentPlayer &&
board[ col + 2 ][ row ] == currentPlayer &&
board[ col ][ row ] != 'X' &&
board[ col ][ row ] != 'O' &&
( ( board[ col ][ row + 1 ] == 'X' ||
board[ col ][ row + 1 ] == 'O' ) || row == 5 )) {
if ( mode == 1 )
return 1;
dropPiece( col, currentPlayer, 0 );

return 1;
}
}

/* check diagonally (NE) */
for ( row = 5; row > 2; row-- )
for ( col = 0; col < 4; col++ ) {
if ( board[ col ][ row ] == currentPlayer &&
board[ col + 1 ][ row - 1 ] == currentPlayer &&
board[ col + 2 ][ row - 2 ] == currentPlayer &&
board[ col + 3 ][ row - 3 ] != 'X' &&
board[ col + 3 ][ row - 3 ] != 'O' &&
(board[ col + 3 ][ row - 3 + 1 ] == 'X' ||
board[ col + 3 ][ row - 3 + 1 ] == 'O' ) ) {
if ( mode == 1 )
return 1;
dropPiece( col + 3, currentPlayer, 0 );

return 1;
}
if ( board[ col ][ row ] == currentPlayer &&
board[ col + 1 ][ row - 1 ] == currentPlayer &&
board[ col + 3 ][ row - 3 ] == currentPlayer &&
board[ col + 2 ][ row - 2 ] != 'X' &&
board[ col + 2 ][ row - 2 ] != 'O' &&
(board[ col + 2 ][ row - 2 + 1 ] == 'X' ||
board[ col + 3 ][ row - 2 + 1 ] == 'O' ) ) {
if ( mode == 1 )
return 1;
dropPiece( col + 2, currentPlayer, 0 );

return 1;
}
if ( board[ col ][ row ] == currentPlayer &&
board[ col + 3 ][ row - 3 ] == currentPlayer &&
board[ col + 2 ][ row - 2 ] == currentPlayer &&
board[ col + 1 ][ row - 1 ] != 'X' &&
board[ col + 1 ][ row - 1 ] != 'O' &&
(board[ col + 1 ][ row - 1 + 1 ] == 'X' ||
board[ col + 1 ][ row - 1 + 1 ] == 'O' ) ) {
if ( mode == 1 )
return 1;
dropPiece( col + 1, currentPlayer, 0 );

return 1;
}
if ( board[ col + 3 ][ row - 3 ] == currentPlayer &&
board[ col + 1 ][ row - 1 ] == currentPlayer &&
board[ col + 2 ][ row - 2 ] == currentPlayer &&
board[ col ][ row ] != 'X' &&
board[ col ][ row ] != 'O' &&
( ( board[ col ][ row + 1 ] == 'X' ||
board[ col ][ row + 1 ] == 'O' ) || row == 5 )) {
if ( mode == 1 )
return 1;
dropPiece( col, currentPlayer, 0 );

return 1;
}

}

/* check diagonally (NW) */
for ( row = 5; row > 2; row-- )
for ( col = 6; col > 2; col-- ) {
if ( board[ col ][ row ] == currentPlayer &&
board[ col - 1 ][ row - 1 ] == currentPlayer &&
board[ col - 2 ][ row - 2 ] == currentPlayer &&
board[ col - 3 ][ row - 3 ] != 'X' &&
board[ col - 3 ][ row - 3 ] != 'O' &&
(board[ col - 3 ][ row - 3 + 1 ] == 'X' ||
board[ col - 3 ][ row - 3 + 1 ] == 'O' ) ) {
if ( mode == 1 )
return 1;
dropPiece( col - 3, currentPlayer, 0 );

return 1;
}
if ( board[ col ][ row ] == currentPlayer &&
board[ col - 1 ][ row - 1 ] == currentPlayer &&
board[ col - 3 ][ row - 3 ] == currentPlayer &&
board[ col - 2 ][ row - 2 ] != 'X' &&
board[ col - 2 ][ row - 2 ] != 'O' &&
(board[ col - 2 ][ row - 2 + 1 ] == 'X' ||
board[ col - 2 ][ row - 2 + 1 ] == 'O' ) ) {
if ( mode == 1 )
return 1;
dropPiece( col - 2, currentPlayer, 0 );

return 1;
}
if ( board[ col ][ row ] == currentPlayer &&
board[ col - 3 ][ row - 3 ] == currentPlayer &&
board[ col - 2 ][ row - 2 ] == currentPlayer &&
board[ col - 1 ][ row - 1 ] != 'X' &&
board[ col - 1 ][ row - 1 ] != 'O' &&
(board[ col - 1 ][ row - 1 + 1 ] == 'X' ||
board[ col - 1 ][ row - 1 + 1 ] == 'O' ) ) {
&nbs

Is This A Good Question/Topic? 0
  • +

Page 1 of 1