Exemple #1
0
	//  Variant 3.
	int calc_utility(PieceColor color)
	{
		//int eval = 0;
		int score[] = {1,2,3,5,7,11,13,17};
		int whitescore = 1;
		int blackscore = 1;
		for (int i = 0; i < BOARD_SIZE; ++i)
		{ 
			for (int j = 0; j < BOARD_SIZE; ++j)
			{ 
				if(get_piece(i,j)!=NULL)
				{
					if(get_piece(i,j)->get_color())
					{
						blackscore *= score[7-i];						
						if(i==0)
							blackscore=INT_MAX;						
					}
					if(!(get_piece(i,j)->get_color()))
					{
						whitescore *= score[i];						
						if(i==7)
							whitescore=INT_MAX;						
					}
				}
			}
		}
		cout<<whitescore << " " <<blackscore<<endl;
		return (whitescore-blackscore);
	}//*/
Exemple #2
0
// on MS-DOS terminals, we only have block characters for upper half and lower
// half. This lets us print two pieces per character.
std::string piece_matrix(lt::bitfield const& p, int width, int* height)
{
	// print two rows of pieces at a time
	int piece = 0;
	++*height;
	std::string ret;
	ret.reserve((p.size() + width * 2 - 1) / width);
	while (piece < p.size())
	{
		for (int i = 0; i < width; ++i)
		{
			// each character has 8 pieces. store them in a byte to use for lookups
			// the ordering of these bits
			int const c = get_piece(p, piece)
				| (get_piece(p, width*2+piece) << 1);

			static char const* const chars[] =
			{
				" ",    // no piece     00
				"\xdf", // top piece    01
				"\xdc", // bottom piece 10
				"\xdb"  // both pieces  11
			};

			ret += chars[c];
			++piece;
		}
		ret += '\n';
		++*height;
		piece += width * 2; // skip another row, as we've already printed it
	}
	return ret;
}
Exemple #3
0
	vector<Board*>* list_all_moves()
	{
		vector<Board*> *moves = new vector<Board*> ();
		Board* temp = NULL;
		PieceColor turn = get_player() ? Black : White;
		int flag = 0;
		for (int i = 0; i < BOARD_SIZE; ++i)
		{
			for (int j = 0; j < BOARD_SIZE; ++j)
			{
			    //  For every piece, check all places if possible move exists.
				if (get_piece(i, j) != NULL && get_piece(i, j)->get_color()== turn)
				{
					for (int k = 0; k < BOARD_SIZE; ++k)
					{
						for (int l = 0; l < BOARD_SIZE; ++l)
						{
							if (i == k && j == l)
							{
								continue;
							}
							if ((temp = move(i, j, k, l)) != NULL)
							{
								moves->push_back(temp);
								flag = 1;
							}
						}
					}
				}
			}
		}
		if (flag == 0)
			return NULL;
		return moves;
	}
