Пример #1
0
        static void Main(string[] args)
        {
            Positions a    = new Positions();
            CheckMove z    = new CheckMove();
            Game      game = new Game();

            game.gameloop(a, z);
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="piece"></param>
        /// <param name="direction"></param>
        /// <param name="turno"></param>
        /// <param name="z"></param>
        public void SetPeace(int piece, char direction, int turno, CheckMove z)
        {
            int piece_remove;

            int[] bw;
            int[] wb;
            int   i = 0;
            int   move;
            int   movement = 0;
            int   check;
            int   temp;

            bool straight = false;

            int[] val = null;


            if ((turno % 2) == 0)
            {
                bw = w;
                wb = b;
            }
            else
            {
                bw = b;
                wb = w;
            }

            switch (direction)
            {
            default:
                temp = z.Validation(0);
                output.invalidInput();
                break;

            case 's':
                movement = 3;
                straight = true;
                val      = new int[] { 3, 5, 22, 24 };
                break;

            case 'w':
                movement = -3;
                straight = true;
                val      = new int[] { 12, 14, 31, 33 };
                break;

            case 'a':
                movement = -1;
                straight = true;
                val      = new int[] { 12, 31 };
                break;

            case 'd':
                movement = +1;
                straight = true;
                val      = new int[] { 14, 33 };
                break;

            case 'q':
                foreach (int j in boardpos)
                {
                    if (j == 12 || j == 33)
                    {
                        movement = -3;
                        check    = -1;
                        if (j == bw[piece - 1])
                        {
                            if (boardpos[i - 3] != 0)
                            {
                                move = z.CheckmoveDiagonal(bw, wb,
                                                           i, movement, check, boardpos);
                                if (move == 0)
                                {
                                    temp = z.Validation(0);

                                    output.InvalidMove();

                                    break;
                                }
                                else if (move == 1)
                                {
                                    bw[piece - 1] = boardpos[i + movement];
                                    break;
                                }
                                else if (move == 2)
                                {
                                    bw[piece - 1] = boardpos[i +
                                                             (2 * movement + check)];
                                    piece_remove =
                                        Array.IndexOf(wb, boardpos[i + movement]);
                                    if (wb == b)
                                    {
                                        b = b.Where((source, index) => index
                                                    != piece_remove).ToArray();
                                    }
                                    else
                                    {
                                        w = w.Where((source, index) => index
                                                    != piece_remove).ToArray();
                                    }
                                    break;
                                }
                            }
                            else
                            {
                                temp = z.Validation(0);
                                output.InvalidMove();
                                break;
                            }
                        }
                    }
                    else
                    {
                        movement = -4;
                        if (j == bw[piece - 1])
                        {
                            if (j == 13 || j == 22)
                            {
                                temp = z.Validation(0);
                                output.InvalidMove();
                                break;
                            }

                            if (boardpos[i - 4] != 0)
                            {
                                move = z.CheckmoveStraight(bw, wb,
                                                           i, movement, boardpos);
                                if (move == 0)
                                {
                                    temp = z.Validation(0);

                                    output.InvalidMove();

                                    break;
                                }
                                else if (move == 1)
                                {
                                    bw[piece - 1] =
                                        boardpos[i + movement];
                                    break;
                                }
                                else if (move == 2)
                                {
                                    if (j == 18)
                                    {
                                        bw[piece - 1] =
                                            boardpos[i + (2 * movement + 1)];
                                    }
                                    else
                                    {
                                        bw[piece - 1] =
                                            boardpos[i + (2 * movement)];
                                    }
                                    piece_remove =
                                        Array.IndexOf(wb, boardpos[i + movement]);
                                    if (wb == b)
                                    {
                                        b = b.Where((source, index) => index
                                                    != piece_remove).ToArray();
                                    }
                                    else
                                    {
                                        w = w.Where((source, index) => index
                                                    != piece_remove).ToArray();
                                    }
                                    break;
                                }
                            }
                            else
                            {
                                temp = z.Validation(0);
                                output.InvalidMove();
                                break;
                            }
                        }
                    }
                    i++;
                }
                break;

            case 'z':
                foreach (int j in boardpos)
                {
                    if (j == 5 || j == 22)
                    {
                        movement = 3;
                        check    = -1;
                        if (j == bw[piece - 1])
                        {
                            if (boardpos[i + 3] != 0)
                            {
                                move = z.CheckmoveDiagonal(bw, wb,
                                                           i, movement, check, boardpos);
                                if (move == 0)
                                {
                                    temp = z.Validation(0);

                                    output.InvalidMove();

                                    break;
                                }
                                else if (move == 1)
                                {
                                    bw[piece - 1] = boardpos[i + movement];
                                    break;
                                }
                                else if (move == 2)
                                {
                                    bw[piece - 1] = boardpos[i +
                                                             (2 * movement + check)];
                                    piece_remove =
                                        Array.IndexOf(wb, boardpos[i + movement]);
                                    if (wb == b)
                                    {
                                        b = b.Where((source, index) => index
                                                    != piece_remove).ToArray();
                                    }
                                    else
                                    {
                                        w = w.Where((source, index) => index
                                                    != piece_remove).ToArray();
                                    }
                                    break;
                                }
                            }
                            else
                            {
                                temp = z.Validation(0);
                                output.InvalidMove();
                                break;
                            }
                        }
                        i += 1;
                    }
                    else
                    {
                        movement = 2;
                        if (j == bw[piece - 1])
                        {
                            if (j == 3 || j == 12 || j == 23 || j == 31)
                            {
                                temp = z.Validation(0);
                                output.InvalidMove();
                                break;
                            }

                            if (boardpos[i + 2] != 0)
                            {
                                move = z.CheckmoveStraight(bw, wb,
                                                           i, movement, boardpos);
                                if (move == 0)
                                {
                                    temp = z.Validation(0);

                                    output.InvalidMove();

                                    break;
                                }
                                else if (move == 1)
                                {
                                    bw[piece - 1] =
                                        boardpos[i + movement];
                                    break;
                                }
                                else if (move == 2)
                                {
                                    if (j == 18)
                                    {
                                        bw[piece - 1] =
                                            boardpos[i + (2 * movement + 1)];
                                    }
                                    else
                                    {
                                        bw[piece - 1] =
                                            boardpos[i + (2 * movement)];
                                    }
                                    piece_remove = Array.IndexOf(wb,
                                                                 boardpos[i + movement]);
                                    if (wb == b)
                                    {
                                        b = b.Where((source, index) => index
                                                    != piece_remove).ToArray();
                                    }
                                    else
                                    {
                                        w = w.Where((source, index) => index
                                                    != piece_remove).ToArray();
                                    }
                                    break;
                                }
                            }
                            else
                            {
                                temp = z.Validation(0);
                                output.InvalidMove();
                                break;
                            }
                        }
                        i += 1;
                    }
                }
                break;

            case 'e':
                foreach (int j in boardpos)
                {
                    if (j == 14 || j == 31)
                    {
                        movement = -3;
                        check    = 1;
                        if (j == bw[piece - 1])
                        {
                            if (boardpos[i - 3] != 0)
                            {
                                move = z.CheckmoveDiagonal(bw, wb,
                                                           i, movement, check, boardpos);
                                if (move == 0)
                                {
                                    temp = z.Validation(0);

                                    output.InvalidMove();

                                    break;
                                }
                                else if (move == 1)
                                {
                                    bw[piece - 1] = boardpos[i + movement];
                                    break;
                                }
                                else if (move == 2)
                                {
                                    bw[piece - 1] = boardpos[i +
                                                             (2 * movement + check)];
                                    piece_remove =
                                        Array.IndexOf(wb, boardpos[i + movement]);
                                    if (wb == b)
                                    {
                                        b = b.Where((source, index) => index
                                                    != piece_remove).ToArray();
                                    }
                                    else
                                    {
                                        w = w.Where((source, index) => index
                                                    != piece_remove).ToArray();
                                    }
                                    break;
                                }
                            }
                            else
                            {
                                temp = z.Validation(0);
                                output.InvalidMove();
                                break;
                            }
                        }
                        i += 1;
                    }
                    else
                    {
                        movement = -2;
                        if (j == bw[piece - 1])
                        {
                            if (j == 24 || j == 33 || j == 5 || j == 13)
                            {
                                temp = z.Validation(0);
                                output.InvalidMove();
                                break;
                            }
                            if (boardpos[i - 2] != 0)
                            {
                                move = z.CheckmoveStraight(bw, wb,
                                                           i, movement, boardpos);
                                if (move == 0)
                                {
                                    temp = z.Validation(0);

                                    output.InvalidMove();

                                    break;
                                }
                                else if (move == 1)
                                {
                                    bw[piece - 1] =
                                        boardpos[i + movement];
                                    break;
                                }
                                else if (move == 2)
                                {
                                    if (j == 18)
                                    {
                                        bw[piece - 1] =
                                            boardpos[i + (2 * movement - 1)];
                                    }
                                    else
                                    {
                                        bw[piece - 1] =
                                            boardpos[i + (2 * movement)];
                                    }
                                    piece_remove =
                                        Array.IndexOf(wb, boardpos[i + movement]);
                                    if (wb == b)
                                    {
                                        b = b.Where((source, index) => index
                                                    != piece_remove).ToArray();
                                    }
                                    else
                                    {
                                        w = w.Where((source, index) => index
                                                    != piece_remove).ToArray();
                                    }
                                    break;
                                }
                            }
                            else
                            {
                                temp = z.Validation(0);
                                output.InvalidMove();
                                break;
                            }
                        }
                        i += 1;
                    }
                }
                break;

            case 'x':
                foreach (int j in boardpos)
                {
                    if (j == 3 || j == 24)
                    {
                        movement = 3;
                        check    = 1;
                        if (j == bw[piece - 1])
                        {
                            if (boardpos[i + 3] != 0)
                            {
                                move = z.CheckmoveDiagonal(bw, wb,
                                                           i, movement, check, boardpos);
                                if (move == 0)
                                {
                                    temp = z.Validation(0);

                                    output.InvalidMove();

                                    break;
                                }
                                else if (move == 1)
                                {
                                    bw[piece - 1] = boardpos[i + movement];
                                    break;
                                }
                                else if (move == 2)
                                {
                                    bw[piece - 1] = boardpos[i +
                                                             (2 * movement + check)];
                                    piece_remove =
                                        Array.IndexOf(wb, boardpos[i + movement]);
                                    if (wb == b)
                                    {
                                        b = b.Where((source, index) => index
                                                    != piece_remove).ToArray();
                                    }
                                    else
                                    {
                                        w = w.Where((source, index) => index
                                                    != piece_remove).ToArray();
                                    }
                                    break;
                                }
                            }
                            else
                            {
                                temp = z.Validation(0);
                                output.InvalidMove();
                                break;
                            }
                        }
                    }
                    else
                    {
                        movement = 4;
                        if (j == bw[piece - 1])
                        {
                            if (j == 14 || j == 23)
                            {
                                temp = z.Validation(0);
                                output.InvalidMove();
                                break;
                            }

                            if (boardpos[i + 4] != 0)
                            {
                                move = z.CheckmoveStraight(bw, wb,
                                                           i, movement, boardpos);
                                if (move == 0)
                                {
                                    temp = z.Validation(0);

                                    output.InvalidMove();

                                    break;
                                }
                                else if (move == 1)
                                {
                                    bw[piece - 1] =
                                        boardpos[i + movement];
                                    break;
                                }
                                else if (move == 2)
                                {
                                    if (j == 18)
                                    {
                                        bw[piece - 1] =
                                            boardpos[i + (2 * movement - 1)];
                                    }
                                    else
                                    {
                                        bw[piece - 1] =
                                            boardpos[i + (2 * movement)];
                                    }

                                    piece_remove =
                                        Array.IndexOf(wb, boardpos[i + movement]);
                                    if (wb == b)
                                    {
                                        b = b.Where((source, index) => index
                                                    != piece_remove).ToArray();
                                    }
                                    else
                                    {
                                        w = w.Where((source, index) => index
                                                    != piece_remove).ToArray();
                                    }
                                    break;
                                }
                            }
                            else
                            {
                                temp = z.Validation(0);
                                output.InvalidMove();
                                break;
                            }
                        }
                    }
                    i += 1;
                }
                break;
            }

            if (straight == true)
            {
                foreach (int j in boardpos)
                {
                    if (j == bw[piece - 1])
                    {
                        if (boardpos[i + movement] != 0)
                        {
                            foreach (int ival in val)
                            {
                                if (j == ival)
                                {
                                    temp = z.Validation(0);
                                    output.InvalidMove();
                                    break;
                                }
                            }
                            move = z.CheckmoveStraight(bw, wb,
                                                       i, movement, boardpos);
                            if (move == 0)
                            {
                                temp = z.Validation(0);
                                output.InvalidMove();
                                break;
                            }
                            else if (move == 1)
                            {
                                bw[piece - 1] = boardpos[i + movement];
                                break;
                            }
                            else if (move == 2)
                            {
                                bw[piece - 1] = boardpos[i + (2 * movement)];
                                piece_remove  = Array.IndexOf(wb, boardpos[i
                                                                           + movement]);
                                if (wb == b)
                                {
                                    b = b.Where((source, index) => index !=
                                                piece_remove).ToArray();
                                }
                                else
                                {
                                    w = w.Where((source, index) => index !=
                                                piece_remove).ToArray();
                                    Console.WriteLine(w.Length);
                                }
                                break;
                            }
                        }
                        else
                        {
                            temp = z.Validation(0);
                            output.InvalidMove();
                            break;
                        }
                    }
                    i += 1;
                }
                straight = false;
            }
        }
Пример #3
0
        /// <summary>
        /// este método utiliza as peças,
        /// </summary>
        /// <param name="a"></param>
        /// <param name="z"></param>
        public void gameloop(Positions a, CheckMove z)
        {
            int  turncounter;
            int  turno       = 0;
            bool gameover    = false;
            bool gamestarted = false;
            bool validmove   = false;
            int  validation  = 0;
            int  lengthB;
            int  lengthW;

            int[]  b = a.GetB();
            int[]  w = a.GetW();
            string second_input;

            int[] boardpos;
            int   possiblemoves;

            //loop principal do jogo
            do
            {
                possiblemoves = 0;
                output.instructions();
                output.printBoard(a);
                //verifica se o jogo começou ou não para os jogadores terem de
                //escolher qual dos jogadores começa
                if (gamestarted == false)
                {
                    output.startOutput();
                    string firstplayer = Console.ReadLine();
                    if (firstplayer == "b")
                    {
                        turno       = 1;
                        gamestarted = true;
                    }
                    else if (firstplayer == "w")
                    {
                        turno       = 2;
                        gamestarted = true;
                    }
                    else
                    {
                        output.invalidInput();
                    }
                }
                //ronda normal
                else
                {
                    b        = a.GetB();
                    w        = a.GetW();
                    boardpos = a.boardpos;
                    if ((turno % 2) == 0)
                    {
                        possiblemoves += z.CheckStuck(b, w, boardpos);

                        if (possiblemoves == 0)
                        {
                            output.BlacksWIn();
                            gamend();
                        }
                    }
                    else
                    {
                        possiblemoves += z.CheckStuck(w, b, boardpos);


                        if (possiblemoves == 0)
                        {
                            output.WhitesWIn();
                            gamend();
                        }
                    }
                    lengthB = b.Length;
                    lengthW = w.Length;
                    //verifica se algum dos jogadores está sem peças
                    if (lengthB != 0 && lengthW != 0)
                    {
                        turncounter = turn(turno);
                        TurnInformation c;
                        //Este while não permite que passe para o próximo turno
                        //sem o jogadores darem input válido
                        while (validmove == false)
                        {
                            second_input = Console.ReadLine();
                            //If verifica se o input dado pelo o utilizador é um
                            //número ou não
                            if (int.TryParse(second_input, out c.piece))
                            {
                                c.direction = Convert.ToChar(Console.ReadLine());
                                //método que irá definir e verificar o movimento
                                //das peças
                                a.SetPeace(c.piece, c.direction, turno, z);
                                validation = z.Validation(-1);

                                if (validation != 0)

                                {
                                    validmove = true;
                                }
                            }
                            else if (second_input == "exit")
                            {
                                gamend();
                            }
                            else
                            {
                                output.invalidInput();
                            }
                        }
                        //termina um turno e repete-se o ciclo do while
                        validmove = false;
                        turno++;
                    }
                    else
                    {
                        if (lengthB == 0)
                        {
                            //White wins
                        }
                        else
                        {
                            //Black wins
                        }
                        gamend();
                    }
                }
            } while (gameover == false);
        }