Do you want to try programming? Python may be for you!

Python is a nice language to use when starting programming, and you can find lots of tutorials online to help you learn how to program with this language. It’s not as difficult of a learning curve as C++ or even Java. Also lots of colleges use it as an introductory language, it can scale in difficulty for sure like any language usually does, but for a beginner to see what programming is about and if you don’t want to be intimidated by too much just set up work just to get a small program to start running, you should check it out, here are a couple links for a tutorial and the official page.

Learning Link: https://data-flair.training/blogs/python-tutorial/

Official Python Link: https://www.python.org

Monkey King vs Android Monkey Testing

I started playing a game called DOTA 2, its pretty fun, a team based action strategy rpg. You should check it out ifyou like these types of games, there are A TON of characters, but the gameplay is so deep you will have to spend time learning the game. Luckily there are alot of new players, seriously, most tactics from guides don’t matter till you get good. But it is intimidating to have so many players to choose from and try to figure out how you want to play.

Official Channel: https://www.youtube.com/user/dota2

I really liked the Dragon Knight character, though he is slow moving and that’s very difficult to learn how to combat. But there is this guy called the Monkey King, it’s a funny character… I mean it’s so close to human, and his gameplay tactics are often kinda comical in nature, using stealth and other tricks to navigate the landscape.

So what is more fun… him or during developement coming across Android’s Monkey command…

Link to Code: https://developer.android.com/studio/test/monkey

You can stress test your application for bugs not previously found. It will use the device randomly in different ways you toggle and it just kinda fun to see it and its a relief to know the application won’t crash at least, you still have to check it’s doing the right thing of course : D

you can use android’s terminal to use this…

adb shell monkey -p anime.applications -v 10000 --wait-dbg

Video

Android: load image from URL

This post will be on an update I am working on for my puzzle games. With a 50mb upload limit for the base APK (zip file) for Google Play, I am limited to how many images I can supply in the application. I managed to get over a hundred different images but with each new image set I have to update the version for the APK. If I am loading the images from a web service, I no longer have to update the application itself. I can now just upload the file urls to the application from my server, and the application will take each url and load an image for each one. From here I can then store the scaled down image in the application folder and then not have to use the service to download from thereafter.

Code to start my service to get the JSON file with artwork data including title, artists and url for image download.

Intent intent = new Intent(getContext(), ArtworkJsonRetrievalService.class);
getContext().startService(intent);

Service class for downloading the file and converting it to a list of Artworks. Make sure you class implements Parcelable to send with the Intent. I have included some error handling for file error on retrieval and for validating an artwork is valid based on data existing.

package rick.artpuzzles0001.service;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;

/**
* A class to get the JSON file with artwork information.
*/
public class ArtworkJsonRetrievalService extends IntentService {

public static final String ARTWORK_SERVICE = "ARTWORK_SERVICE";
public static final String ARTWORK_SERVICE_ERROR = "ARTWORK_SERVICE_ERROR";

final String FILE_NAME = "artworklist.json";

public ArtworkJsonRetrievalService() {
super("ArtworkJsonRetrievalService");
}

@Override
public void onHandleIntent(Intent intent) {
if (intent != null) {
final String jsonString = getJsonFileString();
try {
if (jsonString != null && !jsonString.equals("")) {

// convert string to artworks
JSONObject obj = new JSONObject(jsonString);
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Artwork.class, new JsonDeserializer<Artwork>() {

@Override
public Artwork deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jobject = json.getAsJsonObject();
try {
String titleOfArtwork = jobject.get("titleOfArtwork").getAsString();
if (titleOfArtwork.equals("")) return null;
String titleOfArtist = jobject.get("titleOfArtist").getAsString();
if (titleOfArtist.equals("")) return null;
String urlOfArtist = jobject.get("urlOfArtist").getAsString();
if (urlOfArtist.equals("")) return null;
String imageUrl = jobject.get("imageUrl").getAsString();
if (imageUrl.equals("")) return null;

return new Artwork(titleOfArtwork, titleOfArtist, urlOfArtist, imageUrl);
} catch (NullPointerException npe) {
return null;
}
}
});
Gson gson = gsonBuilder.create();
final Artworks artworks = gson.fromJson(obj.toString(), Artworks.class);

// remove nulls from list
if (artworks.getArtworks() != null) {
artworks.getArtworks().removeAll(Collections.singleton(null));
}

if (artworks.getArtworks() == null || artworks.getArtworks().isEmpty()) {
errorHandler();
} else {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(ARTWORK_SERVICE);
intent.putExtra(Artworks.TAG, artworks);
sendBroadcast(intent);
}
});
}
} else {
errorHandler();
}
} catch (JSONException e) {
errorHandler();
}
}
}