Exemple #4
0
/* We don't bother modeling all the nuances of the game's rules in these functions */
static void
move_pawn(char *board, int x, int y)
{
    if (isupper(get_piece(board, x - 1, y)))
        set_move(x, y, x - 1, y);
    else if (isupper(get_piece(board, x + 1, y)))
        set_move(x, y, x + 1, y);
    else if (get_piece(board, x, y + 2) == ' ')
        set_move(x, y, x, y + 2);
    else if (get_piece(board, x, y + 1) == ' ')
        set_move(x, y, x, y + 1);
}
Exemple #5
0
// KAGGRESSIVE heuristic: bonus for King with more space to back
ev_score_t kaggressive(position_t *p, rnk_t r, fil_t f, full_board_t* board) {
  piece_t x = get_piece(p, r, f, board);
  color_t c = color_of(x);
  tbassert(ptype_of(x) == KING, "ptype_of(x) = %d\n", ptype_of(x));

  square_t opp_sq = board->pieces[opp_color(c)][0];
  fil_t of = fil_of(opp_sq);
  rnk_t _or = (rnk_t) rnk_of(opp_sq);

  int delta_fil = of - f;
  int delta_rnk = _or - r;

  int bonus = 0;

  if (delta_fil >= 0 && delta_rnk >= 0) {
    bonus = (f + 1) * (r + 1);
  } else if (delta_fil <= 0 && delta_rnk >= 0) {
    bonus = (BOARD_WIDTH - f) * (r + 1);
  } else if (delta_fil <= 0 && delta_rnk <= 0) {
    bonus = (BOARD_WIDTH - f) * (BOARD_WIDTH - r);
  } else if (delta_fil >= 0 && delta_rnk <= 0) {
    bonus = (f + 1) * (BOARD_WIDTH - r);
  }

  return (KAGGRESSIVE * bonus) / (BOARD_WIDTH * BOARD_WIDTH);
}
Exemple #6
0
// KFACE heuristic: bonus (or penalty) for King facing toward the other King
ev_score_t kface(position_t *p, rnk_t r, fil_t f, full_board_t* board) {
  piece_t x = get_piece(p, r, f, board);
  color_t c = color_of(x);
  square_t opp_sq = board->pieces[opp_color(c)][0];
  int delta_fil = fil_of(opp_sq) - f;
  int delta_rnk = rnk_of(opp_sq) - r;
  int bonus;

  switch (ori_of(x)) {
    case NN:
      bonus = delta_rnk;
      break;

    case EE:
      bonus = delta_fil;
      break;

    case SS:
      bonus = -delta_rnk;
      break;

    case WW:
      bonus = -delta_fil;
      break;

    default:
      bonus = 0;
      tbassert(false, "Illegal King orientation.\n");
  }

  return (bonus * KFACE) / (abs(delta_rnk) + abs(delta_fil));
}
Exemple #7
0
int			main(void)
{
	char		*line;
	t_box		box;
	int			ret;
	int			i;

	ret = 0;
	ft_bzero(&box, (sizeof(box)));
	while ((ret = get_next_line(0, &line) != 0))
	{
		if ((i = -1) && box.b_play == 0)
			check_player(line, &box);
		else if ((ft_strnequ(line, "Plateau", 7)))
			create_grid(line, &box);
		else if ((ft_strnequ(line, "Piece", 5)))
		{
			get_piece(line, &box);
			print_map(&box);
			while (i < 69999999)
				i++;
			if (!try_rush(&box))
				return (0);
			reinitialise_box(&box);
		}
	}
	return (0);
}
Exemple #8
0
int				main(void)
{
	t_env	env;
	int		x;

	x = -1;
	init_env(&env);
	get_player(&env);
	while (1)
	{
		get_map(&env);
		get_piece(&env);
		if (contact_play(&env, 0, 0) != 1)
		{
			if (play(&env, 0, 0) != 1)
			{
				if (simple_play(&env) != 1)
				{
					ft_putstr("0 0\n");
					break ;
				}
			}
		}
		free_all(&env);
	}
	return (0);
}
void pack_dbf(dbhead_t *dbh)
{
	long	out_off, in_off;
	int	rec_cnt, new_cnt;
	char	*cp;

	if ((cp = (char *)malloc(dbh->db_rlen)) == NULL) {
		return;
	}
	in_off = out_off = dbh->db_hlen;

	new_cnt = 0;
	rec_cnt = dbh->db_records;
	while (rec_cnt > 0) {
		if (get_piece(dbh, in_off, cp, dbh->db_rlen) < 0)
			break;

		if (*cp != DELETED_RECORD) {
			/* write the record into the file */
			if (put_piece(dbh, out_off, cp, dbh->db_rlen) < 0)
				break;
			out_off += dbh->db_rlen;
			new_cnt++;
		}
		in_off += dbh->db_rlen;
		rec_cnt--;
	}
	free(cp);
	if (rec_cnt == 0)
		dbh->db_records = new_cnt;
}
Exemple #10
0
// this function uses the block characters that splits up the glyph in 4
// segments and provide all combinations of a segment lit or not. This allows us
// to print 4 pieces per character.
std::string piece_matrix(lt::bitfield const& p, int width, int* height)
{
	// print two rows of pieces at a time
	int piece = 0;
	++*height;
	std::string ret;
	ret.reserve((p.size() + width * 2 - 1) / width / 2 * 4);
	while (piece < p.size())
	{
		for (int i = 0; i < width; ++i)
		{
			// each character has 4 pieces. store them in a byte to use for lookups
			int const c = get_piece(p, piece)
				| (get_piece(p, piece+1) << 1)
				| (get_piece(p, width*2+piece) << 2)
				| (get_piece(p, width*2+piece+1) << 3);

			// we have 4 bits, 16 different combinations
			static char const* const chars[] =
			{
				" ",      // no bit is set             0000
				"\u2598", // upper left                0001
				"\u259d", // upper right               0010
				"\u2580", // both top bits             0011
				"\u2596", // lower left                0100
				"\u258c", // both left bits            0101
				"\u259e", // upper right, lower left   0110
				"\u259b", // left and upper sides      0111
				"\u2597", // lower right               1000
				"\u259a", // lower right, upper left   1001
				"\u2590", // right side                1010
				"\u259c", // lower right, top side     1011
				"\u2584", // both lower bits           1100
				"\u2599", // both lower, top left      1101
				"\u259f", // both lower, top right     1110
				"\x1b[7m \x1b[27m" // all bits are set (full block)
			};

			ret += chars[c];
			piece += 2;
		}
		ret += "\x1b[K\n";
		++*height;
		piece += width * 2; // skip another row, as we've already printed it
	}
	return ret;
}
Exemple #11
0
int main(void){
	char ply1p;	// Player 1 piece type (X or O)
	char ply2p;	// Player 2/ Computer piece type (X or O)
	char level;	// Human vs human game or human vs computer game ('H' or 'C')
	
	get_piece('c', &ply1p, &ply2p);
	
		}
