I want to check for a straight combination in a poker game.
So, I have this array: var tempArr:Array = new Array;
I have this for sorting the array:
for (i = 0; i < 7; i++)
{
tempArr[i] = pValue[i];
}
tempArr.sort( Array.NUMERIC );
pValue is the value of the cards, it's have range from 2 to 14.
So, if I have this Array: tempArray = [2,3,3,4,5,5,6];
How can I check if I have a straight combination in my hand?
Set a bucket array to save if you got a card in hand
var t:Array = [];
//t[2] = 1;mean you have 2
// t[3] = 0;mean you don't have 3
//first initialize t
for(var i:int = 0; i < 15; i++)
{
t[i] = 0;
}
//then set the values from tempArray
for (var j:int = 0; j < tempArray.length; j++)
{
t[tempArray[j]] = 1;
}
//if you have a straight combination in your hand
//you will get a k that t[k] & t[k-1]& t[k-2] & t[k-3] & t[k-4] == 1
var existed:boolean = false;//the flag save if you got a straight combination
for (var k:int = t.length - 1; k >= 4; k--)
{
if (t[k] == 0)
{
continue;
}
if ((t[k] & t[k-1] & t[k-2] & t[k-3] & t[k-4]) == 1)
{
existed = true;
break;
}
}
Related
The assignment is the spawn several light and dark feathers according to score points from a quiz. The light feathers symbolize the correct points (light_feather), and the dark feather are the incorrect points (dark_feather) (Each are being tracked). All the feathers are supposed to line up on one line, meaning first light feathers, followed by the dark feathers. I got the quiz dynamics figured out, and the function I have posted here is only for when they press end quiz.
var light_feather:LightFeather = new LightFeather();
var dark_feather:DarkFeather = new DarkFeather();
var good_answers:uint = 0;
var bad_answers:uint = 0;
function avsluttFunc (evt:MouseEvent)
{
var sum_LightFeatherX:Number = 0;
for (var i = 0; i < good_answers; i++) {
addChild(light_feather);
light_feather.x += 12 + (i*16);
light_feather.y = 0;
trace("Lys X-verdi: " + light_feather.x);
sum_LightFeatherX += Number(light_feather.x);
return sum_LightFeatherX;
}
trace(sum_LightFeatherX);
dark_feather.x += sum_LightFeatherX;
for (var j = 1; j <= bad_answers; j++) {
addChild(dark_feather);
dark_feather.x += 12 + (j*16);
dark_feather.y = 0;
trace("Mørk X-verdi: " + dark_feather.x);
}
/*
//Resetter poengsummen
good_answers = 0;
bad_answers = 0;
*/
}
You can do what you are looking for using only one for loop, take a look :
var good_answers:uint = 2;
var bad_answers:uint = 4;
function avsluttFunc(evt:MouseEvent)
{
for (var i:int = 0; i < good_answers + bad_answers; i++) {
var feather:DisplayObject = i < good_answers ? new LightFeather() : new DarkFeather();
feather.x += 12 + i * (feather.width + 1);
feather.y = 0;
addChild(feather);
}
}
This code example will create 4 DarkFeather instances next to 2 LightFeather ones.
Edit :
How to add your objects to an array ?
// feathers array should be accessible for both codes (adding and removing objects)
var feathers:Array = [];
for (var i:int = 0; i < good_answers + bad_answers; i++) {
var feather:DisplayObject = i < good_answers ? new LightFeather() : new DarkFeather();
addChild(feather);
feathers.push(feather);
}
then to remove them from the stage, you can do for example :
for (var i:int = 0; i < feathers.length; i++) {
var feather:DisplayObject = DisplayObject(feathers[i]);
feather.parent.removeChild(feather);
}
Hope that can help.
I'm having trouble finding a way to do an array that doesn't make me type all the numbers , for example :
(i found this code online)
var array:Array=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
var odds:Array=[],
evens:Array=[],
odds_sum:int=0,
evens_sum:int=0
;
for(var i:int = 0; i < array.length; i++){
if(array[i] % 2 == 1){
odds.push(array[i]);
odds_sum += array[i];
} else {
evens.push(array[i]);
evens_sum += array[i];
}
}
trace(odds);
trace(odds_sum);
trace(evens);
trace(evens_sum);
i wanted the code to trace all the numbers between 1 and 100 (including the 1 and 100) but don't have to type them all down.
Help solving this would be much appreciated
Your question is ambiguous , If you simply need all the numbers between 1 and 100 you simply do
for(var i:int = 1; i <= 100; i++){
trace(i);
}
or if you want them in your array
var arr:Array = new Array();
for(var i:int = 1; i <= 100; i++){
arr.push(i);
}
or if you want odd and even numbers in separate array
var odds:Array = new Array();
var evens:Array = new Array();
for(var i:int = 1; i <= 100; i++){
if(i%2==0)
evens.push(i);
else
odds.push(i);
}
where the array odds/even have appropriate numbers between 1 and 100.
I'm trying to create a clone of 2048 for a cs class in as3, and I'm having trouble with moving blocks around. I basically declare a 4x4 array:
var tiles:Array = new Array(new Array(4), new Array(4), new Array(4), new Array(4));
I set it all to empty string at the beginning:
for(i = 0; i < 4; i++){
for(j = 0; j < 4; j++){
tiles[i][j] = '';
}
}
I also have a display function in order to display the array on the frame:
function display():void{
for(i = 0; i < 4; i++){
for(j = 0; j < 4; j++){
if(tiles[i][j] != ''){
tiles[i][j].x = 50 + (107 * j)
tiles[i][j].y = 50 + (107 * i)
}
}
}
}
I also have a keydown event to move the tiles:
for(i = 0; i < 4; i++){
for(j = 0; j < 4; j++){
if(tiles[i][j] != ''){
switch (event.keyCode){
case Keyboard.LEFT:
tiles[i][0] = tiles[i][j]
break;
case Keyboard.RIGHT:
tiles[i][3] = tiles[i][j]
break;
case Keyboard.UP:
tiles[0][j] = tiles[i][j]
break;
case Keyboard.DOWN:
tiles[3][j] = tiles[i][j]
break;
}
tiles[i][j] = ''
}
}
}
It's supposed to set a new tile to the equivalent of the old tile, then set the old tile to '', but for some reason it sets both the new tile and the old tile to ''! I'm not sure what is going on here, can someone point me in the right direction?
I have the following scenario:
if (event.status == AMFResultEvent.SUCCESS) {
var lev1:uint = 0;
var lev2:uint = 0;
var lev3:uint = 0;
var lev4:uint = 0;
var lev5:uint = 0;
var lev6:uint = 0;
for (var i:int = 0; i < event.result.length; i++) {
if (mainLevel == "1") {
lev1++;
}
if (mainLevel == "2") {
lev2++;
}
if (mainLevel == "3") {
lev3++;
}
if (mainLevel == "4") {
lev4++;
}
if (mainLevel == "5") {
lev5++;
}
if (mainLevel == "6") {
lev6++;
}
}
for (var j:int = 1; j < 7; j++) {
_row = new StatisticsRow(event.result[j], this);
_rowsPlace.addChild(_row);
_row.y = (_row.height +1) * j;
_row.codeLevel.htmlText = j; // works as it should
// need to access variables lev1 - lev6, called by something like "lev"+j here:
_row.amount.htmlText =
}
// traces correct amounts of mainLevels from the i loop:
trace ("level 1: " + lev1);
trace ("level 2: " + lev2);
trace ("level 3: " + lev3);
trace ("level 4: " + lev4);
trace ("level 5: " + lev5);
trace ("level 6: " + lev6);
}
I'm missing something obvious here, as the ["lev"]+j doen't work. How can I dynamically acces the lev1 - lev6 in the j-loop? As the code comment at the bottoms shows, this traces as expected.
Thanks in advance!
You can access them with brackets, string concatenation, and the this keyword. Here's an example of how you would use bracket notation in a loop:
for (var i:int = 0; i <= 6; i++) {
var currLev = this["lev"+i];
// do stuff to currLev
}
Thanks for answering guys!
I had a lousy approach to my problem anyway, and should have used an array right away:
var mainLevels:Array = new Array();
for (var n:int = 1; n < 7; n++) {
mainLevels[n] = 0;
}
if (event.status == AMFResultEvent.SUCCESS) {
for (var i:int = 0; i < event.result.length; i++) {
var data = event.result[i];
var correctCode:String = data["correct"];
var mainLevelFound:uint = uint(correctCode.substr(0, 1));
for (var k:int = 1; k < 7; k++) {
if (k == mainLevelFound) {
mainLevels[k]++;
}
}
}
for (var j:int = 1; j < 7; j++) {
_row = new StatisticsRow(event.result[j], this);
_rowsPlace.addChild(_row);
_row.y = (_row.height +1) * j;
_row.codeLevel.htmlText = j;
// Now this works as a reference to mainLevels[*] created above!
_row.amount.htmlText = mainLevels[j];
}
Thanks again for your effort :)
mBlocks is a 2-dimensional array of Block objects. Every time my application runs, it runs the InitGridNumbers function. Sometimes, it will get stuck in an infinite loop. Other times, it builds and runs without issues.
public function InitGridNumbers():void
{
var tempRow:Array;
var tempColumn:Array;
var tempNum:int;
for (var i:int = 0; i < mNumRows; i++)
{
tempRow = GetRow(i);
for (var j:int = 0; j < mNumColumns; j++)
{
// if number is unassigned
if (tempRow[j] == 0)
{
var cantMoveOn:Boolean = true;
while (cantMoveOn)
{
tempNum = Math.random() * mNumColumns + 1;
if (!CheckRow(i, tempNum) && !CheckColumn(j, tempNum))
cantMoveOn = false;
}
mBlocks[i][j].SetNumber(tempNum);
}
}
}
}
public function CheckRow(rowNum:int, checkNum:int):Boolean
{
var tempRow:Array = GetRow(rowNum);
for (var i:int = 0; i < mNumColumns; i++)
{
if (checkNum == tempRow[i])
return true;
}
return false;
}
public function CheckColumn(columnNum:int, checkNum:int):Boolean
{
var tempColumn:Array = GetColumn(columnNum);
for (var i:int = 0; i < mNumColumns; i++)
{
if (checkNum == tempColumn[i])
return true;
}
return false;
}
public function GetRow(rowNum:int):Array
{
var rowArray:Array = new Array(mNumRows);
for (var i:int = 0; i < mNumRows; i++)
rowArray[i] = mBlocks[rowNum][i].mNumber;
return rowArray;
}
public function GetColumn(columnNum:int):Array
{
var columnArray:Array = new Array(mNumColumns);
for (var i:int = 0; i < mNumColumns; i++)
columnArray[i] = mBlocks[i][columnNum].mNumber;
return columnArray;
}
To begin with, checkColumn, getColumn and getRow methods are wrong. To get a row, you should copy numColumns items and to get a column, you should copy numRows items. In other words, if there are r rows and c columns, there would be c items per each row and r items per each column.
public function checkColumn(columnNum:int, checkNum:int):Boolean
{
var tempColumn:Array = getColumn(columnNum);
for (var i:int = 0; i < mNumRows; i++)
{
if (checkNum == tempColumn[i])
return true;
}
return false;
}
public function getRow(rowNum:int):Array
{
var rowArray:Array = new Array();//needn't specify length in advance.
for (var i:int = 0; i < mNumColumns; i++)
rowArray[i] = mBlocks[rowNum][i].mNumber;
return rowArray;
}
public function getColumn(columnNum:int):Array
{
var columnArray:Array = new Array();
for (var i:int = 0; i < mNumRows; i++)
columnArray[i] = mBlocks[i][columnNum].mNumber;
return columnArray;
}
while (cantMoveOn)
{
//call Math.floor
tempNum = Math.floor(Math.random() * mNumColumns) + 1;
if (!checkRow(i, tempNum) && !checkColumn(j, tempNum))
cantMoveOn = false;
}
It looks like you're checking for a number that is not present in the current row and column. It's hard to say without knowing more details, but can you think of a scenario where this would be impossible?
For example, if there are four columns and five rows, the tempNum would always be between one and four. Now if the number of rows is five and the corresponding column already has all numbers up to four, the if statement would never evaluate to true and hence you'd end up in an infinite loop
0 1 2 3
1
2
3
4
in case grid is a square, how about this:
0 1 2 3
4
0
0