private void errorHandler() {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(ARTWORK_SERVICE_ERROR);
sendBroadcast(intent);
}
});
}

/**
* Use the internet to get the json file or if on fail check for a backup
*
* @return the string that will be parsed into our artwork list
*/
public String getJsonFileString() {

// hit the server first
HttpURLConnection urlConnection = null;
String result = "";
try {
String jsonUrl = "https://www.artinapplications.com/puzzles/android/" + FILE_NAME;
URL url = new URL(jsonUrl);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
result = sb.toString();

// save to local private storage for re use later
String fileContents = result;
FileOutputStream outputStream;

try {
outputStream = openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
outputStream.write(fileContents.getBytes());
outputStream.close();
} catch (Exception e) {
// don't worry if this breaks we will just run service again for it next time
}
} catch (MalformedURLException e) {

// on bad url check for backup
result = loadLocalJson();
if (result.equals("")) {
errorHandler();
}
} catch (IOException e) {

// something went wrong, check for backup
result = loadLocalJson();
if (result.equals("")) {
errorHandler();
}
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}

return result;
}

private String loadLocalJson() {
File directory = getBaseContext().getFilesDir();
File file = new File(directory, FILE_NAME);
String result = "";
if (file.exists()) {
int length = (int) file.length();
byte[] bytes = new byte[length];
FileInputStream in = null;
try {
in = new FileInputStream(file);
in.read(bytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}

result = new String(bytes);
}
return result;
}
}

Coding PDF for TeamPoker using HTML5, JavaScript, and CSS

I have a guide here ready for anyone looking to create a game similar to or just curious about how to make a game using HTML5, JavaScript and CSS. All of which are free technologies and you can start developing when ever you get an idea! You can play the game here…

ZIP code base TEAM POKER

Download Now

TeamPoker

Download Now

HackerRank problem “The Time in Words”

The time in words solves the problem of writing the time based on a time stamp and putting it into words. Its recommends using dictionary to solve and for this I use a HashMap, which is essentially a dictionary. This creates a one for one relation ship called Key,Value pairings. If you have a key you can map it to a value, this is useful for direct lookups. In Java you will want to use an ArrayList for iterating over every item, but if you are getting one item and you have the key to that item a hash map will greatly increase the time to get the value you are looking for.

https://www.hackerrank.com/challenges/the-time-in-words

import java.util.HashMap;
import java.util.Scanner;

public class Solution {

