Programming Homework: Robot Genetic Algorithm [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
This program is built to run 10000 generations of 200 robots, allowing evolution to shape the digits in the robot's "DNA". The problem I'm having is that, even though there is nothing but numbers going into the 'robotGenes' array, the resulting DNA that prints is a random collection of letters, numbers and symbols. I have absolutly no idea why. I have tried switching up where I declare and assign my variables, unfortunatly its all been in vane. Plz halp.
P.s. I'm posting the entire program code here, as I am unsure what is causing the problem, though I am positive that I am simply being daft. Any help is greatly appreciated.
#include <iostream>
#include <string>
#include <stdlib.h>
#include <cmath>
using namespace std;
int randBattery = 0,
randRobot = 0,
randDirect = 0,
board[144] = { },
testDirect = 0,
randTurn = 0,
randTurnDirect = 0,
genAdd = 0,
masterFit = 0,
abc = 0;
double fitness[10000] = { },
winners[5] = { };
std::string startCond;
unsigned char robotGenes[200][12] = { },
test[12] = { };
class Display {
public:
int results() {
cout << "\n\n\n-Evolution Winning Avg. Fitness Ratings-\n\n";
cout << "1. " << winners[1];
cout << "\n DNA: ";
for (int G = 1; G <= 12; G++)
{
cout << robotGenes[1][G];
}
cout << "\n\n2. " << winners[2];
cout << "\n DNA: ";
for (int G = 1; G <= 12; G++)
{
cout << robotGenes[41][G];
}
cout << "\n\n3. " << winners[3];
cout << "\n DNA: ";
for (int G = 1; G <= 12; G++)
{
cout << robotGenes[81][G];
}
cout << "\n\n4. " << winners[4];
cout << "\n DNA: ";
for (int G = 1; G <= 12; G++)
{
cout << robotGenes[121][G];
}
cout << "\n\n5. " << winners[5];
cout << "\n DNA: ";
for (int G = 1; G <= 12; G++)
{
cout << robotGenes[161][G];
}
return 0;
};
};
class RobotSex {
public:
int bang() {
int newGen, newGen2, B;
newGen = 101;
newGen2 = 102;
B = 2;
for (int C = 1; C <= 100; C += 2)
{
robotGenes[newGen][6] = robotGenes[C][6];
robotGenes[newGen][7] = robotGenes[C][7];
robotGenes[newGen][8] = robotGenes[C][8];
robotGenes[newGen][9] = robotGenes[C][9];
robotGenes[newGen][10] = robotGenes[B][10];
robotGenes[newGen][11] = robotGenes[B][11];
robotGenes[newGen2][6] = robotGenes[B][6];
robotGenes[newGen2][7] = robotGenes[B][7];
robotGenes[newGen2][8] = robotGenes[B][8];
robotGenes[newGen2][9] = robotGenes[B][9];
robotGenes[newGen2][10] = robotGenes[C][10];
robotGenes[newGen2][11] = robotGenes[C][11];
newGen += 2;
newGen2 += 2;
B += 2;
}
};
};
class Genocide {
public:
int ethnicCleansing() {
for (int x = 101; x <= 200; x++)
{
for (int y = 6; y <= 12; y++)
{
robotGenes[x][y] = 0;
}
}
};
};
class Sorting {
public:
int sortPower() {
for (int P = 0; P <= 200; P++)
{
for (int x = 1; x <= 200; x++)
{
int y;
y = x + 1;
if (robotGenes[x][12] < robotGenes[y][12])
for (int Q = 1; Q <= 12; Q++)
swap (robotGenes[x][Q], robotGenes[y][Q]);
}
}
};
int addGenFit() {
for (int o = 1; o <= 200; o++)
{
genAdd += robotGenes[o][12];
}
fitness[masterFit] = genAdd / 200;
::masterFit += 1;
};
int sortGenFit() {
for (int P = 0; P <= 10000; P++)
{
for (int x = 1; x <= 9999; x++)
{
int y;
y = x + 1;
if (fitness[x] < fitness[y])
swap (fitness[x], fitness[y]);
}
}
for (int o = 1; o <= 2000; o++)
{
genAdd += fitness[o];
}
winners[1] = genAdd / 2000;
for (int o = 2001; o <= 4000; o++)
{
genAdd += fitness[o];
}
winners[2] = genAdd / 2000;
for (int o = 4001; o <= 6000; o++)
{
genAdd += fitness[o];
}
winners[3] = genAdd / 2000;
for (int o = 6001; o <= 8000; o++)
{
genAdd += fitness[o];
}
winners[4] = genAdd / 2000;
for (int o = 8001; o <= 10000; o++)
{
genAdd += fitness[o];
}
winners[5] = genAdd / 2000;
};
};
class Establishing {
public:
int clearBoard() {
for (int c = 0; c <= 144; c++) //Clearing board
{
board[c] = 0;
}
for (int x = 0; x <= 12; x++) //Establishing walls
{
board[x] = 9;
}
for (int x = 132; x <= 144; x++)
{
board[x] = 9;
}
for (int x = 13; x <= 121; x += 12)
{
board[x] = 9;
}
for (int x = 24; x <= 132; x += 12)
{
board[x] = 9;
}
};
};
class Randomizer {
public:
int randBattery() {
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R1
::randBattery = (rand() % 14+23);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R2
::randBattery = (rand() % 26+35);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R3
::randBattery = (rand() % 38+47);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R4
::randBattery = (rand() % 50+59);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R5
::randBattery = (rand() % 62+71);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R6
::randBattery = (rand() % 74+82);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R7
::randBattery = (rand() % 85+95);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R8
::randBattery = (rand() % 98+107);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R9
::randBattery = (rand() % 110+119);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R10
::randBattery = (rand() % 122+131);
board[::randBattery] = 1;
}
};
int randPlacement() {
for (int t = 1; t <= 200; t++)
{
srand(time(NULL)); //Random robot placement & resets power level
randRobot = (rand() % 62+71);
robotGenes[t][1] = randRobot;
robotGenes[t][12] = 5;
}
};
char randDirect() {
for (int d = 1; d <= 200; d++)
{
srand(time(NULL)); //Random robot direction
::randDirect = (rand() % 62+71);
robotGenes[d][5] = ::randDirect;
}
};
};
class Reader {
public:
int readDNA() {
do {
if (board[test[1]] == 1) //Pickup battery
test[12] += 5;
if (test[9] == 1) //Check for wall
{
if (board[test[2]] == 9)
{
if (test[5] == 1)
test[5] = 4;
if (test[5] == 2)
test[5] = 1;
if (test[5] == 3)
test[5] = 2;
if (test[5] == 4)
test[5] = 3;
}
if (board[test[2]] == 9 && board[test[3]] == 9)
{
if (test[5] == 1)
test[5] = 2;
if (test[5] == 2)
test[5] = 3;
if (test[5] == 3)
test[5] = 4;
if (test[5] == 4)
test[5] = 1;
}
}
if (test[7] == test[6]) //Changing direction after # of moves
if (test[6] % 2 == 0) //Turning left
{
if (test[5] == 1)
test[5] = 4;
if (test[5] == 2)
test[5] = 1;
if (test[5] == 3)
test[5] = 2;
if (test[5] == 4)
test[5] = 3;
}
else //Turning right
{
if (test[5] == 1)
test[5] = 2;
if (test[5] == 2)
test[5] = 3;
if (test[5] == 3)
test[5] = 4;
if (test[5] == 4)
test[5] = 1;
}
if (test[10] == 1) //Left sensor checking for battery, TURN LEFT
if (test[5] == 1)
test[5] = 4;
if (test[5] == 2)
test[5] = 1;
if (test[5] == 3)
test[5] = 2;
if (test[5] == 4)
test[5] = 3;
if (test[11] == 1) //Right sensor checking for battery, TURN RIGHT
if (test[5] == 1)
test[5] = 2;
if (test[5] == 2)
test[5] = 3;
if (test[5] == 3)
test[5] = 4;
if (test[5] == 4)
test[5] = 1;
if (test[5] == 1) //Face north
{
test[2] = test[1] + 12;
::test[3] = test[1] - 1;
test[4] = test[1] + 1;
test[1] += 12; //Move north
test[2] += 12;
::test[3] += 12;
test[4] += 12;
}
if (test[5] == 2) //Face east
{
test[2] = test[1] + 1;
::test[3] = test[1] + 12;
test[4] = test[1] - 12;
test[1] += 1; //Move east
test[2] += 1;
::test[3] += 1;
test[4] += 1;
}
if (test[5] == 3) //Face south
{
test[2] = test[1] - 12;
::test[3] = test[1] + 1;
test[4] = test[1] - 1;
test[1] -= 12; //Move south
test[2] -= 12;
::test[3] -= 12;
test[4] -= 12;
}
if (test[5] == 4) //Face west
{
test[2] = test[1] - 1;
::test[3] = test[1] - 12;
test[4] = test[1] + 12;
test[1] -= 1; //Move west
test[2] -= 1;
::test[3] -= 1;
test[4] -= 1;
}
test[7] += 1;
test[12] -= 1;
} while (test[12] > 1 && test[7] < 24);
};
};
class Running {
public:
void runRobot() {
for (int q = 1; q <= 200; q++)
{
for (int z = 1; z <= 12; z++)
{
::test[z] = robotGenes[q][z];
}
Reader objectDNA;
objectDNA.readDNA();
for (int z = 1; z <= 12; z++)
{
::robotGenes[q][z] = test[z];
}
}
};
};
int main()
{
//System Greeting
cout << "Enter anything to start simulation.\n\n";
cin >> startCond;
masterFit = 1;
//Setting starting genes
for (int t = 1; t <= 200; t++)
{
srand(time(NULL));
randTurn = (rand() % 10+1);
robotGenes[t][6] = randTurn;
}
for (int o = 1; o <= 40; o++)
{
robotGenes[o][8] = 1;
robotGenes[o][9] = 0;
robotGenes[o][10] = 0;
robotGenes[o][11] = 0;
}
for (int o = 41; o <= 80; o++)
{
robotGenes[o][8] = 0;
robotGenes[o][9] = 1;
robotGenes[o][10] = 1;
robotGenes[o][11] = 1;
}
for (int o = 81; o <= 120; o++)
{
robotGenes[o][8] = 1;
robotGenes[o][9] = 1;
robotGenes[o][10] = 0;
robotGenes[o][11] = 0;
}
for (int o = 121; o <= 160; o++)
{
robotGenes[o][8] = 0;
robotGenes[o][9] = 0;
robotGenes[o][10] = 0;
robotGenes[o][11] = 0;
}
for (int o = 161; o <= 200; o++)
{
robotGenes[o][8] = 1;
robotGenes[o][9] = 0;
robotGenes[o][10] = 1;
robotGenes[o][11] = 1;
}
//Running sims (not the game)
Randomizer objectRaDirect;
objectRaDirect.randDirect();
Randomizer objectRaPlace;
objectRaPlace.randPlacement();
for (int sims = 1; sims <= 10000; sims++)
{
Establishing objectClear;
objectClear.clearBoard();
Randomizer objectRaBattery;
objectRaBattery.randBattery();
Running objectRun;
objectRun.runRobot();
abc += 1;
cout << "\n\nGeneration " << abc << " is complete.\n";
Sorting objectPower;
objectPower.sortPower();
Sorting objectGenFit;
objectGenFit.addGenFit();
Genocide objectHitler;
objectHitler.ethnicCleansing();
RobotSex objectSex;
objectSex.bang();
}
Sorting objectWin;
objectWin.sortGenFit();
for (int B = 1; B <= 5; B++)
{
winners[B] = abs (winners[B]);
}
Display objectEnd;
objectEnd.results();
};

