大家好,我是编程新手,需要您的帮助。
我正在用C语言开发一个名为Hanabi的游戏。
我的问题是如何创建函数或如何使我的last_deckccard
变量告诉我deck_n
牌组中的最后一张卡片并为我提供始终更新的值,因为一旦使用了该卡片,它就会移至下一个,再也无法重复。
基本上,我希望您向每位玩家分发5张卡,last_deckcard
变量始终是卡组中最后一张可用的卡。
抱歉,如果我不能很好地解释您的问题,但是如果有人对您的建议表示感谢,那么现在,如果我的代码混乱而混乱,对不起,这就是我的第一场比赛。
我有疑问的子例程是get_card
:
get_card(int v[], int p1){
last_deckcard = 49-p1;
v[p1] = deck_n[last_deckcard];
}
我的输出是:
5 4 4 3 3 ********* 5 4 4 3 3
但应该是:
5 4 4 3 3 ********* 2 2 1 1 1
代码:
#include <stdlib.h> //Jogo HANABI
#include <math.h>
#include "graphsettings.h"
#include <time.h>
void show_deck();
shuffle(int v[], int dim);
inter(int deck_n[]);
color(int deck_n[], int index);
int deck[50];
//deck com os numeros todos do baralho
int deck_n[50] = {1,1,1,2,2,3,3,4,4,5,1,1,1,2,2,3,3,4,4,5,1,1,1,2,2,3,3,4,4,5,1,1,1,2,2,3,3,4,4,5,1,1,1,2,2,3,3,4,4,5};
//deck para determinar a cor da carta;verde/azul/amarelo/vermelho/ branco
int deck_c[50] ={1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5};
//variaveis para a mao dos jogadores ,
int hand_player1[5], hand_player2[5], table[5]= {0,0,0,0,0}, discard_table[5][5];
int last_deckcard;
int main(void)
{
srand(time(NULL));
int k;
system("mode con:cols=180 lines=50" ); //define o tamanho da console
//give 5 cards to each player
for (k=0; k < 5; k++){
get_card(hand_player1, k);
get_card(hand_player2, k);
}
return 0;
}
//A subrotine that show's the deck
void show_deck()
{
int i;
for (i= 0; i<50; i++)
{
printf("%d ", deck_n[i]);
}
}
//swap positions between a and b in vector v[]
swap (int v[], int a, int b)
{
int aux = v[a];
v[a] = v[b];
v[b] = aux;
}
// subroutine that shuffles a vector by creating a second vector array
// random r [] and sorting the latter, replicating all exchanges
// in the vector v []
shuffle(int v[], int dim){
int r[dim],k,m;
for (k=1;k<dim; k++)
{
r[k] = rand();//creation of random vector with same dimension as deck_n vector
}
for (m= 1; m<=dim-1 ;m++){
for (k=1; k<=dim-m; k++)
{
if (r[k] > r[k+1]){
swap(r, k, k+1);
swap(v, k, k+1);//replicate swap to deck_n vector to shuffle
}
}
}
}
//function to determine the last card available in the deck
//lastcard(int v[], int dim)
//{
// int last_deckcard, i;
//
// for(i=0;i<dim; i++)
// {
// last_deckcard = deck_n[dim-i];
// }
//}
// subroutine that fetches the last available card from the deck
// and put in the vector v [] at position p1
get_card(int v[], int p1){
last_deckcard = 49-p1;
v[p1] = deck_n[last_deckcard];
}
//interface
inter(int deck_n[])
{
int a=0,i;
for (i= 0; i<10; i++, a+=5){
// setColor(2,2);
// showRectAt(0+a,0,5,6);
// setColor(2,2);
// showRectAt(1+a,1,3,4); ter uma carta prenchida totalmente
// setColor(2,2);
// showRectAt(2+a,2,1,2);
// resetColor();
printf("\n");
showRectAt(0+a,0,5,6);
gotoxy(2+a,3);
setColor(15,0);
printf("%d",deck_n[i]);
resetColor();
printf("\n\n\n\n");
}
}
// subroutine that through the position of the number in the deck vector cannot
// determine the color of the card to which this number belongs
// for example: deck_n [3] = 1 which is equal to deck_c [3] = 1 logo is green
color(int deck_n[], int index)
{
deck_n[index] = deck_c[index];
switch(deck_c[index])
{
case 1://cor de verde
setColor(10,0);
break;
case 2://cor de azul
setColor(11,0);
//printf("o numero %d tem cor azul ",deck_n[index]);
break;
case 3://cor de amarelo
setColor(6,0);
break;
case 4://cor de vermelho
setColor(12,0);
break;
case 5://cor de branco
setColor(15,0);
break;
}
}