示例#1
1
文件: game.c 项目: fmalazemi/chess
bool
apply_move (Game *g, int from, int to, char promote_in)
{
  if (g->result != IN_PROGRESS) return FALSE;
  Board *board = current_board (g);
  Board *new_board = NEW_BOARD;
  char capture = 0;
  char *move_done = castling (board, castling_type (board, from, to), new_board);
  if (!move_done) // If move is not a castling
    {
      if (!try_move (board, from, to, promote_in, new_board, &move_done, &capture))
        {
          free (new_board);
          return FALSE;
        }
    }
  // Ok move is legal, update the game
  update_castling (new_board, from);
  update_en_passant (new_board, from, to);
  if (new_board->active_color)
    new_board->fullmove_number++;
  new_board->active_color = !new_board->active_color;
  if (capture || toupper (board->placement[from]) == 'P')
    new_board->halfmove_clock = 0;
  else
    new_board->halfmove_clock++;
  g->boards[g->current] = new_board;
  g->moves[g->current] = move_done;
  g->coord_moves[g->current] = ft_to_coord_move (from, to, promote_in);
  g->current++;
  // Test check or checkmate of opponent king
  if (king_in_check (new_board, new_board->active_color))
    {
      if (king_in_checkmate (new_board, new_board->active_color))
        {
          strcat (move_done, "#");
          g->result = !new_board->active_color;
        }
      else
        strcat (move_done, "+");
    }
  // Test insufficient material
  else if (insufficient_material (new_board))
    g->result = DRAW;
  // Test stalemate
  else if (stalemate (new_board, new_board->active_color))
    g->result = DRAW;
  return TRUE;
}
示例#2
0
void* tram_ep(void *arg){
    task_context_t * ctx = arg;
	int start = rand() % 4;
	int stop = rand() % 3;
	ctx->posX = from_to[start][0][0];
	ctx->posY = from_to[start][0][1];
	while (!place_at(ctx->posX, ctx->posY)) {
		sleep(1);
	}
    int destX = from_to[start][stop+1][0];
    int destY = from_to[start][stop+1][1];
    //printf("going from %d %d to %d %d\n", ctx->posX, ctx->posY, destX, destY);
    while (destX != ctx->posX || destY != ctx->posY) {
        if (destX > ctx->posX && ctx->posY == TRAM_LANE_BOTTOM) {
            try_move(ctx, +1, 0);
        } else if (destX < ctx->posX && ctx->posY == TRAM_LANE_TOP) {
            try_move(ctx, -1, 0);
        } else if (destY > ctx->posY && ctx->posX == TRAM_LANE_LEFT) {
            try_move(ctx, 0, 1);
        } else if (destY < ctx->posY && ctx->posX == TRAM_LANE_RIGHT) {
            try_move(ctx, 0, -1);
        }
        sleep(1);
    }
    
    remove_from(ctx->posX, ctx->posY);
	ctx->alive = 0;
	return NULL;
}
示例#3
0
bool seek_party(short i,location l1,location l2)
{
	bool acted_yet = false;
	short m,n;
				if ((l1.x > l2.x) && (l1.y > l2.y)) 
					acted_yet = try_move(i,l1,-1,-1);
				if ((l1.x < l2.x) & (l1.y < l2.y) & (acted_yet == false)) 
					acted_yet = try_move(i,l1,1,1);											
				if ((l1.x > l2.x) & (l1.y < l2.y) & (acted_yet == false)) 
					acted_yet = try_move(i,l1,-1,1);								
				if ((l1.x < l2.x) & (l1.y > l2.y) & (acted_yet == false)) 
					acted_yet = try_move(i,l1,1,-1);			
				if ((l1.x > l2.x) & (acted_yet == false)) 
					acted_yet = try_move(i,l1,-1,0);
				if ((l1.x < l2.x) & (acted_yet == false)) 
					acted_yet = try_move(i,l1,1,0);											
				if ( (l1.y < l2.y) & (acted_yet == false)) 
					acted_yet = try_move(i,l1,0,1);								
				if ( (l1.y > l2.y) & (acted_yet == false)) 
					acted_yet = try_move(i,l1,0,-1);	
				if (acted_yet == false) {
					futzing++;
					m = get_ran(1,0,2) - 1;
					n = get_ran(1,0,2) - 1;
					acted_yet = try_move(i,l1,m,n);	
					}
	return acted_yet;
}
示例#4
0
bool flee_party(short i,location l1,location l2)
{
	bool acted_yet = false;

				if ((l1.x > l2.x) & (l1.y > l2.y)) 
					acted_yet = try_move(i,l1,1,1);
				if ((l1.x < l2.x) & (l1.y < l2.y) & (acted_yet == false)) 
					acted_yet = try_move(i,l1,-1,-1);											
				if ((l1.x > l2.x) & (l1.y < l2.y) & (acted_yet == false)) 
					acted_yet = try_move(i,l1,1,-1);								
				if ((l1.x < l2.x) & (l1.y > l2.y) & (acted_yet == false)) 
					acted_yet = try_move(i,l1,-1,+1);			
				if ((l1.x > l2.x) & (acted_yet == false)) 
					acted_yet = try_move(i,l1,1,0);
				if ((l1.x < l2.x) & (acted_yet == false)) 
					acted_yet = try_move(i,l1,-1,0);											
				if ( (l1.y < l2.y) & (acted_yet == false)) 
					acted_yet = try_move(i,l1,0,-1);								
				if ( (l1.y > l2.y) & (acted_yet == false)) 
					acted_yet = try_move(i,l1,0,1);			
				if (acted_yet == false) {
					futzing++;
					acted_yet = rand_move(i);	
					}				
	return acted_yet;
}
示例#5
0
void creature_t::step_toward(int px, int py, creature_t* exclude)
{
  TCODMap* map = new TCODMap(current_dungeon->width, current_dungeon->height);

  for (int y = 0; y < current_dungeon->height; y++)
  {
    for (int x = 0; x < current_dungeon->width; x++)
    {
      tile_t* tile = get_tile_at(current_dungeon, x, y);
      bool walkable = tile_is_walkable_by(*tile, this);
      if (flag & CF_INTELLIGENT)
      {
        // Intelligent monsters know how to open doors.
        if (tile->id == TILE_DOOR_CLOSED)
        {
          walkable = true;
        }
      }
      map->setProperties(x, y, tile->properties & TILE_PROP_TRANSPARENT, walkable);
    }
  }

  for (size_t i = 0; i < current_dungeon->creatures.size(); i++)
  {
    creature_t* c = current_dungeon->creatures.at(i);
    if (c == this || c == exclude)
      continue;

    if (sees(c))
    {
      // If this creature can see another creature, then consider that
      // square as unwalkable.
      map->setProperties(c->pos.x, c->pos.y, 1, 0);
    }
  }

  TCODPath* path = new TCODPath(map, 1.0f);
  if (path->compute(pos.x, pos.y, px, py))
  {
    if (path->size() > 0)
    {
      int move_x, move_y;
      path->get(0, &move_x, &move_y);
      try_move(move_x, move_y);
    }
    else
    {
      // No path found, walk around confused.
      random_walk();
    }
  }
  else
  {
    // No path found, walk around confused.
    random_walk();
  }

  delete map;
  delete path;
}
int SpriteEx::update()
{
	IComponent::update();
	list<Sprite*>::iterator i;
	list<Sprite*> sprites = parent->getSprites();

	if (gravity)
	{
		dy += GRAVITY_ACC;
		if (dy > MAX_Y) dy = MAX_Y;
		if (dy < -MAX_Y) dy = -MAX_Y;
	}

	int block = try_move(dx, dy);

	if ((block & Y_BLOCK) > 0)
	{
		onCol(ST_TILE, NULL, DIR_VERTICAL);
		air = false;
		dy = 0;
	}
	else
	{
		air = true;
	}
	if ((block & X_BLOCK) > 0)
	{
		onCol(ST_TILE, NULL, DIR_HORIZONTAL);
	}

	if (y > FLOOR)
	{
		onCol(ST_FLOOR, NULL, DIR_VERTICAL);
	}

	if (y < CEIL)
	{
		y = CEIL;
		onCol(ST_BOUNDS, NULL, DIR_VERTICAL);
	}

	if (x < 0)
	{
		x = 0;
		onCol(ST_BOUNDS, NULL, DIR_HORIZONTAL);
	}

	int mapw = teg_pixelw(parent->getMap());
	if ((x + w) >= mapw)
	{
		x = mapw - w;
		onCol(ST_BOUNDS, NULL, DIR_HORIZONTAL);
	}

	return 0;
}
示例#7
0
int
main(int argc, char **argv)
{
    char *err;
    struct game g = { 0 };
    long w, h, p;

    if (argc != 4 && argc != 5) {
        fprintf(stderr, "Usage: boxes height width playercount [filename]\n");
        exit(1);
    }

    h = strtol(argv[1], &err, 10);
    if (*err != '\0' || h < 2 || h > 999) {
        fprintf(stderr, "Invalid grid dimensions\n");
        exit(2);
    }

    w = strtol(argv[2], &err, 10);
    if (*err != '\0' || w < 2 || w > 999) {
        fprintf(stderr, "Invalid grid dimensions\n");
        exit(2);
    }

    p = strtol(argv[3], &err, 10);
    if (*err != '\0' || p < 2 || p > 100) {
        fprintf(stderr, "Invalid player count\n");
        exit(3);
    }

    g.width = w;
    g.height = h;
    g.num_players = p;
    g.possible_closures = w * h;

    allocate_empty_grid(&g);

    if (argc == 5) {
        read_grid_file(argv[4], &g);
    }

    while (1) {
        print_grid(stdout, &g);
        if (g.close_count == g.possible_closures) {
            pick_winner(&g);
        }
        while (try_move(&g));
        g.current_player = (g.current_player + 1) % g.num_players;
    }

    return 0;
}
示例#8
0
void creature_t::random_walk()
{
  int move_x = pos.x;
  int move_y = pos.y;
  int dir = random(0, 7);
  if (dir == 0)
  {
    move_x++;
  }
  else if (dir == 1)
  {
    move_x--;
  }
  else if (dir == 2)
  {
    move_y++;
  }
  else if (dir == 3)
  {
    move_y--;
  }
  else if (dir == 4)
  {
    move_x++;
    move_y++;
  }
  else if (dir == 5)
  {
    move_x++;
    move_y--;
  }
  else if (dir == 6)
  {
    move_x--;
    move_y++;
  }
  else if (dir == 7)
  {
    move_x--;
    move_y--;
  }

  try_move(move_x, move_y);
}
示例#9
0
文件: sudoku.c 项目: salassam/42
int		find_solution(int **tab, int row, int col, int is_reverse)
{
	if (!(row < 9 && col < 9))
		return (1);
	if (tab[row][col] != 0)
	{
		if ((col + 1) < 9)
			return (find_solution(tab, row, col + 1, is_reverse));
		else if ((row + 1) < 9)
			return (find_solution(tab, row + 1, 0, is_reverse));
		else
			return (1);
	}
	else
	{
		if (is_reverse)
			return (try_move_rev(tab, row, col, is_reverse));
		else
			return (try_move(tab, row, col, is_reverse));
	}
	return (0);
}
示例#10
0
文件: game.cpp 项目: besza/projeszk
void Game::message_handler(Server& server, int player, std::string msg)
{
    std::vector<std::string> words;
    boost::trim(msg);
    boost::split(words, msg, boost::is_any_of("\t "), boost::token_compress_on);

    if (words.empty()) {
        error(server, player);
        return;
    }

    if (words[0] == "ready") {
        if (ready(player)) {
            error(server, player);
            return;
        }

        ready(player) = true;
        player_color(player) = ready(other(player)) ? BLACK : WHITE;
        server.send(player, "color " + show(player_color(player)));

        if (ready1 && ready2) {
            reset_playing();
            server.broadcast("start");
        }
    } else if (words[0] == "say") {
        std::stringstream ss;
        ss << "say" << player;
        for (size_t i = 1; i < words.size(); ++i) {
            ss << " " << words[i];
        }
        server.broadcast(ss.str());
    } else if (words[0] == "move") {
        if (!playing || current_color != player_color(player) ||
            words.size() != 3) {
            server.send(player, "error move");
            return;
        }

        boost::optional<Square> maybe_from = read_square(words[1]);
        boost::optional<Square> maybe_to = read_square(words[2]);
        if (!maybe_from || !maybe_to) {
            error(server, player);
            return;
        }

        boost::optional<MoveResult> maybe_move_result =
            try_move(board, player_color(player), *maybe_from, *maybe_to);
        if (!maybe_move_result) {
            server.send(player, "error move");
            return;
        }

        current_color = current_color == WHITE ? BLACK : WHITE;
        if (maybe_move_result->opponent_cannot_move) {
            reset_waiting();
        }

        server.broadcast(show(*maybe_move_result));
    } else if (words[0] == "resign") {
        if (!playing || current_color != player_color(player)) {
            error(server, player);
            return;
        }

        reset_waiting();
        server.broadcast("resign");
    } else {
        error(server, player);
    }
}