Ir ao conteúdo

Posts recomendados

Postado
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JogoDaVelha
{
    // JogoDaVelha é uma classe abstrata, pois existem tabuleiros, 3x3, 4x4 e 5x5.
    public abstract class JogoDaVelha
    {
        //Atributos
        private int[,] MatrizDoJogoDaVelha { set; get; }
        private char[,] MatrizCopia { set; get; }

        //Métodos Abstratos
        public abstract int verificarLinhas();
        public abstract int verificarColunas();
        public abstract int verificaDiagonalPrincipal();
        public abstract int verificaDiagonalSecundaria();
        public abstract char[,] matrizRecebeEscolha_X_ou_O_do_usuario(Player player);
        public abstract void imprimeTabuleiro();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JogoDaVelha
{
    //Tabuleiro 3X3
    class Tabuleiro3X3 : JogoDaVelha
    {
        private int[,] matrizDoJogoDaVelha;
        private char[,] matrizCopia;

        public Tabuleiro3X3()
        {
            this.MatrizDoJogoDaVelha = new int[3, 3] { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
            this.MatrizCopia = new char[3, 3] { { 'X', 'X', 'X'},
                                                { 'X', 'X', 'X'}, 
                                                { 'X', 'X', 'X'} };
        }
        
        public int[,] MatrizDoJogoDaVelha
        {
            set { this.matrizDoJogoDaVelha = value; }
            get { return this.matrizDoJogoDaVelha; }
        }
        public char[,] MatrizCopia
        {
            set { this.matrizCopia = value; }
            get { return this.matrizCopia; }
        }

        // Verifica linhas do jogo em X e em O
        public override int verificarLinhas()
        {
            for (int i = 0; i < 3; i++)
            {
                if ((this.MatrizDoJogoDaVelha[i, 0] + this.MatrizDoJogoDaVelha[i, 1] + this.MatrizDoJogoDaVelha[i, 2] == -3))
                    return -3;
                else
                {
                    if ((this.MatrizDoJogoDaVelha[i, 0] + this.MatrizDoJogoDaVelha[i, 1] + this.MatrizDoJogoDaVelha[i, 2] == 3))
                        return 3;
                }
            }
            return 0;
        }

        //Verifica colunas do jogo em X e em O
        public override int verificarColunas()
        {
            for(int j = 0; j < 3; j++)
            {
                if ((this.MatrizDoJogoDaVelha[0, j] + this.MatrizDoJogoDaVelha[1, j] + this.MatrizDoJogoDaVelha[2, j] == -3))
                    return -3;
                else
                {
                    if ((this.MatrizDoJogoDaVelha[0, j] + this.MatrizDoJogoDaVelha[1, j] + this.MatrizDoJogoDaVelha[2, j] == 3))
                        return 3;
                }
            }
            return 0;
        }

        //Verifica diagonal principal em X e em O
        public override int verificaDiagonalPrincipal()
        {
            if ((this.MatrizDoJogoDaVelha[0, 0] + this.MatrizDoJogoDaVelha[1, 1] + this.MatrizDoJogoDaVelha[2, 2] == -3))
                return -3;
            else
            {
                if ((this.MatrizDoJogoDaVelha[0, 0] + this.MatrizDoJogoDaVelha[1, 1] + this.MatrizDoJogoDaVelha[2, 2] == 3))
                    return 3;
            }
            return 0;
        }

        //Verifica diagonal secundária em X e em O
        public override int verificaDiagonalSecundaria()
        {
            if ((this.MatrizDoJogoDaVelha[0, 2] + this.MatrizDoJogoDaVelha[1, 1] + this.MatrizDoJogoDaVelha[2, 0] == -3))
                return -3;
            else
            {
                if ((this.MatrizDoJogoDaVelha[0, 2] + this.MatrizDoJogoDaVelha[1, 1] + this.MatrizDoJogoDaVelha[2, 0] == 3))
                    return 3;
            }
            return 0;
        }

        //O método recebe duas matrizes, onde uma vai receber valores -1 e 1 e a outra recebe X ou O
        public override char[,] matrizRecebeEscolha_X_ou_O_do_usuario(Player player)
        {
            this.MatrizDoJogoDaVelha[player.PosLinha, player.PosColuna] = player.Valor;
            if (player.Valor == 1)
                this.MatrizCopia[player.PosLinha, player.PosColuna] = 'O';
            else
            {
                if (player.Valor == -1)
                {
                    this.MatrizCopia[player.PosLinha, player.PosColuna] = 'X';
                }
            }
            return this.MatrizCopia;
        }

        //Método que imprime a grade. Falta terminar!!
        public override void imprimeTabuleiro()
        {
            Console.Write("   0   1   2");
            Console.WriteLine("");
            for (int i=0; i<3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Console.Write(i+"  "+this.MatrizCopia[i, j]);
                }
                Console.WriteLine("");
                Console.WriteLine("  -----------");
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JogoDaVelha
{
    public class Player
    {
        private int posLinha;
        private int posColuna;
        private int valor;
        
        public Player()
        {
            this.PosLinha = 0;
            this.PosColuna = 0;
            this.Valor = 0;
        }
        public Player(int linha, int coluna, int val)
        {
            this.PosLinha = linha;
            this.PosColuna = coluna;
            this.Valor = val;
        }

        public int PosLinha
        {
            set { this.posLinha = value; }
            get { return this.posLinha; }
        }
        public int PosColuna
        {
            set { this.posColuna = value; }
            get { return this.posColuna; }
        }
        public int Valor
        {
            set { this.valor = value; }
            get { return this.valor; }
        }
    }
}

Boa noite pessoal, estou aprendendo orientação à objetos em c#, então para treinar resolvi fazer esse jogo da velha. Não terminei ainda, mas está funcionando e ainda falta várias funcionalidades. Minha dúvida é se esse é o jeito certo de PROGRAMAR ORIENTAÇÃO À OBJETOS ?  e enquanto a esse método 

public override char[,] matrizRecebeEscolha_X_ou_O_do_usuario(Player player)

é correto passar objeto como parâmetro dessa forma ? vocês teria alguma dica para iniciante em ORIENTAÇÃO À OBJETOS ? desde já fico grato.

  • Obrigado 1
Postado

Consigo enxergar sim muitos dos elementos do paradigma.

Tratando-se de C# o melhor local ainda é o site com documentação fornecido no portal da Microsoft.

 

player é basicamente uma estrutura que carrega a jogada: Local e desenho (X ou (O)), mas poderia ter ter o mesmo o jogo inteiro com dois objetos: Quadro e a Caneta.

 

 

  • Curtir 1

Crie uma conta ou entre para comentar

Você precisa ser um usuário para fazer um comentário

Criar uma conta

Crie uma nova conta em nossa comunidade. É fácil!

Crie uma nova conta

Entrar

Já tem uma conta? Faça o login.

Entrar agora

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

GRÁTIS: ebook Redes Wi-Fi – 2ª Edição

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!