Jump to content
  • Advertisement

mihaijulien

Member
  • Content Count

    11
  • Joined

  • Last visited

Community Reputation

193 Neutral

About mihaijulien

  • Rank
    Member

Personal Information

  • Interests
    |programmer|production|qa|
  1. mihaijulien

    Snake game (console app) - move function

    Crystal clear. Thank you so much!
  2. mihaijulien

    Snake game (console app) - move function

    I don't get it. Isn't that the purpose of the moveUp position? Just to redraw the whole snake one position upper, delete the old last position and repeat the whole process all over again? 
  3. mihaijulien

    Snake game (console app) - move function

    Well, could you point out what exactly is wrong in my move method? For example: case(Up): for (int i = 0; i < Snake.size(); i++) { coord.X = Snake[i].x; coord.Y = Snake[i].y-1; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); std::cout << Snake[i].body; } break; This should move the snake up, but it doesn't really work as intended. It actually moves the snake up once, than it won't move. In Main.cpp I changed the following: while(true) { SNAKE.move(dir); } Also, how can I delete the tail in the move method?
  4. Hello,   I have the following code and I need some help:   Snake.h #pragma once #include <vector> #include <iostream> enum direction { Up = 'w', Down = 's', Left = 'a', Right = 'd' }; class snake { private: direction dir; struct SNAKE { int x; int y; char body; }; std::vector<SNAKE> Snake; public: snake(); void startPos(); //void checkValidMove(direction dir); void move(direction dir); }; Snake.cpp #include "snake.h" #include <iostream> #include <Windows.h> snake::snake() { Snake.emplace_back(SNAKE{ 10,12,'@' }); Snake.emplace_back(SNAKE{ 10,13,'o' }); Snake.emplace_back(SNAKE{ 10,14,'o' }); } void snake::startPos() { COORD coord; for (int i = 0; i < Snake.size(); i++) { coord.X = Snake[i].x; coord.Y = Snake[i].y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); std::cout << Snake[i].body; } } //continuos move of the snake in a direction void snake::move(direction dir) { COORD coord; switch(dir) { case(Up): for (int i = 0; i < Snake.size(); i++) { coord.X = Snake[i].x; coord.Y = Snake[i].y-1; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); std::cout << Snake[i].body; } break; case(Down): for (int i = 0; i < Snake.size(); i++) { coord.X = Snake[i].x; coord.Y = Snake[i].y + 1; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); std::cout << Snake[i].body; } break; case(Left): for (int i = 0; i < Snake.size(); i++) { coord.X = Snake[i].x - 1; coord.Y = Snake[i].y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); std::cout << Snake[i].body; } break; case(Right): for (int i = 0; i < Snake.size(); i++) { coord.X = Snake[i].x + 1; coord.Y = Snake[i].y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); std::cout << Snake[i].body; } break; } } Main.cpp // Snake.cpp : Defines the entry point for the console application. // #include "snake.h" #include "map.h" #include <stdlib.h> #include <conio.h> #include <iostream> using namespace std; int main() { map MAP; MAP.draw(); snake SNAKE; SNAKE.startPos(); char input = _getch(); direction dir = Up; SNAKE.move(dir); system("pause"); return 0; } In Snake.cpp, the move(direction dir) method doesn't work as intended. I wanted it to continuously move the snake. (I know I will have to store the position of the snake's head and tail in order to properly change the direction, but for now, this is what I have). How do I exactly make it move when I press the 'w' key for example?   Also, is it ok to initialize in Main.cpp the dir with Up value?   
  5. mihaijulien

    Snake Game (console) - snake display method issue

    One more question: How do I use the push_back function if the typename is a struct?   Thank you!
  6. mihaijulien

    Snake Game (console) - snake display method issue

    Something like:  snake::initSnake(int pos_x, int pos_y) { snakeb.push_back('@'); //head snakeb.push_back('o'); snakeb.push_back('o'); for (int i = 0; i < snakeb.size(); i++) { snakeb[i][0] = pos_x; snakeb[i][1] = pos_y; } } That doesn't seem right.The snakeb is a single dimension array, how do I pass the starting position?
  7. mihaijulien

    Snake Game (console) - snake display method issue

      You mean to define the initSnake() inside the map class and the rest of the methods concerning the snake inside the snake class?
  8. mihaijulien

    Snake Game (console) - snake display method issue

    Oh, I think I get it. I modified my main.cpp: int main() { snake Snake; Snake.initMap(); Snake.initSnake(); Snake.updateMap(); system("pause"); return 0; } Now seems to work fine.   However, is this the way you would recommend me to continue the program?   I want to split my program as this:   the map class to contain the methods for: initial map drawing; random fruit spawning on the map; and the snake class to contain the methods for: the initial snake body; snake movement; snake growing; score update; lose condition.
  9. Hello,   I'm working on a C++ console Snake game. I have the following code:   main.cpp #include <iostream> #include "map.h" #include "snake.h" using namespace std; int main() { map Map; snake Snake; Map.initMap(); Snake.initSnake(); Map.updateMap(); system("pause"); return 0; } map.h #pragma once const int row = 20; const int column = 80; class map { protected: char area[row][column]; public: void initMap(); void updateMap(); }; map.cpp #include "map.h" #include <iostream> using namespace std; void map::initMap() { for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { if (i == 0 || i == row - 1 || j == 0 || j == column - 1) { area[i][j] = '#'; } else { area[i][j] = ' '; } } } } void map::updateMap() { for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { cout << area[i][j]; } } } snake.h #pragma once #include "map.h" #include <vector> class snake : public map { protected: std::vector <char> snakeb; public: void initSnake(); void updateSnake(); }; snake.cpp #include "snake.h" #include <iostream> using namespace std; void snake::initSnake() { snakeb.push_back('@'); // head snakeb.push_back('o'); snakeb.push_back('o'); for (int i = 0; i < snakeb.size(); i++) { area[row / 2][column / 2 + i] = snakeb[i]; // the snake will spawn in the middle of the map } } The program compiles fine, but the actual snake body won't get printed in the console. My guess is that the issue is caused because of the inherited snake class.   I would appreciate if I get any piece of advice to better organise (best practice) my code, splitting the different parts of the game into different classes etc.   Thank you! :)
  10. mihaijulien

    C++ 2048 game - movement function issue

    Hello,   Ok, so the issue is the following: when moving a number up, it goes up to the first row but if there are 2 numbers on the same column, they won't add up. Also, the same algorithm is used for the goDown() function (with the proper changes), but it won't work at all.   The down function would be this one: void game::goDown() { for (int j = 0; j < 4; j++) { for (int i = 0; i < 3; i++) //traverse the rows from the top { for (int k = 0; k < 4; k++) // loop to check that all swaps were done { if (n[i][j] == 0) { n[i + 1][j] = n[i][j]; n[i][j] = 0; } else if (n[i + 1][j] == n[i][j]) { n[i + 1][j] *= 2; n[i][j] = 0; } } } } } This is the array I'm using to move the numbers: game::game() { score = 0; cout << "Score: " << score << "\n\n"; int x = rand() % 3; int y = rand() % 3; for (int j = 0; j < 4; j++) { cout << "+------+------+------+------+\n"; for (int i = 0; i < 4; i++) { if (i == x && j == y) { n[x][y] = 2; cout << "| " << n[x][y] << " "; } else { n[i][j] = 0; cout << "| " << n[i][j] << " "; } } cout << "|\n"; } cout << "+------+------+------+------+\n\n"; cout << "(W)Up (S)Down (A)Left (D)Right\n"; } What is wrong in the code? Also, I'm not sure if I understand what you're saying by moving each stone individually all the way, Alberth. How would the code be different?   Thanks.
  11. Hello,   I am trying to make a c++ implementation of the following game: http://2048game.com/    I have the following movement issue which doesn't really work as expected: void game::goUp() { for (int j = 0; j < 4; j++) //traverse the columns { for (int i = 3; i > 0; i--) //traverse the rows (start going through the array from the bottom) { for (int k = 0; k < 4; k++) //loop purpose: check the same column several times to see if all the swaps were done { if (n[i - 1][j] == 0) { n[i - 1][j] = n[i][j]; n[i][j] = 0; } else if (n[i - 1][j] == n[i][j]) { n[i - 1][j] *= 2; n[i][j] = 0; } } } } } Thanks!
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!