You have two major issues with your code:
1) You have an out-of-bounds array access here:
unsigned char robotGenes[200][12] = { }, test[12] = { };
//...
for (int G = 1; G <= 12; G++)
{
cout << robotGenes[1][G];
}
On the last iteration, you are printing a garbage value. The program actually invokes undefined behavior, since robotGenes[1][G] is out-of-bounds when G is 12.
You are also making the same mistake in the ethnicCleansing function, but probably even worse since you are not just reading an invalid location, you are writing to it:
for (int y = 6; y <= 12; y++)
{
robotGenes[x][y] = 0;
}
When y is 12, you are writing to an invalid index.
Array indices start from 0 up to n-1, where n is the total number of entries in the array. Thus G ranges from 0 to 11, inclusive. Going to index 12 is an out-of-bounds access.
2) You are calling functions that are supposed to return a value, but fail to do so. For example,
int Genocide::ethnicCleansing
This function is supposed to return an int, but doesn't return anything. Thus your program invokes undefined behavior. Not returning a value from a function that is supposed to return a value is undefined behavior. Note that you have several other functions with the same issue.
Please see your full code here.
Fix the warnings, fix the out-of-bounds accesses first. I don't know if these will be the only problems, but they are the two that stick out like sore thumbs.

You are most likely printing the data as a char array instead of an unsigned char array. This causes ASCII characters represented by the numbers to be printed instead of the actual numbers.

