更新 1.cpp

Signed-off-by: ChenKaiLiuG <ckliu119@gmail.com>
This commit is contained in:
2025-12-30 06:43:50 +00:00
parent 12209cd342
commit ed5cb1df40

22
1.cpp
View File

@@ -8,34 +8,33 @@
#include <windows.h> #include <windows.h>
using namespace std; using namespace std;
// test input
int try_num(string b) int try_num(string b)
{ {
int bb; int bb;
try try
{ {
bb = stoi(b); bb = stoi(b); //try int
} }
catch (invalid_argument) catch (invalid_argument) // catch invaild input
{ {
return -1; return -1;
} }
return bb; return bb;
} }
string cardname[21]; string cardname[21];
int score[7]; int score[7];
//define cardname //define cardname
array<int, 21> calculate(int arr[40]) array<int, 21> calculate(int arr[40])
{ {
array<int, 21> cal; array<int, 21> cal;
fill(cal.begin(), cal.end(), 0); fill(cal.begin(), cal.end(), 0); // initilize
for (int j = 0; j < 40; j++) for (int j = 0; j < 40; j++)
{ {
if (arr[j] == -1) if (arr[j] == -1) continue; // pass blank
continue; else cal[arr[j]]++;
else
cal[arr[j]]++;
} }
return cal; return cal;
} }
@@ -57,6 +56,7 @@ void display_p(int arr[][40], int num)
cout << "\n"; cout << "\n";
} }
} }
//display players' cards
void display_s(int arr2[][3], int flag[5], int num) void display_s(int arr2[][3], int flag[5], int num)
{ {
@@ -65,6 +65,7 @@ void display_s(int arr2[][3], int flag[5], int num)
cout << "Stack " << i + 1 << ": "; cout << "Stack " << i + 1 << ": ";
for (int j = 0; j < 3; j++) for (int j = 0; j < 3; j++)
{ {
// show color by ANSI code
if (arr2[i][j] == 1) if (arr2[i][j] == 1)
cout << " \033[31m red \033[0m"; cout << " \033[31m red \033[0m";
else if (arr2[i][j] == 2) else if (arr2[i][j] == 2)
@@ -85,10 +86,11 @@ void display_s(int arr2[][3], int flag[5], int num)
cout << " +2_point "; cout << " +2_point ";
} }
if (flag[i] == 2) if (flag[i] == 2)
cout << "(taken)"; cout << "(taken)"; // mark taken cards
cout << "\n"; cout << "\n";
} }
} }
//dispaly stack
void getcard(int deck[76], bool vis[76], int stack[5][3], int flag_s[5], int pynum, int& counter, int cp_chk) void getcard(int deck[76], bool vis[76], int stack[5][3], int flag_s[5], int pynum, int& counter, int cp_chk)
{ {