Character placement in a grid

Hello, how can I place characters other than the @ in the grid without the grid marking every position of the @? I don't want to keep the @ on every position I run, but I want to be able to keep objects on the grid when I decide to do so. Could you help me or give me some hints please?

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
#include <iostream>
#include <string>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdlib.h>
#include <sstream>
#include <ctime>
#include <vector>
#include <windows.h>
#include <stdio.h>
using namespace std;

constexpr char empty = '.';
constexpr int GridSize = 26;
char grid[GridSize][GridSize];

//function advised by the forum in an article
void ClearScreen()
{
	HANDLE hStdOut;
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	DWORD count;
	DWORD cellCount;
	COORD homeCoords = { 0, 0 };

	hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
	if (hStdOut == INVALID_HANDLE_VALUE) return;

	/*Get the number of cells in the current buffer */
	if (!GetConsoleScreenBufferInfo(hStdOut, &csbi)) return;
	cellCount = csbi.dwSize.X *csbi.dwSize.Y;

	/*Fill the entire buffer with spaces */
	if (!FillConsoleOutputCharacter(
			hStdOut,
			(TCHAR)
			' ',
			cellCount,
			homeCoords, &count
	)) return;

	/*Fill the entire buffer with the current colors and attributes */
	if (!FillConsoleOutputAttribute(
			hStdOut,
			csbi.wAttributes,
			cellCount,
			homeCoords, &count
	)) return;

	/*Move the cursor home */
	SetConsoleCursorPosition(hStdOut, homeCoords);
}

//grid view
void showGrid()
{
	ClearScreen();
	
	for (unsigned int i = 0; i < GridSize; i++)
	{
		for (int j = 0; j < GridSize; j++)
		{
			std::cout << grid[i][j];
			std::cout << " ";
		}

		std::cout << endl;
	}
}

//placement of the @ character
void place(int x, int y)
{
	grid[x][y] = '@';
}

//function to move the @ character with the keyboard arrows
void move()
{
	int x;
	int y;
	srand(time(0));
	memset(grid, '.', sizeof(grid));

	HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
	DWORD NumInputs = 0;
	DWORD InputsRead = 0;

	INPUT_RECORD irInput;

	GetNumberOfConsoleInputEvents(hInput, &NumInputs);

	ReadConsoleInput(hInput, &irInput, 1, &InputsRead);

	switch (irInput.Event.KeyEvent.wVirtualKeyCode)
	{
		case VK_LEFT:
			y = y - 1;
			place(x, y);
			showGrid();
			break;

		case VK_UP:
			x = x - 1;
			place(x, y);
			showGrid();
			break;

		case VK_RIGHT:
			y = y + 1;
			place(x, y);
			showGrid();
			break;

		case VK_DOWN:
			x = x + 1;
			place(x, y);
			showGrid();
			break;
	}

	place(x, y);
}

int main(int argc, char *argv[])
{
        //my infinite loop, which I will correct later.
  	bool running = true;
	while (running)
	{
		move();
	}
}


To clean the screen I used the function that is recommended on the forum in an article :
http://www.cplusplus.com/articles/4z18T05o/
Last edited on
I can't follow your question.
to place something besides @ you need to change the place function, one way to do that is defaults...
void place(int x, int y, char c = '@')
{
grid[x][y] = c;
}

place (3,5, 'x'); //ok puts x at 3,5 (x overrides default)
place (4,6); //ok, places @ at 4,6 using default

if you want to know where all the @s are @ you need to keep track of it or search for them; you can also use a sparse matrix concept if you only store @s and .s then a container that only holds the coordinates of @s is sufficient.