Related

trying to output the element of an array in c++

I am trying to search which elements in this c++ array contain the highest value and then output the key of that element but it keeps outputting a list of numbers like 9044007. What I want to know is how do I output the first number only because I'm guessing it is outputting all the keys of the elements with the same value.
double max = 0;
for (int i = 1; i <= 9; i++) {
if (responselayer[i] > max) {
max = responselayer[i];
}
}
for (int i = 1; i < 10; i++) {
if (responselayer[i] == max) {
return i;
break;
}
}
Here is the part of the code that fills the entire thing. It's a lot of code so it's hard to make it minimal. This is my first time trying stack so I don't really know how everything works on here.
//final layer input calculation and output//
for (int j = 1; j <= 10; j++) {
double subval = 0;
if (j == 1) {
for (int i = 0; i < 5; i++) {
subval = (midlayerweight4[i] * midlayer3[i]) + subval;
responselayer[j] = subval;
}
responselayer[j] = 1 / (1 + exp(-responselayer[j]));
}
else if (j == 2) {
int k = 0;
for (int i = 5; i < 10; i++) {
subval = (midlayerweight4[i] * midlayer3[k]) + subval;
responselayer[j] = subval;
k++;
}
responselayer[j] = 1 / (1 + exp(-responselayer[j]));
}
else if (j == 3) {
int k = 0;
for (int i = 10; i <15; i++) {
subval = (midlayerweight4[i] * midlayer3[k]) + subval;
responselayer[j] = subval;
k++;
}
responselayer[j] = 1 / (1 + exp(-responselayer[j]));
}
else if (j == 4) {
int k = 0;
for (int i = 15; i < 20; i++) {
subval = (midlayerweight4[i] * midlayer3[k]) + subval;
responselayer[j] = subval;
k++;
}
responselayer[j] = 1 / (1 + exp(-responselayer[j]));
}
else if (j == 5) {
int k = 0;
for (int i = 20; i <25; i++) {
subval = (midlayerweight4[i] * midlayer3[k]) + subval;
responselayer[j] = subval;
k++;
}
responselayer[j] = 1 / (1 + exp(-responselayer[j]));
}
else if (j == 6) {
int k = 0;
for (int i = 25; i <30; i++) {
subval = (midlayerweight4[i] * midlayer3[k]) + subval;
responselayer[j] = subval;
k++;
}
responselayer[j] = 1 / (1 + exp(-responselayer[j]));
}
else if (j == 7) {
int k = 0;
for (int i = 30; i <35; i++) {
subval = (midlayerweight4[i] * midlayer3[k]) + subval;
responselayer[j] = subval;
k++;
}
responselayer[j] = 1 / (1 + exp(-responselayer[j]));
}
else if (j == 8) {
int k = 0;
for (int i = 35; i < 40; i++) {
subval = (midlayerweight4[i] * midlayer3[k]) + subval;
responselayer[j] = subval;
k++;
}
responselayer[j] = 1 / (1 + exp(-responselayer[j]));
}
else if (j == 9) {
int k = 0;
for (int i = 40; i <45; i++) {
subval = (midlayerweight4[i] * midlayer3[k]) + subval;
responselayer[j] = subval;
k++;
}
responselayer[j] = 1 / (1 + exp(-responselayer[j]));
}
}
double max = 0;
for (int i = 1; i <10; i++) {
if (responselayer[i] > max) {
max = responselayer[i];
}
}
for (int i = 1; i < 10; i++) {
if (responselayer[i] == max) {
return i;
}
}
}
}
midlayerweight4[] was filled by an array before it in this same fashion and so was others behind that. This is the declaration for responselayer and all the other arras used were of type double and they were all declared outside of the array.
double responselayer[10];
A few things:
Array indexing in C/C++ starts at zero.
You do not show us the type of your array, so I'm going to assume it is double responselayer[SZ]; If this is not the case, you have some issues involving implicit type conversion from integral to floating point types.
It is sloppy to use 2 different styles of conditions in your for-loop.
It is sloppy to use magic numbers in your code.
I would write the code as follows:
size_t find_max_index(const double data[], size_t size) {
size_t max_index = 0;
// No need to compare data[0] to itself on the first iteration, so
// loop starts at one
for (size_t i = 1; i < size; ++i)
if (data[i] > data[max_index])
max_index = i;
return max_index;
}
Or getting a little fancier and taking care of the possibility of floating point strangeness, we can make it a templated function:
template <typename T>
size_t find_max_index(const T data[], size_t size) {
size_t max_index = 0;
for (size_t i = 1; i < size; ++i)
if (data[i] > data[max_index])
max_index = i;
return max_index;
}

