Chess Queen moves [Help]

Pages: 12
So I was asked to build a chess game that only contains a queen and the queen must not have another queen in its side vertically,horizontally and diagonally because it will result an invalid move. I already built it but the problem is, I don't know how can I loop the input every time the move is invalid. Example, when a 2 queens are at the same rows or columns the program will input "This move is not applicable!". I want the program to ask the user to input another Rows and columns. And also my loops are failing to determine if the move was illegal or not. Example, when i try to put row 3 and col 5 + row 2 and col 4, it must be an invalid move, but my program allows it. Can please someone help me with this? or tell me what is wrong with my program?. Here are the codes.

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
  #include<iostream>
#include <cstdlib>
using namespace std;
int main(){
    char pattern[8][8];
    int p,o,z,ROWS,COLS,num_pat=0;
    char ans;
   int digits=0;
    for(int i=0;i<8;i++)
            for(int j=0;j<8;j++)
    pattern[i][j]= 0;
	
	cout<<"+===+===+===+===+===+===+===+===+"<<endl;
	cout<<"!   !   !   !   !   !   !   !   !"<<endl;
	cout<<"+===+===+===+===+===+===+===+===+"<<endl;
	cout<<"!   !   !   !   !   !   !   !   !"<<endl;
	cout<<"+===+===+===+===+===+===+===+===+"<<endl;
	cout<<"!   !   !   !   !   !   !   !   !"<<endl;
	cout<<"+===+===+===+===+===+===+===+===+"<<endl;
    cout<<"!   !   !   !   !   !   !   !   !"<<endl;
    cout<<"+===+===+===+===+===+===+===+===+"<<endl;
	cout<<"!   !   !   !   !   !   !   !   !"<<endl;
	cout<<"+===+===+===+===+===+===+===+===+"<<endl;
	cout<<"!   !   !   !   !   !   !   !   !"<<endl;
	cout<<"+===+===+===+===+===+===+===+===+"<<endl;
	cout<<"!   !   !   !   !   !   !   !   !"<<endl;
	cout<<"+===+===+===+===+===+===+===+===+"<<endl;
    cout<<"!   !   !   !   !   !   !   !   !"<<endl;
    cout<<"+===+===+===+===+===+===+===+===+"<<endl;
   do{
    cout<<"Enter Rows of an 8x8 Chess board (0~7):"; cin>>ROWS;
    cout<<"Enter Columns of an 8x8 Chess board (0~7):"; cin>>COLS; 
    pattern[ROWS][COLS]='Q';
    cout<<endl<<endl<<"Add another move?(press Y/y): ";
    cin>>ans;
    }while(ans=='Y' || ans=='y');
    system("cls");
    cout<<"+===+===+===+===+===+===+===+===+"<<endl;
     
    
    for(int i=0;i<8;i++){
            cout<<"! ";
            for(int j=0;j<8;j++)
            cout<<pattern[i][j]<<" ! ";
            cout<<endl<<"+===+===+===+===+===+===+===+===+"<<endl;
            }       

   for (z = 0; z < COLS; z++)
    {
        if (pattern[ROWS][z])
            {cout<<"This move is not applicable!"<<endl;}
    }
   for (z = 7; z > COLS; z--)
    {
        if (pattern[ROWS][z])
            {cout<<"This move is not applicable!"<<endl;}
    }
    
     for (z = 0; z < ROWS; z++)
    {
        if (pattern[z][COLS])
            {cout<<"This move is not applicable!"<<endl;}
    }
     for (z = 7; z > ROWS; z--)
    {
        if (pattern[z][COLS])
            {cout<<"This move is not applicable!"<<endl;}
    }
   
	for (z= 0, o= COLS-ROWS; z<COLS && ROWS>o ; z++, o++)
    {
		if (pattern[z][o])
        {cout<<"This move is not applicable!"<<endl;}
    }
    for (z= 7, o= ROWS-p; z>=0 && o<7 ; z--, o++)
    {
		if (pattern[z][o])
        {cout<<"This move is not applicable!"<<endl;}
    }
    system("pause");
    }
Last edited on
You need to move your tests into your do-while loop.