if none of that helps, try asking the question again with careful wording and explains as to what you want.
Hello, thank you for the answer, sorry I didn't have time to come back for acknowledgement. I understood your reasoning and your questions. I don't know how to formulate my question. If for example I want to place horizontal bars with _ or vertical | in the grid and the @ moves in between. Do I have to place my bars at grid initialization? But with this line: memset(grid, '.', sizeof(grid)); I don't know how to change the grid that's full of dots. Is it possible to place bars in the grid with the same grid initialization?
memset is a dumb byte-filler. that is, you give it a block of bytes, and it will set them all to the same value -- it is most useful for setting large images/frames in graphics to black or white (0 or 255), clearing out a large block of simple data types (say setting 1 million doubles or integers to zero), and things like that. It cannot be used to fill in data in any sort of pattern, like every other byte or whatnot.

You can set your grid a number of ways..
-- you can use memset to get everything to a default value, then set a few specific cells directly to what you want, using for example the code I gave you before you can call the new place function I wrote eg place(x,y,'|');

-- you can set the whole thing directly, with a large assignment operation, but you are talking 26x26 characters typed in your code to set it up.

-- you could load the grid from a text file.

what I personally would do is dump your 26x26 grid in a print statement to a text file or the screen. If you dump to screen, you can redirect the output to a text file with programname > filename at the command line.
open the text file, and edit the grid carefully like you want it.
save it and load it into grid from the text file OR use that to set up your big initialization statement. the file is really the better option.

a warning: 2-d arrays as you have them can be collapsed to 1-d for memset etc. 2-d arrays that are made from ** do not work that way, each row is in a different block in ram and not all consolidated. If you forget this, and decide to make dynamic 2-d constructs, you will have serious issues.

Last edited on
Hi, thanks again for the answer. I made this code to try but I can't get a character movement that represents the movement of the computer independently of the movements that the user makes. I can place X and Y (computer) and the user's @ but I can't place the computer's characters in the same grid with a function that allows the user to move the @ anyway. How do I do it ?

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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <iostream>
#include <string>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdlib.h>
#include <sstream>
#include <ctime>
#include <vector>
#include <windows.h>
#include <stdio.h>
using namespace std;

constexpr char empty = '.';
constexpr int GridSize = 26;
char grid[GridSize][GridSize];
//Random number generating function
int random(int from, int to)
{
	return rand() % (to - from + 1) + from;
}

