chess move in an array

Pages: 12
closed account (o35DwA7f)
Hello everybody. Isn't this suppose to find a kings move in an array? Increased and decreased stands for +1 , -1. Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  for (int i = decreasedrow; i < increasedrow; i++)
	{
		for (int j = decreasedcol; j < increasedcol; j++)
		{
			if (i < 0 || j < 0)
			{
				cout << "out of bounds\n";
			}
			else if (i > length || j > length)
			{
				cout << "outofbounds\n";
			}
			else if (array[i][j] == number)
			{
				array[i][j];
			}
	
		}
	}
Last edited on
it has no logic for finding the best move, nor if the move is legal (square occupied, square is checked, etc). It also does nothing if not out of bounds, variable; is a do nothing statement (line 15). So all this code does is say out of bounds, and its useless on that part too, as it does not say WHICH coordinates are out of bounds, just that some of them are. I am not sure where you got this code but it seems to be incomplete, or incorrect to me.
Last edited on
closed account (o35DwA7f)
I am not trying to find the best move. Imagine a 2d array. I want to check the boxes surrounding the box i currently choose. If they have the same number i want it to go to my last else if statement. Line 15 means nothing. It will be a return statement i think and it will set the number to 0. I don't understand why it does nothing if not out of bounds? What do you mean? Thank you jonnin.
Hello cppmasternoob,

I would have to say the the answer to your question is 42 https://www.youtube.com/watch?v=aboZctrHfK8

In addition to what jonnin said the whole bit of code is out of context.

There is no way to know what "decreasedrow" and "increasedrow" is. How they are defined and what value they might have. Also "number" and the array.

With out the rest of the code it is hard to tell what the for loops are trying to do. That is in addition to the fact the the code does not work as jonnin said.

Andy
closed account (o35DwA7f)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;
const int length{ 9 };
void showthesudoku(int array[][length]);// just prints the sudoku
int randomnumber(); //generates a random number
int randomrow(); //the same -1 because arrays, to get the coordinates
int randomcol(); //the same just to add a delay
void thanksforplaying(); //prints  thank you
void thegame();//another not important menu
void humaninput(int array[][length]);//input with cin
void machineinput(int array[][length]);//input with random numbers. The idea for this brain fart is, that it could populate my array so i could play the game.Also a help tool if you're stuck.
bool doesitfit(int array[][length]);//forces the rules
void calculations(int array[][length]);//Just adds rows,col and 3x3.I know it is redudant but i imagine when i turn
void reset();								  //this, into a windowed mode you can click a box and it will tell you the sum in rows col and 3x3
int kingsmove(int array[][length]);
void counter();
struct sudoku
{
	int precious[length][length]{ 0 };
	int row{ 0 };
	int col{ 0 };
	int number{ 0 };
	int dummy[length][length]{ 0 }; //i used this because somehow it printed the numbers that didnt fullfill
	char move;                      //the conditions on my original array, very annoying
	int additionrow{ 0 };
	int additioncol{ 0 };
	int addition3x3box{ 0 };
	int countfilledsquares{ 0 };
}sud;

int main()
{
	thegame();
	thanksforplaying();
	return 0;
}

void thegame()
{
	reset();

	while (1)
	{
		cout << "press c to choose numbers or m for the machine to do it for you or q to quit\n";
		cin >> sud.move;
		if (sud.move == 'c')
		{
			humaninput(sud.precious);
			if (sud.number != 0)
			{
				sud.precious[sud.row][sud.col] = sud.number;
			}
			counter();
			showthesudoku(sud.precious);
		}
		else if (sud.move == 'm')
		{
			machineinput(sud.precious);
			if (sud.number != 0)
			{
				sud.precious[sud.row][sud.col] = sud.number;
			}
			counter();
			showthesudoku(sud.precious);
		}
		else if (sud.move == 'q')
		{
			break;
		}
		else continue;
	}
	return;
}

