This commit is contained in:
iridiumR 2021-10-27 23:40:44 +08:00
parent 06731225c0
commit 24618eef54
9 changed files with 829 additions and 8 deletions

View file

@ -104,7 +104,7 @@ select:
} }
while (1) while (1)
{ {
printf("剩余%d张牌\n", pai->getSize());
printf("´ĹĆ2łöĹĆ3˛éż´ĹĆśŃ4˝áĘř"); printf("´ĹĆ2łöĹĆ3˛éż´ĹĆśŃ4˝áĘř");
scanf("%d", &opt); scanf("%d", &opt);
switch (opt) switch (opt)
@ -114,15 +114,16 @@ select:
{ {
std::cout << i << ": " << PU[(*pai)[i]] << "\n"; std::cout << i << ": " << PU[(*pai)[i]] << "\n";
} }
printf("剩余%d张牌\n", pai->getSize());
break; break;
case 2: case 2:
printf("出哪张\n"); printf("选择:");
scanf("%d", &opt); scanf("%d", &opt);
// l.firstInsert((*pai)[opt]); // l.firstInsert((*pai)[opt]);
std::cout << "打出" << PU[(*pai).remove(opt)]; std::cout << "打出" << PU[l.firstInsert((*pai).remove(opt))];
printf("\n"); printf("\n");
printf("剩余%d张牌\n", pai->getSize());
break; break;
case 3: case 3:
@ -131,6 +132,7 @@ select:
std::cout << i << ": " << PU[l[i]]; std::cout << i << ": " << PU[l[i]];
printf("\n"); printf("\n");
} }
break;
case 4: case 4:
goto select; goto select;

View file