	public static void main(String[] args) {
		HashMap<Integer, String> hm = new HashMap<Integer, String>();
		hm.put(0, "o' clock");
		hm.put(1, "one");
		hm.put(2, "two");
		hm.put(3, "three");
		hm.put(4, "four");
		hm.put(5, "five");
		hm.put(6, "six");
		hm.put(7, "seven");
		hm.put(8, "eight");
		hm.put(9, "nine");
		hm.put(10, "ten");
		hm.put(11, "eleven");
		hm.put(12, "twelve");
		hm.put(13, "thirteen");
		hm.put(14, "fourteen");
		hm.put(15, "quarter");
		hm.put(16, "sixteen");
		hm.put(17, "seventeen");
		hm.put(18, "eighteen");
		hm.put(19, "nineteen");
		hm.put(20, "twenty");
		hm.put(21, "twenty one");
		hm.put(23, "twenty three");
		hm.put(24, "twenty four");
		hm.put(25, "twenty five");
		hm.put(26, "twenty six");
		hm.put(27, "twenty seven");
		hm.put(28, "twenty eight");
		hm.put(29, "twenty nine");
		hm.put(30, "half");
		hm.put(31, "thirty one");
		hm.put(32, "thirty two");
		hm.put(32, "thirty three");
		hm.put(34, "thirty four");
		hm.put(35, "thirty five");
		hm.put(36, "thirty six");
		hm.put(37, "thirty seven");
		hm.put(38, "thirty eight");
		hm.put(39, "thirty nine");
		hm.put(40, "fourty");
		hm.put(41, "fourty one");
		hm.put(42, "fourty two");
		hm.put(43, "fourty three");
		hm.put(44, "fourty four");
		hm.put(45, "quarter");
		hm.put(46, "fourty six");
		hm.put(47, "fourty seven");
		hm.put(48, "fourty eight");
		hm.put(49, "fourty nine");
		hm.put(50, "fifty");
		hm.put(51, "fifty one");
		hm.put(52, "fifty two");
		hm.put(53, "fifty three");
		hm.put(54, "fifty four");
		hm.put(55, "fifty five");
		hm.put(56, "fifty six");
		hm.put(57, "fifty seven");
		hm.put(58, "fifty eight");
		hm.put(59, "fifty nine");

		Scanner in = new Scanner(System.in);

		int hours = in.nextInt();
		if (1 > hours || hours >= 12) {
			in.close();
			return;
		}
		in.nextLine();
		int minutes = in.nextInt();
		if (0 > minutes || minutes >= 60) {
			in.close();
			return;
		}

		String temp = "";

		switch (minutes) {
		case 0:
			temp = "" + hm.get(hours) + " " + hm.get(minutes);
			break;
		case 1:
			temp = "" + hm.get(minutes) + " minute past " + hm.get(hours);
			break;
		case 2:
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
		case 9:
		case 10:
		case 11:
		case 12:
		case 13:
		case 14:
			temp = "" + hm.get(minutes) + " minutes past " + hm.get(hours);
			break;
		case 15:
			temp = "" + hm.get(minutes) + " past " + hm.get(hours);
			break;
		case 16:
		case 17:
		case 18:
		case 19:
		case 20:
		case 21:
		case 22:
		case 23:
		case 24:
		case 25:
		case 26:
		case 27:
		case 28:
		case 29:
			temp = "" + hm.get(minutes) + " minutes past " + hm.get(hours);
			break;
		case 30:
			temp = "" + hm.get(minutes) + " past " + hm.get(hours);
			break;
		case 31:
		case 32:
		case 33:
		case 34:
		case 35:
		case 36:
		case 37:
		case 38:
		case 39:
		case 40:
		case 41:
		case 42:
		case 43:
		case 44:
			if (hours == 12)
				hours = 0;
			temp = "" + hm.get(60 - minutes) + " minutes to " + hm.get(hours + 1);
			break;
		case 45:
			if (hours == 12) {
				hours = 0;
			}
			temp = "quarter to " + hm.get(hours + 1);
			break;
		case 46:
		case 47:
		case 48:
		case 49:
		case 50:
		case 51:
		case 52:
		case 53:
		case 54:
		case 55:
		case 56:
		case 57:
		case 58:
			if (hours == 12)
				hours = 0;
			temp = "" + hm.get(60 - minutes) + " minutes to " + hm.get(hours + 1);
			break;
		case 59:
			if (hours == 12)
				hours = 0;
			temp = "" + hm.get(60 - minutes) + " minute to " + hm.get(hours + 1);
			break;
		}

		System.out.println(temp);

		in.close();
	}
}

Android PDF Puzzle Application

Android Coding Guide Puzzle Application built for Android Studio (2016)

Download Now
Puzzle-PDF-020

The Android Android PDF Puzzle Application is intended to help new developers learn how to create an app with touch screen interaction using bitmaps. The PDF covers how to perform simple user and device touch interactions. Its assumes you will be able to set up Android Studio and run the sample applications before continuing to this guide.

Atom IDE: Capable JavaScript IDE on first impression.

An IDE is just a shell to hold code and can help you code by predicting errors and helping you to understand base code provided by Java or Python or any popular language you are working with. You can import demo’s and see download code straight from GitHub within them. Most are also extend-able and have plugin’s to help you if you have specific needs.

https://atom.io

I like the ease of plugin’s for Atom so far, as there a few that make coding in JavaScript much easier. I had some trouble getting settled in on an IDE for JavaScript for one reason or another. I think it comes down to going to method declarations and finding variables declared in separate files. Check it out if you have been using the same IDE for a while and not tried something different. It is advertised as more customizable and seems to have some pretty easily installed plugins that make coding easier by helping you to code smarter.

I have been using it for developing my Team Poker Card Game and found it to be the easiest to get some code rolling and then really getting things done. Check it out, it supports Facebook development with JS really well also. I need to sub folder some files as you see… lol but I still have to determine how I want it organized once the code starts to settle : D

https://nuclide.io

Final Fantasy 13-2 Clock Puzzle (Java)

