#if defined(SDLFUNCS) || defined(DOS)

int clippy_accept_resign(void);
int clippy_accept_draw(void);
void clippy_recommend_move(void);

int select_tile(struct coord *legal_moves, unsigned char legal_move_count, unsigned char *row, unsigned char *col)
{
#ifdef SDLFUNCS
  SDL_Scancode i;
#endif
#ifdef DOS
  int i;
#endif
  static char old_row = 7;
  static char old_col = 7;

  *row = old_row;
  *col = old_col;

  while(1)
  {
    i = poll_keypress();
#ifdef SDLFUNCS
    if(i == SDL_SCANCODE_UNKNOWN) continue;
#endif
    if(i == KEY_ENTER) break;
    switch (i)
    {
      case KEY_UP:
        if(setup.board_rotation == 0) { up:if(*row) --*row; }
        else if(setup.board_rotation == 1) goto left;
        else if(setup.board_rotation == 2) goto down;
        else goto right;
        break;
      case KEY_DOWN:
        if(setup.board_rotation == 0) { down:if(*row < 7) ++*row; }
        else if(setup.board_rotation == 1) goto right;
        else if(setup.board_rotation == 2) goto up;
        else goto left;
        break;
      case KEY_LEFT:
        if(setup.board_rotation == 0) { left:if(*col) --*col; }
        else if(setup.board_rotation == 1) goto down;
        else if(setup.board_rotation == 2) goto right;
        else goto up;
        break;
      case KEY_RIGHT:
        if(setup.board_rotation == 0) { right:if(*col < 7) ++*col; }
        else if(setup.board_rotation == 1) goto up;
        else if(setup.board_rotation == 2) goto left;
        else goto down;
        break;
      case KEY_F7:
        clippy_recommend_move();
        break;
      case KEY_F8:
        if(clippy_accept_resign()) return 1;
        break;
      case KEY_F9:
        if(clippy_accept_draw()) return 1;
        break;
      case KEY_ESC: if(show_menu(mainmenu)) return 1;
    }
    draw_board(&board, legal_moves, legal_move_count, *row, *col);
  }

  old_col = *col;
  old_row = *row;

  return 0;
}
#endif

void init_board(struct board *brd)
{
  memset(brd, 0, sizeof(struct board));
  brd->tiles[0][0] = PC_ROOK | PC_BLACK;
  brd->tiles[0][1] = PC_KNIGHT | PC_BLACK;
  brd->tiles[0][2] = PC_BISHOP | PC_BLACK;
  brd->tiles[0][3] = PC_QUEEN | PC_BLACK;
  brd->tiles[0][4] = PC_KING | PC_BLACK;
  brd->tiles[0][5] = PC_BISHOP | PC_BLACK;
  brd->tiles[0][6] = PC_KNIGHT | PC_BLACK;
  brd->tiles[0][7] = PC_ROOK | PC_BLACK;
  memset(brd->tiles[1], PC_PAWN_UNMOVED | PC_BLACK, 8);

  memset(brd->tiles[6], PC_PAWN_UNMOVED | PC_WHITE, 8);
  brd->tiles[7][0] = PC_ROOK | PC_WHITE;
  brd->tiles[7][1] = PC_KNIGHT | PC_WHITE;
  brd->tiles[7][2] = PC_BISHOP | PC_WHITE;
  brd->tiles[7][3] = PC_QUEEN | PC_WHITE;
  brd->tiles[7][4] = PC_KING | PC_WHITE;
  brd->tiles[7][5] = PC_BISHOP | PC_WHITE;
  brd->tiles[7][6] = PC_KNIGHT | PC_WHITE;
  brd->tiles[7][7] = PC_ROOK | PC_WHITE;

}

struct screen_coord tile_coords(signed char row, signed char col)
{
  struct screen_coord retval;

  if(setup.board_rotation == 0)
  {
    retval.x = 8+col*24;
    retval.y = row*24;
  }
  else if(setup.board_rotation == 1)
  {
    retval.x = 8+(7-row)*24;
    retval.y = 8+col*24;
  }
  else if(setup.board_rotation == 2)
  {
    retval.x = (7-col)*24;
    retval.y = 8+(7-row)*24;
  }
  else
  {
    retval.x = row*24;
    retval.y = (7-col)*24;
  }

  return retval;
}

struct screen_coord piece_coords(unsigned char row, unsigned char col, unsigned char piece)
{
  struct screen_coord retval = tile_coords(row, col);