void ClearScreen()
{
	HANDLE hStdOut;
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	DWORD count;
	DWORD cellCount;
	COORD homeCoords = { 0, 0 };

	hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
	if (hStdOut == INVALID_HANDLE_VALUE) return;

	/*Get the number of cells in the current buffer */
	if (!GetConsoleScreenBufferInfo(hStdOut, &csbi)) return;
	cellCount = csbi.dwSize.X *csbi.dwSize.Y;

	/*Fill the entire buffer with spaces */
	if (!FillConsoleOutputCharacter(			hStdOut,
			(TCHAR)
			' ',
			cellCount,
			homeCoords, &count
	)) return;

	/*Fill the entire buffer with the current colors and attributes */
	if (!FillConsoleOutputAttribute(			hStdOut,
			csbi.wAttributes,
			cellCount,
			homeCoords, &count
	)) return;

	/*Move the cursor home */
	SetConsoleCursorPosition(hStdOut, homeCoords);
}
//function to calculate the distance between characters
double distanceBetweenTwoPoints(double x, double y, double a, double b)
{
	return sqrt(pow(x - a, 2) + pow(y - b, 2));
}
//grid view
void showGrid()
{
	ClearScreen();

	for (unsigned int i = 0; i < GridSize; i++)
	{
		for (int j = 0; j < GridSize; j++)
		{
			std::cout << grid[i][j];
			std::cout << " ";
		}

		std::cout << endl;
	}
}
//function to place characters, modified as suggested
void placement(int x, int y, char c = '@')
{
	grid[x][y] = c;
}
//function to move the user's characters that I can't separate from the computer's movements
void move()
{
	srand(time(0));
	int x;
	int y;
	double x_comput;
	double y_comput;
	double distarray[8];
	double dist;
	double dista;
	double distb;
	int xX;
	int yY;

	int xxX;
	int yyY;

	memset(grid, '.', sizeof(grid));

	HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
	DWORD NumInputs = 0;
	DWORD InputsRead = 0;

	INPUT_RECORD irInput;

	GetNumberOfConsoleInputEvents(hInput, &NumInputs);

	ReadConsoleInput(hInput, &irInput, 1, &InputsRead);

	switch (irInput.Event.KeyEvent.wVirtualKeyCode)
	{
		case VK_LEFT:
			y = y - 1;

			placement(x, y);
			//test for trying to keep a minimum distance between characters
			do { 	xX = random(1, 25);
				yY = random(1, 25);
				dist = distanceBetweenTwoPoints(x, y, xX, yY);
			} while (dist > 6 && dist > 2);
			do { 	xxX = random(1, 25);
				yyY = random(1, 25);
				dista = distanceBetweenTwoPoints(xX, yY, xxX, yyY);
				distb = distanceBetweenTwoPoints(x, y, xxX, yyY);
			} while (dista > 6 && distb > 6 && dista > 2 && distb > 2);

			placement(xX, yY, 'X');
			placement(xxX, yyY, 'Y');
			showGrid();
			break;

		case VK_UP:
			x = x - 1;
			placement(x, y);
			//test for trying to keep a minimum distance between characters
			do { 	xX = random(1, 25);
				yY = random(1, 25);
				dist = distanceBetweenTwoPoints(x, y, xX, yY);
			} while (dist > 6 && dist > 2);
			do { 	xxX = random(1, 25);
				yyY = random(1, 25);
				dista = distanceBetweenTwoPoints(xX, yY, xxX, yyY);
				distb = distanceBetweenTwoPoints(x, y, xxX, yyY);
			} while (dista > 6 && distb > 6 && dista > 2 && distb > 2);

			placement(xX, yY, 'X');
			placement(xxX, yyY, 'Y');
			showGrid();
			break;

		case VK_RIGHT:
			y = y + 1;
			placement(x, y);
			//test for trying to keep a minimum distance between characters
			do { 	xX = random(1, 25);
				yY = random(1, 25);
				dist = distanceBetweenTwoPoints(x, y, xX, yY);
			} while (dist > 6 && dist > 2);
			do { 	xxX = random(1, 25);
				yyY = random(1, 25);
				dista = distanceBetweenTwoPoints(xX, yY, xxX, yyY);
				distb = distanceBetweenTwoPoints(x, y, xxX, yyY);
			} while (dista > 6 && distb > 6 && dista > 2 && distb > 2);

			placement(xX, yY, 'X');
			placement(xxX, yyY, 'Y');
			showGrid();
			break;

		case VK_DOWN:
			x = x + 1;
			placement(x, y);
			//test for trying to keep a minimum distance between characters
			do { 	xX = random(1, 25);
				yY = random(1, 25);
				dist = distanceBetweenTwoPoints(x, y, xX, yY);
			} while (dist > 6 && dist > 2);
			do { 	xxX = random(1, 25);
				yyY = random(1, 25);
				dista = distanceBetweenTwoPoints(xX, yY, xxX, yyY);
				distb = distanceBetweenTwoPoints(x, y, xxX, yyY);
			} while (dista > 6 && distb > 6 && dista > 2 && distb > 2);

			placement(xX, yY, 'X');
			placement(xxX, yyY, 'Y');
			showGrid();
			break;
	}

	placement(x, y);
	//display of distances to check at runtime
	cout << dist << endl;
	cout << dista << endl;
	cout << distb << endl;

}

int main(int argc, char *argv[])
{
	bool running = true;
	while (running)
	{
		move();
	}
}
Last edited on
how do you want the computer to move?
is it tightly coupled (the human moves once, the computer moves once)
or not (the computer moves constantly even if the human got up and went to the restroom)
and if the computer moves like this, how many per second?

some things:
move initializes the grid every time. I believe you want to initialize it once and then after that move and re-draw it.
so if you have
...
.@.
...
and move up once, swap the middle dot top row and the @ and draw the new board:
.@.
...
...

etc.

