Replace input values in arraylist with a custom made class - shuffle

Replacing userinput with the card index
presented on a new row, and is not replacing the actual number the user typed. I've tried to put the letter and the number in the same arraylist but I have no idea how to display these.

one thing I see you should replace String secondCardLetter = card.getCardLetter(); with String secondCardLetter = deck.getCards(i); otherwise second card is same as first try that and let me know
Also recommend replacing ArrayList<Card> card = new ArrayList<Card>(); with List<Card> cards = new ArrayList<Card>(); please note the variable name change too since its a collection of Card(s). Initialize the deck with a loop
char A = 'A';
int repeats = 2, numOfCards = 8;
for ( int i = 0; i<numOfCards;i++){
for (int j = 0; j<repeats; j++){
cards.add(new Card((char) ( A + i ) + "",i+1,""));
}
}
Please pay attention to your names as the to depict what the method may be doing. You getCards method takes an integer and returns the "Card", where as getCards shouldnt take any parameters and simply return the cards list you have stored. If you want to return a specific card, declare a method called public Card getCard(int i) notice the missing letter s as it shows this will return one card, and on the other hand public List<Card> getCards() method would return a List of cards, then that for loop would work.

If I understand you correctly, what you are looking to do is to keep replacing the characters in the terminal output, without breaking the output into new lines right? i.e: a counter going from 1 to 100?
For that you need to use the carriage return character: '\r'
I think this is the line you are printing the letter of the card right?
System.out.println(format("%10s","\n[ " + firstCardLetter + " ]"));
Try changing this to
System.out.println(format("%10s","\r[ " + firstCardLetter + " ]"));
edit
Sorry what you have to use is the \033[F character, as mentioned here
System.out.println(format("%10s","\033[F[ " + firstCardLetter + " ]"));

Related

C# WinForm: Select specific words and change colour

I have a program that allows the user to enter text, and it will highlight any repeat words. It already adds repeated words to one list and all the words to another. I want the program to print out the words, and if a repeat word is used, to highlight it.
I have tried using outputBox.Find(repeatList[i]) with a loop, but this only finds the first word used in the text. I also tried marking the current number for the last letter typed, selecting that point, finding the coordinates after the word.Length was typed, and then changing the colour of that, which didn't work.
for (int h = 0; h < repeatList.Count; h++)
{
for (int c = 0; c < repeatList.Count; c++)
{
outputBox.Find(repeatList[h]);
outputBox.SelectionColor = Color.Red;
}
}
At this point in the code, the outputBox already contains the users input, I just want to know how to compare words and select them for colouring. I've only just started Winforms and have only been coding for a few weeks, so I am sorry - I have looked at other answers but was not able to implement them. Thank you in advance for any responses.
EDIT: I would just like to add that my prefered method for colouring the text would be as it prints each word out, this was my original intention as I'm much more used to console applications where I can just change the colour and print more. If that method is easier to do than checking afterwards, I'm find to do that.
I would use the start index and keep a copy of it.
int startFrom = 0
...
startFrom = outputBox.Find(repeatList[h], startFrom)
You could then use the 'startFrom' index with the text word length to select the text.
Here is an example:
var findText = "test";
int index = 0;
do
{
index = richTextBox1.Find(findText, index, RichTextBoxFinds.WholeWord);
if (index > -1)
{
richTextBox1.Select(index, findText.Length);
richTextBox1.SelectionColor = Color.Red;
index++;
}
} while (index > -1);

Should be easy array situation

My question shouldn't be too difficult but I haven't solved it yet. Basically, what I'm trying to do is to take a message (like this one), preserve each letter in the message, but generate a random message using each letter. So, I can currently read into a textbox (say) "Hello!", but I need to take the message in that textbox and (on the click of a button) have something like "lolH!e". There's got to be a simple way to read each letter into an array (or list, or whatever), and spit them out at random, but while using each letter only once as in the original message. Any thoughts?
In JavaScript you can do something like this:
function randomize(s){
var a = Array.from(s);
for(var j, x, i = a.length; i; j = parseInt(Math.random() * i), x = a[--i], a[i] = a[j], a[j] = x);
return a.join("");
}
Then use it like:
randomize("Hello!")
There are some other good solutions here: How do I shuffle the characters in a string in JavaScript?

AS3 putting each element of an array on a separate line

I'm working on a game which contains the following code; what it's meant to do is pick 8 random names from an array and enter them into some text onscreen, so that each element is on a separate line. Here's the summary of my code:
var a:Array
for (var i:Number=1; i<=packQ; i++)
{
shuffle(packShuffler);
//Note: this function randomly reorganizes the array "packShuffler", it's a pretty complicated function so lets leave it at that.
a.push(packShuffler[0])
a.push(packShuffler[1])
a.push(packShuffler[2])
a.push(packShuffler[3])
a.push(packShuffler[4])
a.push(packShuffler[5])
a.push(packShuffler[6])
a.push(packShuffler[7])
}
cardGet.text=""+a
//textbox looks something like this:
//blue,white,dragon,beast,great,black,Sword,Circle
I know it looks very very awkward, especially the push part, but it's the only way I know at the moment :/
Well, my problem is, at the last line {cardGet.text=""+a}, the text appears as a big block of elements with commas between them, what I want is to make each element appear on a separate line. How do I do this?
Part of the problem is that a.toString() is getting called implicitly, on the line that says cardGet.text=""+a. That being said, an Array's toString() method will, by definition, return element1 + ", " + element2 + ", " + ... + elementn.
But why are you pushing them onto a new array? Why not simply shuffle the old one, as you have, and then just build your own string out of it? Kind of like this:
shuffle(packShuffler);
cardGet.text = "";
// takes every element, except for the last, and adds it and a newline
// to the string
for (var j:int = 0; j < packShuffler.length - 1; j++)
{
cardGet.text += packShuffler[j] + "\n";
}
// adds the very last element to the string without a newline
cardGet.text += packShuffler[packShuffler.length - 1];

How to split sentences in an array

I have a string s which stores a very long sentence and I want to copy the content of s to an array C with each cell storing a sentence each. The following is my code which is not giving me any output, but the dimension of the cell:
while(i<6)
C(i)=s;
end
This is how I get as output when I print C:
C=
[1x76 char]
Can somebody please help me.
Another job for strsplit:
>> sentences = 'This is the first one. Then here is a second. Yet another here.';
>> C = strsplit(sentences,'. ')
C =
'This is the first one' 'Then here is a second' 'Yet another here.'
We are specifying a period followed by a space as the delimiter. Change this as needed.
Suppose Long string is:
longString = "This is first cell. This is second cell. this is third cell".
Now since . is delimiter here means it is acting as separator for sentences. so you can loop through longString character wise and whenever you encounter a . you just increase Array index count and keep storing in this Array index until you find another .
here is sudo code:
array[];
index = 0;
loop through(longString) character wise
{
if(currentChar equals to '.')
{
index++;
}
else
{
array[index] = currentChanracter;
}
}

matlab divide sentences into words

Im new in matlab and ım trying to take the input from matlab gui which will be entered by a user and divide that sentence into words but I need to have them as letters because Im using a robot to write them. this letters will be send to these robots. Im using two robots and for example when I write 'lou reed' in text when ı press the button matlab function will hold this 2 words in to different char arrays so that ı can have the letters c(i) like this and send them to process. so far ı wrote these but ım stuck.
c = char(get(handles.edit1,'String'));
int count1;
int count2;
char word1;
char space=" ";
for i=1:length(c)
int t = isequal(c(i),space);
if(t==0)
count1=count1+1;
word1=;%ım trying to add the char here to find the new word
else
end
end
ı dont know what to do ı searched but ı couldnt find something usefull maybe ı wasnt looking right.
Anything would be helpful, thankss
What characters are allowed? First you should remove all the characters that are not allowed (substitute them with a space character?). After that just this:
str = ' Once upon a time ';
words_in_str = textscan(str,'%s');
words_in_str{1}
If you have a newer version of MATLAB (greater than 2012a I think), you can use strsplit
characterString = 'lou reed';
C = strsplit(characterString);
C will be a cell array with each element being a separate word.
You can simply find the space characters in your string with
mystring = 'Hello Cruel World';
spaces = find(mystring==' ');
The variable spaces is now a vector pointing to where each of your word breaks are. If you want to break this up into words, you could use
mystring = 'Hello Cruel World';
wordboundaries = [0,find(mystring==' ')];
wordlen = diff([wordboundaries,length(mystring)+1])-1;
numwords = length(wordboundaries);
for w = 1:numwords
idx = wordboundaries(w) + (1:wordlen(w));
word{w} = mystring(idx);
end
display(word);
Now word is a cell array containing the individual words.

Resources