  retval.x += 2;
  retval.y += (((piece & 7) < PC_ROOK) ? 2 : 1);

  return retval;
}

void draw_board(struct board *brd, struct coord *legal_moves, unsigned char legal_move_count, unsigned selrow, unsigned selcol)
{
  unsigned char row, col;
  char chr;
  unsigned char piece;
  struct screen_coord px_coords;
  struct screen_coord sel_coords;

#if defined(SDLFUNCS) || defined(DOS)
  if(graphics)
  {
    fill_screen(0x01);
    for(col=0;col<8;col++)
    {
      px_coords = tile_coords(8, col);
      if(setup.board_rotation < 2) px_coords.x += 16;
      else px_coords.y += 16;
      draw_chr(px_coords.x, px_coords.y, 0x7F, 'A' + col);
    }
    for(row=0;row<8;row++)
    {
      px_coords = tile_coords(row, -1);
      if(setup.board_rotation < 2) px_coords.y += 16;
      else px_coords.x -= 16;
      draw_chr(px_coords.x+16, px_coords.y, 0x7F, '1' + (7-row));

      for(col=0;col<8;col++)
      {
        px_coords = tile_coords(row, col);
        draw_rectangle(px_coords.x, px_coords.y, 24, 24, (row & 1) ^ (col & 1) ? 0x67 : 0xFF);
        if(legal_move_count && move_in_list(legal_moves, legal_move_count, row, col))
          draw_rectangle(px_coords.x+6, px_coords.y+6, 12, 12, 0x22);
        if(selrow == row && selcol == col)
        {
          sel_coords = tile_coords(row, col);
          draw_rectangle(sel_coords.x+2, sel_coords.y+2, 20, 20, 0x99);
        }
      }
    }
    draw_rectangle(205, 10, 105, 75, 0xFE);
    push_scrbitmap(208, 12, &clipupil, 0);
    push_scrbitmap(300, 12, &clipupil, 0);
    print_text(220, 12, 0x80, "- Black -");
    print_text(220, 22, 0x80, game_status->last_moves.last_moves_black[0]);
    print_text(220, 32, 0x80, game_status->last_moves.last_moves_black[1]);

    print_text(220, 52, 0x80, "- White -");
    print_text(220, 62, 0x80, game_status->last_moves.last_moves_white[0]);
    print_text(220, 72, 0x80, game_status->last_moves.last_moves_white[1]);

  }
#endif

  if(!graphics)
  {
    for(col=0;col<8;col++)
      printf("  %c  ", 'a' + col);
    printf("\n");
  }
  for(row=0;row<8;row++)
  {
    if(!graphics) printf("%c ", '8' - row);
    for(col=0;col<8;col++)
    {
      piece = piece_in_tile(brd, row, col);
#if defined(SDLFUNCS) || defined(DOS)
      if(graphics)
      {
        if(piece)
        {
          px_coords = piece_coords(row, col, piece);
          push_scrbitmap(px_coords.x, px_coords.y, piece_bitmap(piece), 0);
        }
      }
#endif
      if(!graphics)
      {
        switch(piece & 0x7)
        {
          case 0:
            chr = ' ';
            break;
          case PC_PAWN_UNMOVED: case PC_PAWN_MOVED:
            chr = 'P';
            break;
          case PC_ROOK:
            chr = 'R';
            break;
          case PC_KNIGHT:
            chr = 'H';
            break;
          case PC_BISHOP:
            chr = 'B';
          break;
          case PC_QUEEN:
            chr = 'Q';
            break;
          case PC_KING:
            chr = 'K';
            break;
        }
        if(piece & PC_BLACK)
          chr += 0x20;

        if((selrow == row && selcol == col)
           || (legal_move_count && move_in_list(legal_moves, legal_move_count, row, col)))
        {
          printf("[[%c]]", chr);
        }
        else if((row & 1) ^ (col & 1))
          printf("[ %c ]", chr);
        else printf("] %c [", chr);
      }
    }
    if(!graphics)
      printf("\n");
  }
#if defined(SDLFUNCS) || defined(DOS)
  if(graphics)
  {
    draw_clippy(230, 90, (selrow == -1) ? 0 : -3+sel_coords.y/24, (selcol == -1) ? -1 : -2 + ((sel_coords.x/24) >> 2));
    draw_screen_sprites();
    update_screen();
  }
#endif

}