look at a library to move the cursor. Standard c++ can't do it easily, but there are things like gotoxy(x,y) that let you move the write cursor backwards, allowing you to redraw the board cleanly instead of clearscreen/draw or write blank lines and draw type approaches.

if you are moving the computer on a timed rate regardless of human's behavior you may need a thread to do it.
Hello, that's nice of you but now I understand the principle I think but not what you advise me to do. Then I wanted the computer to move independently of the human (whether he goes to the restroom or take a shower lol). I have to initialize the grid at the start and when I move, the previous position has to be reset to '.' ? And for the function that makes the computer move I do the same thing?
Taking into account the position of both computer and human?
Right?

And for gotoxy(x,y) is this what you're talking about :

http://www.cplusplus.com/forum/beginner/68989/
Last edited on
swapping it will set the previous position to a '.' (assuming you move from @ to .) The computer one does the same as the human except instead of getting it from keyboard, it generates it somehow. You should maybe make a function that does these parts that both sides can use.

I think so. gotoxy is an example -- there are a couple more -- of nonstandard library tools that let you move the cursor around easier. that or something like it will make life easier here.

if the computer is moving without the human being around, you need to learn at least the most basic threading code, so you can have that process running constantly in the 'background' ...
this is going to take a bit if you have not yet done any threading, so be prepared to take a side study for a few days to get it down. You only need the basics, but you do need to watch out for computer and human moving the same tiles at the same time issues and handle that somehow.

Last edited on
Hi, I managed to do it like this, but I don't really keep the tasks separate. That doesn't really help me. Where's my mistake?

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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <iostream>
#include <string>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdlib.h>
#include <sstream>
#include <ctime>
#include <vector>
#include <windows.h>
#include <stdio.h>
#include <thread>
#include <vector>
using namespace std;

constexpr char empty = '.';
constexpr int GridSize = 26;
char grid[GridSize][GridSize];
std::vector<double> coord_x;
std::vector<double> coord_y;
//Random number generating function
int random(int from, int to)
{
	return rand() % (to - from + 1) + from;
}

void ClearScreen()
{
	HANDLE hStdOut;
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	DWORD count;
	DWORD cellCount;
	COORD homeCoords = { 0, 0 };

	hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
	if (hStdOut == INVALID_HANDLE_VALUE) return;

	/*Get the number of cells in the current buffer */
	if (!GetConsoleScreenBufferInfo(hStdOut, &csbi)) return;
	cellCount = csbi.dwSize.X *csbi.dwSize.Y;

	/*Fill the entire buffer with spaces */
	if (!FillConsoleOutputCharacter(
			hStdOut,
			(TCHAR)
			' ',
			cellCount,
			homeCoords, &count
	)) return;

	/*Fill the entire buffer with the current colors and attributes */
	if (!FillConsoleOutputAttribute(
			hStdOut,
			csbi.wAttributes,
			cellCount,
			homeCoords, &count
	)) return;

	/*Move the cursor home */
	SetConsoleCursorPosition(hStdOut, homeCoords);
}

void showGrid()
{
	ClearScreen();
	//grid view
	for (unsigned int i = 0; i < GridSize; i++)
	{
		for (int j = 0; j < GridSize; j++)
		{
			std::cout << grid[i][j];
			std::cout << " ";
		}

		std::cout << endl;
	}
}

void place(int x, int y)
{
	grid[x][y] = '@';
	showGrid();
}

void ComputerPlace(int x, int y)
{
	coord_x.push_back(x);
	coord_y.push_back(y);
	grid[x][y] = 'X';
	showGrid();
}

double distanceBetweenTwoPoints(double x, double y, double a, double b)
{
	return sqrt(pow(x - a, 2) + pow(y - b, 2));
}

void ComputerMove(int x, int y)
{
	int xX;
	int yY;
	int test_x;
	int test_y;
	unsigned dist;
	srand(time(0));
	do {
		test_x = random(1, 25);
		test_y = random(1, 25);
		dist = distanceBetweenTwoPoints(x, y, test_x, test_y);
	} while (dist > 0.5 && dist < 1 && grid[test_x][test_y] == '.');
	xX = test_x;
	yY = test_y;
	ComputerPlace(xX, yY);
}