Exemple #12
0
static void
move_rook(char *board, int x, int y)
{
    if (isupper(get_piece(board, x, y + 1)) || get_piece(board, x, y + 1) == ' ')
        set_move(x, y, x, y + 1);
    else if (isupper(get_piece(board, x - 1, y)) || get_piece(board, x - 1, y) == ' ')
        set_move(x, y, x - 1, y);
    else if (isupper(get_piece(board, x + 1, y)) || get_piece(board, x + 1, y) == ' ')
        set_move(x, y, x + 1, y);
    else if (isupper(get_piece(board, x, y - 1)) || get_piece(board, x, y - 1) == ' ')
        set_move(x, y, x, y - 1);
}
Exemple #13
0
// Translate a position struct into a fen string
// NOTE: When you use the test framework in search.c, you should modify this
// function to match your optimized board representation in move_gen.c
//
// Input:   (populated) position struct
//          empty string where FEN characters will be written
// Output:   null
int pos_to_fen(position_t *p, char *fen) {
  int pos = 0;
  int i;

  for (rnk_t r = BOARD_WIDTH - 1; r >=0 ; --r) {
    int empty_in_a_row = 0;
    for (fil_t f = 0; f < BOARD_WIDTH; ++f) {
      piece_t piece = get_piece(p, r, f, &base_board);
      if (ptype_of(piece) == INVALID) {     // invalid square
        tbassert(false, "Bad news, yo.\n");        // This is bad!
      }

      if (ptype_of(piece) == EMPTY) {       // empty square
        empty_in_a_row++;
        continue;
      } else {
        if (empty_in_a_row) fen[pos++] = '0' + empty_in_a_row;
        empty_in_a_row = 0;
        int ori = ori_of(piece);  // orientation
        color_t c = color_of(piece);

        if (ptype_of(piece) == KING) {
          for (i = 0; i < 2; i++) fen[pos++] = king_ori_to_rep[c][ori][i];
          continue;
        }

        if (ptype_of(piece) == PAWN) {
          for (i = 0; i < 2; i++) fen[pos++] = pawn_ori_to_rep[c][ori][i];
          continue;
        }
      }
    }
    // assert: for larger boards, we need more general solns
    tbassert(BOARD_WIDTH <= 10, "BOARD_WIDTH = %d\n", BOARD_WIDTH);
    if (empty_in_a_row == 10) {
      fen[pos++] = '1';
      fen[pos++] = '0';
    } else if (empty_in_a_row) {
      fen[pos++] = '0' + empty_in_a_row;
    }
    if (r) fen[pos++] = '/';
  }
  fen[pos++] = ' ';
  fen[pos++] = 'W';
  fen[pos++] = '\0';

  return pos;
}
Exemple #14
0
R_API void r_cons_canvas_write(RConsCanvas *c, const char *s) {
	char *p, ch;
	int orig_x, x;
	int left, slen, attr_len, piece_len;

	if (!c || !s || !*s) {
		return;
	}
	/* split the string into pieces of non-ANSI chars and print them normally,
	** using the ANSI chars to set the attr of the canvas */
	orig_x = c->x;
	do {
		const char *s_part = set_attr (c, s);
		ch = 0;
		piece_len = get_piece (s_part, &ch);
		if (piece_len == 0 && ch == '\0' && s_part == s) {
			break;
		}
		left = 0;
		p = prefixline (c, &left);
		slen = R_MIN (left, piece_len);
		attr_len = slen <= 0 && s_part != s? 1: slen;
		if (attr_len > 0) {
			stamp_attr (c, attr_len);
		}
		x = c->x - c->sx;
		if (G (x, c->y - c->sy)) {
			memcpy (p, s_part, slen);
		}
		s = s_part;
		if (ch == '\n') {
			c->y++;
			c->x = orig_x;
			s++;
			if (*s == '\0') {
				break;
			}
		} else {
			c->x += slen;
		}
		s += piece_len;
	} while (*s);
	c->x = orig_x;
}
Exemple #15
0
/*
 * get a record off the database
 */