bool doesitfit(int array[][length])
{	//spent about a lot of hours here, for this thing
	//checks every row for the same number
	for (int j = 0, i = sud.row; j < length; j++)
	{											   //|not needed|    | this one | excludes the number we choose
		if (((sud.number == sud.precious[i][j]) && ((sud.row == i && sud.col != j) && sud.precious[i][j] != 0)))
		{

			cout << "not possible, you already have one in the same line\n";
			reset();

			return true;// not needed
		}

	}

	//checks every column for the same number
	for (int i = 0, j = sud.col; i < length; i++)
	{
		if (((sud.number == sud.precious[i][j]) && ((sud.row != i && sud.col == j) && sud.precious[i][j] != 0)))
		{
			cout << "not possible, you have one in the same column\n";
			reset();
			return true;
		}
	}

	//this one i hate. try to exclude the same number in a 3x3 box spend 6 or more hours here. stop laughing
	int resizedrow = (sud.row / 3) * 3;
	int resizedcolumn = (sud.col / 3) * 3;
	for (int i = resizedrow; i < (resizedrow + 3); i++)
	{
		for (int j = resizedcolumn; j < (resizedcolumn + 3); j++)
		{
			//Always checks for 4 boxes. the diagonal positions if you choose middle box.A 2x2 if you choose corner box and so on
			if (((sud.number == sud.precious[i][j]) && ((sud.row != i && sud.col != j) && sud.precious[i][j] != 0)))
			{
				cout << "you cant, i wish you could\n";
				reset();
				return true;
			}

		}
	}

	calculations(sud.precious);
	if (sud.additionrow > 45)
	{
		cout << "you exceed the max sum allowed in this row\n";
		reset();
		return true;
	}
	else if (sud.additioncol > 45)
	{
		cout << "you exceed the max sum allowed in this column\n";
		reset();
		return true;
	}
	else if (sud.addition3x3box > 45)
	{
		cout << "you exceed the max sum allowed in this box\n";
		reset();
		return true;
	}
	//absolutely worth it
	return false;
}

void calculations(int array[][length])
{
	sud.additionrow = 0;//calculates the sum of all the numbers in the row the user chose, from his row input
	for (int i = sud.row, j = 0; j < length; j++)
	{
		sud.additionrow += sud.precious[i][j];
	}
	sud.additionrow += sud.dummy[sud.row][sud.col];

	sud.additioncol = 0;//calculates the sum of all the numbers in the column the user chose, from his column selection
	for (int i = 0, j = sud.col; i < length; i++)
	{
		sud.additioncol += sud.precious[i][j];
	}
	sud.additioncol += sud.dummy[sud.row][sud.col];


	sud.addition3x3box = 0;//calculates the sum of all the numbers in a 3x3square with magic. learned that here, ty.
	int resizedrow = (sud.row / 3) * 3;     //3 possible outcomes each to get the 3x3box.(0,3,6)^2=1 to 9 possible 3x3box 
	int resizedcolumn = (sud.col / 3) * 3;// i know what it does.I don't know what are the principles behind it.
	//anyway it is still amazing!!
	for (int i = resizedrow; i < (resizedrow + 3); i++)
	{
		for (int j = resizedcolumn; j < (resizedcolumn + 3); j++)
		{
			sud.addition3x3box += sud.precious[i][j];
		}
	}
	sud.addition3x3box += sud.dummy[sud.row][sud.col];

	return;
}

void humaninput(int array[][length])
{
	while (true)
	{
		cout << "enter row, column and your number\n";
		cin >> sud.row >> sud.col >> sud.number;//if i put a letter all hell breaks loose

		if (sud.row < 0 || sud.row >= length)
		{
			cout << "out of bounds row\n";
		}
		else if (sud.col < 0 || sud.col >= length)
		{
			cout << "out of bounds column\n";
		}
		else if (sud.number < 0 || sud.number > length)
		{
			cout << "out of bounds number\n";
		}
		else if (sud.precious[sud.row][sud.col] != 0)
		{
			cout << "already assigned\n";
		}
		else if (doesitfit(sud.precious) != 0);
		else if (kingsmove(sud.precious) != 0);

		else break;
	}
	sud.dummy[sud.row][sud.col] = sud.number;

	return;// i was trying to return sud.dummy[][]. Wrongly thought it's out of scope and gone when i exit but struct are good
}

