Biciobicio (Normal User)
Newbie
Messaggi: 3
Iscritto: 05/10/2006
|
Ciao a tutti,
mi sono appena iscritto. Ho cominciato a studiare il C questa estate da autodidatta anche se non ho alcuna esperienza di programmazione (faccio il camionista). Volevo chiedervi qualche consiglio relativamente agli esercizi perchè ovviamente non ho alcun punto di riferimento. Questo richiedeva di implementare un programma sul poker. Funziona perfettamente ma vorrei sapere se e come è migliorabile. Grazie
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void shuffle (int wDeck[][13]);
void deal (int wDeck[][13], const char *wFace[], const char *wSuit[],
int *oneSuit, int *oneFace,
int *twoSuit, int *twoFace);
void result (int *seed, int *pict, int *score);
int main()
{
const char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};
const char *face[13] =
{"Ace", "Deuce", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King"};
int pOneSuit[4] = {0}, pOneFace[13] = {0};
int pTwoSuit[4] = {0}, pTwoFace[13] = {0};
int deck[4][13] = {0};
int onePoint = 0, twoPoint = 0;
srand (time(0));
shuffle (deck);
deal (deck, face, suit, pOneSuit, pOneFace, pTwoSuit, pTwoFace);
printf ("Player one has: ");
result (pOneSuit, pOneFace, &onePoint);
if (onePoint == 0)
printf ("Nothing");
printf ("\n");
printf ("Player two has: ");
result (pTwoSuit, pTwoFace, &twoPoint);
if (twoPoint == 0)
printf ("Nothing");
printf ("\n");
if (onePoint > twoPoint){
printf ("--------------\n");
printf ("Player one win\n");
printf ("--------------\n");
}
else if (twoPoint > onePoint){
printf ("--------------\n");
printf ("Player two win\n");
printf ("--------------\n");
}
system ("pause");
return 0;
}
void shuffle (int wDeck[][13])
{
int row;
int column;
int card;
for (card = 1; card <= 52; card++){
do{
row = rand() % 4;
column = rand() % 13;
}while (wDeck[row][column] != 0);
wDeck[row][column] = card;
}
}
void deal (int wDeck[][13], const char *wFace[], const char *wSuit[],
int *oneSuit, int *oneFace,
int *twoSuit, int *twoFace)
{
int card;
int row;
int column;
printf ("%15s%22s", "Player One", "Player Two\n\n");
for (card = 1; card <= 10; card++){
for (row = 0; row <= 3; row++){
for (column = 0; column <= 12; column++){
if (wDeck[row][column] == card){
if ((card % 2) == 0){
++twoSuit[row];
++twoFace[column];
}
else{
++oneSuit[row];
++oneFace[column];
}
printf ("%8s of %-8s", wFace[column], wSuit[row]);
if ((card % 2) == 0)
printf ("\n");
}
}
}
}
printf ("\n");
}
void result (int *seed, int *pict, int *score)
{
int pair (int *pict);
int twoPair (int *pict);
int threeOfKind (int *pict);
int poker (int *pict);
int straight (int *pict);
int color (int *seed);
*score += pair (pict);
*score += twoPair (pict);
*score += threeOfKind (pict);
*score += poker (pict);
*score += straight (pict);
*score += color (seed);
}
int pair (int *pict)
{
int card, fPair, count = 0;
for (card = 0; card <= 12; card++){
if (pict[card] == 2){
fPair = card;
++count;
if (pict[card] == 3)
return 0;
}
}
if (count == 1){
printf ("Pair");
return 1 + fPair;
}
else return 0;
}
int twoPair (int *pict)
{
int card, count = 0, fPair, sPair;
for (card = 0; card <= 12; card++){
if (pict[card] == 2 && count == 1){
sPair = card;
count++;
}
else if (pict[card] == 2 && count == 0){
count++;
fPair = card;
}
}
if (count == 2){
printf ("Two Pair");
return 20 + sPair;
}
else return 0;
}
int threeOfKind (int *pict)
{
int card, tris;
for (card = 0; card <= 12; card++){
if (pict[card] == 3){
tris = card;
printf ("Three of a kind");
return 40 + tris;
}
}
}
int poker (int *pict)
{
int card, pok;
for (card = 0; card <= 12; card++){
if (pict[card] == 4){
pok = card;
printf ("Poker!!!");
return 100 + pok;
}
}
return 0;
}
int straight (int *pict)
{
int card, count = 0, firstPict, lastPict;
for (card = 0; card <= 12; card++){
if (pict[card] == 1){
++count;
if (count == 1)
firstPict = card;
if (count == 5)
lastPict = card;
}
}
if ((lastPict - firstPict) == 4 && count == 5){
printf ("Straight");
return 60 + lastPict;
}
else return 0;
}
int color (int *seed)
{
int card;
for (card = 0; card <= 3; card++){
if (seed[card] == 5){
printf ("Color");
return 80 + card;
}
}
return 0;
}
|