Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Same Game AI player (http://www.go4expert.com/forums/game-ai-player-t24315/)

Programming_Kills 30Dec2010 02:07

Same Game AI player
 
Hi All.. i want to implent the AI player of the Famous http://en.wikipedia.org/wiki/SameGame
a matrix containg integer ranging b/w 1 and 3.
1 for red colour
2 for green colour
3 for yello colour
the AI player should delete all the adjacent blocks of same colours untill no adjacent blocks are remaing and put 0 at deleted indexs to represent that it is deleted. matrix sample is given below and each integer represent a specific colour. can anyone please help me with some initial code or data structure? thanks.
text.txt.
1111111111
2323333132
1131123222
2222222113
1111111111
1111111111
1111111111
1111111111
the Human player code is..
Code:

#include<stdio.h>
#define TRUE 1
#define FALSE 0
#define BONUS 1000

#define NROWS 8
#define NCOLS 8

#define NBLOCKS (NROWS * NCOLS)

typedef enum Direction
{
    UP,
    DOWN,
    LEFT,
    RIGHT
}Direction;

int GameBoard[NROWS][NCOLS];      // Board Size


// function prototypes

int DeleteBlocks(int row, int col);
int DeleteNeighborBlocks(int row, int col, int color,Direction direction);
void test33();
void printBoard();
void CompactBoard(void);
int IsGameOver(void) ;




int main(int argc,char *argv[])
{
    int row = 0,col = 0;
    int ndeleted,gameover;
    int points;
    int ntotaldeleted;

    test33();

    system("clear");
    printBoard();

    points = 0;
    ntotaldeleted = 0;
    do
    {
    printf("enter Row\n");
    fscanf(stdin,"%d",&row);
    printf("enter Coloumn\n");
        fscanf(stdin,"%d",&col);
        ndeleted = DeleteBlocks(row,col);
        if(ndeleted > 0)
        {
            CompactBoard();
            ntotaldeleted += ndeleted;
            points += (ndeleted * ndeleted);
            system("clear");
            printBoard();
            gameover = IsGameOver(); 
        }


    }while(!gameover);

    if(ntotaldeleted == NBLOCKS)
        points += BONUS;
    fprintf(stdout,"Points Scored:%d",points);
    fprintf(stdout,"\n----------------------------------Game Over-----------------------------------\n");

    return 0;
}




// initializes GameBoard[NROWS][NCOLS] from input file specified
void test33()
{
    int row=0,col=0;
    int c;
    FILE *fp;

    fp = fopen("text.txt","r");
    if(fp == NULL)return;
    while ( !feof(fp) )
    {
        c = fgetc(fp);
        if(c >= '0' && c <= '9')
        {
            GameBoard[row][col] = c-'0';
            col++;
            if (col > (NCOLS - 1))    // row is filled to NCOLS
            {
                col = 0;
                row++;
                if (row > (NROWS - 1))        // NROWS have been filled
                {
                    fclose(fp);
                    return; 
                }
            }
        }
    }

}

// prints GameBoard[NROWS][NCOLS] to stdout
void printBoard()
{
    int row,col;

    fprintf(stdout,"--------------------------------------------------------------------------------\n");
    fprintf(stdout,"\t\t 0 1 2 3 4 5 6 7\n");
    fprintf(stdout,"--------------------------------------------------------------------------------\n");

    for(row = 0;row < NROWS;row++)
    {
        fprintf(stdout,"\t%d\t|",row);

        for (col = 0; col < NCOLS; col++)
        {
            fprintf(stdout,"%d|",GameBoard[row][col]);
        }
        fprintf(stdout,"\n");
        //fprintf(stdout,"\n--------------------------------------------------------------------------------\n");
    }
 }

int IsGameOver(void)
{
    int row = 0,col = 0,nColor;

    //  Go column by column, left to right
    for(col = 0; col < NCOLS; col++)
    {
        //  Row by row, bottom to top
        for(row = (NROWS - 1); row >= 0; row--)
        {
            nColor = GameBoard[row][col];      //  Once we hit background, this column is done
            if(nColor == 0)
                break;
            else
            {
                //  Check above and right
                if(row - 1 >= 0 &&GameBoard[row - 1][col] == nColor)
                    return 0;
                else if(col + 1 < NCOLS && GameBoard[row][col + 1] == nColor)
                    return 0;
            }
        }
    }

    //  No two found adjacent
    return 1;
}

int DeleteBlocks(int row, int col)
{
    int nColor,nCount;
    //  Make sure that the row and column are valid
    if(row < 0 || row >= NROWS || col < 0 || col >= NCOLS)
        return -1;
    //  Can't delete background blocks
    nColor = GameBoard[row][col];
    if(nColor == 0)
        return -1;
    //  First check if there are any of the adjacent sides
    //  with the same color
    nCount = -1;
    if((row - 1 >= 0 && GameBoard[row - 1][col] == nColor) ||(row + 1 < NROWS && GameBoard[row + 1][col] == nColor) ||
    (col - 1 >= 0 && GameBoard[row][col - 1] == nColor) ||
    (col + 1 < NCOLS && GameBoard[row][col + 1] == nColor))
    {
        //  Then call the recursive function to eliminate all
        //  other touching blocks with same color
        GameBoard[row][col] = 0;
        nCount = 1;
        //  Recursive call for up
        nCount += DeleteNeighborBlocks(row - 1, col, nColor, DOWN);
        //  Recursive call for down
        nCount += DeleteNeighborBlocks(row + 1,col, nColor, UP);
        //  Recursive call for left
        nCount += DeleteNeighborBlocks(row, col - 1, nColor, RIGHT);
        //  Recursive call for right
        nCount += DeleteNeighborBlocks(row, col + 1, nColor, LEFT);
    }

    //  Return the total number of pieces deleted
    return nCount;
}

int DeleteNeighborBlocks(int row, int col, int color,Direction direction)
{
    //  Check if it is on the board
    if(row < 0 || row >= NROWS || col < 0 || col >= NCOLS)
        return 0;
    //  Check if it has the same color
    if(GameBoard[row][col] != color)
        return 0;
    int nCount = 1;
    GameBoard[row][col] = 0;
    //  If we weren't told to not go back up, check up
    if(direction != UP)
        nCount +=DeleteNeighborBlocks(row - 1, col, color, DOWN);
    //  If we weren't told to not go back down, check down
    if(direction != DOWN)
        nCount +=DeleteNeighborBlocks(row + 1, col, color, UP);
    //  If we weren't told to not go back left, check left
    if(direction != LEFT)
        nCount +=DeleteNeighborBlocks(row, col - 1, color, RIGHT);
    //  If we weren't told to not go back right, check right
    if(direction != RIGHT)
        nCount +=DeleteNeighborBlocks(row, col + 1, color, LEFT);
    //  Return the total number of pieces deleted
    return nCount;
}

void CompactBoard(void)
{
    int row,col;
    int nNextEmptyRow,nNextOccupiedRow;
    int nNextEmptyCol,nNextOccupiedCol;

    //  First move everything down
    for(col = 0; col < NCOLS; col++)
    {
        nNextEmptyRow = NROWS - 1;
        nNextOccupiedRow = nNextEmptyRow;
        while(nNextOccupiedRow >= 0 && nNextEmptyRow >= 0)
        {
            //  First find the next empty row
            while(nNextEmptyRow >= 0 && GameBoard[nNextEmptyRow][col] != 0)
                nNextEmptyRow--;
            if(nNextEmptyRow >= 0)
            {
                //  Then find the next occupied row from the next empty row
                nNextOccupiedRow = nNextEmptyRow - 1;
                while(nNextOccupiedRow >= 0 && GameBoard[nNextOccupiedRow][col] == 0)
                    nNextOccupiedRow--;
                if(nNextOccupiedRow >= 0)
                {
                    //  Now move the block from occupied to empty
                    GameBoard[nNextEmptyRow][col] =
                    GameBoard[nNextOccupiedRow][col];
                    GameBoard[nNextOccupiedRow][col] = 0;
                }
            }
        }
    }
    //  Then move everything from right to left
    nNextEmptyCol = 0;
    nNextOccupiedCol = nNextEmptyCol;
    while(nNextEmptyCol < NCOLS && nNextOccupiedCol < NCOLS)
    {
        //  First find the next empty column
        while(nNextEmptyCol < NCOLS && GameBoard[NROWS - 1][nNextEmptyCol] != 0)
            nNextEmptyCol++;
        if(nNextEmptyCol < NCOLS)
        {
            //  Then find the next column with something in it
            nNextOccupiedCol++;
            while(nNextOccupiedCol < NCOLS && GameBoard[NROWS - 1][nNextOccupiedCol] == 0)
                nNextOccupiedCol++;
            if(nNextOccupiedCol < NCOLS)
            {
                //  Move entire column to the left
                for(row = 0; row < NROWS; row++)
                {
                    GameBoard[row][nNextEmptyCol] = GameBoard[row][nNextOccupiedCol];
                    GameBoard[row][nNextOccupiedCol] = 0;
                }
            }
        }
    }
}



All times are GMT +5.5. The time now is 12:43.