void machineinput(int array[][length])
{
	while (1)
	{
		reset();
		sud.row = randomrow();
		sud.col = randomcol();
		sud.number = randomnumber();
		if (sud.precious[sud.row][sud.col] != 0);
		else if (doesitfit(sud.precious) != 0);
		else if ((kingsmove(sud.precious)) != 0);
		else break;
	}
	sud.dummy[sud.row][sud.col] = sud.number;
	return;
}

void counter()
{
	sud.countfilledsquares = 0;
	for (int i = 0; i < length; i++)
	{
		for (int j = 0; j < length; j++)
		{
			if (sud.precious[i][j] != 0)
			{
				sud.countfilledsquares++;
			}
		}
	}
	cout << sud.countfilledsquares << "\n";
}

int kingsmove(int array[][length])
{
	int k = 0;
	int increasedrow = 0;
	int increasedcol = 0;
	int decreasedrow = 0;
	int decreasedcol = 0;
	increasedrow = sud.row + 1;
	increasedcol = sud.col + 1;
	decreasedrow = sud.row - 1;
	decreasedcol = sud.col - 1;
	for (int i = decreasedrow; i < increasedrow; i++)
	{
		for (int j = decreasedcol; j < increasedcol; j++)
		{
			if (i < 0 || j < 0)
			{
				cout << "less than0\n";
			}
			else if (i > 8 || j > 8)
			{
				cout << "morethan8\n";
			}
			else if (sud.precious[i][j] == sud.number)
			{
				sud.precious[i][j];
				k = 1;
				sud.number = 0;
				sud.row = 0;
				sud.col = 0;
				return k;
			}
	
		}
	}

	return k;
}

int randomnumber()//the numbers follow a pattern unfortunately, i thought the delay will fix it
{
	unsigned int k = 0;
	k = (rand() % 9) + 1;
	return k;
}

int randomrow()
{
	unsigned int l = 0;
	l = rand() % 9;
	return l;
}

int randomcol()
{
	unsigned int m = 0;
	m = rand() % 9;
	return m;
}

void showthesudoku(int array[][length])
{
	for (int i = 0; i < length; i++)
	{
		for (int j = 0; j < length; j++)
		{
			cout << setw(3) << array[i][j];
		}cout << "\n";
	}
}

void reset()
{
	sud.row = 0;
	sud.col = 0;
	sud.number = 0;
	sud.dummy[sud.row][sud.col] = 0;
	return;
}

void thanksforplaying()
{
	cout << "thanks for playing";
}

Sorry about that but you asked for it. The thing we talk about is in function kingsmove
Last edited on
array[i][j];
Well, I can't see that line doing much.
I don't understand why it does nothing if not out of bounds? What do you mean? Thank you jonnin.

1
2
3
4
5
6
7
8
9
10
11
12
if (i < 0 || j < 0)
			{
				cout << "out of bounds\n";
			}
			else if (i > length || j > length)
			{
				cout << "outofbounds\n";
			}
			else if (array[i][j] == number)
			{
				array[i][j];
			}


translated:
if something, say out of bounds
else if something else, say outofbounds all jumbled up
else ... what? it does nothing.
array[i][j]; //what does this do exactly?!
^^
this is effective as saying
int x = 3;
x; //what does this do, exactly?
hint. It does nothing that you care about. Technically it evaluates if x is true or false based off 0 is false, anything else is true, and then it discards that bool result. But this is likely cut out by a smart compiler because it has no effect.

Even with all the code, I am not sure what you want to do. Assuming line 15 returns something instead, OK, if it makes sense to YOU :)
Last edited on
closed account (o35DwA7f)
If you are refering to the void showthesudoku function, it prints the array of the structure on screen. I don't think there is anywhere else. Answer to lastchance. Also thank you.
Last edited on
closed account (o35DwA7f)
A 2d array. We assign some values in an array. I want to check the surrounding boxes (the boxes that are accesible with a kings move, a 3x3 area with our selected box being the center), so that they will not have the same value. If they dont, then the value is assigned. If they do the value is discarded. Reply to jonnin. I hope i make myself clear.
Last edited on
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Your first post used code tags, the second post with a LARGE code dump doesn't use them. Why not?

Edit the post, add code tags and then making comments on the code will be MUCH easier since we can reference line numbers.