@ -47,7 +47,7 @@ Node List::succInsert(Node Old, int New)
_size++; _size++;
return T; return T;
} }
void List::firstInsert(int New) int List::firstInsert(int New)
{ {
Node T = new node; Node T = new node;
T->data = New; T->data = New;
@ -56,8 +56,9 @@ void List::firstInsert(int New)
header->succ = T; header->succ = T;
T->succ->pred = T; T->succ->pred = T;
_size++; _size++;
return New;
} }
void List::lastInsert(int New) int List::lastInsert(int New)
{ {
Node T = new node; Node T = new node;
T->data = New; T->data = New;
@ -66,6 +67,7 @@ void List::lastInsert(int New)
trailer->pred = T; trailer->pred = T;
T->pred->succ = T; T->pred->succ = T;
_size++; _size++;
return New;
} }
Node List::predInsertN(Node Old, Node New) Node List::predInsertN(Node Old, Node New)
{ {

View file

@ -34,8 +34,8 @@ public:
void clear(); void clear();
Node first() { return header->succ; } Node first() { return header->succ; }
Node last() { return trailer->pred; } Node last() { return trailer->pred; }
void firstInsert(int New); int firstInsert(int New);
void lastInsert(int New); int lastInsert(int New);
Node succInsert(Node Old, int New); Node succInsert(Node Old, int New);
Node predInsert(Node Old, int New); Node predInsert(Node Old, int New);

236
ex6/ex6.cpp Normal file
View file

@ -0,0 +1,236 @@
#include "vec.cpp"
#include "list.cpp"
#include <time.h>
#include <stdio.h>
#include "score.h"
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;
printf("生成随机扑克序列\n");
srand(time(NULL));
Vec v(54, 2);
// v.printall();
List l;
printf("开始建立链表\n");
for (int i = 0; i <= 53; i++)
{
l.firstInsert(v.get(i));
std::cout << PU[l[0]] << " ";
if (i % 2 != 0)
printf("\n");
}
Begin:
printf("\n是否重新洗牌?(0/1)");
scanf("%d", &xipai_flag);
if (xipai_flag == 1)
{
v.reorder();
l.clear();
printf("===============开始建立链表===============\n");
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;
}
printf("===============开始发牌===============\n");
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;
}
}
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;
printf("发牌完成,选择你的操作:\n");
while (finish_num < 3)
{
printf("+++++===========================+++++\n 轮次%d \n+++++===========================+++++\n", round);
A:
if (p1.getSize())
{
printf("你手中的牌:\n");
for (int i = 0; i < p1.getSize(); i++)
{
std::cout << i << ": " << PU[(p1)[i]] << "\n";
}
printf("剩余%d张牌\n", p1.getSize());
if (l[0] > p1[p1.getSize() - 1])
{
if (pass_num == 2 - finish_num)
{
pass_num = 0;
goto A;
}
printf("无法出牌,已自动跳过\n");
pass_num++;
goto B;
}
printf("1出牌2查看牌堆");
scanf("%d", &opt);
switch (opt)
{
case 1:
printf("选择:");
scanf("%d", &opt);
if (opt >= p1.getSize())
{
printf("错误输入\n");
goto A;
}
std::cout << "打出" << PU[l.firstInsert((p1).remove(opt))];
if (int size = p1.getSize() == 0)
{
s.finish(0);
printf(",玩家A出完所有牌\n");
finish_num++;
}
else
printf(",剩余%d张牌\n", p1.getSize());
break;
case 2:
for (int i = 0; i < l.getSize() - 1; i++)
{
std::cout << i << ": " << PU[l[i]];
printf("\n");
}
goto A;
default:
printf("错误输入\n");
goto A;
}
}
B:
if (p2.getSize())
{
printf("================================\n");
printf("B玩家");
if (pass_num == 2 - finish_num)
{
std::cout << "打出" << PU[l.firstInsert((p2).remove(0))];
printf(",剩余%d张牌\n", p2.getSize());
pass_num = 0;
}
else if (l[0] > p2[p2.getSize() - 1])
{
printf("无牌可出,跳过\n");
pass_num++;
}
else
for (int i = 0; i < p2.getSize(); i++)
{
if (l[0] < p2[i])
{
std::cout << "打出" << PU[l.firstInsert((p2).remove(i))];
if (int size = p2.getSize() == 0)
{
s.finish(1);
printf(",玩家B出完所有牌\n");
finish_num++;
}
else
printf(",剩余%d张牌\n", p2.getSize());
break;
}
}
}
C:
if (p3.getSize())
{
printf("================================\n");
printf("C玩家");
if (pass_num == 2 - finish_num)
{
std::cout << "打出" << PU[l.firstInsert((p3).remove(0))];
printf(",剩余%d张牌\n", p3.getSize());
pass_num = 0;
}
else if (l[0] > p3[p3.getSize() - 1])
{
printf("无牌可出,跳过\n");
pass_num++;
}
else
for (int i = 0; i < p3.getSize(); i++)
{
if (l[0] < p3[i])
{
std::cout << "打出" << PU[l.firstInsert((p3).remove(i))];
if (int size = p3.getSize() == 0)
{
s.finish(2);
printf(",玩家C出完所有牌\n");
finish_num++;
}
else
printf(",剩余%d张牌\n", p3.getSize());
break;
}
}
}
round++;
}
printf("=====================================\n");
printf("分数结算:\n");
printf("玩家A:%d分\n", s.score(0));
printf("玩家B:%d分\n", s.score(1));
printf("玩家C:%d分\n", s.score(2));
return 0;
}

171
ex6/list.cpp Normal file
View file