char *get_dbf_record(dbhead_t *dbh, long rec_num)
{
    long	offset;
    char	*cp;

    if (rec_num > dbh->db_records) {
        return NULL;
    }
    if ((cp = (char *)malloc(dbh->db_rlen)) == NULL) {
        return NULL;
    }

    /* go to the correct spot on the file */
    offset = dbh->db_hlen + (rec_num - 1) * dbh->db_rlen;
    if (get_piece(dbh, offset, cp, dbh->db_rlen) != dbh->db_rlen) {
        free(cp);
        cp = NULL;
    }
    if (cp)
        dbh->db_cur_rec = rec_num;
    return cp;
}
Exemple #16
0
void pack_dbf(dbhead_t *dbh)
{
    long	out_off, in_off;
    int	rec_cnt, new_cnt;
    char	*cp;

    if ((cp = (char *)malloc(dbh->db_rlen)) == NULL) {
        return;
    }
    in_off = out_off = dbh->db_hlen;

    new_cnt = 0;
    rec_cnt = dbh->db_records;
    while (rec_cnt > 0) {
        if (get_piece(dbh, in_off, cp, dbh->db_rlen) < 0)
            break;

        if (*cp != DELETED_RECORD) {
            /* write the record into the file */
            if (put_piece(dbh, out_off, cp, dbh->db_rlen) < 0)
                break;
            out_off += dbh->db_rlen;
            new_cnt++;
        }
        in_off += dbh->db_rlen;
        rec_cnt--;
    }
    free(cp);

    /* Try to truncate the file to the right size. */
    if (ftruncate(dbh->db_fd, out_off) != 0) {
        TSRMLS_FETCH();
        php_error_docref(NULL TSRMLS_CC, E_WARNING, "dbase_pack() couldn't truncate the file to the right size. Some deleted records may still be left in there.");
    }

    if (rec_cnt == 0)
        dbh->db_records = new_cnt;
}
Exemple #17
0
R_API void r_cons_canvas_write(RConsCanvas *c, const char *s) {
	char *p, ch;
	int orig_x, x;
	int left, slen, piece_len;

	if (!c || !s || !*s) return;

	/* split the string into pieces of non-ANSI chars and print them normally,
	 * using the ANSI chars to set the attr of the canvas */
	orig_x = c->x;
	do {
		s = set_attr (c, s);
		piece_len = get_piece (s, &ch);
		if (piece_len == 0 && ch == '\0') break;

		slen = 0;
		if (piece_len > 0) {
			p = prefixline (c, &left);
			slen = R_MIN (left, piece_len);
			x = c->x - c->sx;
			if (G (x, c->y - c->sy)) {
				stamp_attr(c, slen);
				memcpy (p, s, slen);
			}
		}

		if (ch == '\n') {
			c->y++;
			c->x = orig_x;
			s++;
			if (*s == '\0') break;
		} else {
			c->x += slen;
		}
		s += piece_len;
	} while (*s);
}
Exemple #18
0
static void
move_knight(char *board, int x, int y)
{
    if (isupper(get_piece(board, x + 1, y + 2)) || get_piece(board, x + 1, y + 2) == ' ')
        set_move(x, y, x + 1, y + 2);
    else if (isupper(get_piece(board, x + 2, y + 1)) || get_piece(board, x + 2, y + 1) == ' ')
        set_move(x, y, x + 2, y + 1);
    else if (isupper(get_piece(board, x + 2, y - 1)) || get_piece(board, x + 2, y - 1) == ' ')
        set_move(x, y, x + 2, y - 1);
    else if (isupper(get_piece(board, x + 1, y - 2)) || get_piece(board, x + 1, y - 2) == ' ')
        set_move(x, y, x + 1, y - 2);
    else if (isupper(get_piece(board, x - 1, y + 2)) || get_piece(board, x - 1, y + 2) == ' ')
        set_move(x, y, x - 1, y + 2);
    else if (isupper(get_piece(board, x - 2, y + 1)) || get_piece(board, x - 2, y + 1) == ' ')
        set_move(x, y, x - 2, y + 1);
    else if (isupper(get_piece(board, x - 2, y - 1)) || get_piece(board, x - 2, y - 1) == ' ')
        set_move(x, y, x - 2, y - 1);
    else if (isupper(get_piece(board, x - 1, y - 2)) || get_piece(board, x - 1, y - 2) == ' ')
        set_move(x, y, x - 1, y - 2);
}
int HighlightBestMove(int blinknum, char** error)
{
	struct move_list* best_move_list = NULL;
	int number_of_boards_evaluated = 0;

	// Get the best next move
	int current_move_grade = get_best_moves(curSettings->minimax_depth, board, curSettings->next_turn, curMovesList, &best_move_list, &number_of_boards_evaluated);

	// Check for errors
	if (FAILED_ERROR == current_move_grade) {
		free_move_list(best_move_list);
		return -1;
	}

	move bestMove = best_move_list->mov;
	position startPos = bestMove.start_pos;
	position endPos = bestMove.end_pos;

	control* startSquare = buttonsBoard[(int)startPos.col][7 - (int)startPos.row];
	control* endSquare = buttonsBoard[(int)endPos.col][7 - (int)endPos.row];

	bool startHiglighted = startSquare->ishighlighted;
	bool endHiglighted = endSquare->ishighlighted;

	startSquare->ishighlighted = 1;
	endSquare->ishighlighted = 1;

	for (int i = 0; i < blinknum; i++){
		SwitchButtonHighlight(startSquare);
		SwitchButtonHighlight(endSquare);

		if (isPawnUpgradePossible(&bestMove, get_piece(board, startPos)))
		{
			char* fileName = ResolveFileNameFromLetter(bestMove.new_disc);
			char* name = ResolveNameFromLetter(bestMove.new_disc);

			if (fileName != NULL){
				control* chessPiece_control;
				if (-1 == Create_panel_from_bmp(
					fileName,
					name,
					(Sint16)(GAMEBOARDBACKGROUND_W - BUTTON_W - MARGIN),
					(Sint16)(0.5*BOARD_H - SQUARE_H - 0.5),
					(Uint16)SQUARE_W,
					(Uint16)SQUARE_H,
					&chessPiece_control, error))
				{
					return -1;
				}
				UINode* chessPiece_node;
				if (-1 == CreateAndAddNodeToTree(chessPiece_control, tree->children[0], &chessPiece_node, error))
				{
					FreeControl(chessPiece_control);
					return -1;
				}
			}
		}

		// DrawTree
		if (-1 == FlipTree(error))
		{
			return -1;
		}
		SDL_Delay(250);
	}



	startSquare->ishighlighted = startHiglighted;
	endSquare->ishighlighted = endHiglighted;

	free_move_list(best_move_list);
	return 0;
}
void GameBoardSquare_ButtonClick(control* input)
{
	if ((curSettings->next_turn == curSettings->user_color || curSettings->game_mode == TWO_PLAYERS_GAME_MODE || isUpgrade) && gameOver == false){
		if (input == gameSelectedSquare_control)
		{
			switchOffAllButtons();
			gameSelectedSquare_control = NULL;
			free_move_list(posMovesFromCurPos);
			posMovesFromCurPos = NULL;
		}
		else if (gameSelectedSquare_control == NULL)
		{
			position chosenPos = GetPosOfSquare(input);

			if (is_piece_of_color(get_piece(board, chosenPos), curSettings->next_turn) == true){
				SwitchButtonHighlight(input);
				gameSelectedSquare_control = input;
				get_moves_from_pos(curMovesList, chosenPos, &posMovesFromCurPos);
				HightlightPosMoves(posMovesFromCurPos);
			}
		}
		else if (gameSelectedSquare_control != NULL)
		{
			position startPos = GetPosOfSquare(gameSelectedSquare_control);

			position endPos = GetPosOfSquare(input);

			chosenMove = (move*)(calloc(1, sizeof(move)));
			if (chosenMove == NULL)
			{
				guiQuit = -1;
				return;
			}
			chosenMove->start_pos = startPos;
			chosenMove->end_pos = endPos;
			chosenMove->new_disc = EMPTY;

			if (is_move_in_move_list(chosenMove, curMovesList) == true){
				if (isPawnUpgradePossible(chosenMove, board[(int)startPos.col][(int)startPos.row]) == true){
					if (-1 == DrawPiecesOnSidePanelFilterColor(tree->children[0], &upgradePieces_ButtonClick, curSettings->next_turn, &error_global))
					{
						free(chosenMove);
						chosenMove = NULL;
						guiQuit = -1;

					}
					isUpgrade = true;
				}
				else
				{
					make_move(board, chosenMove);
					SwitchButtonHighlight(gameSelectedSquare_control);
					gameSelectedSquare_control = NULL;
					curSettings->next_turn = get_opposite_color(curSettings->next_turn);
					free_move_list(curMovesList);
					curMovesList = NULL;
					free_move_list(posMovesFromCurPos);
					posMovesFromCurPos = NULL;
					free(chosenMove);
					chosenMove = NULL;
					Game();
					return;
				}
			}
		}

		// DrawTree
		if (-1 == FlipTree(&error_global))
		{
			guiQuit = -1;
			return;
		}
	}
}
Exemple #21
0
    Board* move(short int src_x, short int src_y, short int dest_x,short int dest_y)
    {

        Piece* src = get_piece(src_x, src_y);
        Piece* dest = get_piece(dest_x, dest_y);

	    if (src == NULL) 
	    {
		    cout << "\tSource state is not valid." << endl;
		    return NULL;
	    }

	    if (src_x == dest_x && src_y == dest_y) 
	    {
		    cout << "\tSource is equal to destination." << endl;
		    return NULL;
	    }

	    if (src->get_color() == White)
	    {
		    if ((src_x - dest_x) == -1) 
		    {
		        // Forward move.
			    if (src_y == dest_y && dest == NULL) 
			    {
			        //  Normal move.
				    return move_piece(src_x, src_y, dest_x, dest_y);
			    } 
			    else if (abs(src_y - dest_y) == 1 && dest != NULL && dest->get_color() == Black) 
			    {
			        //  Kill move.
				    return move_piece(src_x, src_y, dest_x, dest_y);
			    } 
			    else
			    {
			        //  Impossible move.
				    return NULL;
			    }
		    } 
		    else
		    {
		        //  Impossible move.
			    return NULL;
		    }
	    } 
	    else 
	    {
	        // Black Piece
		    if ((src_x - dest_x) == 1)
		    {
		        // Forward move.
			    if (src_y == dest_y && dest == NULL)
			    {
			        //  Normal move.
				    return move_piece(src_x, src_y, dest_x, dest_y);
			    } 
			    else if (abs(src_y - dest_y) == 1 && dest != NULL && dest->get_color() == White)
			    {
			        //  Kill move.
				    return move_piece(src_x, src_y, dest_x, dest_y);
			    } 
			    else
			    {
			        //  Impossible move.
				    return NULL;
			    }
		    } 
		    else
		    {
		        //  Impossible move.
			    return NULL;
		    }
	    } 

        return NULL;
    }