Skip to content

Instantly share code, notes, and snippets.

View jcbribeiro's full-sized avatar

José Carlos Bregieiro Ribeiro jcbribeiro

View GitHub Profile
@jcbribeiro
jcbribeiro / DistanceMatrixExample.java
Created March 24, 2017 14:33
An AsyncTask that gets the walking distance and estimated travel time from the ESTG-IPLeiria building to the Praça Rodrigues Lobo in Leiria, and displays it in a Toast.
/*
Notes:
-- you must enable the INTERNET permission in the app's manifest.
-- you must enable "Google Maps Distance Matrix API" for the project in the Google Developers Console
-- example URL:
https://maps.googleapis.com/maps/api/distancematrix/json?origins=ESTG Leiria, Leiria, Portugal&destinations=Praça Rodrigues Lobo, Leiria, Portugal&mode=walking&key=YOUR_API_KEY
@jcbribeiro
jcbribeiro / PlacePhotosExample.java
Created March 24, 2017 14:36
An AsyncTask that gets an image for a Place and displays it in a dialog.
/*
Notes:
-- you must enable the "Google Places API for Android" for the project in
the Google Developers Console:
https://developers.google.com/places/android-api/signup#api-console
-- you must connect to the to the Places API using the Google Play services API client:
https://developers.google.com/places/android-api/start#connect-client
@jcbribeiro
jcbribeiro / MainActivity.java
Last active May 2, 2017 10:08
Addicional image annotation requests to be added to the Cloud Vision Android Sample
// add the features we want
annotateImageRequest.setFeatures(new ArrayList<Feature>() {{
Feature labelDetection = new Feature();
labelDetection.setType("LABEL_DETECTION");
labelDetection.setMaxResults(10);
add(labelDetection);
Feature textDetection = new Feature();
textDetection.setType("TEXT_DETECTION");
textDetection.setMaxResults(10);
@jcbribeiro
jcbribeiro / MainActivity.java
Created May 2, 2017 10:10
Updated convertResponseToString() method, that deals with all annotations (not only label detection)
private String convertResponseToString(BatchAnnotateImagesResponse response) {
String message = "";
List<EntityAnnotation> annotations;
AnnotateImageResponse annotateImageResponse = response.getResponses().get(0);
message += "\n___\n Web Detection:\n\n";
Object webDetection = annotateImageResponse.get("webDetection");
if (webDetection != null) {
ArrayMap webDetectionAnnotations = (ArrayMap) webDetection;
public double computeTilesOutOfPlace(EightPuzzleState finalState) {
double h = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
// Blank is ignored so that the heuristic is admissible
if (this.matrix[i][j] != 0 && this.matrix[i][j]
!= finalState.matrix[i][j]) {
h++;
}
}
@jcbribeiro
jcbribeiro / AgentState.java
Created May 17, 2017 12:06
evaluate and getWinningPossibilities methods for the Games worksheet
public double evaluate() {
return isEndOfGameState() // se of jogo acabou (alguém ganhou ou o tabuleiro está cheio)
? utility() // então devolver pontuação 0, +infinito ou -infinito (consoante seja empate, o agente a ganhar ou o oponente a ganhar)
: getWinningPossibilities(agentPiece) - getWinningPossibilities(opponentPiece); // senão devolver uma pontuação de acordo com a heurística definida
}
// devolve o número de possibilidades que determinado jogador tem de ganhar a partir deste estado
private int getWinningPossibilities(char player) {
char other = player == X ? O : X;
int possibilities = 0;
public class IDAStarSearch extends InformedSearch {
/*
* Note that, on each iteration, the search is done in a depth first search way.
*/
private double limit;
private double newLimit;
@Override
public Solution search(Problem problem) {
private String placeTypesToString(List<Integer> placeTypes) {
String res = "";
for (int placeType :
placeTypes) {
res += placeTypeToString(placeType) + " ";
}
return res;
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="pt.ipleiria.contacts.SearchActivity">
<EditText
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="pt.ipleiria.contacts.AddActivity">
<EditText