@ -0,0 +1,171 @@
#include "list.h"
List::List()
{
_size = 0;
header->data = 0;
header->succ = trailer;
header->pred = NULL;
trailer->succ = NULL;
trailer->pred = header;
}
List::~List()
{
clear();
delete trailer;
delete header;
}
void List::clear()
{
while (0 < _size)
remove(0);
}
Node List::predInsert(Node Old, int New)
{
Node T = new node;
T->data = New;
T->succ = Old;
T->pred = Old->pred;
Old->pred = T;
T->pred->succ = T;
_size++;
return T;
}
Node List::succInsert(Node Old, int New)
{
Node T = new node;
T->data = New;
T->pred = Old;
T->succ = Old->succ;
Old->succ = T;
T->succ->pred = T;
_size++;
return T;
}
int List::firstInsert(int New)
{
Node T = new node;
T->data = New;
T->pred = header;
T->succ = header->succ;
header->succ = T;
T->succ->pred = T;
_size++;
return New;
}
int List::lastInsert(int New)
{
Node T = new node;
T->data = New;
T->succ = trailer;
T->pred = trailer->pred;
trailer->pred = T;
T->pred->succ = T;
_size++;
return New;
}
Node List::predInsertN(Node Old, Node New)
{
Node T = new node;
T = New;
T->succ = Old;
T->pred = Old->pred;
Old->pred = T;
T->pred->succ = T;
_size++;
return T;
}
Node List::succInsertN(Node Old, Node New)
{
Node T = new node;
T = New;
T->pred = Old;
T->succ = Old->succ;
Old->succ = T;
T->succ->pred = T;
_size++;
return T;
}
Node List::firstInsertN(Node New)
{
Node T = new node;
T = New;
T->pred = header;
T->succ = header->succ;
header->succ = T;
T->pred->succ = T;
_size++;
return T;
}
Node List::lastInsertN(Node New)
{
Node T = new node;
T = New;
T->succ = trailer;
T->pred = trailer->pred;
trailer->pred = T;
T->pred->succ = T;
_size++;
return T;
}
Node List::find(int i)
{
Node p = first();
while (0 < i--)
{
p = p->succ;
}
return p;
}
int List::get(int i)
{
return find(i)->data;
}
int List::remove(int i)
{
Node T = find(i);
int ans = T->data;
removeN(T);
return ans;
}
void List::removeN(Node i)
{
(i->pred)->succ = i->succ;
(i->succ)->pred = i->pred;
delete i;
_size--;
}
Node List::searchN(int value, int n, Node p)
{
while (0 <= n--)
if (((p = p->pred)->data) <= value)
break;
return p;
}
void List::sort()
{
Node p=first();
for (int r = 0; r < _size; r++)
{
succInsert(searchN(p->data,r,p),p->data);
p=p->succ;
removeN(p->pred);
}
}
int List::valid(Node p)
{
return p && (trailer != p) && (header != p);
}

58
ex6/list.h Normal file
View file

@ -0,0 +1,58 @@
#include <iostream>
#include <string>
#define Node node *
typedef struct node
{
int data;
Node pred;
Node succ;
} node;
class List
{
private:
Node header = new node;
Node trailer = new node;
int _size;
protected:
Node firstInsertN(Node New);
Node lastInsertN(Node New);
Node succInsertN(Node old, Node ne);
Node predInsertN(Node old, Node ne);
void removeN(Node i);
Node searchN(int value,int n,Node p);
public:
int getSize()
{
return _size;
}
List();
~List();
void clear();
Node first() { return header->succ; }
Node last() { return trailer->pred; }
int firstInsert(int New);
int lastInsert(int New);
Node succInsert(Node Old, int New);
Node predInsert(Node Old, int New);
int get(int i);
Node find(int i);
int remove(int i);
Node searchN(int value,int n);
int valid(Node p);
void sort();
int operator[](int i)
{
Node p = first();
while (0 < i--)
p = p->succ;
return p->data;
}
};

41
ex6/score.h Normal file
View file

@ -0,0 +1,41 @@
class Score
{
private:
int time = 0;
int *_s;
int _size;
public:
Score(int i)
{
_size = i;
_s = new int[_size - 1];
for(int i=0;i<_size;i++)
{
_s[i]=0;
}
}
void finish(int t)
{
_s[t] = 3 - (time++);
}
bool isFinish(int i)
{
if (_s[i] > 0)
return true;
return false;
}
int score(int i)
{
return _s[i];
}
bool isallFinish()
{
for (int i = 0; i < _size; i++)
{
if (!isFinish(i))
return false;
}
return true;
}
};

246
ex6/vec.cpp Normal file
View file

