Esempio n. 1
0
int main(int argc, char *argv[])
{
	int x, y, en, side, depth, score; 

	// true reversi starts with empty board, but that is not implemented yet!
	// therefore, here is the othello starting arrangement.
	int board[100] =
	{
		2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
		2, 0, 0, 0, 0, 0, 0, 0, 0, 2,
		2, 0, 0, 0, 0, 0, 0, 0, 0, 2,
		2, 0, 0, 0, 0, 0, 0, 0, 0, 2,
		2, 0, 0, 0,-1, 1, 0, 0, 0, 2,
		2, 0, 0, 0, 1,-1, 0, 0, 0, 2,
		2, 0, 0, 0, 0, 0, 0, 0, 0, 2,
		2, 0, 0, 0, 0, 0, 0, 0, 0, 2,
		2, 0, 0, 0, 0, 0, 0, 0, 0, 2,
		2, 2, 2, 2, 2, 2, 2, 2, 2, 2
	};

	if (argc > 1)
	{
		side  = atoi(argv[1]); 
	}
	else
	{
		printf("Insufficient arguments\n\n");
		printf("Syntax: %s {computer's side} [difficulty]\n", argv[0]);
		return 0;
	}
	if (argc == 3)
	{
		// side is the color that the computer plays
		// '1' is black, '-1' white
		// depth is the difficulty, the number of plies.
		depth = atoi(argv[2]);
	}
	else
	{
		depth = 4;
	}
	if (depth % 2 == 0) parity = 1; else parity = -1;

	printf("Computer playing as %c\n", pchar(side));
	printf("Difficulty %i\n", depth);
	printf("Right-click to quit\n");

	if (allegro_init() != 0) return 1;

	install_keyboard();
	install_mouse();
	install_timer();
	if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 440, 440 + 20, 0, 0))
	{
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		allegro_message("Unable to enter graphics mode\n%s\n", allegro_error);
		return 1;
	}
	
	set_palette(desktop_palette);
	clear_to_color(screen, makecol(0, 200, 0)); // green background

	textout_centre_ex(screen, font, "REVERSI", SCREEN_W/2, 10, 
						makecol(0, 0, 0), -1);

	printbd(board);
	printst(board, 0);
	show_mouse(screen);

	if (side == 1)
	{
		// make first move
		findmv(side, board, &en, depth);
		move(board, en, side);
		highlight(en, side);

		printbd(board);
		printst(board, 0);
	}
	// game loop
	while (1)
	{
		if (mouse_b & 1)
		{
			// the user has clicked to place a move
			x = mouse_pos >> 16;
			y = mouse_pos & 0x0000ffff;

			readmv(x, y, &en);
			
			// perform user move
			if (move(board, en, -side))
			{
				// highlight user move
				highlight(en, -side);
				printbd(board);
				printst(board, 0);
				
				// find reply
				score = findmv(side, board, &en, depth);
				if (move(board, en, side))
				{
					highlight(en, side);

					printbd(board);
					printst(board, 0);
				}
			}
			else
			{
				// check and see if user CAN move at all
				en = -1;
				score = findmv(-side, board, &en, depth);
				if (en == -1)
				{
					// computer move instead
					score = findmv(side, board, &en, depth);
					if (move(board, en, side))
					{
						highlight(en, side);

						printbd(board);
						printst(board, 0);
					}
					else
					{
						// Game over
						printst(board, 1);
					}
				}
			}
		}

		if (mouse_b & 2) return 0; // exit on right-click
	}