void spawnThreads(int n)
{
	int x = 1;
	int y = 1;
	int j;
	int prec_coord_x;
	int prec_coord_y;
	std::vector<thread> PlayerThreads(n);
	std::vector<thread> CompuerThreads(n);
	// spawn n threads:
	for (int i = 0; i < n; i++)
	{
		j = i;
		if (j > 0)
		{
			prec_coord_x = coord_x[j - 1];
			prec_coord_y = coord_y[j - 1];
			grid[prec_coord_x][prec_coord_y] = '.';

		}

		HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
		DWORD NumInputs = 0;
		DWORD InputsRead = 0;

		INPUT_RECORD irInput;

		GetNumberOfConsoleInputEvents(hInput, &NumInputs);

		ReadConsoleInput(hInput, &irInput, 1, &InputsRead);

		switch (irInput.Event.KeyEvent.wVirtualKeyCode)
		{
			case VK_LEFT:
				grid[x][y] = '.';
				y = y - 1;
				PlayerThreads[i] = thread(place, x, y);
				PlayerThreads[i].join();
				CompuerThreads[j] = thread(ComputerMove, x, y);
				CompuerThreads[j].join();

				break;

			case VK_UP:
				grid[x][y] = '.';
				x = x - 1;
				PlayerThreads[i] = thread(place, x, y);
				PlayerThreads[i].join();
				CompuerThreads[j] = thread(ComputerMove, x, y);
				CompuerThreads[j].join();
				break;

			case VK_RIGHT:
				grid[x][y] = '.';
				y = y + 1;
				PlayerThreads[i] = thread(place, x, y);
				PlayerThreads[i].join();
				CompuerThreads[j] = thread(ComputerMove, x, y);
				CompuerThreads[j].join();
				break;

			case VK_DOWN:
				grid[x][y] = '.';
				x = x + 1;
				PlayerThreads[i] = thread(place, x, y);
				PlayerThreads[i].join();
				CompuerThreads[j] = thread(ComputerMove, x, y);
				CompuerThreads[j].join();
				break;

		}
	}
}

int main()
{
	memset(grid, '.', sizeof(grid));
	spawnThreads(5000);
}
Last edited on
Hello here is my wish is that the user moves a character in the grid through various obstacles and that the computer follows the user. I started studying threads, this is the code that makes the computer get closer to a fixed point by avoiding obstacles. I'm not sure I'll be able to do my multithreading with it. Do you have any ideas?

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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <string>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdlib.h>
#include <fstream>
#include <vector>
#include <windows.h>
#include <stdio.h>
#include <ctime>
using namespace std;

int x;
int y;
int homeX;
int homeY;
int caveX;
int caveY;
int bisX;
int bisY;
int trisX;
int trisY;
char c;
int seed;
int turns;

int random(int from, int to)
{
	return rand() % (to - from + 1) + from;
}

void ClearScreen()
{
	HANDLE hStdOut;
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	DWORD count;
	DWORD cellCount;
	COORD homeCoords = { 0, 0 };

	hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
	if (hStdOut == INVALID_HANDLE_VALUE) return;

	/*Get the number of cells in the current buffer */
	if (!GetConsoleScreenBufferInfo(hStdOut, &csbi)) return;
	cellCount = csbi.dwSize.X *csbi.dwSize.Y;

	/*Fill the entire buffer with spaces */
	if (!FillConsoleOutputCharacter(
			hStdOut,
			(TCHAR)
			' ',
			cellCount,
			homeCoords, &count
	)) return;

	/*Fill the entire buffer with the current colors and attributes */
	if (!FillConsoleOutputAttribute(
			hStdOut,
			csbi.wAttributes,
			cellCount,
			homeCoords, &count
	)) return;

	/*Move the cursor home */
	SetConsoleCursorPosition(hStdOut, homeCoords);
}

