PIL_tpCondRet PIL_PopCarta( PIL_tppPilha pPilha , CAR_tppCarta * pCarta ) { int valor ; char naipe ; PIL_tpCondRet condRet = PIL_VerCarta( pPilha , pCarta , 0 ) ; *pCarta = NULL ; if ( condRet != PIL_CondRetOK ) { return condRet ; } CAR_ObterNaipe( *pCarta, &naipe ); CAR_ObterValor( *pCarta, &valor ); LIS_ExcluirElemento( pPilha->pListaCartas ); /*LIS_ExcluirElemento irá destruir a carta junto do nó. É necessário que ela seja criada novamente*/ CAR_CriarCarta( pCarta ) ; CAR_PreencheCarta( *pCarta , naipe, valor ) ; return PIL_CondRetOK ; }/* Fim função: PIL Pop Carta */
LIS_tppLista FRC_CriarBaralho( ) { int np ; int rk ; CAR_tppCarta pAuxCar; LIS_tppLista pNovo = LIS_CriarLista( NULL ); if( pNovo == NULL) { return NULL ; } /* if */ for( np = CAR_NaipePaus ; np <= CAR_NaipeOuros ; np++ ) { for( rk = CAR_RankAs ; rk <= CAR_RankRei ; rk++ ) { pAuxCar = CAR_CriarCarta( ( CAR_tpNaipe ) ( np ) , ( CAR_tpRank ) ( rk ) ); LIS_InserirElementoApos( pNovo , ( void *) pAuxCar ) ; } /* for */ } /* if */ IrInicioLista( pNovo ) ; return pNovo; }
/*************************************************************************** * * Função: PIL &Adicionar Carta * ****/ PIL_tpCondRet PIL_AdicionarCarta (PIL_tpPilha pPilha, CAR_tpCarta carta) { LIS_tpCondRet retorno; CAR_tpCarta copiaCarta; CAR_tpValor copiaValor; CAR_tpNaipe copiaNaipe; CAR_tpEstado copiaEstado; CAR_tpCondRet condRetCarta; int tornarInvisivel = 0; if (pPilha == NULL) return PIL_CondRetPilhaNaoExiste; if (carta == NULL) return PIL_CondRetCartaNaoExiste; condRetCarta = CAR_ObterEstado(carta,&copiaEstado); if (condRetCarta != CAR_CondRetOK) return PIL_CondRetCartaNaoExiste; if (copiaEstado == CAR_EstadoInvisivel){ CAR_TornarVisivel(carta); tornarInvisivel = 1; } condRetCarta = CAR_ObterValor(carta,&copiaValor); if (condRetCarta != CAR_CondRetOK) return PIL_CondRetCartaNaoExiste; condRetCarta = CAR_ObterNaipe(carta,&copiaNaipe); if (condRetCarta != CAR_CondRetOK) return PIL_CondRetCartaNaoExiste; condRetCarta = CAR_CriarCarta(&copiaCarta,copiaValor,copiaNaipe); if(condRetCarta == CAR_CondRetFaltouMemoria) return PIL_CondRetFaltouMemoria; if (tornarInvisivel) CAR_TornarInvisivel(copiaCarta); retorno = LIS_InserirElementoAntes( pPilha->cabeca ,copiaCarta); if (retorno == LIS_CondRetFaltouMemoria) return PIL_CondRetFaltouMemoria; return PIL_CondRetOK; }
/*************************************************************************** * * Função: PIL &Remover Carta * ****/ PIL_tpCondRet PIL_RemoverCarta( PIL_tpPilha pPilha , CAR_tpCarta * pCarta ) { LIS_tpCondRet condRetLis; CAR_tpCarta carta; if (pPilha == NULL) return PIL_CondRetPilhaNaoExiste; condRetLis = LIS_ObterValor( pPilha->cabeca , (void**)&carta); if (condRetLis != LIS_CondRetOK) return PIL_CondRetPilhaVazia; else { CAR_tpCarta cartaCriada; CAR_tpValor valor; CAR_tpNaipe naipe; CAR_tpEstado estado; CAR_tpCondRet condRetCar; //nao precisa testar cond de ret porque já foi garantido que carta existe CAR_ObterValor(carta,&valor); CAR_ObterNaipe(carta,&naipe); CAR_ObterEstado(carta,&estado); condRetCar = CAR_CriarCarta( &cartaCriada,valor,naipe); if (estado == CAR_EstadoInvisivel) CAR_TornarInvisivel(cartaCriada); if (condRetCar == CAR_CondRetFaltouMemoria) return PIL_CondRetFaltouMemoria; else { LIS_ExcluirElemento( pPilha->cabeca ); *pCarta = cartaCriada; //aqui há garantia que a lista nao esta vazia pois ja se obteve o valor anteriormente return PIL_CondRetOK; } } }
TST_tpCondRet TST_EfetuarComando( char * ComandoTeste ) { PIL_tpCondRet CondRetObtido = PIL_CondRetOK ; PIL_tpCondRet CondRetEsperada = PIL_CondRetFaltouMemoria ; /* inicializa para qualquer coisa */ char NaipeEsperado = '?' ; char NaipeObtido = '!' ; char NaipeDado = '\0' ; int ValorEsperado = -1 ; int ValorObtido = -2 ; int ValorDado = 0 ; int PosicaoPilha = -1 ; int NumPilha = -1 ; int NumLidos = -1 ; CAR_tppCarta CartaObtida ; CAR_tppCarta CartaDada ; TST_tpCondRet Ret ; /* Testar PIL Criar Pilha de Cartas */ if ( strcmp( ComandoTeste , CRIAR_PIL_CMD ) == 0 ) { NumLidos = LER_LerParametros( "ii" , &NumPilha , &CondRetEsperada ) ; if ( NumLidos != 2 || NumPilha >= MAX_PILS || NumPilha < 0 ) { return TST_CondRetParm ; } /* if */ CondRetObtido = PIL_CriarPilhaVazia( &vPilhas[NumPilha] ) ; return TST_CompararInt( CondRetEsperada , CondRetObtido , "Retorno errado ao criar pilha." ); } /* fim ativa: Testar PIL Criar Pilha de cartas*/ /* Testar PIL Push Carta na pilha */ else if ( strcmp( ComandoTeste , PUSH_CMD ) == 0 ) { NumLidos = LER_LerParametros( "icii" , &NumPilha, &NaipeDado , &ValorDado, &CondRetEsperada ) ; if ( NumLidos != 4 || NumPilha >= MAX_PILS || NumPilha < 0 ) { return TST_CondRetParm ; } /* if */ CAR_CriarCarta( &CartaDada ) ; CAR_PreencheCarta( CartaDada, NaipeDado , ValorDado ); CondRetObtido = PIL_PushCarta( vPilhas[NumPilha] , CartaDada ) ; return TST_CompararInt( CondRetEsperada , CondRetObtido , "Retorno errado ao operar 'push' na pilha" ); } /* fim ativa: Testar PIL Push Carta na pilha */ /* Testar PIL Pop Carta da Pilha */ else if ( strcmp( ComandoTeste , POP_CMD ) == 0 ) { NumLidos = LER_LerParametros( "ii" , &NumPilha , &CondRetEsperada ) ; if ( NumLidos != 2 || NumPilha >= MAX_PILS || NumPilha < 0 ) { return TST_CondRetParm ; } /* if */ CondRetObtido = PIL_PopCarta( vPilhas[NumPilha] , &CartaObtida ) ; return TST_CompararInt( CondRetEsperada , CondRetObtido , "Retorno errado ao operar 'pop' na pilha") ; } /* Testar PIL Ver Carta da Pilha */ else if ( strcmp( ComandoTeste , VER_CMD ) == 0 ) { NumLidos = LER_LerParametros( "iii" , &NumPilha , &PosicaoPilha , &CondRetEsperada ) ; if ( NumLidos != 3 || NumPilha >= MAX_PILS || NumPilha < 0 ) { return TST_CondRetParm ; } /* if */ CondRetObtido = PIL_VerCarta( vPilhas[NumPilha] , &CartaObtida , PosicaoPilha ) ; return TST_CompararInt( CondRetEsperada , CondRetObtido , "Retorno errado ao ver carta da pilha") ; } /* Testar PIL Ver Carta da Pilha */ /* Testar PIL Destruir pilha de cartas */ else if ( strcmp( ComandoTeste , DESTROI_CMD ) == 0 ) { NumLidos = LER_LerParametros( "ii" , &NumPilha , &CondRetEsperada ) ; if ( NumLidos != 2 || NumPilha >= MAX_PILS || NumPilha < 0 ) { return TST_CondRetParm ; } /* if */ CondRetObtido = PIL_DestruirPilha( vPilhas[NumPilha] ) ; return TST_CompararInt( CondRetEsperada , CondRetObtido , "Retorno errado destruir pilha." ); } /* fim ativa: Testar PIL Destruir Pilha de Cartas */ /* Testar PIL Destruir Pilha */ return TST_CondRetNaoConhec ; } /* Fim função: TPIL Efetuar operações de teste específicas para pilha de cartas */