Esempio n. 2
0
void processinterMbType(
    unsigned char mb_type,
    unsigned char slice_type,
    NALU_t * nalu,
    unsigned char num_ref_idx_active1,
    unsigned char num_ref_idx_active0,
    char refidx0[2][2],
    char refidx1[2][2],
    int mvd0[4][4][2],
    int mvd1[4][4][2],
    char globref0[PicWidthInMBs*2][FrameHeightInMbs*2],
    char globref1[PicWidthInMBs*2][FrameHeightInMbs*2],
    int globmvd0[PicWidthInMBs*4][FrameHeightInMbs*4][2],
    int globmvd1[PicWidthInMBs*4][FrameHeightInMbs*4][2],
    int list0[MAX_REFERENCE_PICTURES],
    int list1[MAX_REFERENCE_PICTURES],
    int Mbaddress,
    int skipflag,
    char refcol[2][2],
    int mvcol[4][4][2],
    unsigned char MbType
)
{
    unsigned char substepx[2][2];
    unsigned char substepy[2][2];
    unsigned char stepx;
    unsigned char stepy;
    unsigned char listuse[2][2];
    unsigned char preddir[2][2];
    unsigned char subtype;
    unsigned char dflag;
    int i,j;

    int x,y;
    int subx,suby;
    char refidx;
    int mvdx;
    int mvdy;
    int tpmmv[2];
    int tpmmv1[2];
    int foundmv0[2];
    int foundmv1[2];
    char tmpref01[2];

    int startblkx= (Mbaddress%PicWidthInMBs)*4;
    int startblky= (Mbaddress/PicWidthInMBs)*4;



    const unsigned char val_stepX[30]=  {4,4,4,2,2,2, // this part is for P slice
                                         2,2,4,4,4, // following is for B slice
                                         4,2,4,2,
                                         4,2,4,2,
                                         4,2,4,2,
                                         4,2,4,2,
                                         4,2,2
                                        };
    const unsigned char val_stepY[30]= {4,4,2,4,2,2,
                                        2,2,4,4,4,
                                        2,4,2,4,
                                        2,4,2,4,
                                        2,4,2,4,
                                        2,4,2,4,
                                        2,4,2
                                       };

    const unsigned char val_listuse[30][4]= {
        //following are P slice
        {1,1,1,1},//skip
        {1,1,1,1},//0
        {1,1,1,1},//1
        {1,1,1,1},//2
        {1,1,1,1},//3
        {1,1,1,1},//4
        // following are B slice
        {0,0,0,0},//skip
        {0,0,0,0},//direct
        {1,1,1,1},//1
        {2,2,2,2},//2
        {3,3,3,3},//3
        {1,1,1,1},//4
        {1,1,1,1},//5
        {2,2,2,2},//6
        {2,2,2,2},//7
        {1,1,2,2},//8
        {1,2,1,2},//9
        {2,2,1,1},//10
        {2,1,2,1},//11
        {1,1,3,3},//12
        {1,3,1,3},//13
        {2,2,3,3},//14
        {2,3,2,3},//15
        {3,3,1,1},//16
        {3,1,3,1},//17
        {3,3,2,2},//18
        {3,2,3,2},//19
        {3,3,3,3},//20
        {3,3,3,3},//21
        {0,0,0,0},//22
    };

    const unsigned char val_predir[30][4]=  {
        //following are P slice
        {0,0,0,0},//skip
        {0,0,0,0},//0
        {2,2,1,1},//1
        {1,3,1,3},//2
        {0,0,0,0},//3
        {0,0,0,0},//4
        //following are B slice
        {0,0,0,0},//skip
        {0,0,0,0},//direct
        {0,0,0,0},//1
        {0,0,0,0},//2
        {0,0,0,0},//3
        {2,2,1,1},//4
        {1,3,1,3},//5
        {2,2,1,1},//6
        {1,3,1,3},//7
        {2,2,1,1},//8
        {1,3,1,3},//9
        {2,2,1,1},//10
        {1,3,1,3},//11
        {2,2,1,1},//12
        {1,3,1,3},//13
        {2,2,1,1},//14
        {1,3,1,3},//15
        {2,2,1,1},//16
        {1,3,1,3},//17
        {2,2,1,1},//18
        {1,3,1,3},//19
        {2,2,1,1},//20
        {1,3,1,3},//21
        {0,0,0,0},//22
    };



    const unsigned char val_sublistuse[17]= { 1,1,1,1,
                                              0,1,2,3,1,1,2,2,3,3,1,2,3
                                            };


    const unsigned char val_subX[17]= {
        2,2,1,1,// P slice
        2,2,2,2,
        2,1,2,1,
        2,1,1,1,1
    };
    const unsigned char val_subY[17]= {
        2,1,2,1,// P slice
        2,2,2,2,
        1,2,1,2,
        1,2,1,1,1
    };

    listuse[0][0]=val_listuse[MbType][0];
    listuse[1][0]=val_listuse[MbType][1];
    listuse[0][1]=val_listuse[MbType][2];
    listuse[1][1]=val_listuse[MbType][3];
    stepx=val_stepX[MbType];
    stepy=val_stepY[MbType];
    substepx[0][0]=stepx;
    substepy[0][0]=stepy;
    substepx[0][1]=stepx;
    substepy[0][1]=stepy;
    substepx[1][0]=stepx;
    substepy[1][0]=stepy;
    substepx[1][1]=stepx;
    substepy[1][1]=stepy;
    preddir[0][0]=val_predir[MbType][0];
    preddir[1][0]=val_predir[MbType][1];
    preddir[0][1]=val_predir[MbType][2];
    preddir[1][1]=val_predir[MbType][3];

    if(MbType==5 || MbType==29)
    {

        for(i=0; i<2; i++)
            for(j=0; j<2; j++)
            {
                subtype=u_e(nalu)+slice_type*4;
                substepx[j][i]=val_subX[subtype];
                substepy[j][i]=val_subY[subtype];
                listuse[j][i]=val_sublistuse[subtype];
            }
    }

    skipflag= (MbType==6 || MbType==7)?0:skipflag;

    if(MbType==0 || MbType==5  )
    {

        for(i=0; i<2; i++)
            for(j=0; j<2; j++)
            {
                refidx0[i][j] = 0;
                refidx1[i][j] = -1;
                globref0[startblkx/2+j][startblky/2+i]=0;
                globref1[startblkx/2+j][startblky/2+i]=-1;
            }
    }
    else
    {
        if(num_ref_idx_active0>0 )
        {
            for(y=0; y<2; y+= (stepy/2) )
                for(x=0; x<2; x+= (stepx/2) )
                {
                    if(listuse[x][y]== 0)
                    {
                        dflag= find_directzeroflag
                               (
                                   globref0,
                                   globref1,
                                   tmpref01,
                                   startblkx,
                                   startblky
                               );
                        refidx0[x][y]=globref0[startblkx/2+x][startblky/2+y]=tmpref01[0];
                        refidx1[x][y]=globref1[startblkx/2+x][startblky/2+y]=tmpref01[1];


                    }
                    else if(listuse[x][y] & 0x01)
                    {
                        refidx=t_e(num_ref_idx_active0,nalu);
#if _N_HLS_
                        fprintf(trace_bit,"refIdxL0[i][j] %d %d \n" ,refidx, num_ref_idx_active0);
#endif // _N_HLS_
                        for(i=0; i<stepy/2; i++)
                            for(j=0; j<stepx/2; j++)
                            {
                                refidx0[x+j][y+i]=refidx;
                                globref0[startblkx/2+x+j][startblky/2+y+i]=refidx;
                            }
                    }
                    else
                    {
                        for(i=0; i<stepy/2; i++)
                            for(j=0; j<stepx/2; j++)
                            {
                                refidx0[x+j][y+i]=-1;
                                globref0[startblkx/2+x+j][startblky/2+y+i]=-1;
                            }
                    }
                }
        }
        else
        {
            for(i=0; i<2; i++)
                for(j=0; j<2; j++)
                {
                    refidx0[j][i]=0;
                    globref0[startblkx/2+j][startblky/2+i]=0;
                }
        }
        if(num_ref_idx_active1>0  )
        {
            for(y=0; y<2; y+= (stepy/2) )
                for(x=0; x<2; x+= (stepx/2) )
                {

                    if(listuse[x][y]== 0)
                    {
                        //donotihing
                    }
                    else if( (listuse[x][y] & 0x02) !=0)
                    {
                        refidx=t_e(num_ref_idx_active1,nalu);
#if _N_HLS_
                        fprintf(trace_bit,"refIdxL1[i][j] %d %d\n" , refidx, num_ref_idx_active1);
#endif // _N_HLS_
                        for(i=0; i<stepy/2; i++)
                            for(j=0; j<stepx/2; j++)
                            {
                                refidx1[x+j][y+i]=refidx;
                                globref1[startblkx/2+x+j][startblky/2+y+i]=refidx;
#if _N_HLS_
                                fprintf(trace_bit,"refIdx1[%d][%d] %d\n" ,x+j, y+i, refidx);
#endif // _N_HLS_
                            }
                    }
                    else
                    {
                        for(i=0; i<stepy/2; i++)
                            for(j=0; j<stepx/2; j++)
                            {
                                refidx1[x+j][y+i]=-1;
                                globref1[startblkx/2+x+j][startblky/2+y+i]=-1;
                            }
                    }
                }
        }
        else
        {

            for(i=0; i<2; i++)
                for(j=0; j<2; j++)
                {
                    refidx1[j][i]=0;
                    globref1[startblkx/2+j][startblky/2+i]=0;
                }
        }
    }
    unsigned char stpw;
    unsigned char stph;

    for(y=0; y<4; y+=stepy)
        for(x=0; x<4; x+=stepx)
        {
            if(listuse[x>>1][y>>1]==0)
            {
                findmv( globref0,globmvd0,startblkx, startblky,0,4,0,0,refidx0[x/2][y/2],foundmv0,0,0);
                findmv( globref1,globmvd1,startblkx, startblky,0,4,0,0,refidx1[x/2][y/2],foundmv1,0,0);

                for(i=0; i<2; i++)
                    for(j=0; j<2; j++)
                    {
                        tpmmv[0]=foundmv0[0];
                        tpmmv[1]=foundmv0[1];
                        tpmmv1[0]=foundmv1[0];
                        tpmmv1[1]=foundmv1[1];

                        find_directmv_flag(dflag,refcol,mvcol,x+i,y+j,tpmmv1,tpmmv,refidx0[x/2][y/2],refidx1[x/2][y/2]);
                        mvd0[x+i][y+j][0]=globmvd0[startblkx+x+i][startblky+y+j][0]=tpmmv[0];
                        mvd0[x+i][y+j][1]=globmvd0[startblkx+x+i][startblky+y+j][1]=tpmmv[1];
                        mvd1[x+i][y+j][0]=globmvd1[startblkx+x+i][startblky+y+j][0]=tpmmv1[0];
                        mvd1[x+i][y+j][1]=globmvd1[startblkx+x+i][startblky+y+j][1]=tpmmv1[1];
                    }
            }
            else if( listuse[x>>1][y>>1] & 0x01 )
            {
                stpw=substepx[x>>1][y>>1];
                stph=substepy[x>>1][y>>1];

                for(suby=0; suby<stepy; suby+=stph)
                    for(subx=0; subx<stepx; subx+=stpw)
                    {
                        //calculate mvbase
                        if(skipflag!=1  )
                        {
                            mvdx=s_e(nalu);
                            mvdy=s_e(nalu);
#if _N_HLS_
                            fprintf(trace_bit," curMB->mvd_l0[%d][%d][0] %d\n" ,startblkx+x+subx,startblky+y+suby,mvdx);
#endif // _N_HLS_
#if _N_HLS_
                            fprintf(trace_bit," curMB->mvd_l0[%d][%d][1] %d\n" ,startblkx+x+subx,startblky+y+suby,mvdy);

#endif // _N_HLS_
                            // calculate and write x
                        }
                        else
                        {
                            mvdx=0;
                            mvdy=0;
                        }

                        findmv
                        (
                            globref0,
                            globmvd0,
                            startblkx,
                            startblky,
                            XYTOK(x+subx,y+suby),
                            stpw,
                            x+subx,
                            y+suby,
                            refidx0[x/2][y/2],
                            tpmmv,
                            preddir[x/2][y/2],
                            skipflag
                        );

                        for(i=0; i<stpw; i++)
                            for(j=0; j<stph; j++)
                            {
                                mvd0[x+subx+i][y+suby+j][0]=globmvd0[startblkx+x+subx+i][startblky+y+suby+j][0]=mvdx+tpmmv[0];
                                mvd0[x+subx+i][y+suby+j][1]=globmvd0[startblkx+x+subx+i][startblky+y+suby+j][1]=mvdy+tpmmv[1];
                            }
                    }
            }
        }