Plus, if you are going to let the user to specify the rows and columns, you need to make sure that those values are within range of your pattern structure, and if so, then draw your chess board.
Sorry noob questions, I'm kinda new in programming. How can i move my tests in the do-while? Can you give me a hint ? Thanks hehe.
Sorry, I misread what you were using ROWS and COLS for.

After the user enters ROW and COLS, do your for-loops right after that -- inside the do-while.

Pseudo code:

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

do
    {
    cout << "Enter the row: " << endl;

    cin >> ROWS;

    /*    Check ROWS to make sure it is valid:    */

    if( ROWS < 0 || ROWS > 8 )
        {
        cout << "The value " << ROWS << " is not a valid row!" << endl;

        continue;

        }    /*    if( ROWS is not valid )    */

    /*    Do the same thing for the COLS:

    /*   
         If you get here, now check to see if your pattern array has something 
         at that location:  (NOTE: PUT YOUR "IF" logic here)
    */

    }
        while( 'Y' != toupper( ans ) );


Thanks ! It worked. But one more request, when i put this code to show if the move is valid. it shows that my move was valid but it shows multiple "This move is valid"
The code i used is:
1
2
3
4
5
6
7
8
9
for (z = 0; z < COLS; z++)
    {
        if (pattern[ROWS][z])
            {cout<<"This move is not applicable!"<<endl;}
            else
            {
            	cout<<"Move is applicable!"<<endl;
			}
    }
Well, you are looping through all of the columns on the current row. Shouldn't you just test the row and column in question?
Oh, okay i get it thanks! :)
You need to learn how to use functions. This can be simplified considerably by putting all your tests in a bool function.

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
bool valid_move (char pattern[8][8], int ROWS, int COLS))
{  int z, o;

    for (z = 0; z < COLS; z++)
    {  if (pattern[ROWS][z])
           return false;
    }
   for (z = 7; z > COLS; z--)
   { if (pattern[ROWS][z])
        return false;   
    }
    for (z = 0; z < ROWS; z++)
    { if (pattern[z][COLS])
         return false;
     }
     for (z = 7; z > ROWS; z--)
    {  if (pattern[z][COLS])
          return false;
    }
    for (z= 0, o= COLS-ROWS; z<COLS && ROWS>o ; z++, o++)
    {  if (pattern[z][o])
           return false;
    }
    for (z= 7, o= ROWS-p; z>=0 && o<7 ; z--, o++)
    { if (pattern[z][o])
         return false;
    }
    return true;  // All tests passed
}
...
    if (valid_move (pattern, ROWS, COLS))
      cout << "Move is applicable" << endl;
    else
      cout << "Move is not applicable" << endl;
Last edited on
@kroragna: If I've answered your question to your satisfaction, please mark it so! But don't hesitate to ask more questions!
offbeat solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main(){	
	int x1, y1;
	cout << "first coordinate : ";
	cin >> x1 >> y1;
	int x2, y2;	
	cout << "second coordinate : ";
	cin >> x2 >> y2;
	
	if( (y2-y1)/(x2-x1)==1 || (y2-y1)/(x2-x1)==-1 || x1==x2 || y1==y2) cout << "invalid";
	else cout << "valid";
	
}

you only need to check legality, such as x1= -2
I already made a bool function. But the problem is, it doesn't show if the move is valid or not. I use the code you put @anup30
>
I already made a bool function. But the problem is, it doesn't show if the move is valid or not. I use the code you put @anup30


if you do in my method, line 12 is "self-complete" you don't need another function for that.

but, if its a homework, your teacher may not understand how the line actually works!
I mean in the bool function. Look at my new program. Its not showing if the move is not applicable or applicable. Here they are:
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
 #include<iostream>