Needed some code to finish this one… if you made it to the clock size puzzle with 12 sphere’s here’s some java code for you to solve it quickly if you like!
You can use pen and paper if you like. To get all values start by creeping up really slowly until you can’t see the 12:00 number but can see the color if it and thereby tell what the value is going to be. Enter values in the hourMoveValues array and increment the startHourMinusOne value if you need a new starting point.

import java.util.ArrayList;

/**
*
* This class will print out a string of numbers that are similar to a clock
* where one is the first sphere just past noon at the center top. You will have
* to account for the hours you have on the screen. This is the spheres you see
* in the circle. The Array values represent the numbers in the spheres. You may
* have to adjust the Array based on hours. A print out of the possible path
* will show itself if possible. If this is not possible you will have to change
* the start hour minus one variable.
*
* @author Rick
*
*/

public class Main {

static ArrayList<Integer> order = new ArrayList<>();
static Integer[] hourMoveValues;
// you may have to increment this, but 0 is the 1:00 value
static int startHourMinusOne = 4;
// total number of spheres on screen.
static int hours = 12;

public static void main(String[] args) {
reset();
}

private static void reset() {
hourMoveValues = new Integer[hours];
initArray();
order.add(startHourMinusOne);
nextSpacesToMoveCount(startHourMinusOne, order, hourMoveValues);
}

private static void initArray() {
// the position is 1 - 12 on the clock so 0 is really 1:00 value
hourMoveValues[0] = 3;
hourMoveValues[1] = 5;
hourMoveValues[2] = 3;
hourMoveValues[3] = 3;
hourMoveValues[4] = 2;
hourMoveValues[5] = 5;
hourMoveValues[6] = 1;
hourMoveValues[7] = 5;
hourMoveValues[8] = 2;
hourMoveValues[9] = 4;
hourMoveValues[10] = 1;
hourMoveValues[11] = 4;

}

private static void nextSpacesToMoveCount(int index, ArrayList<Integer> order2, Integer[] ints2) {
// check for full order
Integer[] tempInts = new Integer[hours];
copyArrayToTemp(tempInts, ints2);

int moves = tempInts[index];

// block from accepting this as valid next move space
tempInts[index] = -1;

// get pair of next traverse indexes
int moveUp = addToStartOrder(index, moves);
int moveDown = subFromStartOrder(index, moves);

checkForCompleteOrder(order2);

if (tempInts[moveUp] != -1) {
// keep current instance of order separate from the one sent
ArrayList<Integer> currOrder = new ArrayList<Integer>(order2);
currOrder.add(moveUp);
checkForCompleteOrder(currOrder);
nextSpacesToMoveCount(moveUp, currOrder, tempInts);
}
if (moveUp != moveDown) {
if (tempInts[moveDown] != -1) {
// keep current instance of order separate from the one sent
ArrayList<Integer> currOrder = new ArrayList<Integer>(order2);
currOrder.add(moveDown);
checkForCompleteOrder(currOrder);
nextSpacesToMoveCount(moveDown, currOrder, tempInts);
}
}
}

private static void copyArrayToTemp(Integer[] tempInts, Integer[] ints2) {
for (int i = 0; i < hours; i++) {
tempInts[i] = ints2[i];
}
}

private static void checkForCompleteOrder(ArrayList<Integer> currOrder) {
// if the order is full you have a solution
if (currOrder.size() >= hours) {
// get value for index
System.out.println("Complete: numbers are reference from 1 as 1:00");
printOrder(currOrder);
System.exit(0);
}
}

private static void printOrder(ArrayList<Integer> currOrder) {
for (Integer i : currOrder) {
System.out.println(i + 1);
}
}

private static int subFromStartOrder(int startOrder, int spacesToMove) {
// get the new hour based on moves
int tempOrder = startOrder -= spacesToMove;
if (tempOrder < 0) {
tempOrder = hours + tempOrder;
return tempOrder;
}
return tempOrder;
}

private static int addToStartOrder(int startOrder, int spacesToMove) {
// get the new hour based on moves
int tempOrder = startOrder += spacesToMove;
if (tempOrder > (hours - 1)) {
return tempOrder - hours;
}
return tempOrder;
}
}

Code bases for download and free music

I made a subdirectory here for anyone who would like to mass download projects for Android and some HTML5 code. Feel free to use any of them to pull images or code. Most of the guide’s I have put together have samples in this code so you can run it and see how it works for yourself and make edits for your own needs if you like!

Google Drive: LINK