Can't seem to ever get the right movement function for 2048
May 24, 2019 at 10:32pm UTC
Exactly as title says, can't seem to do it. Here is my code, if you want to try to help. I deleted my old movement function. Forgive me for my names.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
#include <iostream>
#include<iomanip>
#include<ctime>
#include<unistd.h>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
void griddyBois(int a[4][4]);
void stupidStarters(int a[4][4]);
bool check4win(int a[4][4]);
void newNumbers(int a[4][4]);
void moveyBois(int a[4][4], char b);
bool checkLegality(int a[4][4], char b);
int main()
{
int grid[4][4];
bool winner;
char yee;
cout << "Welcome to 2048." << endl << "Navigate the blocks by using WASD." << endl << "W = up, A = left, S = down, D = right." << endl;
stupidStarters(grid);
griddyBois(grid);
cin >> yee;
moveyBois(grid, yee);
griddyBois(grid);
cin >> yee;
moveyBois(grid, yee);
griddyBois(grid);
return 0;
}
bool checkLegality(int a[4][4], char b)
{
}
void moveyBois(int a[4][4], char b)
{
}
void newNumbers(int a[4][4])
{
double x;
double y;
double kindOfAnnoyed;
int yeetus;
int yotus;
for (int i = 0; i < 1;)
{
srand(time(0));
x = (rand() % 4);
y = (rand() % 4);
yeetus = (int )x;
yotus = (int )y;
if (a[yeetus][yotus] == 0)
{
kindOfAnnoyed = (rand() % 100) + 1;
if (kindOfAnnoyed <= 70)
{
a[yeetus][yotus] = 2;
i++;
}
if (kindOfAnnoyed > 70)
{
a[yeetus][yotus] = 4;
i++;
}
}
}
}
void stupidStarters(int a[4][4])
{
for (int i = 0; i < 4;)
{
a[i][0] = 0;
i++;
}
for (int i = 0; i < 4;)
{
a[i][1] = 0;
i++;
}
for (int i = 0; i < 4;)
{
a[i][2] = 0;
i++;
}
for (int i = 0; i < 4;)
{
a[i][3] = 0;
i++;
}
a[0][0] = 2;
a[0][3] = 2;
}
bool check4win(int a[4][4])
{
for (int i = 0; i < 4;)
{
if (a[i][0] == 2048)
{
return true ;
i = 4;
}
else
i++;
}
for (int i = 0; i < 4;)
{
if (a[i][1] == 2048)
{
return true ;
i = 4;
}
else
i++;
}
for (int i = 0; i < 4;)
{
if (a[i][2] == 2048)
{
return true ;
i = 4;
}
else
i++;
}
for (int i = 0; i < 4;)
{
if (a[i][3] == 2048)
{
return true ;
i = 4;
}
else
i++;
}
}
void griddyBois(int a[4][4])
{
cout << "+------+------+------+------+" << endl;
cout << "| " << setw(4) << a[0][0] << " | " << setw(4) << a[1][0] << " | " << setw(4) << a[2][0] << " | " << setw(4) << a[3][0] << " |" << endl;
cout << "+------+------+------+------+" ;
cout << "\n| " << setw(4) << a[0][1] << " | " << setw(4) << a[1][1] << " | " << setw(4) << a[2][1] << " | " << setw(4) << a[3][1] << " |" << endl;
cout << "+------+------+------+------+" ;
cout << "\n| " << setw(4) << a[0][2] << " | " << setw(4) << a[1][2] << " | " << setw(4) << a[2][2] << " | " << setw(4) << a[3][2] << " |" << endl;
cout << "+------+------+------+------+" ;
cout << "\n| " << setw(4) << a[0][3] << " | " << setw(4) << a[1][3] << " | " << setw(4) << a[2][3] << " | " << setw(4) << a[3][3] << " |" << endl;
cout << "+------+------+------+------+" << endl;
}
May 25, 2019 at 10:43pm UTC
Hello broon,
I deleted my old movement function.
You can start by creating a reply and showing what you did.
DO NOT change your original post this will confuse people in the future.
You have presented an empty function which implies to most that you want someone to writ it for you. This is not likely to happen although you may get a generic function to give you an idea.
Better to post what you did and receive input about what is wrong.
Something your program would benefit from:
1 2 3
constexpr size_t MAXSIZE{ 4 };
void griddyBois(int a[MAXSIZE][MAXSIZE]);
By using "MAXSIZE" anywhere you refer to the size of the array and in for loops, should you want to change the size of the array you only have one place to do this. Should the array change shape you can use "MAXROW" and "MAXCOL". This save a lot of work having to check the whole program to make changes.
Hope that helps,
Andy
May 26, 2019 at 3:51pm UTC
@broon..
I changed your code a bit, following Handy Andy's advice.
( Try changing the 4 on line 11, to a higher number. )
Also shortened it a little. I did not add the coding to slide the numbers in the direction of the input, as I would like to see what ideas you come up with. I did add in check for legal moves, but didn't do anything other than inform the user it was a bad input, since the movement of the numbers is not yet implemented.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
#include <iostream>
#include <iomanip>
#include <ctime>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;
const int SIZE = 4;
void griddyBois(int grid[SIZE][SIZE]);
void stupidStarters(int grid[SIZE][SIZE]);
bool check4win(int grid[SIZE][SIZE]);
void newNumbers(int grid[SIZE][SIZE]);
void moveyBois(int grid[SIZE][SIZE], char b);
bool checkLegality(char b);
int main()
{
srand((unsigned )time(0));
int grid[SIZE][SIZE];
bool winner = false ;
bool legal = false ;
char yee;
cout << "Welcome to 2048." << endl << "Navigate the blocks by using WASD." << endl << "W = up, A = left, S = down, D = right." << endl;
stupidStarters(grid);
do
{
griddyBois(grid);
cin >> yee;
legal = checkLegality(yee);
if (!legal)
cout << "Bad input. Try a different key. W, A, S or D" << endl;
winner = check4win(grid);
newNumbers(grid);
}while (!winner);
return 0;
}
bool checkLegality(char b)
{
char Legal_Keys[4] = {'W' ,'A' ,'S' ,'D' };
bool ok = false ;
for (int i=0;i<4;i++)
{
if (toupper(b) == Legal_Keys[i])
ok = true ;
}
return ok;
}
void moveyBois(int grid[SIZE][SIZE], char b)
{
}
void newNumbers(int grid[SIZE][SIZE])
{
int x;
int y;
int kindOfAnnoyed;
do
{
x = (rand() % SIZE);
y = (rand() % SIZE);
}while (grid[x][y] != 0);
kindOfAnnoyed = (rand() % 100) + 1;
if (kindOfAnnoyed <= 70)
{
grid[x][y] = 2;
}
else
{
grid[x][y] = 4;
}
}
void stupidStarters(int grid[SIZE][SIZE])
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
grid[i][j] = 0;
}
grid[0][0] = 2;
grid[SIZE-1][0] = 2;
}
bool check4win(int grid[SIZE][SIZE])
{
bool ok = false ;
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (grid[i][j] == 2048)
{
ok = true ;
}
}
}
return ok;
}
void griddyBois(int grid[SIZE][SIZE])
{
string bar = "+------" ;
for (int x=0;x<SIZE;x++)
cout << bar;
cout << "+" << endl;
for (int y=0;y<SIZE;y++)
{
cout << "| " ;
for (int x=0;x<SIZE;x++)
{
cout << setw(4) << grid[y][x] << " | " ;
}
cout << endl;
for (int x=0;x<SIZE;x++)
cout << bar;
cout << "+" << endl;
}
}
Last edited on May 26, 2019 at 4:13pm UTC
May 26, 2019 at 7:35pm UTC
So... my deleted code wasn't entirely deleted for no reason. I had to do it, because it made it too long to fit in a post. Here's the pastebin for it:
https://pastebin.com/6Q4K9b6s
Though, I warn you... it'll make your head spin. There's just so many if statements and all that jazz.
If either of you guys could help with that... it'd be godsend. Also, thank you for the condensed code, whitenite. Also, the whole constant thing would forever be 4, as the game board will always be 4x4. Thanks for the suggestion though, Andy.
Topic archived. No new replies allowed.