#include <cstdlib>
using namespace std;
int main(){
    char board[8][8];
    int p,o,z,ROWS,COLS,num_pat=0;
    char ans;
    char again;
   int digits=0;
    for(int i=0;i<8;i++)
            for(int j=0;j<8;j++)
    board[i][j]= 0;
	
	cout<<"+===+===+===+===+===+===+===+===+"<<endl;
	cout<<"!   !   !   !   !   !   !   !   !"<<endl;
	cout<<"+===+===+===+===+===+===+===+===+"<<endl;
	cout<<"!   !   !   !   !   !   !   !   !"<<endl;
	cout<<"+===+===+===+===+===+===+===+===+"<<endl;
	cout<<"!   !   !   !   !   !   !   !   !"<<endl;
	cout<<"+===+===+===+===+===+===+===+===+"<<endl;
    cout<<"!   !   !   !   !   !   !   !   !"<<endl;
    cout<<"+===+===+===+===+===+===+===+===+"<<endl;
	cout<<"!   !   !   !   !   !   !   !   !"<<endl;
	cout<<"+===+===+===+===+===+===+===+===+"<<endl;
	cout<<"!   !   !   !   !   !   !   !   !"<<endl;
	cout<<"+===+===+===+===+===+===+===+===+"<<endl;
	cout<<"!   !   !   !   !   !   !   !   !"<<endl;
	cout<<"+===+===+===+===+===+===+===+===+"<<endl;
    cout<<"!   !   !   !   !   !   !   !   !"<<endl;
    cout<<"+===+===+===+===+===+===+===+===+"<<endl;
   do{
    cout<<"Enter Rows:"; cin>>ROWS;
    if(ROWS < 0 || ROWS > 7)
    {
    	cout<<"The value of "<<ROWS<<" is not a valid row!\n";
	}
    cout<<"Enter Columns:"; cin>>COLS;
	if(COLS < 0 || COLS > 7)
	{
		cout<<"The value of "<<COLS<<" is not a valid column!\n";
	 } 
    board[ROWS][COLS]='Q';
    cout<<endl<<endl<<"Add another move?(press Y/y): ";
    cin>>ans;
    }while(ans=='Y' || ans=='y');
    system("cls");
    cout<<"+===+===+===+===+===+===+===+===+"<<endl;
     
    
    for(int i=0;i<8;i++){
            cout<<"! ";
            for(int j=0;j<8;j++)
            cout<<board[i][j]<<" ! ";
            cout<<endl<<"+===+===+===+===+===+===+===+===+"<<endl;
            }       
    system("pause");
    }
bool valid_move (char board[8][8], int ROWS, int COLS)
{  int z, o, p;

    for (z = 0; z < COLS; z++)
    {  
	if (board[ROWS][z])
           return false;
    }
   for (z = 7; z > COLS; z--)
   { 
   if (board[ROWS][z])
        return false;   
    }
    for (z = 0; z < ROWS; z++)
    { 
	if (board[z][COLS])
         return false;
     }
     for (z = 7; z > ROWS; z--)
    {  
	if (board[z][COLS])
          return false;
    }
    for (z= 0, o= COLS-ROWS; z<COLS && ROWS>o ; z++, o++)
    {  
	if (board[z][o])
           return false;
    }
    for (z= 7, o= ROWS-p ; z>=0 && o<7 ; z--, o++)
    { 
	if (board[z][o])
         return false;
    }
    return true;
    if (valid_move (board, ROWS, COLS))
      cout << "Move is applicable" << endl;
    else
      cout << "Move is not applicable" << endl;
    system("pause");
}

That's the only problem I have. I just want my program to show if the move is valid or invalid.
Last edited on
Do you actually call your valid_move() function? (besides recursively)
I don't know i just followed the method that anup30 told me to do. So I did it, but the problem was, the move is applicable and move is not applicable doesn't display on the screen. So maybe its not being called by the program.
Okay, let me put it another way: It's definitely not being called :)

edit:
and even when you do call it, this won't be reached:
1
2
3
4
5
    if (valid_move (board, ROWS, COLS))
      cout << "Move is applicable" << endl;
    else
      cout << "Move is not applicable" << endl;
    system("pause");

have a read of this about calling functions:
http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
So how can i fix this? Can you tell me what's wrong? :D
I think if you don't know how to call a function, you should start by reading that link.
Also, when you detect that a ROW or COL value is incorrect, you should put in a continue so that the user can input new values -- See my earlier post.
Now I know how to call functions. But, I can't understand why is it the last if-else statement is not showing in my program but the other program works.
Last edited on
Pages: 12