Exemple #1
0
static int
try_read_move(const char *cmd)
{
    move move;

    if (is_end())
        return 1;

    switch (read_move(current_position(), cmd, &move, turn())) {
    case none_move:
        return 1;
    case illegal_move:
        printf("Illegal move: %s\n", cmd);
        return 0;
    case 0:
        if (!is_force_mode && !is_opp_turn()) {
            printf("It is not %s's turn\n",
                   whose_turn[opponent_of(computer_side)]);
            return 0;
        }
        operator_move(move);
        return 0;
    default:
        assert(0);
    }
    return 0;
}
Exemple #2
0
int main(void) {
   char board[9];
   char player = who_plays_first(); //funkcja losuje kto gra pierwszy
   char move;

   system("cls");
   fill_in_board(board); //wypelniamy tablice liczbami od 1 do 9
   do {
      if(player == 'x') {
         player = 'o'; //zmieniamy gracz
         display_board(board);
         do {
            move = read_move(player); //odczytujemy ruch gracza
         } while(!check_move_possibility(move, board));
      } else {
         player = 'x'; //zmieniamy gracza
         move = computer_move(board, player);
         system("cls");
         printf("Komputer wybral %d.\n\n", move + 1);
      }

      save_move(move, player, board);
   } while(!is_it_end(board)); //powtarzamy az do konca gry

   print_result(board, player);
   play_again();

   return 0;
}
Exemple #3
0
main(){
	int fd1, fd2;

	exp_loguser = 1;
	exp_timeout = 3600;

	fd1 = exp_spawnl("chess","chess",(char *)0);

	if (-1 == exp_expectl(fd1,exp_glob,"Chess\r\n",0,exp_end)) exit;

	if (-1 == write(fd1,"first\r",6)) exit;

	read_first_move(fd1);

	fd2 = exp_spawnl("chess","chess",(char *)0);

	if (-1 == exp_expectl(fd2,exp_glob,"Chess\r\n",0,exp_end)) exit;

	for (;;) {
		send_move(fd2);
		read_counter_move(fd2);

		send_move(fd1);
		read_move(fd1);
	}
}
int main(int argc, char *argv[]) {
	int CONTINUE,WINNER,HUMAN,COMPUTER;

	if(argc!=4) {
		fprintf(stderr,"Usage: mnkgame <row> <col> <k>\n");
		return 1;
	}
	if(!read_parameters(argv)) return 1;

	if(!setup_game(M,N,K)) {
		fprintf(stderr,"Error: the board is too big!\n");
		return 1;
	}

	COMPUTER=1;
	HUMAN=2;

	setup_player(M,N,K);
	printf("Computer Player ready\n");
	mypause();

	CONTINUE=N*M;
  WINNER=DRAW;

	while(CONTINUE>0) {
		if(read_move(PLAYER1,HUMAN)) {
			WINNER=PLAYER1;
			CONTINUE=0;
		}
		if(--CONTINUE>0) {
			if(read_move(PLAYER2,HUMAN)) {
				WINNER=PLAYER2;
				CONTINUE=0;
			}
			--CONTINUE;
		}
	}
	system("cls");
	print_board();
	end_game(WINNER,HUMAN);


	free_player();
	free_game();

	return 0;
}
position_t select_interface_gameplay(position_t pos){
    if(G_current_game.interface_mode == html){
        PL_HTMLread_move(&pos);
    }
    else{
        read_move(&pos);
    }

    return pos;
}
Exemple #6
0
void proceed(void)
{
    frac ++;
    if (stage == 1)
    {
	if (piece2 && frac <= MOVE_FRAC * 2)
	{
	    CZ2 = -((GLfloat) frac)/MOVE_FRAC/1.8;
	    return;
	}
	frac = 0;
	piece2 = 0;
	stage ++;
	return;
    }
    else if (stage == 3)
	return;
    else
	CZ2 = 0.0;

    if (frac >= MOVE_FRAC)
    {
	frac = 0;
	X = NX;
	Y = NY;
	if (NX == XD && NY == YD)
	{
	    board[XD][YD] = piece;
	    cycle[XD][YD] = cyclem;
	    stunt[XD][YD] = stuntm;
	    piece = 0;
	    read_move();
	    return;
	}
        switch(path[X][Y])
        {
            case NORTH:		NY--; break;
            case SOUTH:		NY++; break;
            case WEST:		NX--; break;
            case EAST:		NX++; break;
            case NORTHWEST:	NX--; NY--; break;
            case NORTHEAST:	NX++; NY--; break;
            case SOUTHWEST:	NX--; NY++; break;
            case SOUTHEAST:	NX++; NY++; break;
        }
	if (NX == XD && NY == YD)
	    stage ++;
    }
    CX1 = ((GLfloat) (X*(MOVE_FRAC-frac) + NX*frac))/ MOVE_FRAC;
    CY1 = ((GLfloat) (Y*(MOVE_FRAC-frac) + NY*frac))/ MOVE_FRAC;
}
void test_reading_converting_validating()
{
    int counter = 1, check = 0;
    reset_data_structs();
    board_set_size(BOARD_MEDIUM);
    int dimension = board_get_size();
    position_t pos;

    while(counter == 1)
    {
        test_representation_matrix(get_current_game_ptr()->board, dimension);

        do
        {
            read_move(&pos);

            check = function_validate_move(pos);
        }
        while(check != 0);
        //arise the variable "int playNumber".
        //save in log
    }
}
Exemple #8
0
/* Read a PGN file (a collection of one or more games in PGN format) and store
   the positions and their win/loss ratios in an AVL tree (**tree).
   The AVL tree can later be written in an opening book file (book.bin).
   
   Returns the number of new positions added to the tree, or -1 on error.  */
