ช่วยทีครับ คิดไม่ออกแจกไพ่ได้แล้ว คือจะทำเกมไพ่ผสมสิบ โดยแจกไพ่ให้ผู้เล่นคนละ5ใบ แล้วดูว่ามีไพ่ใบไหนบ้างที่รวมกันได้ 10 แต้มบ้าง แล้วแสดงไพ่ที่เหลือที่รวมกันไม่ได้ 10 ถ้าไพ่ที่เหลือของใครมีแต้มน้อยที่สุดจะชนะทันที
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using std::cout;
using std::cin;
using std::endl;
using std::left;
using std::right;
using std::setw;
void shuffle(int[][13]);
void deal(const int[][13], const char *[], const char *[]);
int main()
{
// initialize suit array
const char *suit[4] =
{ "Hearts", "Diamonds", "Clubs", "Spades" };
// initialize face array
const char *face[13] =
{ "1", "2", "3", "4",
"5", "6", "7", "8",
"9", "10", "Jack", "Queen", "King" };
// initialize deck array
int deck[4][13] = { 0 };
srand(time(0)); // seed random number generator
shuffle(deck);
deal(deck, face, suit);
return 0; // indicates successful termination
}
void shuffle(int wDeck[][13])
{
int row;
int column;
// for each of the 52 cards, choose slot of deck randomly
for (int card = 1; card <= 52; card++) {
// choose new random location until unoccupied slot found
do {
row = rand() % 4;
column = rand() % 13;
} while (wDeck[row][column] != 0); // end do/while
// place card number in chosen slot of deck
wDeck[row][column] = card;
}
}
void deal(const int wDeck[][13],const char *wFace[],const char *wSuit[])
{
cout << "bot1 = ";
// for each of the 52 cards
for (int card = 1; card <= 5; card++)
// loop through rows of wDeck
for (int row = 0; row <= 3; row++)
// loop through columns of wDeck for current row
for (int column = 0; column <= 12; column++)
// if slot contains current card, display card
if (wDeck[row][column] == card) {
cout << wFace[column] << " of " << wSuit[row] << ",";
}
cout << endl;
cout << "bot2 = ";
for (int card = 6; card <= 10; card++)
// loop through rows of wDeck
for (int row = 0; row <= 3; row++)
// loop through columns of wDeck for current row
for (int column = 0; column <= 12; column++)
// if slot contains current card, display card
if (wDeck[row][column] == card) {
cout << wFace[column] << " of " << wSuit[row] << ",";
}
cout << endl;
cout << "bot3 = ";
for (int card = 11; card <= 15; card++)
// loop through rows of wDeck
for (int row = 0; row <= 3; row++)
// loop through columns of wDeck for current row
for (int column = 0; column <= 12; column++)
// if slot contains current card, display card
if (wDeck[row][column] == card) {
cout << wFace[column] << " of " << wSuit[row] << ",";
}
cout << endl;
cout << "bot4 = ";
for (int card = 16; card <= 20; card++)
// loop through rows of wDeck
for (int row = 0; row <= 3; row++)
// loop through columns of wDeck for current row
for (int column = 0; column <= 12; column++)
// if slot contains current card, display card
if (wDeck[row][column] == card) {
cout << wFace[column] << " of " << wSuit[row] << ",";
}
cout << endl;
cout << "bot5 = ";
for (int card = 21; card <= 25; card++)
// loop through rows of wDeck
for (int row = 0; row <= 3; row++)
// loop through columns of wDeck for current row
for (int column = 0; column <= 12; column++)
// if slot contains current card, display card
if (wDeck[row][column] == card) {
cout << wFace[column] << " of " << wSuit[row] << ",";
}
cout << endl;
cout << "bot6 = ";
for (int card = 26; card <= 30; card++)
// loop through rows of wDeck
for (int row = 0; row <= 3; row++)
// loop through columns of wDeck for current row
for (int column = 0; column <= 12; column++)
// if slot contains current card, display card
if (wDeck[row][column] == card) {
cout << wFace[column] << " of " << wSuit[row] << ",";
}
cout << endl;
}
อันนี้คือโค้ดที่เขียนถึงแจกไพ่ครับ
code เกมส์สมสิบ c++
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using std::cout;
using std::cin;
using std::endl;
using std::left;
using std::right;
using std::setw;
void shuffle(int[][13]);
void deal(const int[][13], const char *[], const char *[]);
int main()
{
// initialize suit array
const char *suit[4] =
{ "Hearts", "Diamonds", "Clubs", "Spades" };
// initialize face array
const char *face[13] =
{ "1", "2", "3", "4",
"5", "6", "7", "8",
"9", "10", "Jack", "Queen", "King" };
// initialize deck array
int deck[4][13] = { 0 };
srand(time(0)); // seed random number generator
shuffle(deck);
deal(deck, face, suit);
return 0; // indicates successful termination
}
void shuffle(int wDeck[][13])
{
int row;
int column;
// for each of the 52 cards, choose slot of deck randomly
for (int card = 1; card <= 52; card++) {
// choose new random location until unoccupied slot found
do {
row = rand() % 4;
column = rand() % 13;
} while (wDeck[row][column] != 0); // end do/while
// place card number in chosen slot of deck
wDeck[row][column] = card;
}
}
void deal(const int wDeck[][13],const char *wFace[],const char *wSuit[])
{
cout << "bot1 = ";
// for each of the 52 cards
for (int card = 1; card <= 5; card++)
// loop through rows of wDeck
for (int row = 0; row <= 3; row++)
// loop through columns of wDeck for current row
for (int column = 0; column <= 12; column++)
// if slot contains current card, display card
if (wDeck[row][column] == card) {
cout << wFace[column] << " of " << wSuit[row] << ",";
}
cout << endl;
cout << "bot2 = ";
for (int card = 6; card <= 10; card++)
// loop through rows of wDeck
for (int row = 0; row <= 3; row++)
// loop through columns of wDeck for current row
for (int column = 0; column <= 12; column++)
// if slot contains current card, display card
if (wDeck[row][column] == card) {
cout << wFace[column] << " of " << wSuit[row] << ",";
}
cout << endl;
cout << "bot3 = ";
for (int card = 11; card <= 15; card++)
// loop through rows of wDeck
for (int row = 0; row <= 3; row++)
// loop through columns of wDeck for current row
for (int column = 0; column <= 12; column++)
// if slot contains current card, display card
if (wDeck[row][column] == card) {
cout << wFace[column] << " of " << wSuit[row] << ",";
}
cout << endl;
cout << "bot4 = ";
for (int card = 16; card <= 20; card++)
// loop through rows of wDeck
for (int row = 0; row <= 3; row++)
// loop through columns of wDeck for current row
for (int column = 0; column <= 12; column++)
// if slot contains current card, display card
if (wDeck[row][column] == card) {
cout << wFace[column] << " of " << wSuit[row] << ",";
}
cout << endl;
cout << "bot5 = ";
for (int card = 21; card <= 25; card++)
// loop through rows of wDeck
for (int row = 0; row <= 3; row++)
// loop through columns of wDeck for current row
for (int column = 0; column <= 12; column++)
// if slot contains current card, display card
if (wDeck[row][column] == card) {
cout << wFace[column] << " of " << wSuit[row] << ",";
}
cout << endl;
cout << "bot6 = ";
for (int card = 26; card <= 30; card++)
// loop through rows of wDeck
for (int row = 0; row <= 3; row++)
// loop through columns of wDeck for current row
for (int column = 0; column <= 12; column++)
// if slot contains current card, display card
if (wDeck[row][column] == card) {
cout << wFace[column] << " of " << wSuit[row] << ",";
}
cout << endl;
}
อันนี้คือโค้ดที่เขียนถึงแจกไพ่ครับ