Shuffling cards and assigning them to user and computer in android development - android

I am creating a basic top trumps app in android development. I only need 6 top trumps card which have been made by using a database. I already have the code for this and have successfully created the cards.
I am stuck on this bit
When the user selects Play you should randomly assign half of the cards in the database to the human player, and half of the cards to the computer.
And was wondering if anyone had any ideas on how to do this? Thanks everyone.
UPDATE
I have tried doing this
ArrayList<Integer> cards = new ArrayList<Integer>();
for(int i=1;i<=6;i++)
{
this.cards.add(i);
}
Collections.shuffle(this.cards);
To shuffle the cards but I'm unsure on how to assign the computer and user the cards.

I'm not sure what you are aiming for here, but would something like this do (after you have shuffled):
ArrayList<Integer> playerCards = new ArrayList<Integer>(cards.subList(0,2));
ArrayList<Integer> computerCards = new ArrayList<Integer>(cards.subList(3,5));
Of course, you should use variables instead of constants everywhere, but you get the idea.

I am not an android programmer, but once long ago I realized the way to deal shuffled cards is to store the cards in an array or whatever in their natural order rather than a shuffled order -- for regular poker cards, 2-spades to A-spades, then 2-clubs to A-clubs, etc. Then, instead of dealing cards in-order from the shuffled deck, deal the cards by random draw from the unshuffled deck. You card elements need a "used" setting or value so they don't get reused or used twice. Saves LOADS of time doing the shuffle. In your case you on;y need to draw three cards, then the other guy gets the rest.

Related

Save and Load contents TStringGrid

I have a simple form containing TstringGrid with 2 columns, a TStringColumn and TCheckColumn added. I have seen many examples of saving the contents to file if the cells contain text or numbers. I have not seen any examples of saving with a TCheckColumn. I am assuming that I must check each CheckColumn cell, determine its state and assign a value that can be saved to file. Or maybe there is a more elegant way to do this.
As for sorting - again many examples using strings or numbers but none with TCheckColumn. I have HeaderClick enabled. On the TStringColumn I would like to sort Alphabetically - On the TCheckColumn - I would like checked items at the top of the column.
I am using Delphi 10.2.1 and will compile for Android.
Without saying you shouldn't start from here - I will just answer the specific questioN;
To keep it simple, I would:
Save: iterate through the rows and take the state of the checkboxes and prefix the string item with BoolToStr(theCheckValue)+':'+theContents of the string.
Then save the stringList.
To Load:
load into the stringList and then iterate and break the string apart using pos on the ':' and StrToBool the left portion, setting the checked item based on this.
Not got an IDE up, so haven't tested, but that would be my approach as a bit of a hack.

Handling integers bigger than Long with Firebase and android

I am developing an app with Firebase, it is a game in which certain scores are shared between players and can grow without a limit. I tried storing them as a String, but then I could not order them with orderByChild for the leaderboard. How can I handle this problem?
You could store the number as a linked list, with each node in the list representing each digit. Be careful with the order; putting the last number (the ones digit) in the list first, makes math with the linked list easier, while the other direction makes it easier to return the nodes in the list to a number on the screen.
To store integers which are big in size java provides a BigInteger Class to handle those numbers. I will suggeat you to use that first read the concept and then try to findout what you exactly want!
Check this one BigInteger

Getting relational data faster with Parse

I'm using parse to get a number of places into a recyclerview.
The problem is it's quite slow, which probably has to do with my workflow.
In the RecyclerView I show: a picture, some textfields, tags, how many likes a place has, and how many comments.
For this I query my Place object, which has relations with a picture object (which holds a ParseFile), tag objects & comments object.
Getting 10 places like this takes about 10 seconds, this seems to be extremely show.
Worksflow:
in a AsyncTask I have a query to get all the Places.
I do a for loop on these places and per place I get the relations for the pictureUrls, tags & comments. (These are loaded into arraylists to pass to the adapter).
By the way, in the adapter the pictures are loaded with Picasso.
Any help on this would be appreciated :)
I solved it, for now.
What makes getting the placeobject slow is iterating over each object, getting the relation with picture object, tags & comments.
I dropped this iteration, instead I:
pictures
- I stored the url to the thumb in my place object, I load this picture with Picasso
comments
- I added a field "commentscount" and increment this everytime there is a one. I just needed the count, so this is enough. (I show the comments in the detailfragment)
tags
- I dropped the tags, and I will show them in the details too.
So, dirty fix maybe, but it might be just what this problem needed.. :)

How do I save history in an android app?

I have a listview called history that I call every time that I so something significant (ie open or close the app or record some info) The problem I have is that it records things from top to bottom meaning the most recent information is on the bottom of the listview. The second problem I have is that the listview gets deleted after the app is turned off. How do people usually save their history? (ie shared preferences can only store one variable and thats not enough, I would like to save a maximum of 30 entries to the listview)
Thanks
One option I use is to JSON encode my history list and store the resulting string to SharedPreferences. You can then order it as well (simply add items to the List that backs the ListView with new events first) and save it and restore it from the SharedPreferences file.
EDIT: Try something like this:
org.json.JSONArray tracking_users = new org.json.JSONArray();
tracking_users.put("history 1");
tracking_users.put("history 2");
tracking_users.put("history 3");
You could also consider using a database to store the Log of these events, tends to be more efficient.
See one of these for an introduction to android databases

Randomly Generating Sentences for Display

I have a few questions concerning the application I'm designing. I have many different routes I could try in order to get what I wanted, but I thought I would get suggestions instead of doing trial and error.
I'm developing a simple app that has one Game screen (activity) and a main menu to start the game. The game screen will have two buttons, "Next" and "Repeat". Every time "Next" is hit, a new sentence in a different language (with the english translation below it) will appear, audio will pronounce the sentence, and hopefully I can get a highlighter to highlight the part of the sentence being spoken. You can guess what the Repeat button does.
My question is, what do you guys think would be the best way to store these sentences so they can be randomly picked out? I thought about making an array of structures or classes with the English definition, audio, and sentence in each structure. Then using a random iterator to pick one out. However, it would take a long time to do this approach and I wanted to get some ideas before I tried it.
Also, I'm not sure how I would print the sentence and definition on the screen.
Thanks!
Using an array of structs/classes seems like that would be the normal way to go.
Not really sure what you mean by a random iterator, but when picking out random sentences from the array of sentences, you might want to avoid repeats until you've gone through all the elements. To do that, you can make a second array of indices, select one at random from those, use the element that corresponds to that index, and remove that number from the array of indices.
In Java, that would look something like
ArrayList<Sentence> sentences;
ArrayList<Integer> indices;
Random rand;
private void populateIndices() {
for(int i = 0; i < sentences.size(); i++)
indices.add(i);
}
public Sentence getNextSentence() {
if(indices.isEmpty())
populateIndices();
int idx = rand.nextInt(indices.size());
int val = indices.get(idx);
indices.remove(idx);
return sentences.get(val);
}
Quite frankly I would load out of copyright books from Project Gutenberg and randomly pull sentences from them. I would then pass the sentences into Google APIs to translate and pronounce the sentences. Relying on external services is at the very heart of what a connected OS like Android is made for. It would be a much more compelling use of the platform than a canned Rosetta Stone like CD solution and your ability to tap into a broader amount of content would be increased exponentially.

Categories

Resources