int
pgn_to_tree(const char *filename, AvlNode **tree)
{
	PgnResult result;
	int prev_progress;
	int npos = 0;
	long file_len;
	FILE *fp;
	Board board;

	ASSERT(1, filename != NULL);

	if ((fp = fopen(filename, "r")) == NULL) {
		my_perror("Can't open PGN file %s", filename);
		return -1;
	}

	if (settings.book_type != BOOK_MEM) {
		settings.book_type = BOOK_MEM;
		printf("Changed book mode to \"book in memory\"\n");
	}
	if (*tree == NULL && file_exists(settings.book_file)) {
		printf("Loading opening book to memory...\n");
		book_to_tree(settings.book_file, tree);
	} else if (*tree == NULL)
		printf("Creating a new opening book...\n");

	/* Find out how big the file is.  */
	fseek(fp, 0, SEEK_END);
	file_len = ftell(fp);
	rewind(fp);

	printf("Reading PGN file %s...\n", filename);
	prev_progress = 0;
	progressbar(50, 0);
	while ((result = get_pgn_result(fp)) != RESULT_ERROR) {
		int depth;
		int progress;
		int len;
		char san_move[MAX_BUF];

		/* Games with an unknown result are ignored.  */
		ASSERT(1, result != RESULT_ERROR);
		if (result == NO_RESULT || result == DRAWN_GAME)
			continue;

		depth = 0;
		fen_to_board(&board, START_FEN);

		while ((len = read_move(san_move, MAX_BUF, fp)) >= 0) {
			int points = 0;
			U32 move;

			/* break out of the loop when a new game begins */
			if (depth > 0 && san_move[0] == '[')
				break;
			if (len < 2)
				continue;

			move = san_to_move(&board, san_move);
			if (move == NULLMOVE) {
				#if DEBUG_LEVEL > 0
				update_log("Illegal move in %s: %s, line: %d\n",
				          filename, san_move, get_line_num(fp));
				#endif /* DEBUG_LEVEL > 0 */
				break;
			}

			if ((result == WHITE_WINS && board.color == WHITE)
			||  (result == BLACK_WINS && board.color == BLACK))
				points = 2;

			make_move(&board, move);
			if (save_book_pos(board.posp->key, points, tree))
				npos++;

			if (++depth >= MAX_BOOK_PLIES)
				break;
		}
		progress = (ftell(fp) * 50) / file_len;
		if (progress > prev_progress) {
			progressbar(50, progress);
			prev_progress = progress;
		}
	}
	progressbar(50, 50);
	my_close(fp, filename);
	printf("\n");

	return npos;
}
Exemple #9
0
/*=====================================
* main
*===================================*/
int main(void){
reset_data_structs();
clearscr();
welcome_screen();
show_menu();

int check=0;
if(G_current_game.game_mode==pvp){ //pvp mode

    if(G_current_game.player_first==1){  // who first start game,  first move
        cmp.current_player_move=G_players[0];
        cmp.previous_player_move=G_players[1];
    }
    else{
       cmp.current_player_move=G_players[1];
       cmp.previous_player_move=G_players[0];
    }

//    while(1){   // in while add finish_game...
    while( !finish_game_wrapper(pos) ){
            //system("cls");
            clearscr();
            board_print_raw();
                do{
                        //
                    printf("%s your move! (all your moves: %d)\n",cmp.current_player_move.name,cmp.current_player_move.moves);
                    read_move(&pos);
                    check = function_validate_move(pos);
                    }while(check != 0);

            cmp.current_player_move.moves+=1;
            cmp.tmp=cmp.current_player_move;                   //swap current player
            cmp.current_player_move=cmp.previous_player_move;  //
            cmp.previous_player_move=cmp.tmp;                  //
    }//end while(1)
    cmp.tmp=cmp.current_player_move;                   //swap current player
    cmp.current_player_move=cmp.previous_player_move;  //
    cmp.previous_player_move=cmp.tmp;
    board_print_raw();
    printf("%s wins! (In %d moves!)\n",cmp.current_player_move.name,cmp.current_player_move.moves);
}// end pvp mode

if(G_current_game.game_mode==pvc){ //pvc mode

    if(G_current_game.player_first==1){  // who first start game,  first move
        cmp.current_player_move=G_players[0];
        cmp.previous_player_move=G_players[1];// G_players[1] is cpu player
    }
    else{
       cmp.current_player_move=G_players[1];
       cmp.previous_player_move=G_players[0];
    }
    while(1){
            //system("cls");
            clearscr();
            board_print_raw();
            do{
                printf("%s your move! (all your moves: %d)\n",cmp.current_player_move.name,cmp.current_player_move.moves);
                read_move(&pos);
                check = function_validate_move(pos);
                }while(check != 0);

            cmp.current_player_move.moves+=1;
            cmp.tmp=cmp.current_player_move;                   //swap current player
            cmp.current_player_move=cmp.previous_player_move;  //
            cmp.previous_player_move=cmp.tmp;                  //
    }//end while(1)
}// end pvc
return 0;
}
Exemple #10
0
void loadLogs(int gameCounter)
{

    FILE *playLog;

    position_t posMove;
    char logName[30] = "TripletsLog-";
    int playNumber;
    char dateExtended[32];
    char playerName[30];
    int dimension;
    int i;
    int moveX;
    char moveY;
    int lineCounter=0;
    int headLinesNumber=6;
    int ch;

    sprintf(logName, "%s%d.txt", logName, gameCounter);

    playLog = fopen(logName, "r");
    if(playLog == NULL)
    {
        printf("\n\tERR: Unable to read Log!");
    }
    else
    {


        do
        {
            ch = fgetc(playLog);
            if( ch== '\n')
            {
                lineCounter++;
            }
        }
        while( ch != EOF );



        fclose(playLog);

        lineCounter-=headLinesNumber;
        lineCounter++; // to add the last line which doesn't have /n !!


    }
    playLog = fopen(logName, "r");
    if(playLog == NULL)
    {
        printf("\n\tERR: Unable to read Log!");
    }
    else
    {
        gameCounter=0;
        dimension=0;
        fscanf(playLog, "--Triplets Log--\n");
        fscanf(playLog, "Game #%d\n", &gameCounter);
        fscanf(playLog, "Matrix Dimension: %d\n", &dimension);
        fscanf(playLog, "Started on: %s\n", dateExtended);

        for (i=0; i<lineCounter; i++)
        {
            fscanf(playLog, "Player %s ; Play %d ; Move [%d][%c]\n", playerName, &playNumber, &moveX, &moveY);
//         printf("player %s, play number %d, moveX %d, moveY %c\n",playerName, playNumber, moveX, moveY); // was just to test if it's reading right
            posMove.X=moveX;
            posMove.Y=moveY;

            read_move(&posMove);
            board_print_raw();
        }

    }

    fclose(playLog);

}
Exemple #11
0
static void
test_move_str(void)
{
	struct position pos;
	enum player turn;
	int ep_index;
	char str[1024];
	const char *end;
	move m;

	end = position_read_fen(&pos, positions[0].FEN, &ep_index, &turn);
	assert(end != NULL);
	assert(ep_index == 0);
	assert(turn == black);
	assert(end == (positions[0].FEN + strlen(positions[0].FEN)));
	end = position_print_fen(&pos, str, 0, black);
	assert(strcmp(str, positions[0].FEN) == 0);
	assert(end == (str + strlen(positions[0].FEN)));
	assert(read_move(&pos, "h8h7", &m, black) == 0);
	assert(m == create_move_g(sq_h1, sq_h2, king, 0, false));
	print_move(&pos, m, str, mn_coordinate, black);
	assert(strcmp(str, "h8h7") == 0);
	assert(read_move(&pos, "h8g8", &m, black) == 0);
	assert(m == create_move_g(sq_h1, sq_g1, king, 0, false));
	print_move(&pos, m, str, mn_coordinate, black);
	assert(strcmp(str, "h8g8") == 0);
	assert(read_move(&pos, "b5b4", &m, black) == 0);
	assert(m == create_move_g(sq_b4, sq_b5, pawn, 0, false));
	print_move(&pos, m, str, mn_coordinate, black);
	assert(strcmp(str, "b5b4") == 0);
	assert(read_move(&pos, "b5a4", &m, black) == 0);
	assert(m == create_move_g(sq_b4, sq_a5, pawn, pawn, false));
	print_move(&pos, m, str, mn_coordinate, black);
	assert(strcmp(str, "b5a4") == 0);
	assert(read_move(&pos, "b5c4", &m, black) != 0);
	assert(read_move(&pos, "Kh7", &m, black) == 0);
	assert(m == create_move_g(sq_h1, sq_h2, king, 0, false));
	print_move(&pos, m, str, mn_san, black);
	assert(strcmp(str, "Kh7") == 0);
	assert(read_move(&pos, "Kg8", &m, black) == 0);
	assert(m == create_move_g(sq_h1, sq_g1, king, 0, false));
	print_move(&pos, m, str, mn_san, black);
	assert(strcmp(str, "Kg8") == 0);
	assert(read_move(&pos, "b4", &m, black) == 0);
	assert(m == create_move_g(sq_b4, sq_b5, pawn, 0, false));
	print_move(&pos, m, str, mn_san, black);
	assert(strcmp(str, "b4") == 0);
	assert(read_move(&pos, "bxa4", &m, black) == 0);
	assert(m == create_move_g(sq_b4, sq_a5, pawn, pawn, false));
	print_move(&pos, m, str, mn_san, black);
	assert(strcmp(str, "bxa4") == 0);
	assert(read_move(&pos, "bxc4", &m, black) != 0);
	assert(read_move(&pos, "o-o", &m, black) != 0);
	assert(read_move(&pos, "o-o-o", &m, black) != 0);

	end = position_read_fen(&pos, positions[1].FEN, &ep_index, &turn);
	assert(end != NULL);
	assert(ep_index == 0);
	assert(turn == white);
	assert(end == (positions[1].FEN + strlen(positions[1].FEN)));
	end = position_print_fen(&pos, str, 0, white);
	assert(strcmp(str, positions[1].FEN) == 0);
	assert(end == (str + strlen(positions[1].FEN)));
	assert(read_move(&pos, "o-o", &m, white) != 0);
	assert(read_move(&pos, "o-o-o", &m, white) != 0);

	end = position_read_fen(&pos, positions[2].FEN, &ep_index, &turn);
	assert(end != NULL);
	assert(ep_index == sq_d5);
	assert(turn == white);
	assert(end == (positions[2].FEN + strlen(positions[2].FEN)));
	end = position_print_fen(&pos, str, sq_d5, white);
	assert(strcmp(str, positions[2].FEN) == 0);
	assert(end == (str + strlen(positions[2].FEN)));
	assert(read_move(&pos, "o-o", &m, white) == 0);
	assert(m == mcastle_king_side);
	assert(read_move(&pos, "O-O", &m, white) == 0);
	assert(m == mcastle_king_side);
	print_move(&pos, m, str, mn_coordinate, white);
	assert(strcmp(str, "e1g1") == 0);
	print_move(&pos, m, str, mn_san, white);
	assert(strcmp(str, "O-O") == 0);
	assert(read_move(&pos, "o-o-o", &m, white) != 0);
	assert(read_move(&pos, "e5d6", &m, white) == 0);
	assert(m ==
	    create_move_t(sq_e5, sq_d6, mt_en_passant, pawn, pawn, false));
	print_move(&pos, m, str, mn_coordinate, white);
	assert(strcmp(str, "e5d6") == 0);
	assert(read_move(&pos, "exd6", &m, white) == 0);
	assert(m ==
	    create_move_t(sq_e5, sq_d6, mt_en_passant, pawn, pawn, false));
	print_move(&pos, m, str, mn_san, white);
	assert(strcmp(str, "exd6e.p.") == 0);

	end = position_read_fen(&pos, positions[3].FEN, &ep_index, &turn);
	assert(end != NULL);
	assert(ep_index == 0);
	assert(turn == white);
	assert(end == (positions[3].FEN + strlen(positions[3].FEN)));
	end = position_print_fen(&pos, str, 0, white);
	assert(strcmp(str, positions[3].FEN) == 0);
	assert(end == (str + strlen(positions[3].FEN)));
	assert(read_move(&pos, "o-o", &m, white) == 0);
	assert(m == mcastle_king_side_check);
	assert(read_move(&pos, "O-O", &m, white) == 0);
	assert(m == mcastle_king_side_check);
	print_move(&pos, m, str, mn_coordinate, white);
	assert(strcmp(str, "e1g1") == 0);
	print_move(&pos, m, str, mn_san, white);
	assert(strcmp(str, "O-O") == 0);
	assert(read_move(&pos, "c1b2", &m, white) == 0);
	assert(m == create_move_g(sq_c1, sq_b2, bishop, 0, true));
	assert(read_move(&pos, "Bb2", &m, white) == 0);
	assert(m == create_move_g(sq_c1, sq_b2, bishop, 0, true));
	assert(read_move(&pos, "Bb2+", &m, white) == 0);
	assert(m == create_move_g(sq_c1, sq_b2, bishop, 0, true));
	print_move(&pos, m, str, mn_san, white);
	assert(strcmp(str, "Bb2+") == 0);

	end = position_read_fen(&pos, positions[4].FEN, &ep_index, &turn);
	assert(end != NULL);
	assert(ep_index == 0);
	assert(turn == white);
	assert(end == (positions[4].FEN + strlen(positions[4].FEN)));
	end = position_print_fen(&pos, str, 0, white);
	assert(strcmp(str, positions[4].FEN) == 0);
	assert(end == (str + strlen(positions[4].FEN)));
	assert(read_move(&pos, "c7b8q", &m, white) == 0);
	assert(m ==
	    create_move_t(sq_c7, sq_b8, mt_promotion, queen, knight, false));
	assert(read_move(&pos, "c7b8Q", &m, white) == 0);
	assert(m ==
	    create_move_t(sq_c7, sq_b8, mt_promotion, queen, knight, false));
	assert(read_move(&pos, "c7b8n", &m, white) == 0);
	assert(m ==
	    create_move_t(sq_c7, sq_b8, mt_promotion, knight, knight, false));
	assert(read_move(&pos, "c7b8r", &m, white) == 0);
	assert(m ==
	    create_move_t(sq_c7, sq_b8, mt_promotion, rook, knight, false));
	assert(read_move(&pos, "c7b8b", &m, white) == 0);
	assert(m ==
	    create_move_t(sq_c7, sq_b8, mt_promotion, bishop, knight, false));
	assert(read_move(&pos, "c7d8q", &m, white) == 0);
	assert(m ==
	    create_move_t(sq_c7, sq_d8, mt_promotion, queen, queen, true));
	assert(read_move(&pos, "c7d8Q", &m, white) == 0);
	assert(m ==
	    create_move_t(sq_c7, sq_d8, mt_promotion, queen, queen, true));
	assert(read_move(&pos, "cxd8=Q", &m, white) == 0);
	assert(m ==
	    create_move_t(sq_c7, sq_d8, mt_promotion, queen, queen, true));
	assert(read_move(&pos, "cxd8=Q+", &m, white) == 0);
	assert(m ==
	    create_move_t(sq_c7, sq_d8, mt_promotion, queen, queen, true));
	assert(read_move(&pos, "cxd8=R+", &m, white) == 0);
	assert(m ==
	    create_move_t(sq_c7, sq_d8, mt_promotion, rook, queen, true));

	end = position_read_fen(&pos,
	    "rnbqkbnr/ppppp2p/5p2/6p1/8/4P3/PPPP1PPP/RNBQKBNR w KQkq -",
	    NULL, NULL);
	assert(end != NULL);
	assert(read_move(&pos, "d1h5", &m, white) == 0);
	assert(m == create_move_g(sq_d1, sq_h5, queen, 0, true));
	assert(read_move(&pos, "Qh5", &m, white) == 0);
	assert(m == create_move_g(sq_d1, sq_h5, queen, 0, true));
	assert(read_move(&pos, "Qh5+", &m, white) == 0);
	assert(m == create_move_g(sq_d1, sq_h5, queen, 0, true));
	assert(read_move(&pos, "Qh5#", &m, white) == 0);
	assert(m == create_move_g(sq_d1, sq_h5, queen, 0, true));
	print_move(&pos, m, str, mn_san, white);
	assert(strcmp(str, "Qh5#") == 0);
}
Exemple #12
0
void init_lists(void)
{
    int i,j;

    printf("Generating textures.\n");
    init_textures();

    for (i=0;i<17;i++)
	transl[i+12] = transl[44-i] = sin(((GLfloat) i)*M_PI/32)/1.5;

    for (i=0;i<17;i++)
	list[i+8] = list[40-i] = i*8;

    for (i=0;i<8;i++)
	list[40+i] = list[7-i] = i*8;

    printf("Generating display lists.\n");

    prepare_pion();
    prepare_koning();
    for (i=0;i<=16;i++)
    {
	phase = ((GLfloat) i) * M_PI/16.0;
	glNewList(PION + 8*i, GL_COMPILE);
	width[PION+8*i] = bwidth;
	height[PION+8*i] = bheight;
	do_pion();
	glEndList();

	glNewList(TOREN + 8*i, GL_COMPILE);
	do_toren();
	width[TOREN+8*i] = bwidth;
	height[TOREN+8*i] = bheight;
	glEndList();

	glNewList(PAARD + 8*i, GL_COMPILE);
	do_paard();
	width[PAARD+8*i] = bwidth;
	height[PAARD+8*i] = bheight;
	glEndList();

	glNewList(LOPER + 8*i, GL_COMPILE);
	do_loper();
	width[LOPER+8*i] = bwidth;
	height[LOPER+8*i] = bheight;
	glEndList();

	glNewList(KONING + 8*i, GL_COMPILE);
	do_koning();
	width[KONING+8*i] = bwidth;
	height[KONING+8*i] = bheight;
	glEndList();

	glNewList(DAME + 8*i, GL_COMPILE);
	do_dame();
	width[DAME+8*i] = bwidth;
	height[DAME+8*i] = bheight;
	glEndList();
    }

    for (i=0;i<10;i++)
	for (j=0;j<10;j++)
	{
	    board[i][j] = 0;
	    cycle[i][j] = -1;
	}

    for (i=1;i<9;i++)
    {
	board[i][2] = PION + WIT;
	board[i][7] = PION + ZWART;
    }
    board[1][1] = board[8][1] = TOREN + WIT;
    board[2][1] = board[7][1] = PAARD + WIT;
    board[3][1] = board[6][1] = LOPER + WIT;
    board[4][1] = DAME + WIT;
    board[5][1] = KONING + WIT;

    board[1][8] = board[8][8] = TOREN + ZWART;
    board[2][8] = board[7][8] = PAARD + ZWART;
    board[3][8] = board[6][8] = LOPER + ZWART;
    board[4][8] = DAME + ZWART;
    board[5][8] = KONING + ZWART;
    read_move();
}