由于现在没有 linux,所以 for linux 版本还没做。
exe 下载:https://wwbwn.lanzout.com/iV3qo3jsc16f
密码:plW4
此次更新包括:
- 增加查看赛程功能;
- 增加查看排名功能(具体的自己体验);
- 修复对阵表出现两只球队进行多场比赛的 bug(一开始就没想着修,因为很难)。
code:
/*
* This is the code of Game Premier League
* Game Version: 1.0.4 (with fixtures view)
* Developed by George Deng and George Deng only
* Still some bugs waiting to be fixed
* Please report bugs to me if you find any
* Contact: GeorgeDeng11451410086@hotmail.com OR @GeorgeDeng(Uid: 1021786) on www.luogu.com.cn
* Due to the age of the author, it won't be perfect. If you have some advices, please contact me either.
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <random>
#include <algorithm>
#include <iomanip>
#include <thread>
#include <chrono>
#include <windows.h>
#define game_rand rand
using namespace std;
void clearScreen(){
// std::cout << "\033[2J\033[3J\033[1;1H";
system("cls");
}
void SSSleep(int millis){
// std::this_thread::sleep_for(std::chrono::milliseconds(millis));
Sleep(millis);
}
void pressAnyKeyToContinue() {
system("pause");
// std::cout << "Press any key to continue...";
// std::cout.flush();
// // 保存当前终端属性
// struct termios old_tio, new_tio;
// tcgetattr(STDIN_FILENO, &old_tio);
// new_tio = old_tio;
// // 修改:禁用规范模式(ICANON)和回显(ECHO)
// new_tio.c_lflag &= ~(ICANON | ECHO);
// tcsetattr(STDIN_FILENO, TCSANOW, &new_tio);
// // 读取一个字符
// getchar();
// // 恢复原始终端属性
// tcsetattr(STDIN_FILENO, TCSANOW, &old_tio);
// cout<<"\n";
}
struct node {
string name;
int win;
int draw;
int lost;
int ablity;
int point;
int goal, lose;
int train_remain;
int money;
} team[25];
struct MatchResult {
int home, away;
int home_goal, away_goal;
} history[38][10]; // 38轮,每轮10场比赛
int cli_team = 0;
int oponment;
// 预生成的赛程:38轮,每轮10场比赛,每场[主队,客队]
int schedule[38][10][2];
bool cmp(node a, node b) {
if (a.point != b.point) return a.point > b.point;
if (a.goal - a.lose != b.goal - b.lose) return a.goal - a.lose > b.goal - b.lose;
if (a.goal != b.goal) return a.goal > b.goal;
if (a.win != b.win) return a.win > b.win;
return a.ablity > b.ablity;
}
void print_board() {
node temp_team[25];
for (int i = 1; i <= 20; i++) {
temp_team[i] = team[i];
}
sort(temp_team + 1, temp_team + 1 + 20, cmp);
// 使用 setw 设置场宽
cout << left << setw(5) << "Rank"
<< setw(20) << "Team"
<< setw(6) << "Wins"
<< setw(6) << "Draws"
<< setw(6) << "Losts"
<< setw(7) << "Goals"
<< setw(7) << "Lose"
<< setw(6) << "Points"
<< setw(7) << "GD" << endl;
for (int i = 1; i <= 20; i++) {
cout << left << setw(5) << i
<< setw(20) << temp_team[i].name
<< setw(6) << temp_team[i].win
<< setw(6) << temp_team[i].draw
<< setw(6) << temp_team[i].lost
<< setw(7) << temp_team[i].goal
<< setw(7) << temp_team[i].lose
<< setw(6) << temp_team[i].point
<< setw(7) << (temp_team[i].goal - temp_team[i].lose);
if (temp_team[i].name == team[cli_team].name) {
cout << " (My team)";
}
cout << endl;
}
pressAnyKeyToContinue();
clearScreen();
}
int get_rank(string team_name){
node temp_team[25];
for (int i = 1; i <= 20; i++) {
temp_team[i] = team[i];
}
sort(temp_team + 1, temp_team + 1 + 20, cmp);
for (int i = 1; i <= 20; i++) {
if(temp_team[i].name==team_name){
return i;
}
}
return -1;
}
void game(int home, int away, int round, int match_idx){
int home_goal, away_goal; // 移到外部
if(home==cli_team){
cout<<"Your oponment is "<<team[away].name<<" (home)"<<endl;
pressAnyKeyToContinue();
}
if(away==cli_team){
cout<<"Your oponment is "<<team[home].name<<" (away)"<<endl;
pressAnyKeyToContinue();
}
int result = game_rand()%(team[home].ablity+team[away].ablity);
if(result<team[home].ablity){
team[home].win++;
team[away].lost++;
team[home].point+=3;
home_goal = game_rand()%7;
while(home_goal==0) home_goal = game_rand()%7;
away_goal = game_rand()%3;
while(away_goal>=home_goal) away_goal = game_rand()%3;
team[home].goal+=home_goal;
team[home].lose+=away_goal;
team[away].goal+=away_goal;
team[away].lose+=home_goal;
if(home==cli_team){
cout<<"You win by "<<home_goal<<"-"<<away_goal<<"!"<<endl;
cout<<"You win yourself "<<home_goal-away_goal<<" M�"<<endl;
team[home].money+=home_goal-away_goal;
pressAnyKeyToContinue();
}else if(away==cli_team){
cout<<"You lost by "<<away_goal<<"-"<<home_goal<<"!"<<endl;
cout<<"You lose yourself "<<home_goal-away_goal<<" M�"<<endl;
team[away].money-=home_goal-away_goal;
team[away].money = max(team[away].money,0);
pressAnyKeyToContinue();
}
}else if(result==team[home].ablity){
team[home].draw++;
team[away].draw++;
team[home].point+=1;
team[away].point+=1;
home_goal = game_rand()%7;
away_goal = home_goal;
team[home].goal+=home_goal;
team[home].lose+=away_goal;
team[away].goal+=away_goal;
team[away].lose+=home_goal;
if(home==cli_team){
cout<<"You draw by "<<home_goal<<"-"<<away_goal<<"!"<<endl;
pressAnyKeyToContinue();
}else if(away==cli_team){
cout<<"You draw by "<<away_goal<<"-"<<home_goal<<"!"<<endl;
pressAnyKeyToContinue();
}
}else if(result>team[home].ablity){
team[home].lost++;
team[away].win++;
team[away].point+=3;
home_goal = game_rand()%7;
while(home_goal==0) home_goal = game_rand()%7;
away_goal = game_rand()%3;
while(away_goal>=home_goal) away_goal = game_rand()%3;
swap(home_goal,away_goal);
team[home].goal+=home_goal;
team[home].lose+=away_goal;
team[away].goal+=away_goal;
team[away].lose+=home_goal;
if(home==cli_team){
cout<<"You lost by "<<home_goal<<"-"<<away_goal<<"!"<<endl;
cout<<"You lose yourself "<<-(home_goal-away_goal)<<" M�"<<endl;
team[home].money+=home_goal-away_goal;
team[home].money = max(team[home].money,0);
pressAnyKeyToContinue();
}else if(away==cli_team){
cout<<"You win by "<<away_goal<<"-"<<home_goal<<"!"<<endl;
cout<<"You win yourself "<<-(home_goal-away_goal)<<" M�"<<endl;
team[away].money-=home_goal-away_goal;
pressAnyKeyToContinue();
}
}
// 存储比赛结果
history[round][match_idx].home = home;
history[round][match_idx].away = away;
history[round][match_idx].home_goal = home_goal;
history[round][match_idx].away_goal = away_goal;
}
// 新的 round_draw:直接使用预生成赛程
void round_draw(int now_round){
int r = now_round - 1; // 转换为0-based
for (int i = 0; i < 10; i++) {
int home = schedule[r][i][0];
int away = schedule[r][i][1];
game(home, away,r,i);
}
clearScreen();
}
void watcher_game(int home, int away, int round, int match_idx){
int result = game_rand()%(team[home].ablity+team[away].ablity);
int home_goal, away_goal;
if(result<team[home].ablity){
team[home].win++;
team[away].lost++;
team[home].point+=3;
home_goal = game_rand()%7;
while(home_goal==0) home_goal = game_rand()%7;
away_goal = game_rand()%3;
while(away_goal>=home_goal) away_goal = game_rand()%3;
team[home].goal+=home_goal;
team[home].lose+=away_goal;
team[away].goal+=away_goal;
team[away].lose+=home_goal;
cout<<team[home].name<<' '<<home_goal<<'-'<<away_goal<<' '<<team[away].name<<endl;
}else if(result==team[home].ablity){
team[home].draw++;
team[away].draw++;
team[home].point+=1;
team[away].point+=1;
home_goal = game_rand()%7;
away_goal = home_goal;
team[home].goal+=home_goal;
team[home].lose+=away_goal;
team[away].goal+=away_goal;
team[away].lose+=home_goal;
cout<<team[home].name<<' '<<home_goal<<'-'<<away_goal<<' '<<team[away].name<<endl;
}else if(result>team[home].ablity){
team[home].lost++;
team[away].win++;
team[away].point+=3;
home_goal = game_rand()%7;
while(home_goal==0) home_goal = game_rand()%7;
away_goal = game_rand()%3;
while(away_goal>=home_goal) away_goal = game_rand()%3;
swap(home_goal,away_goal);
team[home].goal+=home_goal;
team[home].lose+=away_goal;
team[away].goal+=away_goal;
team[away].lose+=home_goal;
cout<<team[home].name<<' '<<home_goal<<'-'<<away_goal<<' '<<team[away].name<<endl;
}
history[round][match_idx].home = home;
history[round][match_idx].away = away;
history[round][match_idx].home_goal = home_goal;
history[round][match_idx].away_goal = away_goal;
}
// 新的 watcher_round_draw
void watcher_round_draw(int now_round){
int r = now_round - 1;
for (int i = 0; i < 10; i++) {
SSSleep(200);
int home = schedule[r][i][0];
int away = schedule[r][i][1];
watcher_game(home, away,r,i);
}
pressAnyKeyToContinue();
clearScreen();
}
void change_ability(){
clearScreen();
cout<<"Choose the team you want to change ability and the new ability:"<<endl;
for(int i = 1;i<=20;i++){
cout<<"("<<i<<"): "<<team[i].name<<"("<<team[i].ablity<<")"<<endl;
}
int op;
cin>>op;
int new_ability;
cin>>new_ability;
team[op].ablity = new_ability;
cout<<"The ability of team "<<team[op].name<<" has been changed to "<<new_ability<<"."<<endl;
pressAnyKeyToContinue();
clearScreen();
}
// 查看自己球队的历史赛程(带比分)
void viewMyHistory(int currentRound) {
clearScreen();
bool hasAny = false;
for (int r = 1; r < currentRound; r++) {
int roundIdx = r - 1;
for (int i = 0; i < 10; i++) {
int home = history[roundIdx][i].home;
int away = history[roundIdx][i].away;
if (home == cli_team || away == cli_team) {
hasAny = true;
int my_goal = (home == cli_team) ? history[roundIdx][i].home_goal : history[roundIdx][i].away_goal;
int opp_goal = (home == cli_team) ? history[roundIdx][i].away_goal : history[roundIdx][i].home_goal;
string opponent = (home == cli_team) ? team[away].name : team[home].name;
string location = (home == cli_team) ? "home" : "away";
cout << "Round " << r << ": " << my_goal << "-" << opp_goal << " " << opponent << " (" << location << ")" << endl;
}
}
}
// if (!hasAny) {
// cout << "No previous fixtures." << endl;
// }
// pressAnyKeyToContinue();
// clearScreen();
}
// 查看所有球队的历史赛程(完整比分)
void viewAllHistory(int currentRound) {
clearScreen();
for (int r = 0; r < currentRound - 1; r++) {
cout << "Round " << r + 1 << ":" << endl;
int roundIdx = r;
for (int i = 0; i < 10; i++) {
int home = history[roundIdx][i].home;
int away = history[roundIdx][i].away;
cout << team[home].name << "(rk. "<<get_rank(team[home].name)<<')' << " " << history[roundIdx][i].home_goal << "-"
<< history[roundIdx][i].away_goal << " " << team[away].name << "(rk. "<<get_rank(team[away].name)<<')' << endl;
}
cout << endl;
}
// pressAnyKeyToContinue();
// clearScreen();
}
// ========== 新增功能:查看赛程 ==========
void viewMyFixtures(int currentRound) {
clearScreen();
cout << "=== Your Fixtures ===" << endl;
viewMyHistory(currentRound);
bool hasFixtures = false;
for (int r = currentRound - 1; r <= 37; r++) {
int roundIdx = r;
for (int i = 0; i < 10; i++) {
int home = schedule[roundIdx][i][0];
int away = schedule[roundIdx][i][1];
if (home == cli_team || away == cli_team) {
if (!hasFixtures) {
hasFixtures = true;
}
cout << "Round " << r + 1 << endl;
// cout<<endl;
cout << "vs. " << team[(home == cli_team) ? away : home].name;
if (home == cli_team) {
cout << " (home)";
} else {
cout << " (away)";
}
cout << endl << endl;
}
}
}
if (!hasFixtures) {
cout << "No more fixtures this season." << endl;
}
pressAnyKeyToContinue();
clearScreen();
}
void viewAllFixtures(int currentRound) {
clearScreen();
cout << "=== Past Season Fixtures ===" << endl;
viewAllHistory(currentRound);
cout << "\n=== Upcoming Season Fixtures ===" << endl;
for (int r = currentRound - 1; r < 38; r++) {
cout << "Round " << r + 1 << ":" << endl;
cout<<endl;
int roundIdx = r;
for (int i = 0; i < 10; i++) {
int home = schedule[roundIdx][i][0];
int away = schedule[roundIdx][i][1];
cout << team[home].name << "(rk. "<<get_rank(team[home].name)<<')' << " vs. " << team[away].name << "(rk. "<<get_rank(team[away].name)<<')'<< endl;
}
cout << endl;
}
pressAnyKeyToContinue();
clearScreen();
}
// =====================================
void watcher() {
for(int i = 1;i<=38;i++){
cout<<"You are in the watcher mode now"<<endl;
cout<<"Round "<<i<<": "<<endl;
cout<<endl;
cout<<"(1) Check the ranking board"<<endl;
cout<<"(2) Begin next games"<<endl;
cout<<"(3) Change a team's ability"<<endl;
cout<<"(4) View all fixtures"<<endl; // 新增选项
int op;
cin>>op;
if(op==1){
clearScreen();
print_board();
i--;
}else if(op==2){
clearScreen();
watcher_round_draw(i);
}else if(op==3){
clearScreen();
change_ability();
i--;
}else if(op==4){
viewAllFixtures(i); // 查看全部赛程,不推进轮次
i--;
}
}
}
int main() {
start:
srand(time(NULL));
// mt19937 game_rand(time(NULL));
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cout << "Premier League" << endl;
SSSleep(2000);
clearScreen();
// 初始化球队
team[1].name = "Liverpool";
team[2].name = "Arsenal";
team[3].name = "Aston Villa";
team[4].name = "Manchester City";
team[5].name = "Manchester United";
team[6].name = "Tottenham Hotspur";
team[7].name = "Newcastle United";
team[8].name = "Chelsea";
team[9].name = "Nottingham Forest";
team[10].name = "Bournemouth";
team[11].name = "Brighton";
team[12].name = "Fulham";
team[13].name = "Brentford";
team[14].name = "Crystal Palace";
team[15].name = "West Ham United";
team[16].name = "Everton";
team[17].name = "Wolverhampton";
team[18].name = "Ipswich";
team[19].name = "Leicester City";
team[20].name = "Southampton";
team[1].ablity = game_rand() % 101;
team[2].ablity = game_rand() % 95;
team[3].ablity = game_rand() % 90;
team[4].ablity = game_rand() % 87;
team[5].ablity = game_rand() % 77;
team[6].ablity = game_rand() % 75;
team[7].ablity = game_rand() % 75;
team[8].ablity = game_rand() %73;
team[9].ablity = game_rand()%70;
team[10].ablity = game_rand()%68;
team[11].ablity = game_rand()%68;
team[12].ablity = game_rand()%67;
team[13].ablity = game_rand()%69;
team[14].ablity = game_rand()%65;
team[15].ablity = game_rand()%65;
team[16].ablity = game_rand()%63;
team[17].ablity = game_rand()%60;
team[18].ablity = game_rand()%55;
team[19].ablity = game_rand()%52;
team[20].ablity = game_rand()%50;
// 生成随机赛程
int order[20];
for (int i = 0; i < 20; i++) order[i] = i+1;
// 随机打乱顺序
random_device rd;
mt19937 g(rd());
shuffle(order, order+20, g);
// 生成第一循环(19轮)
for (int round = 0; round < 19; round++) {
// 配对:order[0] vs order[19], order[1] vs order[18], ... order[9] vs order[10]
for (int i = 0; i < 10; i++) {
int home = order[i];
int away = order[19-i];
schedule[round][i][0] = home;
schedule[round][i][1] = away;
}
// 旋转 order[1] 到 order[19](循环右移一位)
int last = order[19];
for (int j = 19; j > 1; j--) {
order[j] = order[j-1];
}
order[1] = last;
}
// 生成第二循环(19轮),交换主客场
for (int round = 0; round < 19; round++) {
for (int i = 0; i < 10; i++) {
int home = schedule[round][i][1]; // 原客队变主队
int away = schedule[round][i][0]; // 原主队变客队
schedule[38-round-1][i][0] = home;
schedule[38-round-1][i][1] = away;
}
}
for(int round = 0;round<19;round++){
for(int i = 0;i<10;i++){
int swap_op = game_rand()%100;
if(swap_op<=65){
swap(schedule[round][i][0],schedule[round][i][1]);
swap(schedule[38-round-1][i][0],schedule[38-round-1][i][1]);
}
}
}
for (int i = 1; i <= 20; i++) {
cout << "(" << i << "):" << team[i].name << endl;
}
cout << "Choose your team:(21 Watcher mode)" << endl;
cin >> cli_team;
clearScreen();
if (cli_team <= 0 || cli_team > 21) {
cout << "Input Error. Game Over";
pressAnyKeyToContinue();
return 0;
}
if(cli_team==21){
watcher();
clearScreen();
cout << "The season is over" << endl;
SSSleep(2000);
cout << "This is the final table:" << endl;
print_board();
return 0;
}
team[cli_team].train_remain = game_rand()%6;
for (int i = 1; i <= 19 * 2; i++) {
laodazhouji:
cout << "Round " << i << ": " << endl;
cout << "Your team: " << team[cli_team].name << endl;
cout << "League ranking: "<<get_rank(team[cli_team].name)<<endl;
SSSleep(500);
cout << "Train remain: " << team[cli_team].train_remain << endl;
SSSleep(500);
cout << "Your team ablity: " << team[cli_team].ablity << endl;
SSSleep(500);
cout<<"Your money: "<<team[cli_team].money<<" M�"<<endl;
SSSleep(500);
cout << "Begin Next Game" << endl;
SSSleep(500);
cout << "Check the ranking board" << endl;
SSSleep(500);
cout << "Buy chance to train your team" <<endl;
SSSleep(1000);
cout << endl;
cout << "(1): Train" << endl;
cout << "(2): Check the ranking board" << endl;
cout << "(3): Begin next game" << endl;
cout << "(4): Buy chance to train your team" << endl;
cout << "(5): The fixtures" << endl; // 新增选项
int op;
cin >> op;
while (op < 1 || op > 6) {
cout << "Input Error, please input it again ";
SSSleep(2000);
clearScreen();
goto laodazhouji;
}
clearScreen();
if (op == 1) {
if (team[cli_team].train_remain == 0) {
cout << "There are no chance to train!" << endl;
SSSleep(2000);
clearScreen();
goto laodazhouji;
}
team[cli_team].train_remain--;
int win = game_rand() % 2;
if (win == 1) {
cout << "Train successfully! Team's ability +1!" << endl;
SSSleep(2000);
team[cli_team].ablity++;
} else {
cout << "Train unsuccessfully! Team's ability -1!" << endl;
SSSleep(2000);
team[cli_team].ablity--;
}
clearScreen();
goto laodazhouji;
} else if (op == 2) {
print_board();
goto laodazhouji;
} else if (op == 3) {
round_draw(i);
}else if(op==4){
clearScreen();
money:
clearScreen();
cout<<"You have "<<team[cli_team].money <<"M�"<<endl;
int money[11],chance[11];
for(int i = 1;i<=10;i++){
money[i] = game_rand()%20+1,chance[i] = game_rand()%30+1;
cout<<"("<<i<<"): "<<money[i]<<"M�---> "<<chance[i]<<" chances."<<endl;
}
cout<<"(11) exit"<<endl;
int cli_op;
cin>>cli_op;
if(cli_op==11){
clearScreen();
goto laodazhouji;
}
if(cli_op<1||cli_op>10){
cout<<"Input error";
SSSleep(2000);
goto money;
}else{
if(team[cli_team].money>=money[cli_op]){
cout<<"Deal!"<<endl;
SSSleep(2000);
team[cli_team].money-=money[cli_op];
team[cli_team].train_remain+=chance[cli_op];
}else{
cout<<"You can't buy it!"<<endl;
SSSleep(2000);
goto money;
}
}
SSSleep(2000);
clearScreen();
goto laodazhouji;
}else if(op == 5) {
viewAllFixtures(i);
// viewMyFixtures(i); // 查看个人剩余赛程,不推进轮次
// i--; // 保持当前轮次
goto laodazhouji;
}
}
clearScreen();
cout << "The season is over" << endl;
SSSleep(2000);
cout << "This is the final table:" << endl;
print_board();
return 0;
}