Code tags will also make it easier to READ the code (if properly indented and formatted), it won't be a huge wall of text.
closed account (o35DwA7f)
You are right Furry Guy. Code tags.
Thank you, your second post is much easier to read and comment on now.

Line 270: sud.precious[i][j]; does absolutely nothing. You don't assign any value to the element, nor do you assign the element to another variable. You are not changing the value at all.

With a decent compiler line 270 might be elided out of the program. Why leave it in the source if it does nothing?

+===========+

I would also suggest you learn when writing C++ code how to generate random numbers with the C++ library <random> (and C++ time library <chrono>). Even the C standard itself recommends not using srand()/rand().
https://web.archive.org/web/20180123103235/http://cpp.indi.frih.net/blog/2014/12/the-bell-has-tolled-for-rand/
https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful

Using <random> and <chrono> looks intimidating at first because the libraries offer many options vs. one with rand().

A C++ working paper pdf, a quick overview how to generate random numbers in C++.
http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3551.pdf

I used that working paper as the starting point for creating a custom header-only file I add to a C++ project when I want random numbers. As easy as using the C libraries functions.
closed account (o35DwA7f)
You are right line 270 does nothing. I was messing with that function trying to get the result which is the question i asked here. I know it has a lot of useless stuff. I just pasted the code from the point i stopped. Random numbers is something i need to study. But the question remains. Does the kingsmove function line 245 checks the surrounding boxes for the same value, and if not what am i doing wrong? Thank you for the random numbers pointers Furry Guy. I know it works some of the time but not always which confuses me more.
Last edited on
I don't play chess IRL because I am not a good player. With that said.....

A King has a total of 8 different possible moves available to it. Less if the King is at the edge(s) of the board, other pieces are in a possible move location or is in check by the other player.

How would a player find a possible King's move on paper for those conditions? (Consider writing out the steps in pseudo-code.)

Some (off the top of my head) basic steps for determining a King's move:

1. King's move blocked by other pieces?

2. King at edge(s)?

3. King in check, and will get out of check when moved? This step is a bit 'tricky' because moving another piece instead the King makes it possible to get out of check.

Each step you narrow down what moves are allowed for the King.

Something to look at:
https://codereview.stackexchange.com/questions/248987/c-chess-game-engine-using-minimax-and-alpha-beta-pruning
closed account (o35DwA7f)
A 9 x 9 array[9][9]. I assign array[2][2]=5. A random number generates, column=3 row=3 and number=5. That array[3][3] will be assigned with the value 5. array[3][3] ''sees'' array[2][2] with a king's move (one box away).It sees it diagonally.I dont want them to have the same value. So before the value is assigned i want to check every box that is 1 king's move away for that value. Let us say that it chooses array[3][3]=5 randomly. So row =3 column=3 and number=5. My idea is to iterate
for (int i=(row-1); i<(row+1);i++)
for (int j=(column-1);j<(column+1);j++).
Then i use an if statement to get the one that is forbidden.
(if (array[i][j]==number) kill it with fire.
If the king is at edge i use another 2 if statements if (column>edge(8)||row>edge(8)
if (column<edge(0)||row<edge(0). So that will eliminate the out of bounds positions. I have no problem getting the king out if checked. This is a sudoku not a chess game. The problem is that it works most of the time but not everytime which is annoying. What am i doing wrong? I can't understand why this iteration works sometimes and not everytime.
Last edited on
I am confused: chess in the middle of sudoku?
closed account (o35DwA7f)
There is no chess.It was a figure of speech. Picture a 2d array.like this
5 5 5
5 2 5
5 5 1
Now move from number 2 (in the center) to 1(in the bottom right) like a king in chess. Are you trolling me?
Last edited on
Nothing you have said so far make any sense in the context of sudoku, including the title of this thread. Are you a site traffic generator?
closed account (o35DwA7f)
What doesn't make sense? Please tell me and i will explain. Was the previous reply clear?
Last edited on
closed account (o35DwA7f)
If you guys don't want me to ask questions here, that's ok. I will leave. I didn't come here to fight or create problems. Nobody forced me here and if you don't want to talk with me i have no problem leaving. TheIdeasMan you haven't replied to me
Last edited on
Pages: 12