Well I made a game. It basically works like this. Every half second it enters my if condition makes necessary calculations after then it prints everything to the console. This is my Game loop.
//game loop
while(!IsGameOver)
{
// Updates every half a second
if ((int)(ElapsedTime / 500) > PreviousRoundQSec)
{
PreviousRoundQSec = (int)(ElapsedTime / 500);
BulletCalculation(bullets);
RemoveBulletsOutsideOfTheBoard(bullets, BoardLength);
HealthCalculation(SpaceShips, bullets);
RemoveDead(SpaceShips, bullets);
DrawGame(BoardWidth, BoardLength, SpaceShips, bullets);
}
// Updates EverySecond
if ((int)(ElapsedTime / 1000) > PreviousRoundSec)
{
PreviousRoundSec = (int)(ElapsedTime / 1000);
PreDir = HordLogic(SpaceShips, bullets,BoardWidth, BoardLength, PreDir, PreNearBorder);
}
IsGameOver = GameOver(SpaceShips);
// To keep time
auto end = chrono::steady_clock::now();
ElapsedTime = chrono::duration_cast<chrono::milliseconds>(end - start).count();
}
At first I tried cout ing every character after calculating. But it printing was pretty bad and you could see printing every character individually. that did pretty bad. After some search on the internet I found printf func was faster but didn't really helped me either. My final soultion was adding evey character to a string and printing that string at once. This was the best solution but not enough because you can see the game flashing and this makes everything complicating.
this my draw function's latest version.
void DrawGame(int BoardWidth, int BoardLength, vector<Entity*> &SpaceShips, vector<Bullet> &bullets)
{
system("cls");
string screen = "";
for (int x = 0; x < BoardWidth + 2; x++)
{
screen += (char)178;
}
screen += "\n";
for (int y = 1; y < BoardLength + 1; y++)
{
for (int x = 0; x < BoardWidth + 2; x++)
{
if (x == 0 || x == BoardWidth + 1)
{
screen += (char)178;
}
else
{
bool enteredIf = false;
for (int i = 0; i < SpaceShips.size(); i++)
{
// Tough Enemy drawing
if (SpaceShips[i]->x == x && SpaceShips[i]->y == y && SpaceShips[i]->type == 't')
{
screen += "#";
enteredIf = true;
}
else if (SpaceShips[i]->x + 1 == x && SpaceShips[i]->y == y && SpaceShips[i]->type == 't')
{
screen += "#";
enteredIf = true;
}
else if (SpaceShips[i]->x + 2 == x && SpaceShips[i]->y == y && SpaceShips[i]->type == 't')
{
screen += "#";
enteredIf = true;
}
else if (SpaceShips[i]->x == x && SpaceShips[i]->y + 1 == y && SpaceShips[i]->type == 't')
{
screen += "#";
enteredIf = true;
}
else if (SpaceShips[i]->x + 2 == x && SpaceShips[i]->y + 1 == y && SpaceShips[i]->type == 't')
{
screen += "#";
enteredIf = true;
}
//Idle Enemy drawing
if (SpaceShips[i]->x == x && SpaceShips[i]->y == y && SpaceShips[i]->type == 'i')
{
screen += "#";
enteredIf = true;
}
else if (SpaceShips[i]->x + 1 == x && SpaceShips[i]->y == y && SpaceShips[i]->type == 'i')
{
screen += "#";
enteredIf = true;
}
else if (SpaceShips[i]->x + 2 == x && SpaceShips[i]->y == y && SpaceShips[i]->type == 'i')
{
screen += "#";
enteredIf = true;
}
else if (SpaceShips[i]->x + 1 == x && SpaceShips[i]->y + 1 == y && SpaceShips[i]->type == 'i')
{
screen += "U";
enteredIf = true;
}
//Shooting Enemy Drawing
if (SpaceShips[i]->x == x && SpaceShips[i]->y == y && SpaceShips[i]->type == 's')
{
screen += "#";
enteredIf = true;
}
else if (SpaceShips[i]->x + 1 == x && SpaceShips[i]->y == y && SpaceShips[i]->type == 's')
{
screen += "#";
enteredIf = true;
}
else if (SpaceShips[i]->x + 2 == x && SpaceShips[i]->y == y && SpaceShips[i]->type == 's')
{
screen += "#";
enteredIf = true;
}
else if (SpaceShips[i]->x + 2 == x && SpaceShips[i]->y + 1 == y && SpaceShips[i]->type == 's')
{
screen += "/";
enteredIf = true;
}
else if (SpaceShips[i]->x == x && SpaceShips[i]->y + 1 == y && SpaceShips[i]->type == 's')
{
screen += "\\";
enteredIf = true;
}
//Player Drawing
if (SpaceShips[i]->x + 1 == x && SpaceShips[i]->y == y && SpaceShips[i]->type == 'p')
{
screen += "^";
enteredIf = true;
}
else if (SpaceShips[i]->x == x && SpaceShips[i]->y + 1 == y && SpaceShips[i]->type == 'p')
{
screen += "<";
enteredIf = true;
}
else if (SpaceShips[i]->x + 1 == x && SpaceShips[i]->y + 1 == y && SpaceShips[i]->type == 'p')
{
screen += "_";
enteredIf = true;
}
else if (SpaceShips[i]->x + 2 == x && SpaceShips[i]->y + 1 == y && SpaceShips[i]->type == 'p')
{
screen += ">";
enteredIf = true;
}
}
if (!enteredIf)
{
screen += " ";
}
}
}
screen += "\n";
}
for (int x = 0; x < BoardWidth + 2; x++)
{
screen += (char)178;
}
// draw bullets
for (int i = 0; i < bullets.size(); i++)
{
if (bullets[i].GetDir() == 's')
{
screen.at(((bullets[i].y * (BoardWidth + 3)) - 1) + (bullets[i].x + 1)) = (char)203;
}
else if (bullets[i].GetDir() == 'w')
{
screen.at(((bullets[i].y* (BoardWidth + 3)) - 1) + (bullets[i].x + 1)) = (char)202;
}
}
screen += "\n Remaining lives: ";
screen += itoa(SpaceShips[0]->hp);
cout << screen;
}
This is how game looks (I didn't see any rules against sharing links. if so any warning is welcome): https://www.loom.com/share/1bcc97fa0cbe4ee3aec62e317b10fcd0
The problem is likely with the fact that you're using std::cout. The way the standard output stream interacts with your terminal is as a continuous sequence of lines, which naturally scroll upwards as new lines are added. You need to get to a lower level of interaction with your terminal, at which you are able to better control its behavior.
One such option is using the ncurses library for Unix systems (or PDCurses for Windows command-line)
This will allow you to use the "fixed" terminal window, and set the values of characters on that fixed window.
Related
I made a grid where i can draw every pixel using the get and store function
and now I want to store also the color value every time I change the color from the color picker. Right now all the cells on the grid are affected when I choose a color instead of staying in e.g. red when I choose red and stay red when I choose green.
Below is the code:
let sizewidth = 17
let sizeheight = 17
function setup() {
createCanvas(sizewidth*30, sizeheight*30);
col = createColorPicker('#000000');
}
function draw() {
background(255);
for(y = 0; y < sizeheight; y++){
for(x = 0; x < sizewidth; x++){
if(getItem(x + ":" + y) == null){
storeItem(x + ":" + y, false)
}
if(getItem(x + ":" + y) == true){
fill(col.color());
}
rect(x*30, y*30, 30, 30)
noFill()
}
}
}
function mouseClicked(){
for(y = 0; y < sizeheight; y++){
for(x = 0; x < sizewidth; x++){
if(mouseX < x*30+30 && mouseX > x*30 && mouseY < y*30+30 && mouseY > y*30){
storeItem(x + ":" + y, true)
}
}
}
}
function keyTyped(){
if(key == "c"){
clearStorage()
}
}
I think that you can store the color data instead of boolean value, like this:
let sizewidth = 17
let sizeheight = 17
function setup() {
clearStorage() // clearing the previous boolean data (can be deleted later)
createCanvas(sizewidth*30, sizeheight*30);
col = createColorPicker('#000000');
}
function draw() {
background(255);
for(y = 0; y < sizeheight; y++){
for(x = 0; x < sizewidth; x++){
if(getItem(x + ":" + y) == null){
storeItem(x + ":" + y, [255,255,255]) // filling all white
}
else {
let c = getItem(x + ":" + y); // getting color
fill(c[0],c[1],c[2]); // fill cell with the color
}
rect(x*30, y*30, 30, 30)
noFill()
}
}
}
function mouseClicked(){
for(y = 0; y < sizeheight; y++){
for(x = 0; x < sizewidth; x++){
if(mouseX < x*30+30 && mouseX > x*30 && mouseY < y*30+30 && mouseY > y*30){
let c = col.color();
storeItem(x + ":" + y, [red(c),green(c),blue(c)]); // storing the color
}
}
}
}
function keyTyped(){
if(key == "c"){
clearStorage()
}
}
please if there is any specific algorithm for implementing the divide operator as a function, guide me about their name. I want to implement a function that takes two floating number and return the result of the divide, but in implementation, I won't use "/".
I have done this in a much simpler version when we want just the q in integer,
function divide(num0, num1) {
if ("bigint" != typeof num0 || "bigint" != typeof num1) {
throw new TypeError("The arguments should be bigint.");
}
if (num1 > num0) {
return 0;
}
for (var i = 0n; num0 >= num1; i++) {
num0 -= num1;
}
return i;
}
"I use bigint numeric type just two restrict to integer"
but I think this approach couldn't extend two return floating results. my guess is I approach binary level operation or so; thanks if learning me about any flowchart, pseudo-code, or code-snippet "in any language" to deal with this problem
I wrote this one for this question in js:
function justIntegerDivide(num0, num1) {
for (var q = 0; num0 >= num1; q++) {
num0 -= num1;
}
return [q, num0];
}
const divide = (n0, n1, afterPoint = 10) => {
if ((0 == n1 || 0n == n1) && 0 < n0) return Infinity;
if ((0 == n1 || 0n == n1) && 0 > n0) return -Infinity;
if ((0 == n1 || 0n == n1) && 0 == n0) return NaN;
if ("number" == typeof n0 && "number" == typeof n1) {
let sign = Math.sign(n0) * Math.sign(n1);
let num0 = Math.abs(n0),
num1 = Math.abs(n1);
let counter = 0;
let [q, r] = justIntegerDivide(num0, num1);
result = `${q}.`;
for (counter = 1; counter < afterPoint; counter++) {
var newReminder;
let qAfter;
previousReminder = 1 == counter ? r : newReminder;
[qAfter, newReminder] = justIntegerDivide(previousReminder * 10, num1);
result += qAfter;
if (0 == newReminder) {
return +result * sign;
}
}
return +result * sign;
} else if ("bigint" == typeof n0 && "bigint" == typeof n1) {
let sign = (n0 > 0 && n1 > 0) || (n0 < 0 && n1 < 0) ? 1n : -1n;
let num0 = n0 > 0 ? n0 : -n0;
let num1 = n1 > 0 ? n1 : -n1;
if (num0 < num1) {
return 0n;
}
for (var i = 0n; num0 >= num1; i++) {
num0 -= num1;
}
return i * sign;
} else {
throw new TypeError("Both arguments should be number or bigint");
}
};
I'm trying to program a code decoder. But I get the following error for all the comparisons in the if statements:
'error: ISO C++ forbids comparison between pointer and integer
[-fpermissive]'
The examples for the input string are ".-.--" and "-..-.--".
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
int c[100], t = 0, l, i = 0;
l = s.length();
cin >> s;
if (s[0] == '.') {
c[0] = 0;
t += 1;
while (i < l) {
if (s[i] == '-' && s[i + 1] == '.') {
c[t] = 1;
t += 1;
i += 2;
}
if (s[i] == '.') {
c[t] = 0;
t += 1;
i++;
}
if (s[i] == '-' && s[i + 1] == '-') {
c[t] = 2;
t += 1;
i += 2;
}
}
}
if (s[0] == '-' && s[1] == '.') {
c[0] = 1;
t += 1;
while (i < l) {
if (s[i] == '-' && s[i + 1] == '.'
'){
c[t] = 1; t += 1; i += 2;
}
if (s[i] == '.') {
c[t] = 0;
t += 1;
i++;
}
if (s[i] == '-' && s[i + 1] == '-') {
c[t] = 2;
t += 1;
i += 2;
}
}
}
if (s[0] == '-' && s[1] == '-') {
c[0] = 2;
t += 1;
while (i < l) {
if (s[i] == '-' && s[i + 1] == '.') {
c[t] = 1;
t += 1;
i += 2;
}
if (s[i] == ".") {
c[t] = 0;
t += 1;
i++;
}
if (s[i] == "-" && s[i + 1] == "-") {
c[t] = 2;
t += 1;
i += 2;
}
}
}
for (i = 0; i < t; i++) {
cout << s[t];
}
return 0;
}
How do I resolve this issue?
You were using single quotes until you got here:
if(s[i]=="-"&&s[i+1]=="-"){
You need to change it to single quotes so you have an int to int comparison.
if(s[i]=='-'&&s[i+1]=='-'){
When you say
"-"
you are creating a pointer.
When you say
'='
you are creating an int.
(" ") is a string literal which is char const * which is a pointer and (' ') is char which get promoted to int, so you can't compare them. They must be type compatible.
I have tried in two IDEs namely Clion and Xcode and I get the same results.
The debugger steps into isMatch function again even if I use stepOver. I've successfully used 'stepOver' in case of other functions but I'm not sure if it works the expected way for recursive calls. Can someone help me with this?
bool isMatch(string s, string p) {
//Base case
if(s.length() == 0 && p.length() == 0)
return true;
else if(p[1] == '*') {
int i = 0;
for (; (s[i] == p[0] || p[0] == '.') && i<s.length() ; i++) {
string temp1 = s.substr(i);
string temp2 = !p.length()? "" : p.substr(2);
if (isMatch(temp1, temp2))
return true;
}
if( isMatch(s.substr(i), !p.length()? "" : p.substr(2) ))
return true;
return false;
}
else
return (s[0] == p[0] || p[0] == '.') && isMatch(s.substr(1), p.substr(1));
}
I'm looking for CZ national idenity card number validation algorithm. There can be 9 or 10 digits sequenced by special algoritm, but there are not just numbers :) I've found this function:
function checkId(id) {
var x, sum_nb=0, check;
var steps = new Array(7,3,1,7,3,1,7,3);
id = id.toUpperCase(),
splitted = id.split("");
if (splitted.length != 9 && splitted.length != 10) {return false;}
for (x = 0; x < 9; x++)
{
if (x == 0 || x == 1 || x == 2) {
sum_nb += steps[x] * parseInt(id.charCodeAt(x)-55);
}
if (x > 3) {
sum_nb += steps[x-1] * parseInt(id.charAt(x));
}
}
check = sum_nb % 10;
if (check == id.charAt(3))
return true;
else
return false;
}
But I'm not sure that it's correct. Is anyone knows correct algoritm? It can be written on any language, it does not matter.