@ -0,0 +1,246 @@
#include "vec.h"
Vec::Vec(int len, int mode)
{
_used = len - 1;
_len = len * 2;
_v = new int[_len];
// for (int i = 0; i <= _used; i++)
// _v[i] = i + 1;
if (mode == 1)
{
for (int i = 0; i <= _used; i++)
_v[i] = rand() % (2 * (_used + 1));
for (int i = 0; i <= _used; i++)
swap(i, rand() % (2 * (_used + 1)));
}
else if (mode == 2)
{
for (int i = 0; i <= _used; i++)
_v[i] = _used - i;
for (int i = 0; i <= _used; i++)
swap(i, rand() % ((_used + 1)));
}
}
int Vec::get(int a)
{
return _v[a];
}
void Vec::put(int a, int value)
{
_v[a] = value;
}
void Vec::swap(int a, int b)
{
int temp = _v[a];
_v[a] = _v[b];
_v[b] = temp;
}
void Vec::expand()
{
_len = _len * 2;
int *p = new int[_len];
for (int i = 0; i <= _used; i++)
p[i] = _v[i];
delete[] _v;
_v = p;
}
void Vec::shrink()
{
if (((double)_used / (double)_len) >= 0.25)
return;
_len = _len >> 1;
int *p = new int[_len];
for (int i = 0; i <= _used; i++)
p[i] = _v[i];
delete[] _v;
_v = p;
}
//位置 数值
int Vec::insert(int locate, int value)
{
if (locate == _used + 1)
{
_used++;
_v[_used] = value;
return 0;
}
if (locate < 0 || locate > _used)
return -1;
_used++;
if (_used >= _len)
expand();
for (int i = _used; i > locate; i--)
_v[i] = _v[i - 1];
_v[locate] = value;
return 0;
}
//位置 删除个数
int Vec::remove(int locate, int value)
{
if (locate < 0 || ((_used - value) < 0) || ((locate + value - 1) > _used))
return -1;
_used = _used - value;
for (int i = locate; i <= _used; i++)
_v[i] = _v[i + value];
shrink();
return value;
}
//删除某一元素
int Vec::remove_sorted(int value)
{
int j, i;
for (i = 0; i <= _used && _v[i] != value; i++)
;
for (j = 1; _v[i + j] == value; j++)
;
return remove(i, j);
}
int Vec::count(int value)
{
int j, i;
for (i = 0; i <= _used && _v[i] != value; i++)
;
for (j = 0; _v[i + j] == value && i + j <= _used; j++)
;
if (j == 0)
return -1;
return j;
}
int Vec::find(int value)
{
int i = 0;
for (i; i <= _used; i++)
{
if (_v[i] == value)
{
return i;
}
}
return -1;
}
int Vec::getlen()
{
return _len;
}
int Vec::getused()
{
return _used;
}
void Vec::printall()
{
for (int i = 0; i <= _used; i++)
printf("%d\n", _v[i]);
}
void Vec::bubbleSort()
{
int n = _used;
bool sorted = false;
while (!sorted)
{
sorted = true;
for (int i = 1; i <= n; i++)
{
if (_v[i - 1] > _v[i])
{
swap(i - 1, i);
sorted = false;
}
}
n--;
}
_sorted = true;
}
void Vec::mergeSort(int lo, int hi)
{
if (lo >= hi)
{
return;
}
int mi = lo + (hi - lo) / 2;
mergeSort(lo, mi);
mergeSort(mi + 1, hi);
merge(lo, mi, hi);
_sorted = true;
}
int Vec::search(int e)
{
if (_sorted == false)
return -1;
int i = 0;
for (i; i <= _used && _v[i] <= e; i++)
;
return i - 1;
}
void Vec::merge(int lo, int mi, int hi)
{
int i = lo, j = mi + 1, k = 0;
int *temp = new int[hi - lo + 1];
while (i <= mi && j <= hi)
{
if (_v[i] <= _v[j])
temp[k++] = _v[i++];
else
temp[k++] = _v[j++];
}
while (i <= mi)
temp[k++] = _v[i++];
while (j <= hi)
temp[k++] = _v[j++];
for (i = lo, k = 0; i <= hi; i++, k++)
_v[i] = temp[k];
delete[] temp;
}
void Vec::reorder()
{
for (int i = 0; i <= _used; i++)
swap(i, rand() % ((_used + 1)));
}

65
ex6/vec.h Normal file
View file

@ -0,0 +1,65 @@
#ifndef _INC_STDIO
#include <stdio.h>
#endif
#ifndef _GLIBCXX_STDLIB_H
#include <stdlib.h>
#endif
#include <time.h>
class Vec
{
private:
int *_v;
int _len;
int _used;
bool _sorted = false;
void expand();
void shrink();
public:
~Vec()
{
delete[] _v;
delete &_len;
delete &_used;
delete &_sorted;
}
Vec(int _len, int mode);
int get(int a);
int search(int value);
void put(int a, int value);
void swap(int a, int b);
int insert(int locate, int value);
int remove(int locate, int value);
int remove_sorted(int value);
int find(int value);
void bubbleSort();
void printall();
int getlen();
int getused();
int count(int value);
void mergeSort(int lo, int hi);
void reorder();
private:
void merge(int lo, int mi, int hi);
};