Skip to content

Instantly share code, notes, and snippets.

@Randl
Created April 12, 2014 07:43
Show Gist options
  • Save Randl/10523471 to your computer and use it in GitHub Desktop.
Save Randl/10523471 to your computer and use it in GitHub Desktop.
#include "stdafx.h"
#include <utility>
#include <array>
#include <random>
#include <iostream>
#include <cstdint>
#include <string>
#include <math.h>
const uint16_t kHeight = 10, kLength = 20;
std::mt19937 rng;
uint16_t up_obstacle_count = 1, down_obstacle_count = 1;
uint64_t score = 0;
const uint16_t min_obstacle_distance = 7, max_obstacle_distance = 10;
void rng_initialize()
{
rng.seed(std::random_device()());
}
std::pair<bool, uint16_t> generate_map(std::pair<bool, uint16_t> prev) {
// no 2 obstacles in a row
if (prev.second > 0)
return std::make_pair(false, 0);
std::uniform_int_distribution<uint16_t> dist10(0, 10);
++up_obstacle_count;
++down_obstacle_count;
bool is_up_obstacle = false, is_down_obstacle = false;
// if counter of both up and down obstacles is high
if (up_obstacle_count > min_obstacle_distance && down_obstacle_count > min_obstacle_distance)
if (up_obstacle_count > down_obstacle_count)
is_up_obstacle = true;
else
is_down_obstacle = true;
if (!is_up_obstacle && !is_down_obstacle)
if (up_obstacle_count == max_obstacle_distance)
is_up_obstacle = true;
else if (up_obstacle_count >= min_obstacle_distance)
is_up_obstacle = (dist10(rng) > 7);
if (!is_up_obstacle && !is_down_obstacle)
if (down_obstacle_count == max_obstacle_distance)
is_down_obstacle = true;
else if (down_obstacle_count >= min_obstacle_distance)
is_down_obstacle = (dist10(rng) > 7);
//restarting counters
if (is_up_obstacle)
up_obstacle_count = 0;
else if (is_down_obstacle)
down_obstacle_count = 0;
uint16_t obst_length = dist10(rng) / 5 + 2;
if (!is_down_obstacle && !is_up_obstacle)
obst_length = 0;
return std::make_pair(is_down_obstacle, obst_length);
}
void print_map(std::array<std::pair<bool, uint16_t>, kLength> game_map, uint16_t position) {
std::array <std::string, kHeight> print_array;
for (uint16_t i = 0; i < kHeight; ++i)
print_array[i] = "";
for (uint16_t i = 0; i < kLength; ++i) {
if (game_map[i].first == false) { // if obstacle is up
for (uint16_t j = 0; j < game_map[i].second; ++j)
print_array[j] += "#";
for (uint16_t j = game_map[i].second; j < kHeight; ++j)
print_array[j] += ".";
}
else { //if obstacle is down
for (uint16_t j = kHeight - 1; j > kHeight - game_map[i].second - 1; --j)
print_array[j] += "#";
for (int16_t j = kHeight - game_map[i].second - 1; j >= 0; --j)
print_array[j] += ".";
}
}
print_array[kHeight - position][1] = '@'; //adding bird
for (uint16_t i = 0; i < kHeight; ++i) //outputing
std::cout << print_array[i] << std::endl;
}
bool make_move(std::array<std::pair<bool, uint16_t>, kLength> game_map, uint16_t position, uint16_t current_move) {
//out of bounds
if (position + current_move >= kHeight || (current_move == 0 && position < 2))
return false;
int16_t first_move, second_move;
//splitting move in two parts
if (current_move == 0)
first_move = second_move = -1;
else {
first_move = current_move / 2;
second_move = current_move - first_move;
}
// first part of move
position += first_move;
bool pos_is_obst = false;
if (game_map[2].first == true && position <= game_map[2].second)
pos_is_obst = true;
if (game_map[2].first == false && kHeight - position <= game_map[2].second)
pos_is_obst = true;
if (pos_is_obst)
return false;
if (game_map[2].second > 0)
++score;
// second part of move
position += second_move;
pos_is_obst = false;
if (game_map[3].first == true && position <= game_map[3].second)
pos_is_obst = true;
if (game_map[3].first == false && kHeight - position <= game_map[3].second)
pos_is_obst = true;
if (pos_is_obst)
return false;
if (game_map[3].second > 0)
++score;
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
rng_initialize();
bool alive = true;
uint16_t bird_position = kHeight / 2, current_move;
std::array<std::pair<bool, uint16_t>, kLength> game_map;
game_map[0] = std::make_pair(false, 0);
for (uint16_t i = 1; i < kLength; ++i)
game_map[i] = generate_map(game_map[i - 1]);
while (alive) {
print_map(game_map, bird_position);
std::cout << "(score " << score << ") 0 - 4 ? ";
std::cin >> current_move;
alive = make_move(game_map, bird_position, current_move);
for (uint16_t i = 0; i < kLength - 2; ++i)
game_map[i] = game_map[i + 2];
game_map[kLength - 2] = generate_map(game_map[kLength - 3]);
game_map[kLength - 1] = generate_map(game_map[kLength - 2]);
bird_position += current_move == 0 ? -2 : current_move;
}
std::cout << "GAME OVER!" << std::endl << "YOUR SCORE IS " << score << "!";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment