so I tried to recreate Conways Game of Life and i got it pretty much working but i have numbers in my output and i have no idea where they are coming from.
Im fairly new to c++and programming in general i learned python and java before but i have never seen this and google didnt seem to understand my question so i hope you fellow humans do.
My output looks like this:
1664447571170186994045474010000255652800 /its about this number
0000000000
0011000000
0110000000
0001000000
0000000000
0000000000
0000000000
0000000000
0000000000
anyone has any idea where they come from cause i certainly don't.
sometimes these numbers even "collide" with the board like this:
0000-1-1-1-116644475711701869940
4547401010000000
791621423110000000
0101000000
0000000000
0000000000
0000000000
0000000000
0000000000
0020000000
My sphagetthi code:
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
using namespace std::this_thread;
using namespace std::chrono;
int main(){
int x = 10, y = 10, i, j, c, b;
int field[y][x] =
{{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,1,1,0,0,0,0},
{ 0,0,0,0,1,0,1,0,0,0},
{ 0,0,0,0,1,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0}};
int updateField[y][x];
cout << "start";
for(b = 0;b<=10; b++){
cout << "main loop: "<<b<<"\n";
for(y = 0; y < 10; y++)
{
for(x = 0; x < 10; x++)
{
c = 0;
if(y == 0 || y == 9 || x == 0 || x == 9){
}
else{
/*v v v lebende zelle v v v*/
if(field[y][x] == 1){
if(field[y-1][x-1] == 1){
c++;
}
if(field[y-1][x] == 1){
c++;
}
if(field[y-1][x+1] == 1){
c++;
}
if(field[y][x-1] == 1){
c++;
}
if(field[y+1][x-1] == 1){
c++;
}
if(field[y+1][x] == 1){
c++;
}
if(field[y+1][x+1] == 1){
c++;
}
if(field[y][x+1] == 1){
c++;
}
/*regeln v v v*/
if(c < 2 or c > 3){
updateField[y][x] = 0;
}
else
{
updateField[y][x] = 1;
}
}
/*^ ^ ^ lebende zelle ^ ^ ^*/
/*v v v tote zelle v v v*/
else if(field[y][x] == 0){
if(field[y-1][x-1] == 1){
c++;
}
if(field[y-1][x] == 1){
c++;
}
if(field[y-1][x+1] == 1){
c++;
}
if(field[y][x-1] == 1){
c++;
}
if(field[y+1][x-1] == 1){
c++;
}
if(field[y+1][x] == 1){
c++;
}
if(field[y+1][x+1] == 1){
c++;
}
if(field[y][x+1] == 1){
c++;
}
/*regeln v v v*/
if(c == 3)
{
updateField[y][x] = 1;
}
else
{
updateField[y][x] = 0;
}
}
}
}
}
cout << "\n";
for(i = 0; i < 10; i++){
for(j = 0; j < 10; j++){
field[i][j] = updateField[i][j];
}
}
for(i = 0;i < x; i++){
for(j = 0; j < y; j++ )
{
cout << field[i][j];
}
cout << "\n";
}
sleep_for(nanoseconds(500000000));
}
}
I'd appreciate any answer. thanks in advance.
The problem is that you ignore the borders when building updateField (0 or 9 in a component) but then copy updateField including those borders into field. The borders in updateField were never set, so they just contain any number which was in the memory before which is not controlled by your program.
You should either set the borders of updateField to zero or do not copy the borders into field iterating from 1 to excluding 9 instead of from 0 to 10.
While I hope that this solves your problem I would also like to share some ideas how to refactor your code. For instance you could use arrays of boolean values instead of integers as the values clearly can only be 0 or 1 according to the games logic. Scanning the neighborhood of a cell also adds a lot of redundant code which can be made shorter. Just see the following as a general idea about some improvements which can be done:
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
using namespace std::this_thread;
using namespace std::chrono;
int main(){
int x = 10, y = 10, i, j, c, b;
bool field[y][x] =
{{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,1,1,0,0,0,0},
{ 0,0,0,0,1,0,1,0,0,0},
{ 0,0,0,0,1,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0}};//too lazy to rewrite to true/false, should also work this way
bool updateField[y][x];
cout << "start";
for(b = 0;b<=10; b++){
cout << "main loop: "<<b<<"\n";
for(y = 0; y < 10; y++)
{
for(x = 0; x < 10; x++)
{
c = 0;
if(y == 0 || y == 9 || x == 0 || x == 9){
updateField[y][x] = false;
}
else{
for(i=-1;i<2;i++){
for(j=-1;j<2;j++){
if((i != 0 || j != 0) && field[y+i][x+j] == 1){// exclude i == j == 0
c++;
}
}
}
if(field[y][x]){
if(c < 2 or c > 3){
updateField[y][x] = false;
}
else
{
updateField[y][x] = true;
}
}else{
if(c == 3)
{
updateField[y][x] = true;
}
else
{
updateField[y][x] = false;
}
}
}
}
}
cout << "\n";
for(i = 0; i < 10; i++){
for(j = 0; j < 10; j++){
field[i][j] = updateField[i][j];
}
}
for(i = 0;i < x; i++){
for(j = 0; j < y; j++ )
{
cout << field[i][j];
}
cout << "\n";
}
sleep_for(nanoseconds(500000000));
}
}
If you initialize updateField as well, the weird numbers will be gone.
Because of this if statement, the edge of updatefield doesn't update:
if (y == 0 || y == 9 || x == 0 || x == 9) ;
else {
///long statements
if (c < 2 || c > 3) {
updateField[y][x] = 0;
}
else
{
updateField[y][x] = 1;
}
}
Also, you should be more consistent with your usage of variables, for example you define your map to be x*y sized, but later in the for loops you specify the maximum value of x and y to be 10.
What you're seeing is the contents of uninitialized memory. When you declare int updateField[y][x]; the compiler reserves an appropriate space of memory for you, but it doesn't initialize the memory for you, so it contains whatever random junk was already there.
To fix this, initialize updateField to be identical to field before running the loop:
for (int i = 0; i < y; ++i) {
for (int j = 0; j < x; ++j) {
updateField[i][j] = field[i][j];
}
}
This is my first question on this site. I learn programming for a year and I always found a answer for my problem in projects. I know that this is REALLY simple problem and here is a code:
#include "stdafx.h"
#include "Wave.h"
Wave::~Wave()
{
}
void Wave::setEnemyCount(int count) {
nemyCount = count;
}
bool Wave::createWave() {
for (int i = 0; i < enemyCount; i++) {
enemyArray.push_back(Enemy());
}
for (int i = 0; i < enemyArray.size(); i++) {
int randomPositionX = rand() % 150 + 10;
int randomPositionY = rand() % 50 + 10;
if (!enemyArray.at(i).init(randomPositionX, randomPositionY)) {
std::cout << "Create wave(init enemies) - failed\n";
return false;
}
}
return true;
}
void Wave::drawWave(Window* window) {
window->setCursor({ 0, 2 });
window->setColor(Window::Color::RED);
std::cout << "Enemies: " << enemyArray.size();
window->setColor(Window::Color::WHITE);
for (int i = 0; i < enemyArray.size(); i++) {
enemyArray.at(i).draw(window);
//std::cout << i;
}
}
void Wave::update(Bullet* bullet, Player* player) {
for (int i = 0; i < enemyArray.size(); i++) {
if (player->checkShoot()) {
if ((bullet->getPositionX() >= enemyArray.at(i).getPositionX() && bullet->getPositionX() <= enemyArray.at(i).getPositionX() + 5)) {
if ((bullet->getPositionY() >= enemyArray.at(i).getPositionY() && bullet->getPositionY() <= enemyArray.at(i).getPositionY() + 5)) {
player->addScore(10);
enemyArray.erase(enemyArray.begin() + i);
}
}
}
}
if (enemyArray.size() <= 0) {
std::cout << "WAVE COMPLETE";
Sleep(2000);
enemyCount += 5;
createWave();
}
}
int Wave::getEnemyCount() const {
return enemyCount;
}
The collision work correct, but when bullet touch a enemy of index for example: 5, a code always remove the enemy of last index, so I can remove each enemy only shoot to one enemy with index 5 or 1 or 7.
When you do enemyArray.erase(...) you shouldn't increment i because you will skip one element.
So you should rewrite the loop like this:
for (int i = 0; i < enemyArray.size();) {
if (player->checkShoot()) {
if ((bullet->getPositionX() >= enemyArray.at(i).getPositionX() && bullet->getPositionX() <= enemyArray.at(i).getPositionX() + 5)) {
if ((bullet->getPositionY() >= enemyArray.at(i).getPositionY() && bullet->getPositionY() <= enemyArray.at(i).getPositionY() + 5)) {
player->addScore(10);
enemyArray.erase(enemyArray.begin() + i);
continue;
}
}
}
++i;
}
Or, with a more idiomatic style:
for (auto it = enemyArray.begin(), end = enemyArray.end();it!=end;) {
if (player->checkShoot()) {
if ((bullet->getPositionX() >= it->getPositionX() && bullet->getPositionX() <= it->getPositionX() + 5)) {
if ((bullet->getPositionY() >= it->.getPositionY() && bullet->getPositionY() <= it->getPositionY() + 5)) {
player->addScore(10);
enemyArray.erase(it);
continue;
}
}
}
++it;
}
Of course, if you wan a single enemy to be removed at a time, you should break instead of continue in the loop, after having erased the element.
For completeness, you can also use std::remove_if if you want to remove all enemies touched by the bullet, it's more optimal than the 2 loops above, because it does less moving of elements when multiple elements are removed.
My program is suppose to count how many times the data in my array changed from increase to decrease or vice versa. For example: {1,2,3,4,3,4}
changes twice as the first four elements are increasing then decrease o 3 (causing one change) and then increase back to four causing a second change.
The idea in my code was that every time greater or decrease changed to false it would count when it happened but I cannot get it to work.
Any help is greatly appreciated as I am really struggling with this!
unsigned count = 0;
bool greater = true;
bool decrease = true;
for (unsigned i = 0; i < elements; i++){
if (a[i + 1] > a[i]){
greater = true;
}
else
greater = false;
count++;
}
for (unsigned i = 0; i < elements; i++){
if (a[i + 1] < a[i]){
decrease = true;
}
else
decrease = false;
count++;
}
return count;
Your logic is wrong
you may do something like
enum class EDirection { none, decreasing, increasing};
std::size_t count_direction_changes(const std::vector<int>& v)
{
std::size_t res = 0;
EDirection direction = EDirection::none;
for (std::size_t i = 1; i != v.size(); ++i) {
const int diff = v[i] - v[i - 1];
switch (direction)
{
case EDirection::none: {
if (diff == 0) {
break;
}
direction = (diff > 0) ? EDirection::increasing : EDirection::decreasing;
break;
}
case EDirection::increasing: {
if (diff < 0) {
++res;
direction = EDirection::decreasing;
}
break;
}
case EDirection::decreasing: {
if (diff > 0) {
++res;
direction = EDirection::increasing;
}
break;
}
}
}
return res;
}
Demo
You must change your loops. First of all you should stop the loop at size-1. Because you are comparing with next element and you can go out of bounds if your for is running until elements instead of elements-1.
Furthermore, you have a logic issue. If you are using a boolean variable as flag, you should check if it's true or not before increasing your counter. And in case you increments your counter you must reset that flag. Something similar to down loop should work. Maybe there is some little mistake because I don't have anythin to test it now. But it should be something similar to this.
for (unsigned i = 0; i < elements-1; i++){
if (a[i + 1] > a[i]){
greater = true;
}
else{
greater = false;
}
if(greater){
count++;
greater = false;
}
}
This is a lot like Jarod42's, but seeing as I've already coded it will throw it out there. BTW, I use the slightly awkward v[n] < v[n - 1] so it's only necessary to implement operator< to apply the algo to a user-defined type (i.e. not operator> as well).
#include <iostream>
#include <vector>
template <typename T>
size_t changes(const std::vector<T>& v)
{
if (v.size() <= 2) return 0;
size_t count = 0;
enum { Increasing, Decreasing, Flat } last;
last = v[0] < v[1] ? Increasing : v[1] < v[0] ? Decreasing : Flat;
for (size_t i = 2; i < v.size(); ++i)
if (v[i - 1] < v[i])
{
if (last == Decreasing) ++count;
last = Increasing;
}
else if (v[i] < v[i - 1])
{
if (last == Increasing) ++count;
last = Decreasing;
}
return count;
}
int main()
{
std::cout << changes<int>({ 1, 3, 5, 4, 6 }) << '\n';
std::cout << changes<int>({ 3, 3, 5, 4, 6 }) << '\n';
std::cout << changes<int>({ 4, 3, 5, 4, 2, 2, 1 }) << '\n';
}
See it run here.
Here is another approach, similar to Tony's and Jarod's:
#include <vector>
#include <cassert>
#include <iostream>
size_t countTrendChanges(const std::vector<int>& a) {
if (a.size() < 3)
return 0;
int trend = 0;
size_t count = 0;
for (size_t i = 1; i != a.size(); ++i) {
int new_trend = (a[i-1] < a[i]) - (a[i] < a[i-1]);
if (new_trend == 0)
continue;
if (trend != 0 && new_trend != trend)
count++;
trend = new_trend;
}
return count;
}
int main() {
assert(countTrendChanges({}) == 0);
assert(countTrendChanges({1}) == 0);
assert(countTrendChanges({3,2,1}) == 0);
assert(countTrendChanges({1,2,3}) == 0);
assert(countTrendChanges({1,2,2,3}) == 0);
assert(countTrendChanges({3,2,1,2,3}) == 1);
assert(countTrendChanges({1,2,3,2}) == 1);
assert(countTrendChanges({2,1,1,2}) == 1);
assert(countTrendChanges({1,2,2,1}) == 1);
assert(countTrendChanges({1,2,3,4,3,4}) == 2);
}