void displayPosition(int x, int y)
{
	int dx;
	int dy;

	for (dy = 10; dy >= 0; dy--)
	{
		cout << "\n-----------------------\n";

		for (dx = 0; dx <= 11; dx++)
		{
			if ((x == dx) && (y == dy))
			{
				cout << "|x";
			}
			else
			if ((homeX == dx) && (homeY == dy))
			{
				cout << "|1";
			}
			else
			if ((bisX == dx) && (bisY == dy))
			{
				cout << "|2";
			}
			else
			if ((trisX == dx) && (trisY == dy))
			{
				cout << "|3";
			}
			else if ((caveX == dx) && (caveY == dy))
			{
				cout << "|C";
			}
			else
			{
				cout << "| ";
			}
		}
	}

	cout << "\n-----------------------\n\n";

}

int main()

{
	ClearScreen();
	std::ofstream log("results.txt", std::ios_base::app | std::ios_base::out);
	srand(time(0));

	x = random(1, 8);
	y = 1;
	homeX = random(3, 8);
	homeY = random(3, 8);
	bisX = random(3, 8);
	bisY = random(3, 8);
	trisX = random(3, 8);
	trisY = random(3, 8);
	caveX = random(8, 10);
	caveY = random(8, 10);

	displayPosition(x, y);
	Sleep(100);
	ClearScreen();
	int x2;
	int y2;
	double distance1;
	double distance2;
	double distance3;
	double distance4;
	double distance5;
	int id_move;

	bool trying;

	// -1 1 => 21% (4747)
	// 0 1 => 26%  (5685)
	// 1 -1 => 21%  (4745)
	// 1 0 => 15%  (3288)
	// 1 1 => 11%  (2382)
	// -1 0 => 6%  (1338)
	// -1 -1 => 0% (5)
	int dx[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
	int dy[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
	
	for (int i = 0; i < 50; i++)
	{
		do {
			distance1 = sqrt(((x - caveX) *(x - caveX)) + ((y - caveY) *(y - caveY)));

			for (int k = 0; k <= 7; k++)
			{
				x2 = x + (dx[k]);
				y2 = y + (dy[k]);
				distance2 = sqrt(((x2 - caveX) *(x2 - caveX)) + ((y2 - caveY) *(y2 - caveY)));
				distance3 = sqrt(((x2 - homeX) *(x2 - homeX)) + ((y2 - homeY) *(y2 - homeY)));
				distance4 = sqrt(((x2 - bisX) *(x2 - bisX)) + ((y2 - bisY) *(y2 - bisY)));
				distance5 = sqrt(((x2 - trisX) *(x2 - trisX)) + ((y2 - trisY) *(y2 - trisY)));
				if (distance2 < distance1 && distance3 > 0.5 && distance4 > 0.5 && distance5 > 0.5)
				{
					if (x2 > 0 && x2 < 10 && y2 > 0 && y2 < 10)
					{
						id_move = id_move + 1;
						trying = true;
						x = x2;
						y = y2;
						displayPosition(x, y);

						cout << id_move << "(" << x << "-" << y << ")" << ",obst1(" << homeX << "-" << homeY << ")" << ",obst2(" << bisX << "-" << bisY << ")" << ",obst3(" << trisX << "-" << trisY << "), " << distance1 << endl;
	
						Sleep(100);
						ClearScreen();
					}
					else
					{
						trying = false;
					}
				}
				else if (distance2 > distance1)
				{
					trying = false;
				}
			}
		} while (trying);

	}

	return 0;
}
Last edited on
I haven't tried gotoxy yet, I haven't taken the time, sorry.
there isnt any need if it does what you want without it. its nonstandard anyway.
do you still have a question, or does it work now? If it works, SetConsoleCursorPosition looks to be identical to gotoxy.
Last edited on
Hello the two separate functions work as I want. But I have to be able to make them work simultaneously. I'm going to deepen the multithreading and I'll come back to you if I can't do it. And to ask questions just in case. I'll try to make do with std::async. Thank you for your attention.
Last edited on
Registered users can post here. Sign in or register to post.