& operator for bitwise programming returns wrong value (CPP)

I have this piece of code in cpp on Visual Studio
((handrule1 - maskRule1[0]) & test)
All of the variables are unsigned int.
Their values are respectively
66848250
50138096
0x80808080.
I keep getting value zero as the outcome for this line, which should not be possible.
How does this come?
I already tried working with long unsigned variables instead.
I am guessing that maybe I am doing something else wrong when choosing the data types.
Underneath you can find my full code.
Some of the variables are not defined but that's because they are already defined in another cpp-file we are not supposed to use.
void init(void) {
int aa, ab, l, x = 0;
for (int i = 4; i <= 13; i++) {
aa = 13 - i;
for (int j = (aa + 2) / 3; j <= i && j <= aa; j++) {
ab = aa - j;
for (int k = (ab + 1) / 2; k <= j && k <= ab; k++) {
adj[x] = i + j - 8;
l = ab - k; code[x++] = (((i - 4) * 7) + j) * 5 + k;
// printf("%d %d %d %d: %d\n", i, j, k, l, (((i-4)*7)+j)*5+k);
}
}
}
return;
}
char countSetBits(long long unsigned n)
{
if (n == 0)
return 0;
else
return 1 + countSetBits(n & (n - 1));
}
void init_set(void) {
long long unsigned hand = 0;
char honorPoints = 0;
char nrOfSpades = 0;
char nrOfHearts = 0;
char nrOfDiamonds = 0;
char nrOfClubs = 0;
long long unsigned maskAces = 0x8004002001;
long long unsigned maskKings = 0x10008004002;
long long unsigned maskQueens = 0x20010008004;
long long unsigned maskJacks = 0x40020010008;
long long unsigned maskSpades = 0x1FFF;
long long unsigned maskHearts = 0x3FFE000;
long long unsigned maskDiamonds = 0x7FFC000000;
long long unsigned maskClubs = 0xFFF8000000000;
char upperbound[RU][4];
char lowerbound[RU][4];
unsigned int handrule1 = 0;
unsigned int handrule2 = 0;
unsigned int handrule3 = 0;
unsigned int maskRule1[RU];
unsigned int maskRule2[RU];
unsigned int maskRule3[RU];
unsigned int maskInverse = 0x00FF00FF;
unsigned int test = 0x80808080;
unsigned int result1 = 0;
bool applicableRule = false;
unsigned int fuck = 0xFF936636;
result1 = fuck & test;
for (int r = 0; r < nrr; r++)
{
for (int i = 0; i < 4; i++)
{
upperbound[r][i] = 13;
lowerbound[r][i] = 0;
}
}
for (int r = 0; r < nrr; r++)
{
if (res[r] != 0)
{
for (int i = 0; i < res[r]; i++)
{
upperbound[r][color[r][i]] = (char) nru[r][i];
lowerbound[r][color[r][i]] = (char) nrl[r][i];
}
}
maskRule1[r] = (((char) distl[r] << 24) | ((char) distu[r] << 16) | ((char) ahpl[r] << 8) | (char) ahpu[r]) ^ maskInverse;
maskRule2[r] = ((lowerbound[r][0] << 24) | (upperbound[r][0] << 16) | (lowerbound[r][1] << 8) | upperbound[r][1]) ^ maskInverse;
maskRule3[r] = ((lowerbound[r][2] << 24) | (upperbound[r][2] << 16) | (lowerbound[r][3] << 8) | upperbound[r][3]) ^ maskInverse;
}
int x[52], y, a;
for (int i = 0; i < 52; i++) x[i] = i;
srand(1);
for (int i = 0; i < CRD; i++) {
for (int j = 52; --j > 1;) {
y = rand() % j;
a = x[y]; x[y] = x[j]; x[j] = a;
}
for (int j = 0; j < 4; j++)
{
for (int k = 13 * j; k < 13 * (j + 1); k++)
{
hand |= 1LLU << x[k];
}
//Counting honorpoints
honorPoints = (countSetBits(hand & maskAces) * 4) + (countSetBits(hand & maskKings) * 3) + (countSetBits(hand & maskQueens) * 2) + (countSetBits(hand & maskJacks) * 1);
hp[i][j] = (char) honorPoints;
honorPoints = 0;
//Counting distributions
nrOfSpades = countSetBits(hand & maskSpades);
nrOfHearts = countSetBits(hand & maskHearts);
nrOfDiamonds = countSetBits(hand & maskDiamonds);
nrOfClubs = countSetBits(hand & maskClubs);
std::array<char, 4> arrayTest = { nrOfSpades, nrOfHearts, nrOfDiamonds, nrOfClubs };
std::sort(arrayTest.begin(), arrayTest.end());
char p = arrayTest[3];
char o = arrayTest[2];
char m = arrayTest[1];
int test = (((p - 4) * 7) + o) * 5 + m;
for (int x = 0; x < 39; x++)
{
if (code[x] == test)
{
dis[i][j] = (char) x;
}
}
//Counting opening bids
ahp[i][j] = hp[i][j] + adj[dis[i][j]];
if (ahp[i][j] < 0) ahp[i][j] = 0;
handrule1 = ((dis[i][j] << 24) | (dis[i][j] << 16) | (ahp[i][j] << 8) | ahp[i][j]) ^ maskInverse;
handrule2 = ((nrOfSpades << 24) | (nrOfSpades << 16) | (nrOfHearts << 8) | nrOfHearts) ^ maskInverse;
handrule3 = ((nrOfDiamonds << 24) | (nrOfDiamonds << 16) | (nrOfClubs << 8) | nrOfClubs) ^ maskInverse;
printf("%u \n", handrule1);
printf("%u \n", maskRule1[0]);
for (int r = 0; r < nrr; r++)
{
if ((((handrule1 - maskRule1[r]) & test) == 0) && (((handrule2 - maskRule2[r]) & test) == 0) && (((handrule3 - maskRule3[r]) & test) == 0))
{
cnt[bid[r]][j]++;
applicableRule = true;
break;
}
}
if (applicableRule == false)
{
cnt[0][j]++;
}
applicableRule = false;
handrule1 = 0;
handrule2 = 0;
handrule3 = 0;
nrOfSpades = 0;
nrOfHearts = 0;
nrOfDiamonds = 0;
nrOfClubs = 0;
hand = 0;
}
}
return;
}

