feat: first time using setw but its interesting; used in window for numbers

This commit is contained in:
DarthMooMancer
2025-09-08 20:06:57 -04:00
parent 63344fb83b
commit 4c1d4d5185
5 changed files with 34 additions and 4 deletions
+1
View File
@@ -7,6 +7,7 @@ include_directories(include)
set(SOURCES set(SOURCES
src/main.cpp src/main.cpp
src/input.cpp src/input.cpp
src/window.cpp
) )
add_executable(2048 ${SOURCES}) add_executable(2048 ${SOURCES})
+2 -2
View File
@@ -1,7 +1,7 @@
#ifndef GLOBALS_HPP #ifndef GLOBALS_HPP
#define GLOBALS_HPP #define GLOBALS_HPP
constexpr int ROW = 10; constexpr int ROW = 4;
constexpr int COL = 10; constexpr int COL = 4;
#endif #endif
+7 -1
View File
@@ -1,6 +1,12 @@
#ifndef WINDOW_HPP #ifndef WINDOW_HPP
#define WINDOW_HPP #define WINDOW_HPP
// Add code here #include "globals.hpp"
#include <array>
struct Window {
std::array<std::array<int, COL>, ROW> m_board = {};
void draw_window();
};
#endif #endif
+7 -1
View File
@@ -1,14 +1,20 @@
#include <iostream> #include <iostream>
#include <thread> #include <thread>
#include "input.hpp" #include "input.hpp"
#include "window.hpp"
int main() { int main() {
Input input; Input input;
Window window;
bool running = true; bool running = true;
std::thread input_thread(&Input::get_input, &input, std::ref(running)); std::thread input_thread(&Input::get_input, &input, std::ref(running));
window.m_board[0][0] = 4;
while (running) { while (running) {
std::cout << "Hello World" << std::endl; window.draw_window();
} }
input_thread.join(); input_thread.join();
return 0; return 0;
} }
+17
View File
@@ -0,0 +1,17 @@
#include "window.hpp"
#include <iomanip>
#include <iostream>
#include <thread>
void Window::draw_window() {
// for(std::array<int, COL> &row : m_board) { row.fill(0); }
std::cout << "\033[H" << std::flush;
for(int i = 0; i < ROW; i++) {
for(int j = 0; j < COL; j++) {
if(m_board[i][j] == 0) std::cout << std::setw(4) << '.';
else std::cout << std::setw(4) << m_board[i][j];
}
std::cout << "\r\n";
}
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}