dimanche 14 février 2021

Control Flow for pieces

So basically, I'm making a game in which I need to ask for the input from the user to get his choice and move the pieces accordingly. For context, Player 1(always go first) has piece 'A', 'B' and 'C', where Player 2 has 'D', 'E', 'F'. As for the direction, '0' as going up-left, '1' going upward, '2' going up-right, so on so for up to 7, where it indicates going leftward.

So for example, I choose 'A', then '4'. A_x and A_y should change from 0,0 to 1,1 but instead what I got was 7,2( I have added line of cout << A_x << endl << A_y to check the x and y value)

What went wrong? Thanks in advance.

Here is the code for piece 'A'

For canMove1, there is a while loop that will repeat when canMove1 = false, so that we can asking for the direction again when the current can't be moved to, and I have change canMove1 to true the second the loop is entered.

switch (direction) {
                        case 1:
                            if (--A_x < 0 || --A_x == B_x && A_y == B_y || --A_x == C_x && A_y == C_y || --A_x == D_x &&
                            A_y == D_y || --A_x == E_x && A_y == E_y || --A_x == F_x && A_y == F_y){
                                cout << "Can't move to direction " << direction << endl;
                                canMove1 = false;
                            } else {
                                A_x -= 1; //update position
                            }
                            break;
                        case 2:
                            if (--A_x < 0 || ++A_y > 2 || --A_x == B_x && ++A_y == B_y || --A_x == C_x && ++A_y == C_y ||
                            --A_x == D_x && ++A_y == D_y || --A_x == E_x && ++A_y == E_y || --A_x == F_x && ++A_y == F_y){
                                cout << "Can't move to direction " << direction << endl;
                                canMove1 = false;
                            } else {
                                A_x -= 1; //update position
                                A_y += 1;
                            }
                            break;
                        case 3:
                            if (++A_y > 2 || A_x == B_x && ++A_y == B_y || A_x == C_x && ++A_y == C_y || A_x == D_x &&
                            ++A_y == D_y || A_x == E_x && ++A_y == E_y || A_x == F_x && ++A_y == F_y){
                                cout << "Can't move to direction " << direction << endl;
                                canMove1 = false;
                            } else {
                                A_y += 1; //update position
                            }
                            break;
                        case 4:
                            if (++A_x > 2 || ++A_y > 2 || ++A_x == B_x && ++A_y == B_y || ++A_x == C_x && ++A_y == C_y ||
                            ++A_x == D_x && ++A_y == D_y || ++A_x == E_x && ++A_y == E_y || ++A_x == F_x && ++A_y == F_y){
                                cout << "Can't move to direction " << direction << endl;
                                canMove1 = false;
                            } else {
                                A_x += 1; //update position
                                A_y += 1;
                            }
                            break;
                        case 5:
                            if (++A_x > 2 || ++A_x == B_x && A_y == B_y || ++A_x == C_x && A_y == C_y || ++A_x == D_x &&
                            A_y == D_y || ++A_x == E_x && A_y == E_y || ++A_x == F_x && A_y == F_y){
                                cout << "Can't move to direction " << direction << endl;
                                canMove1 = false;
                            } else {
                                A_x += 1; //update position
                            }
                            break;
                        case 6:
                            if (++A_x > 2 || --A_y < 0 || ++A_x == B_x && --A_y == B_y || ++A_x == C_x && --A_y == C_y ||
                            ++A_x == D_x && --A_y == D_y || ++A_x == E_x && --A_y == E_y || ++A_x == F_x && --A_y == F_y){
                                cout << "Can't move to direction " << direction << endl;
                                canMove1 = false;
                            } else {
                                A_x += 1; //update position
                                A_y -= 1;
                            }
                            break;
                        case 7:
                            if (--A_y < 0 || A_x == B_x && --A_y == B_y || A_x == C_x && --A_y == C_y || A_x == D_x &&
                            --A_y == D_y || A_x == E_x && --A_y == E_y || A_x == F_x && --A_y == F_y){
                                cout << "Can't move to direction " << direction << endl;
                                canMove1 = false;
                            } else {
                                A_y -= 1; //update position
                            }
                            break;
                        case 0:
                            if (--A_x < 0 || --A_y < 0 || --A_x == B_x && --A_y == B_y || --A_x == C_x && --A_y == C_y ||
                            --A_x == D_x && --A_y == D_y || --A_x == E_x && --A_y == E_y || --A_x == F_x && --A_y == F_y){
                                cout << "Can't move to direction " << direction << endl;
                                canMove1 = false;
                            } else {
                                A_x -= 1; //update position
                                A_y -= 1;
                            }
                            break;
                        default:
                            cout << "Something went wrong with the direction..." << endl;
                            return 1;
                    }

Aucun commentaire:

Enregistrer un commentaire