Monogame Breakout: having trouble with adding abilities like longer paddle or more balls

I'm gonna add some abilities to random blocks, u know that drops down when you hit a random block. Like you get 2 balls or bigger paddle or slower ball speed.
I need help with writing the for loop and if-statement that can add some abilities to my block levels. Like 1 random ability on level 1 and so on. So when you hit 1 random block of like 20 blocks, that has my ability. It would drop down to the paddle like the original breakout game.
I was thinking of a switch, if one random block with ability is hit and using that switch and randomize it.
void PowerUp()
{
powerups.Add(abilityballs_rect);
powerups.Add(abilitylong_rect);
powerups.Add(abilityslow_rect);
}
-
List<Rectangle> block = new List<Rectangle>();
List<Rectangle> block2 = new List<Rectangle>();
List<Rectangle> block3 = new List<Rectangle>();
List<Rectangle> block4 = new List<Rectangle>();
List<Rectangle> block5 = new List<Rectangle>();
List<Rectangle> block6 = new List<Rectangle>();
List<Rectangle> powerups = new List<Rectangle>();
-
if (level == 1)
{
if (block.Count == 14 && block2.Count == 14)
{
spriteBatch.DrawString(spritefont2, "LEVEL " + level, new Vector2(252, 400), Color.White);
}
foreach (Rectangle g in block)
{
spriteBatch.Draw(block_texture, g, Color.LimeGreen);
}
foreach (Rectangle r in block2)
{
spriteBatch.Draw(block_texture, r, Color.IndianRed);
}
}
else if (level == 2)
{
if (block3.Count == 18 && block4.Count == 27)
{
spriteBatch.DrawString(spritefont2, "LEVEL " + level, new Vector2(246, 400), Color.White);
}
foreach (Rectangle b in block3)
{
spriteBatch.Draw(block_texture, b, Color.CornflowerBlue);
}
foreach (Rectangle y in block4)
{
spriteBatch.Draw(block_texture, y, Color.Yellow);
}
}
else if (level == 3)
{
if (block5.Count == 36 && block6.Count == 18)
{
spriteBatch.DrawString(spritefont2, "LEVEL " + level, new Vector2(246, 400), Color.White);
}
foreach (Rectangle o in block5)
{
spriteBatch.Draw(block_texture, o, Color.Orange);
}
foreach (Rectangle p in block6)
{
spriteBatch.Draw(block_texture, p, Color.HotPink);
}
}
-
void AddBlocks()
{
//LEVEL 1
for (int i = 1; i < 3; i++)
{
for (int f = 1; f < 8; f++)
{
block.Add(new Rectangle((f * 63) + 94, (i * 40) + 60, block_texture.Width, block_texture.Height));
}
}
for (int i = 1; i < 3; i++)
{
for (int g = 1; g < 8; g++)
{
block2.Add(new Rectangle((g * 63) + 94, (i * 40) + 40, block_texture.Width, block_texture.Height));
}
}
//LEVEL 2
for (int i = 1; i < 3; i++)
{
for (int j = 1; j < 10; j++)
{
block3.Add(new Rectangle((j * 63) + 34, (i * 200) - 60, block_texture.Width, block_texture.Height));
}
}
for (int i = 1; i < 10; i++)
{
for (int k = 1; k < 4; k++)
{
block4.Add(new Rectangle((k * 103) + 143, (i * 20) + 140, block_texture.Width, block_texture.Height));
}
}
//LEVEL 3
for (int i = 1; i < 7; i++)
{
for (int j = 1; j < 7; j++)
{
block5.Add(new Rectangle((j * 63) + 127, (i * 20) + 190, block_texture.Width, block_texture.Height));
}
}
for (int i = 1; i < 10; i++)
{
for (int k = 1; k < 3; k++)
{
block6.Add(new Rectangle((k * 443) - 317, (i * 20) + 160, block_texture.Width, block_texture.Height));
}
}
}
-
void DeleteBlocks()
{
if (level == 1)
{
for (int j = 0; j < block.Count; j++)
{
if (ball_rect.Intersects(block[j]))
{
ball_speed.Y *= -1;
points += 1;
block.RemoveAt(j);
if (points > highscore)
{
highscore = points;
}
}
}
for (int k = 0; k < block2.Count; k++)
{
if (ball_rect.Intersects(block2[k]))
{
ball_speed.Y *= -1;
points += 1;
block2.RemoveAt(k);
if (points > highscore)
{
highscore = points;
}
}
}
if (block.Count == 0 && block2.Count == 0)
{
level++;
StartValueBallPaddle();
Start = false;
}
}
else if (level == 2)
{
for (int l = 0; l < block3.Count; l++)
{
if (ball_rect.Intersects(block3[l]))
{
ball_speed.Y *= -1;
points += 1;
block3.RemoveAt(l);
if (points > highscore)
{
highscore = points;
}
}
}
for (int m = 0; m < block4.Count; m++)
{
if (ball_rect.Intersects(block4[m]))
{
ball_speed.Y *= -1;
points += 1;
block4.RemoveAt(m);
if (points > highscore)
{
highscore = points;
}
}
}
if (block3.Count == 0 && block4.Count == 0)
{
level++;
StartValueBallPaddle();
Start = false;
}
}
else if (level == 3)
{
for (int n = 0; n < block5.Count; n++)
{
if (ball_rect.Intersects(block5[n]))
{
ball_speed.Y *= -1;
points += 1;
block5.RemoveAt(n);
if (points > highscore)
{
highscore = points;
}
}
}
for (int o = 0; o < block6.Count; o++)
{
if (ball_rect.Intersects(block6[o]))
{
ball_speed.Y *= -1;
points += 1;
block6.RemoveAt(o);
if (points > highscore)
{
highscore = points;
}
}
}
}
}
You might want to encapsulate your block data into a class, containing the color, the position and powerup of the block.
You could have a method inside that would be called "AddPowerup()" or something, this method will be used to add a random (or specific) powerup to the block. You'll maybe need a getter for your powerup.
Now in your game, you would hold all the blocks of a level inside a list, or a 2d array, or any collection that suits you
Then, to apply powerup to random blocks in the level, you could do something like this (mostly pseudo code, so not tested) :
List<Block> blockList = CreateBlockLevel(1);
int randomIndex = new Random().Next(blockList.Length-1);
blockList[randomIndex].AddPowerup();
If you want to apply a powerup to multiple blocks, you can put that in a for-loop, but then you might want to check if the block has already a powerup or not.

Image processing error

I must implement in C++ using diblok The Seeded Region Growing algorithm due to Adams and Bischof which can be found here http://bit.ly/1nIxphj.
It is the fig.2 pseudocode.
After I choose the seeded points using the mouse , it throws this message : Unhandled exception at 0x00416ca0 in diblook.exe: 0xC0000005: Access violation reading location 0x3d2f6e68.
This is the code of the function:
void CDibView::OnLButtonDblClk(UINT nFlags, CPoint point)
{ BEGIN_SOURCE_PROCESSING;
int** labels = new int* [dwHeight];
for(int k = 0;k < dwHeight; k++)
labels[k] = new int[dwWidth];
int noOfRegions = 2;
double meanRegion[2];
double noOfPointsInRegion[2];
for(int i = 0; i < dwHeight ; i++)
for(int j = 0; j < dwWidth ; j++)
{
labels[i][j] = -1;
}
if(noOfPoints < 6)
{
CPoint p = GetScrollPosition() + point;
pos[noOfPoints].x = p.x;
pos[noOfPoints].y = p.y;
int regionLabel = 0;
if(noOfPoints <= noOfPoints / 2)
labels[p.x][p.y] = regionLabel;
else
labels[p.x][p.y] = regionLabel + 1;
noOfPoints++;
}
else
{
// Calculate the mean of each region
for(int i = 0; i < noOfRegions; i++)
{
for(int j = 0 ; j < noOfPoints; j++)
{
if(labels[pos[j].x][pos[j].y] == i)
{
meanRegion[i] += lpSrc[pos[j].x * w + pos[j].y];
}
}
meanRegion[i] /= 3;
noOfPointsInRegion[i] = 3;
}
for(int seedPoint = 0; seedPoint < noOfPoints; seedPoint++)
{
// define list
node *start, *temp;
start = (node *) malloc (sizeof(node));
temp = start;
temp -> next = NULL;
for(int i = -1; i <= 1; i++)
for(int j = -1; j<= 1; j++)
{
if(i == 0 && j == 0) continue;
int gamma = lpSrc[(pos[seedPoint].x + i) * + pos[seedPoint].y + j] - lpSrc[pos[seedPoint].x * w + pos[seedPoint].y];
push(start, pos[seedPoint].x + i, pos[seedPoint].y + j, gamma);
}
sort(start);
if(start != NULL)
{
node *y = start;
pop(start);
int sameNeighbour = 1;
int neighValue = -1;
for(int k = -1; k <= 1; k++)
for(int l = -1; l <= 1;l++)
{
if(k ==0 && l==0) continue;
if(labels[y -> x + k][y -> y + l] != -1)
{
neighValue = labels[y -> x + k][y -> y + l];
break;
}
}
for(int k = -1; k <= 1; k++)
for(int l = -1; l <= 1;l++)
{
if(k == 0 && l==0) continue;
if(labels[y -> x + k][y -> y = 1] != -1 && labels[y -> x + k][y -> y + l] != neighValue)
sameNeighbour = 0;
}
if(sameNeighbour == 1)
{
labels[y -> x][y -> y] = neighValue;
meanRegion[neighValue] = meanRegion[neighValue] * noOfPointsInRegion[neighValue] / noOfPointsInRegion[neighValue] + 1;
noOfPointsInRegion[neighValue]++;
for(int k = -1; k <= 1; k++)
for(int l = -1; l <= 1;l++)
{
if(k == 0 && l == 0) continue;
if(labels[y -> x + k][y -> y + l] == -1 && find(start, y->x + k, y->y + l) == 0)
{
int gammak = meanRegion[neighValue] - lpSrc[(y->x +k) * w + (y->y + l)];
push(start, y->x + k, y->y + l, gammak);
sort(start);
}
}
}
else
{
labels[y->x][y->y] = -1;
}
}
}
int noOfRegionOne = 0;
int noOfRegionTwo = 0;
int noOfBoundary = 0;
for(int i = 0; i< dwHeight; i++)
for(int j = 0;j<dwWidth; j++)
{
if(labels[i][j] == -1)
noOfBoundary++;
else if(labels[i][j] == 0)
noOfRegionOne++;
else if(labels[i][j] == 1)
noOfRegionTwo++;
}
CString info;
info.Format("Boundary %d, One %d, Two %d", noOfBoundary, noOfRegionOne, noOfRegionTwo);
AfxMessageBox(info);
noOfPoints = 0;
}
CScrollView::OnLButtonDblClk(nFlags, point);
END_SOURCE_PROCESSING;
}
After a choose to break the running, this is what is shown http://postimg.org/image/j2sh9k0a1/
Can anybody tell what is wrong and why it doesn't work?
Thanks.
Your screenshot shows that your node (Y is a terrible name, incidentally) has garbage values in it. Offhand, I suspect that 'sort' is overwriting your node values, resulting in garbage. I would create a static copy of your current node to prevent it from changing during processing:
Change
node *y = start;
pop(start);
to
node y = *start;
pop(start);

