大学生。我不能使用String类,还没有学过指针。
我在cardCopy()函数内部进行cout以确保strCopy()正在执行它的工作并且一切看起来很好,但是一旦我检查了deckCopy中的输出就不匹配了。
如果你编译,你会明白我的意思。
帮助?
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include <utility>
using namespace std;
/******************************************************************************
* STRINGS
******************************************************************************/
int strSize(char str[])
{
int size = 0;
while (str[size] != '\0')
{
size++;
}
return size;
}
/**
* Empty out a string
*
* @param str
*/
void strEmpty(char str[])
{
for (int i = 0; i < strSize(str); i++)
{
str[i] = 0;
}
}
/**
* Copy a string.
*
* @param from
* @param to
*/
void strCopy(char from[], char to[], int len)
{
//cout << "From: " << from << ", " << strSize(from) << endl;
for (int i = 0; i < len; i++)
{
to[i] = from[i];
}
//cout << "To: " << to << ", " << strSize(to) << endl;
}
/**
* Append one string to the end of another
*
* @param from
* @param to
*/
void strAppend(char from[], char to[])
{
// First we need to find the end of char to[]
int toPos= 0;
while (to[toPos] != '\0')
{
toPos++;
}
// Now that we have that, we can append
int fromPos = 0; // Start at the beginning of from array.
while (from[fromPos] != '\0')
{
to[toPos] = from[fromPos];
toPos++;
fromPos++;
}
toPos++;
to[toPos] = '\0'; // Add null terminator
}
/**
* Add an additional word to a sentence.
*
* @param from
* @param to
*/
void strAddWord(char from[], char to[])
{
char space[2] = " ";
strAppend(space, to);
strAppend(from, to);
}
/*******************************************************************************
* Card
******************************************************************************/
const int suits = 4;
const int suitStrSize = 9;
const int ranks = 13;
const int rankStrSize = 6;
const int cardsAmt = 52;
const int cardStrSize = 255;
const int locationStrSize = 10;
struct card
{
char suit[suitStrSize];
char rank[rankStrSize];
int cvalue;
char location[locationStrSize];
};
/**
* Print Card
*
* @param card
*/
void printCard(card card)
{
cout << card.rank << " of " << card.suit << endl;
}
/**
* Create a card with default values.
*
* @return card
*/
card makeCard()
{
card card;
strCopy("suit", card.suit, suitStrSize);
strCopy("rank", card.rank, rankStrSize);
card.cvalue = 0;
strCopy("location", card.location, locationStrSize);
return card;
}
/**
* Copy card
*
* @param from
* @param to
*/
void copyCard(card from, card to)
{
cout << "2) Card Copy, from: "; printCard(from);
to.cvalue = from.cvalue;
strCopy(from.location, to.location, locationStrSize);
strCopy(from.rank, to.rank, rankStrSize);
strCopy(from.suit, to.suit, suitStrSize);
cout << "3) Card Copy, to: "; printCard(to);
}
/*******************************************************************************
* Player
******************************************************************************/
const int playerAmt = 4;
const int handAmt = 3;
const int nameStrSize = 55;
struct player
{
char name[nameStrSize];
int total;
card hand[handAmt];
};
/**
* Print player name
*
* @param player
*/
void printPlayerName(player player)
{
cout << player.name << endl;
}
/**
* Print out player
*
* @param player
*/
void printPlayer(player player)
{
printPlayerName(player);
for (int i = 0; i < handAmt; i++)
{
cout << " ";
printCard(player.hand[i]);
}
}
/**
* Load players from file
*
* @param players
*/
void loadPlayers(player players[playerAmt])
{
fstream file;
file.open("players.txt");
int playerPos;
int playerElement;
char name[255];
char word[255];
for (int i = 0; !file.eof(); i++)
{
playerPos = i/2;
playerElement = i%2;
strEmpty(word);
file >> word;
switch(playerElement)
{
case 0:
strEmpty(name);
strCopy(word, name, nameStrSize);
break;
case 1:
strAddWord(word, name);
strCopy(name, players[playerPos].name, nameStrSize);
break;
}
}
cout << "Players loaded.";
}
/**
* Print players to screen
*
* @param players
*/
void printPlayers(player players[playerAmt])
{
for (int i = 0; i < playerAmt; i++)
{
cout << players[i].name << endl;
}
}
/*******************************************************************************
* Deck
******************************************************************************/
/**
* Create deck to file.
*/
void createDeck()
{
char suit[suits][suitStrSize] = {"Spades", "Clubs", "Diamonds", "Hearts"};
char rank[ranks][rankStrSize] = {"Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack",
"Queen", "King", "Ace"};
// Let's create our deck.
fstream deckfile;
deckfile.open("deck.txt", ios::trunc|ios::in|ios::out);
int suitCounter = 0;
for (int n = 0; n < cardsAmt; n++)
{
// Card names
deckfile << rank[n%ranks] << " ";
deckfile << suit[suitCounter] << " ";
int currentCard = (n%13) + 2;
// Scores
if (currentCard <= 10) {
deckfile << (n%13) + 2 << " ";
} else if (currentCard == 14) { // Ace
deckfile << 11 << endl;
} else {
deckfile << 10 << " ";
}
if (n%ranks == ranks - 1) // If you're finished with the rank
{
suitCounter++;
}
}
deckfile.close();
cout << "Deck Created.";
}
/**
* Load deck from file.
*
* @param deck
*/
void newDeck(card deck[cardsAmt])
{
fstream file;
file.open("deck.txt");
int deckPos;
int fileElement;
for (int i = 0; !file.eof(); i++)
{
deckPos = i/3;
fileElement = i%3;
switch (fileElement)
{
case 0:
file >> deck[deckPos].rank;
break;
case 1:
file >> deck[deckPos].suit;
break;
case 2:
file >> deck[deckPos].cvalue;
break;
}
}
file.close();
cout << "Fresh deck loaded.";
}
/**
* Print the deck.
*
* @param deck
*/
void printDeck(card deck[cardsAmt])
{
for (int i = 0; i < cardsAmt; i++)
{
printCard(deck[i]);
}
}
/**
* Copy deck
*
* @param from
* @param to
*/
void copyDeck(card from[cardsAmt], card to[cardsAmt])
{
for (int i = 0; i < cardsAmt; i++)
{
cout << "1) Deck Copy, from: "; printCard(from[i]);
copyCard(from[i], to[i]);
cout << "4) Deck Copy, to: "; printCard(to[i]); cout << endl;
}
}
/**
* Shuffle deck
*
* @param deck
*/
void shuffleDeck (card deck[cardsAmt], card shuffled[cardsAmt])
{
srand(time(NULL)); // Seed rand();
copyDeck(deck, shuffled);
// Shuffle
for (int i = 0; i < 300; i++)
{
swap(shuffled[rand()%cardsAmt], shuffled[rand()%cardsAmt]);
}
cout << "Deck shuffled.";
}
/**
* Save deck to file
*
* @param deck
*/
void saveDeck(card deck[cardsAmt])
{
// Write shuffled deck to file
fstream save;
save.open("saved.txt", ios::trunc|ios::in|ios::out);
for (int i = 0; i < cardsAmt; i++)
{
save << deck[i].rank << " of " << deck[i].suit << endl;
}
save.close();
cout << "Deck saved.";
}
/*******************************************************************************
* Game operations
******************************************************************************/
void deal(player players[playerAmt], card deck[cardsAmt])
{
int dealAmt = playerAmt * handAmt;
int playerCnt;
int handCnt;
for (int i = 0; i < dealAmt; i++)
{
playerCnt = i/3;
handCnt = i%3;
players[playerCnt].hand[handCnt] = deck[i];
}
cout << "Cards dealt.";
}
/**
* Print hands
*
* @param players
*/
void printHands(player players[playerAmt])
{
for (int i = 0; i < playerAmt; i++)
{
printPlayer(players[i]);
}
}
/**
* Print menu.
*/
void printMenu()
{
cout << endl;
cout << "Menu:" << endl;
cout << " 1 - Show menu" << endl;
cout << " 2 - Show Deck" << endl;
cout << " 3 - Shuffle Deck" << endl;
cout << " 4 - Save Deck" << endl;
cout << " 5 - Reset Deck" << endl;
cout << " 6 - Deal" << endl;
cout << " 7 - Show players" << endl;
cout << " 8 - Show hands" << endl;
cout << " 0 - Exit" << endl;
}
/*******************************************************************************
* Let's play
******************************************************************************/
/**
* Lets do work
*
* @return int
*/
int main()
{
createDeck();
cout << endl;
// Let's create our deck.
card deck[cardsAmt];
newDeck(deck);
cout << endl;
// Card positions
card shuffled[cardsAmt];
card discard[cardsAmt];
card stockPile[cardsAmt];
shuffleDeck(deck, shuffled);
printDeck(shuffled);
return 0;
// Now our players
player players[playerAmt];
loadPlayers(players);
cout << endl;
cout << endl << "Welcome! Let's play." << endl;
// Give them a menu
printMenu();
cout << endl;
do
{
int input;
cin >> input;
switch (input)
{
case 0: return 0;
case 1: printMenu(); break;
case 2: printDeck(deck); break;
case 3: shuffleDeck(deck, shuffled); break;
case 4: saveDeck(deck); break;
case 5: newDeck(deck); break;
case 6: deal(players, deck); break;
case 7: printPlayers(players); break;
case 8: printHands(players); break;
default:
cout << "Unknown option " << input ;
break;
}
cout << endl << endl;
} while (true);
}
答案 0 :(得分:0)
在此功能中:
void copyCard(card from, card to)
from和to参数按值传递,因此您在函数内所做的任何更改都不会反映在调用函数中。
最简单的更改是将to参数更改为通过引用传递:
void copyCard(card from, card &to)
注意:您可能还没有学过参考资料。它们值得查看,比指针更容易理解。
另请注意:在from
参数上使用引用也有效率原因,但它不是您当前问题的一部分。