This repository has been archived on 2024-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
justhomework/DataStructure/Code/ex06/ex6.cpp

260 lines
6.7 KiB
C++
Raw Permalink Normal View History

2021-10-29 07:46:26 +00:00
#include "vec.h"
#include "list.h"
2021-10-28 15:05:13 +00:00
#include "score.h"
2021-10-27 15:40:44 +00:00
#include <time.h>
#include <stdio.h>
2021-10-28 15:05:13 +00:00
2021-10-27 15:40:44 +00:00
std::string PU[54] = {
"3D", "3C", "3B", "3A",
"4D", "4C", "4B", "4A",
"5D", "5C", "5B", "5A",
"6D", "6C", "6B", "6A",
"7D", "7C", "7B", "7A",
"8D", "8C", "8B", "8A",
"9D", "9C", "9B", "9A",
"XD", "XC", "XB", "XA",
"JD", "JC", "JB", "JA",
"QD", "QC", "QB", "QA",
"KD", "KC", "KB", "KA",
"1D", "1C", "1B", "1A",
"2D", "2C", "2B", "2A",
"BJ", "RJ"};
int main()
{
Score s(3);
int xipai_flag = 0;
2021-10-29 08:51:54 +00:00
printf("生成随机扑克序列\n");
2021-10-27 15:40:44 +00:00
srand(time(NULL));
Vec v(54, 2);
List l;
2021-10-29 08:51:54 +00:00
printf("开始建立链表\n");
2021-10-27 15:40:44 +00:00
for (int i = 0; i <= 53; i++)
{
l.firstInsert(v.get(i));
std::cout << PU[l[0]] << " ";
if (i % 2 != 0)
printf("\n");
}
Begin:
2021-10-29 08:51:54 +00:00
printf("\n是否重新洗牌?(0/1)");
2021-10-27 15:40:44 +00:00
scanf("%d", &xipai_flag);
if (xipai_flag == 1)
{
v.reorder();
l.clear();
2021-10-29 08:51:54 +00:00
printf("===============开始建立链表===============\n");
2021-10-27 15:40:44 +00:00
for (int i = 0; i <= 53; i++)
{
l.firstInsert(v.get(i));
std::cout << PU[l[0]] << " ";
if (i % 2 != 0)
printf("\n");
}
xipai_flag == 0;
goto Begin;
}
2021-10-29 08:51:54 +00:00
printf("================开始发牌================\n");
2021-10-27 15:40:44 +00:00
List p1, p2, p3;
for (int i = 0; i <= 53; i++)
{
switch (i % 3)
{
case 0:
p1.firstInsert(l[i]);
break;
case 1:
p2.firstInsert(l[i]);
break;
case 2:
p3.firstInsert(l[i]);
break;
}
}
2021-10-29 08:51:54 +00:00
printf("你手中的牌:\n");
2021-10-28 11:30:56 +00:00
for (int i = 0; i < p1.getSize(); i++)
{
std::cout << i << ": " << PU[(p1)[i]] << "\n";
}
2021-10-27 15:40:44 +00:00
l.clear();
p1.sort();
p2.sort();
p3.sort();
l.firstInsert(-1);
int opt = 0;
int pass_num = 0;
int round = 1;
int finish_num = 0;
2021-10-29 08:51:54 +00:00
printf("理牌完成,选择你的操作:\n");
2021-10-28 11:30:56 +00:00
while (finish_num < 2 && round < 54)
2021-10-27 15:40:44 +00:00
{
2021-10-27 16:20:29 +00:00
if (pass_num > 3 - finish_num)
pass_num = 3 - finish_num;
2021-10-29 08:51:54 +00:00
printf("+++++===========================+++++\n 轮次%d \n+++++===========================+++++\n", round);
2021-10-27 16:20:29 +00:00
2021-10-27 15:40:44 +00:00
if (p1.getSize())
{
2021-10-29 08:51:54 +00:00
printf("你手中的牌:\n");
2021-10-27 15:40:44 +00:00
for (int i = 0; i < p1.getSize(); i++)
{
std::cout << i << ": " << PU[(p1)[i]] << "\n";
}
2021-10-29 08:51:54 +00:00
printf("剩余%d张牌\n", p1.getSize());
2021-10-27 15:40:44 +00:00
2021-10-28 15:02:22 +00:00
// if (l[0] > p1[p1.getSize() - 1])
// {
// if (pass_num == 2 - finish_num)
// {
// pass_num = 0;
// goto A;
// }
2021-10-29 08:51:54 +00:00
// printf("无法出牌,已自动跳过\n");
2021-10-28 15:02:22 +00:00
// }
2021-10-27 16:20:29 +00:00
A:
2021-10-29 08:51:54 +00:00
printf("0出牌1查看牌堆2跳过");
2021-10-27 15:40:44 +00:00
scanf("%d", &opt);
switch (opt)
{
2021-10-28 15:02:22 +00:00
case 0:
2021-10-29 08:51:54 +00:00
printf("选择:");
2021-10-27 15:40:44 +00:00
scanf("%d", &opt);
2021-10-28 15:02:22 +00:00
if (opt >= p1.getSize() || (p1[opt] < l[0]&&pass_num!=2))
2021-10-27 15:40:44 +00:00
{
2021-10-29 08:51:54 +00:00
printf("错误输入\n");
2021-10-27 15:40:44 +00:00
goto A;
}
2021-10-29 08:51:54 +00:00
std::cout << "打出" << PU[l.firstInsert((p1).remove(opt))];
2021-10-28 15:02:22 +00:00
if (p1.getSize() == 0)
2021-10-27 15:40:44 +00:00
{
s.finish(0);
2021-10-29 08:51:54 +00:00
printf(",玩家A出完所有牌\n");
2021-10-27 15:40:44 +00:00
finish_num++;
2021-10-28 11:30:56 +00:00
pass_num = 0;
2021-10-27 15:40:44 +00:00
}
else
2021-10-29 08:51:54 +00:00
printf(",剩余%d张牌\n", p1.getSize());
2021-10-28 15:02:22 +00:00
pass_num=0;
2021-10-27 15:40:44 +00:00
break;
2021-10-28 15:02:22 +00:00
case 1:
2021-10-27 15:40:44 +00:00
for (int i = 0; i < l.getSize() - 1; i++)
{
std::cout << i << ": " << PU[l[i]];
printf("\n");
}
goto A;
2021-10-28 15:02:22 +00:00
case 2:
pass_num++;
goto B;
2021-10-27 15:40:44 +00:00
default:
2021-10-29 08:51:54 +00:00
printf("错误输入\n");
2021-10-27 15:40:44 +00:00
goto A;
}
}
2021-10-27 16:20:29 +00:00
else
2021-10-29 08:51:54 +00:00
printf("玩家A轮空\n");
2021-10-27 15:40:44 +00:00
B:
if (p2.getSize())
{
2021-10-28 11:30:56 +00:00
printf("--------------------\n");
2021-10-29 08:51:54 +00:00
printf("B玩家");
2021-10-27 15:40:44 +00:00
if (pass_num == 2 - finish_num)
{
2021-10-29 08:51:54 +00:00
std::cout << "打出" << PU[l.firstInsert((p2).remove(0))];
printf(",剩余%d张牌\n", p2.getSize());
2021-10-27 15:40:44 +00:00
pass_num = 0;
}
else if (l[0] > p2[p2.getSize() - 1])
{
2021-10-29 08:51:54 +00:00
printf("无牌可出,跳过\n");
2021-10-27 15:40:44 +00:00
pass_num++;
}
else
for (int i = 0; i < p2.getSize(); i++)
{
if (l[0] < p2[i])
{
2021-10-29 08:51:54 +00:00
std::cout << "打出" << PU[l.firstInsert((p2).remove(i))];
2021-10-28 11:30:56 +00:00
2021-10-29 08:51:54 +00:00
printf(",剩余%d张牌\n", p2.getSize());
2021-10-28 15:02:22 +00:00
pass_num = 0;
2021-10-27 15:40:44 +00:00
break;
}
}
2021-10-28 11:30:56 +00:00
if (p2.getSize() == 0)
{
s.finish(1);
2021-10-29 08:51:54 +00:00
printf("玩家B出完所有牌\n");
2021-10-28 11:30:56 +00:00
finish_num++;
pass_num = 0;
}
2021-10-27 15:40:44 +00:00
}
2021-10-27 16:20:29 +00:00
else
2021-10-29 08:51:54 +00:00
printf("玩家B轮空\n");
2021-10-27 15:40:44 +00:00
C:
if (p3.getSize())
{
2021-10-28 11:30:56 +00:00
printf("--------------------\n");
2021-10-29 08:51:54 +00:00
printf("C玩家");
2021-10-27 15:40:44 +00:00
if (pass_num == 2 - finish_num)
{
2021-10-29 08:51:54 +00:00
std::cout << "打出" << PU[l.firstInsert((p3).remove(0))];
printf(",剩余%d张牌\n", p3.getSize());
2021-10-27 15:40:44 +00:00
pass_num = 0;
}
else if (l[0] > p3[p3.getSize() - 1])
{
2021-10-29 08:51:54 +00:00
printf("无牌可出,跳过\n");
2021-10-27 15:40:44 +00:00
pass_num++;
}
else
for (int i = 0; i < p3.getSize(); i++)
{
if (l[0] < p3[i])
{
2021-10-29 08:51:54 +00:00
std::cout << "打出" << PU[l.firstInsert((p3).remove(i))];
2021-10-28 11:30:56 +00:00
2021-10-29 08:51:54 +00:00
printf(",剩余%d张牌\n", p3.getSize());
2021-10-28 15:02:22 +00:00
pass_num = 0;
2021-10-27 15:40:44 +00:00
break;
}
}
2021-10-28 11:30:56 +00:00
if (p3.getSize() == 0)
{
s.finish(2);
2021-10-29 08:51:54 +00:00
printf("玩家C出完所有牌\n");
2021-10-28 11:30:56 +00:00
finish_num++;
pass_num = 0;
}
2021-10-27 15:40:44 +00:00
}
2021-10-27 16:20:29 +00:00
else
2021-10-29 08:51:54 +00:00
printf("玩家C轮空\n");
2021-10-27 15:40:44 +00:00
round++;
}
printf("=====================================\n");
2021-10-29 08:51:54 +00:00
printf("分数结算:\n");
printf("玩家A:%d分\n", s.score(0));
printf("玩家B:%d分\n", s.score(1));
printf("玩家C:%d分\n", s.score(2));
2021-10-27 15:40:44 +00:00
2021-10-28 15:02:22 +00:00
system("pause");
2021-10-27 15:40:44 +00:00
return 0;
}