Laguerre interpolation algorithm, something's wrong with my implementation

This is a problem I have been struggling for a week, coming back just to give up after wasted hours...
I am supposed to find coefficents for the following Laguerre polynomial:
P0(x) = 1
P1(x) = 1 - x
Pn(x) = ((2n - 1 - x) / n) * P(n-1) - ((n - 1) / n) * P(n-2)
I believe there is an error in my implementation, because for some reason the coefficents I get seem way too big. This is the output this program generates:
a1 = -190.234
a2 = -295.833
a3 = 378.283
a4 = -939.537
a5 = 774.861
a6 = -400.612
Description of code (given below):
If you scroll the code down a little to the part where I declare array, you'll find given x's and y's.
The function polynomial just fills an array with values of said polynomial for certain x. It's a recursive function. I believe it works well, because I have checked the output values.
The gauss function finds coefficents by performing Gaussian elimination on output array. I think this is where the problems begin. I am wondering, if there's a mistake in this code or perhaps my method of veryfying results is bad? I am trying to verify them like that:
-190.234 * 1.5 ^ 5 - 295.833 * 1.5 ^ 4 ... - 400.612 = -3017,817625 =/= 2
Code:
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
double polynomial(int i, int j, double **tab)
{
double n = i;
double **array = tab;
double x = array[j][0];
if (i == 0) {
return 1;
} else if (i == 1) {
return 1 - x;
} else {
double minusone = polynomial(i - 1, j, array);
double minustwo = polynomial(i - 2, j, array);
double result = (((2.0 * n) - 1 - x) / n) * minusone - ((n - 1.0) / n) * minustwo;
return result;
}
}
int gauss(int n, double tab[6][7], double results[7])
{
double multiplier, divider;
for (int m = 0; m <= n; m++)
{
for (int i = m + 1; i <= n; i++)
{
multiplier = tab[i][m];
divider = tab[m][m];
if (divider == 0) {
return 1;
}
for (int j = m; j <= n; j++)
{
if (i == n) {
break;
}
tab[i][j] = (tab[m][j] * multiplier / divider) - tab[i][j];
}
for (int j = m; j <= n; j++) {
tab[i - 1][j] = tab[i - 1][j] / divider;
}
}
}
double s = 0;
results[n - 1] = tab[n - 1][n];
int y = 0;
for (int i = n-2; i >= 0; i--)
{
s = 0;
y++;
for (int x = 0; x < n; x++)
{
s = s + (tab[i][n - 1 - x] * results[n-(x + 1)]);
if (y == x + 1) {
break;
}
}
results[i] = tab[i][n] - s;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int num;
double **array;
array = new double*[5];
for (int i = 0; i <= 5; i++)
{
array[i] = new double[2];
}
//i 0 1 2 3 4 5
array[0][0] = 1.5; //xi 1.5 2 2.5 3.5 3.8 4.1
array[0][1] = 2; //yi 2 5 -1 0.5 3 7
array[1][0] = 2;
array[1][1] = 5;
array[2][0] = 2.5;
array[2][1] = -1;
array[3][0] = 3.5;
array[3][1] = 0.5;
array[4][0] = 3.8;
array[4][1] = 3;
array[5][0] = 4.1;
array[5][1] = 7;
double W[6][7]; //n + 1
for (int i = 0; i <= 5; i++)
{
for (int j = 0; j <= 5; j++)
{
W[i][j] = polynomial(j, i, array);
}
W[i][6] = array[i][1];
}
for (int i = 0; i <= 5; i++)
{
for (int j = 0; j <= 6; j++)
{
cout << W[i][j] << "\t";
}
cout << endl;
}
double results[6];
gauss(6, W, results);
for (int i = 0; i < 6; i++) {
cout << "a" << i + 1 << " = " << results[i] << endl;
}
_getch();
return 0;
}
I believe your interpretation of the recursive polynomial generation either needs revising or is a bit too clever for me.
given P[0][5] = {1,0,0,0,0,...}; P[1][5]={1,-1,0,0,0,...};
then P[2] is a*P[0] + convolution(P[1], { c, d });
where a = -((n - 1) / n)
c = (2n - 1)/n and d= - 1/n
This can be generalized: P[n] == a*P[n-2] + conv(P[n-1], { c,d });
In every step there is involved a polynomial multiplication with (c + d*x), which increases the degree by one (just by one...) and adding to P[n-1] multiplied with a scalar a.
Then most likely the interpolation factor x is in range [0..1].
(convolution means, that you should implement polynomial multiplication, which luckily is easy...)
[a,b,c,d]
* [e,f]
------------------
af,bf,cf,df +
ae,be,ce,de, 0 +
--------------------------
(= coefficients of the final polynomial)
The definition of P1(x) = x - 1 is not implemented as stated. You have 1 - x in the computation.
I did not look any further.