private void buscarBarralho() { Itens_Compartilhados.RetornoJogada embaralhado = new Itens_Compartilhados.RetornoJogada(); embaralhado = cliente.buscarBarralho(); for (int i = 0; i < embaralhado.jogadores.Length; i++) { if (embaralhado.jogadores[i].ID == cliente.ID) { for (int j = 1; j < cartas.Length; j++) { if (embaralhado.jogadores[i].cartas[j].ativo) { cartas[j].ativo = true; } else { cartas[j].ativo = false; } } } } timer1.Enabled = true; }
public Itens_Compartilhados.RetornoJogada statusJogadores() { Itens_Compartilhados.RetornoJogada status = new Itens_Compartilhados.RetornoJogada(); status.jogadores = this.jogadores; status.qtdJogadores = this.qtdJogadores; status.jogadorDaVez = this.jogadorDaVez; status.atributoDaVez = this.atributoDaVez; return(status); }
private void jogar() { Itens_Compartilhados.RetornoJogada jogada = new Itens_Compartilhados.RetornoJogada(); String atributo_teste; atributo_teste = ""; pb_jogar.Image = Super_Trunfo_Cliente.Properties.Resources.Espera; pb_jogar.Refresh(); this.primeiraJogada = true; if (rb_altura.Checked) { atributo_teste = "altura"; } if (rb_comprimento.Checked) { atributo_teste = "comprimento"; } if (rb_peso.Checked) { atributo_teste = "peso"; } if (rb_idade.Checked) { atributo_teste = "idade"; } cartas[posicao].atributo_teste = atributo_teste; jogada = cliente.jogada(cartas[posicao]); if (jogada.vencedor == cliente.ID) { for (int i = 0; i < jogada.cartasJogadas.Length; i++) { cartas[funcoes.retornoPosCarta(jogada.cartasJogadas[i].cod_Carta)].ativo = true; } pb_ganhou.Visible = true; } else { cartas[posicao].ativo = false; posicao = 0; mudaCarta(); pb_perdeu.Visible = true; } quantidadeCartas(); mudaCartaVencedor(funcoes.retornoPosCarta(jogada.cartaVencedora.cod_Carta)); pb_jogar.Image = Super_Trunfo_Cliente.Properties.Resources.Jogada; pb_jogar.Refresh(); timer2.Enabled = true; }
public Itens_Compartilhados.RetornoJogada buscarBarralho() { Itens_Compartilhados.Comandos comando = new Itens_Compartilhados.Comandos(); comando.comando = "BUSCAR_BARALHO"; byte[] baralho = this.funcoes.ObjectToByteArray(comando); TrySend(baralho); byte[] buff = new byte[32768]; int received = socketClient.Receive(buff); byte[] dataReceived = new byte[received]; Array.Copy(buff, dataReceived, received); Itens_Compartilhados.RetornoJogada retornoJogada = new Itens_Compartilhados.RetornoJogada(); retornoJogada = (Itens_Compartilhados.RetornoJogada)funcoes.ByteArrayToObject(dataReceived); return(retornoJogada); }
private void verificaStatus() { Itens_Compartilhados.Comandos comando = new Itens_Compartilhados.Comandos(); comando.comando = "STATUS_JOGADORES"; byte[] envioStatusJogador = this.funcoes.ObjectToByteArray(comando); socket.Send(envioStatusJogador); byte[] buff = new byte[32768]; int received = socket.Receive(buff); byte[] dataReceived = new byte[received]; Array.Copy(buff, dataReceived, received); Itens_Compartilhados.RetornoJogada retornoJogada = new Itens_Compartilhados.RetornoJogada(); retornoJogada = (Itens_Compartilhados.RetornoJogada)funcoes.ByteArrayToObject(buff); refreshLista(retornoJogada.jogadores, retornoJogada.embaralhado); iniciaJogo(retornoJogada.jogadores); }
public Itens_Compartilhados.RetornoJogada verificaStatus(String atrib) { Itens_Compartilhados.Comandos comando = new Itens_Compartilhados.Comandos(); comando.comando = "STATUS_JOGADORES_ATRIBUTO"; comando.atributo = atrib; comando.ID = this.ID; byte[] envioStatusJogador = this.funcoes.ObjectToByteArray(comando); socketClient.Send(envioStatusJogador); byte[] buff = new byte[32768]; int received = socketClient.Receive(buff); byte[] dataReceived = new byte[received]; Array.Copy(buff, dataReceived, received); Itens_Compartilhados.RetornoJogada retornoJogada = new Itens_Compartilhados.RetornoJogada(); retornoJogada = (Itens_Compartilhados.RetornoJogada)funcoes.ByteArrayToObject(buff); return(retornoJogada); }
public Itens_Compartilhados.RetornoJogada jogada(Itens_Compartilhados.Carta cartaEnv) { Itens_Compartilhados.Comandos comando = new Itens_Compartilhados.Comandos(); comando.comando = "JOGADA"; comando.carta = cartaEnv; comando.ID = this.ID; byte[] jogada = this.funcoes.ObjectToByteArray(comando); TrySend(jogada); byte[] buff = new byte[32768]; int received = socketClient.Receive(buff); byte[] dataReceived = new byte[received]; Array.Copy(buff, dataReceived, received); Itens_Compartilhados.RetornoJogada retornoJogada = new Itens_Compartilhados.RetornoJogada(); retornoJogada = (Itens_Compartilhados.RetornoJogada)funcoes.ByteArrayToObject(buff); return(retornoJogada); }
public Jogo() { this.jogadores = new Itens_Compartilhados.Jogador[8]; this.jogadaEmEspera = new Itens_Compartilhados.Carta[8]; this.retornoJogada = new Itens_Compartilhados.RetornoJogada(); this.embaralhado = new Itens_Compartilhados.RetornoJogada(); this.qtdJogadores = 0; this.jogadoresEmEspera = 0; this.isEmbaralhado = false; this.jogadorDaVez = -1; this.jogadorDaVezJogou = false; this.atributoDaVez = "altura"; for (int i = 0; i < this.jogadores.Length; i++) { this.jogadores[i] = new Itens_Compartilhados.Jogador(i); } for (int i = 0; i < this.jogadaEmEspera.Length; i++) { this.jogadaEmEspera[i] = new Itens_Compartilhados.Carta(); this.jogadaEmEspera[i].ativo = false; } }
private void timer1_Tick(object sender, EventArgs e) { Itens_Compartilhados.RetornoJogada status = new Itens_Compartilhados.RetornoJogada(); int provaReal = 0; int qtdCartas = 0; status = cliente.verificaStatus(atrib()); toolStripStatusLabel5.Text = "Jogadores: " + status.qtdJogadores.ToString(); setAtrib(status.jogadorDaVez, status.atributoDaVez, status.jogadorDaVezJogou); for (int i = 1; i < cartas.Length; i++) { if (cartas[i].ativo) { qtdCartas++; } } if (qtdCartas <= 0) { cliente.perder(); mudaStatusServidor(); perder(); } else { if (!this.primeiraJogada) { for (int i = 0; i < status.jogadores.Length; i++) { if (status.jogadores[i].ID == cliente.ID) { for (int j = 1; j < cartas.Length; j++) { if (status.jogadores[i].cartas[j].ativo) { cartas[j].ativo = true; } else { cartas[j].ativo = false; } } } } } } quantidadeCartas(); for (int i = 0; i < status.jogadores.Length; i++) { if ((status.jogadores[i].conectado) && (status.jogadores[i].jogando)) { provaReal++; } } if (provaReal < status.qtdJogadores) { mudaStatusServidor(); } if (status.qtdJogadores == 1) { mudaStatusServidor(); ganhar(); } }
/// <summary>ReceiveCallBack método da classe Server /// Evento realizado para receber e enviar dados do cliente através de um IASyncResult /// </summary> private void ReceiveCallback(IAsyncResult asyncronousResult) { Socket current = (Socket)asyncronousResult.AsyncState; int received; try { received = current.EndReceive(asyncronousResult); } catch (SocketException) /*Catch realizado caso houver perca de conexão com o cliente*/ { Console.WriteLine("Conexão com o cliente " + current.RemoteEndPoint.ToString() + " perdida."); jogo.removeJogador(current.RemoteEndPoint.ToString()); current.Close(); ListaDeClientesSockets.Remove(current); return; } byte[] recBuf = new byte[received]; Array.Copy(_buffer, recBuf, received); Itens_Compartilhados.Comandos comando = new Itens_Compartilhados.Comandos(); comando = (Itens_Compartilhados.Comandos)funcoes.ByteArrayToObject(recBuf); if (comando.comando == "JOGADA") { int primeiro; if (comando.ID == jogo.jogadorDaVez) { jogo.jogadorDaVezJogou = true; } funcoes.escreveInfoCarta(comando.carta); jogo.jogadoresEmEspera++; primeiro = jogo.jogadoresEmEspera; jogo.jogadaEmEspera[jogo.jogadoresEmEspera] = comando.carta; if (primeiro == 0) { while (jogo.qtdJogadores > (jogo.jogadoresEmEspera + 1)) { ; } Itens_Compartilhados.RetornoJogada retornoJogada = new Itens_Compartilhados.RetornoJogada(); retornoJogada = jogo.jogada(); byte[] envioRetornoJogada = this.funcoes.ObjectToByteArray(retornoJogada); Send(envioRetornoJogada); jogo.resetJogada(); } else { while (jogo.jogadoresEmEspera > -1) { ; } } } else if (comando.comando == "DESCONECTAR") { jogo.removeJogador(current.RemoteEndPoint.ToString()); current.Shutdown(SocketShutdown.Both); current.Close(); ListaDeClientesSockets.Remove(current); Console.WriteLine("Cliente Desconectado "); jogo.resetBarralho(); return; } else if (comando.comando == "STATUS_JOGADORES") { Itens_Compartilhados.RetornoJogada statusJogadores = new Itens_Compartilhados.RetornoJogada(); statusJogadores = jogo.statusJogadores(); statusJogadores.embaralhado = jogo.isEmbaralhado; byte[] envioStatusJogadores = this.funcoes.ObjectToByteArray(statusJogadores); current.Send(envioStatusJogadores); } else if (comando.comando == "STATUS_JOGADORES_ATRIBUTO") { Itens_Compartilhados.RetornoJogada statusJogadores = new Itens_Compartilhados.RetornoJogada(); statusJogadores = jogo.statusJogadores(); statusJogadores.jogadorDaVezJogou = jogo.jogadorDaVezJogou; if (comando.ID == jogo.jogadorDaVez) { jogo.atributoDaVez = comando.atributo; } byte[] envioStatusJogadores = this.funcoes.ObjectToByteArray(statusJogadores); current.Send(envioStatusJogadores); } else if (comando.comando == "EMBARALHAR") { int primeiro; jogo.jogadoresEmEspera++; primeiro = jogo.jogadoresEmEspera; if (primeiro == 0) { jogo.embaralhar(); while (!jogo.isEmbaralhado) { ; } } jogo.resetJogada(); } else if (comando.comando == "BUSCAR_BARALHO") { Itens_Compartilhados.RetornoJogada barralho = new Itens_Compartilhados.RetornoJogada(); barralho = jogo.embaralhado; byte[] envioBarralho = this.funcoes.ObjectToByteArray(barralho); current.Send(envioBarralho); } else if (comando.comando == "JOGAR") { jogo.setJogar(comando.jogadores[0]); } else if (comando.comando == "PERDER") { for (int i = 0; i < jogo.jogadores.Length; i++) { if (jogo.jogadores[i].ID == comando.ID) { jogo.jogadores[i].jogando = false; jogo.qtdJogadores--; if (jogo.jogadorDaVez == comando.ID) { jogo.reJogador(); } Thread.Sleep(550); } } } current.BeginReceive(_buffer, 0, _BUFFER_SIZE, SocketFlags.None, ReceiveCallback, current); }