Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //GoFish.c
- //DSC
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- void printDeck(int deck[][5]);
- int goFish(int deck[][5], int player);
- void printHand(int p, int d[][5]);
- char ps(int suit);
- int checkInput(int chkfor, int p, int d[][5]);
- int checkPlayer(int chkfor, int src, int tgt, int deck[][5]);
- void checkFull(int deck[][5], int p);
- void printScores(int d[][5]);
- int checkEmpty(int p, int d[][5]);
- int checkGameOver(int d[][5]);
- void determineWinner(int d[][5]);
- // Deck[x][y] where
- // x = Rank (2-10,J=11,Q=12,K=13,A=14)
- // y = Suit (0=Status,1=Club,2=Diamond,3=Heart,4=Spade)
- int main(void)
- {
- int deck[15][5] = { { 0 },{ 0 } };
- //int score[15][5]; // Use [0][x] as Deck Score
- int p, x;
- //int r=0,s=0;
- int chkfor;
- int chkplayer;
- int valinp;
- int newcard;
- int gameOver = 1;
- char rank;
- // Seed the random number generator
- srand(time(NULL));
- printf("Welcome to Go Fish\n");
- printf("Dealing cards...\n");
- for (x = 1; x <= 5; x++) {
- for (p = 1; p <= 3; p++) {
- //printf("GoFish:P=%d\n",p);
- goFish(deck, p);
- //printDeck(deck);
- }
- }
- // Manually assign some cards
- //Print the results of the deal (debug)
- //printDeck(deck);
- // Take turns asking
- p = 1; // Player 1
- while (gameOver) { // while always true, need to fix that
- valinp = 0; // Get some valid input
- while (!valinp) {
- // Make sure the player has a card to select first
- if (checkEmpty(p, deck)) {
- // If P's hand is empty, we need another card...
- printf("You have no cards! Go Fish!\n");
- if (goFish(deck, p) == -1) { // No more cards in the deck
- printf("Player %d is Out\n", p);
- // Move on to the next player
- if (p < 3) {
- p++;
- }
- else {
- p = 1;
- }
- printf("New Player = %d\n", p);
- }
- }
- else { // Continue with current player
- //printDeck(deck);
- printf("Player %d, please select your card:\n", p);
- printf("P%d = ",p);
- printHand(p, deck);
- /*printf("P2 = ");
- printHand(2, deck);
- printf("P3 = ");
- printHand(3, deck);*/
- scanf("%s", &rank);
- char str2[];
- str2="j";
- int result;
- result = strcmp(rank, str2)==0;
- if (result <0 && result >0){
- chkfor=11;}
- if (chkfor == 0) { //debug
- printScores(deck);
- }
- if (checkInput(chkfor, p, deck)) {
- valinp = 1;
- // Select a player to ask
- int notSelf;
- do {
- notSelf = 0;
- printf("Player %d, who do you want to ask?\n", p);
- //Debug:
- /*printf("P1 = ");
- printHand(1, deck);
- printf("P2 = ");
- printHand(2, deck);
- printf("P3 = ");
- printHand(3, deck);
- printf("\n");*/
- scanf("%d", &chkplayer);
- if (chkplayer == p) {
- printf("You can't pick yourself!\n");
- notSelf = 1;
- }
- else if ((chkplayer >3 || chkplayer <1)){
- printf("Choose valid player\n");
- notSelf = 1;
- }
- } while (notSelf);
- //check for yourself
- if (checkPlayer(chkfor, p, chkplayer, deck)) {
- // Go Again
- }
- else {
- // Go Fish
- printf("No Match. Go fish.\n");
- newcard = goFish(deck, p);
- printf("Hoped for %d, Fished %d\n", chkfor, newcard);
- if (newcard == chkfor) {
- // if gofish=1, go again
- printf("Got what we asked for! Go Again\n");
- }
- else {
- //printf("Else Move on\n");
- // Move on to the next player
- if (p < 3) {
- p++;
- }
- else {
- p = 1;
- }
- printf("Enter ""c"" to clear screen for next player\n"); //allows player to clear the screen
- while ( getchar() != 'c'); //so the next player doesnt see his hand
- for (int i = 0; i < 100; i++) { //makes a bunch of newlines to "clear" the screen
- printf("\n");
- }
- printf("Enter ""c"" to continue to next player's turn\n");
- while ( getchar() != 'c');
- for (int i = 0; i < 100; i++) { //makes a bunch of newlines to "clear" the screen
- printf("\n");
- }
- //printf("New Player = %d\n", p);
- //turn over
- }
- }
- // Check to see if Player owns all 4, and remove from hand
- //printf("Check Full\n");
- checkFull(deck, p);
- }
- else { // Input was not valid
- printf("Please select a card you have\n");
- valinp = 0;
- }
- } // switching players
- }
- /*int rank, suit;
- int gameOver = 0;
- for (rank = 2; rank <= 14; rank++) {
- for (suit = 1; suit <= 4; suit++) {
- deck[rank][suit] = -1;
- }
- }*/
- gameOver = checkGameOver(deck);
- //This is where need to determine if the game is over
- //game over = all cards = -1
- }
- return 0;
- }
- int goFish(int d[][5], int player)
- {
- int rank, suit;
- int found = 0;
- // Need to check if deck has any cards available before
- // doing a random seek
- for (rank = 2; rank <= 14; rank++) {
- for (suit = 1; suit <= 4; suit++) {
- if (d[rank][suit] == 0) {
- found = 1;
- }
- }
- }
- if (found) { // We found at least 1 card available
- found = 0; // now go look for a random one
- while (!found) {
- rank = (2 + rand() % 13);
- suit = (1 + rand() % 4);
- //printf("Check %d,%d\n",rank,suit);
- if (d[rank][suit] == 0) {
- found = 1;
- d[rank][suit] = player;
- //printf("ASSIGN: P%d=%d,%d=%d\n",player,rank,suit,d[rank][suit]);
- }
- }
- }
- else { // There are no cards to GoFish
- printf("The deck is empty.\n");
- return -1;
- }
- return rank;
- }
- void printDeck(int d[][5])
- {
- int x, y;
- printf("R 2 3 4 5 6 7 8 9 0 J Q K A\n");
- printf("-:--------------------------\n");
- for (x = 0; x <= 4; x++) {
- switch (x) {
- case 0: printf("W: "); break;
- case 1: printf("C: "); break;
- case 2: printf("D: "); break;
- case 3: printf("H: "); break;
- case 4: printf("S: "); break;
- }
- for (y = 2; y <= 14; y++) {
- printf("%d ", d[y][x]);
- }
- printf("\n");
- }
- printf("\n");
- }
- void printHand(int p, int d[][5])
- {
- // Prints out the hand for a specific player
- int r, s;
- char c;
- for (r = 2; r <= 14; r++) {
- for (s = 1; s <= 4; s++) {
- if (d[r][s] == p) {
- switch (s) {
- case 1: c = 'C'; break;
- case 2: c = 'D'; break;
- case 3: c = 'H'; break;
- case 4: c = 'S'; break;
- }
- printf("%d%c ", r, c);
- }
- }
- }
- printf("\n");
- }
- char ps(int suit)
- {
- // Return the suit associated with that value
- switch (suit) {
- case 1: return 'C'; break;
- case 2: return 'D'; break;
- case 3: return 'H'; break;
- case 4: return 'S'; break;
- }
- return ' ';
- }
- int checkEmpty(int p, int deck[][5])
- {
- // Check to see if the player has any cards,
- // If the player's hand is empty, return 1 (true)
- int r, s;
- //printf("CheckEmpty P=%d\n", p);
- for (r = 2; r <= 14; r++) {
- for (s = 1; s <= 4; s++) {
- if (deck[r][s] == p) {
- //printf("Return 0");
- return 0;
- }
- }
- }
- // Nothing hit, we must be empty
- //printf("Return 1");
- return 1;
- }
- int checkInput(int chkfor, int p, int d[][5]) //checks if you have card
- {
- int s;
- for (s = 1; s <= 4; s++) {
- if (d[chkfor][s] == p) {
- printf("Found\n");
- return 1;
- }
- }
- return 0;
- }
- int checkPlayer(int chkfor, int src, int tgt, int deck[][5]) { //checkPlayer determines if the target player has the card the source player is looking for.
- int suit;
- int found;
- found = 0;
- for (suit = 1; suit <= 4; suit++) {
- if (deck[chkfor][suit] == tgt) {
- found = 1;
- printf("Found %d%c\n",chkfor,ps(suit));
- deck[chkfor][suit] = src;
- }
- }
- if (found)
- return 1;
- else
- return 0;
- }
- void checkFull(int deck[][5], int p) //checks if player has all of specific rank
- {
- int r, s;
- int rankctr;
- for (r = 2; r <= 14; r++) {
- rankctr = 0;
- for (s = 1; s <= 4; s++) {
- if (deck[r][s] == p) {
- rankctr++;
- }
- }
- if (rankctr == 4) {
- // We have all 4 cards.
- // Set "owner" bit to player
- printf("Player %d has all the %d's\n", p, r);
- deck[r][0] = p;
- // Remove 4 cards from player's hand
- for (s = 1; s <= 4; s++) {
- deck[r][s] = -1;
- }
- }
- }
- }
- void printScores(int d[][5])
- {
- int y;
- printf("Scoreboard\n");
- printf("2 3 4 5 6 7 8 9 0 J Q K A\n");
- printf("--------------------------\n");
- for (y = 2; y <= 14; y++) {
- printf("%d ", d[y][0]);
- }
- printf("\n");
- printf("\n");
- }
- int checkGameOver(int d[][5]) {
- int rank, suit;
- int gameOver = 0;
- for (rank = 2; rank <= 14; rank++) {
- for (suit = 1; suit <= 4; suit++) {
- if (d[rank][suit] != -1) {
- gameOver = 1;
- }
- }
- }
- return gameOver;
- }
- void determineWinner(int d[][5]) {
- int p1Score = 0, p2Score = 0, p3Score = 0;
- int winner;
- //Tallies score
- for (int i = 2; i < 14; i++) {
- switch (d[i][0]) {
- case 1: p1Score++;
- break;
- case 2: p2Score++;
- break;
- case 3: p3Score++;
- break;
- }
- }
- //determines winner
- if ((p1Score > p2Score) && (p1Score > p3Score)) {
- winner = 1;
- }
- else if ((p2Score > p1Score) && (p2Score > p3Score)) {
- winner = 2;
- }
- else if ((p3Score > p1Score) && (p3Score > p2Score)) {
- winner = 3;
- } else if ((p1Score == p2Score) && (p1Score > p3Score)) {
- winner = 12;
- }
- else if ((p1Score == p3Score) && (p1Score > p2Score)) {
- winner = 13;
- }
- else if ((p2Score == p3Score) && (p2Score > p1Score)) {
- winner = 23;
- }
- printf("Player 1 Score: %d\n", p1Score);
- printf("Player 2 Score: %d\n", p2Score);
- printf("Player 3 Score: %d\n", p3Score);
- switch (winner) {
- case 1: printf("Player 1 wins!"); break;
- case 2: printf("Player 2 wins!"); break;
- case 3: printf("Player 3 wins!"); break;
- case 12: printf("Players 1 and 2 tie!"); break;
- case 13: printf("Players 1 and 3 tie!"); break;
- case 23: printf("Players 2 and 3 tie!"); break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement