Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
  • magnuswj/prog-1004-2022-group-4
  • alexholt/dcst1008
  • nilstesd/dcst1008
3 results
Select Git revision
Show changes
Commits on Source (88)
Showing
with 995 additions and 0 deletions
......@@ -23,3 +23,4 @@ generatepdf:
only:
- web # Only web gjør at vi kun kjører denne delen av pipeline når en trykker på knappen "Run pipeline"
{
"files.associations": {
"iostream": "cpp",
"fstream": "cpp"
}
}
\ No newline at end of file
File added
File added
File added
File added
File added
File added
/**
* Enkel verktykasse for lese: tegn og tall.
*
* FULLSTENDIG IDENTISK til 'LesData2.h' bare at adskilt
* funksjonenes DEKLARASJON og DEFINISJON (som er p DENNE filen).
*
* @file LesData3.CPP
* @author Frode Haug, NTNU
*/
#include <iostream> // cin, cout
#include <iomanip> // setprecision
#include <cctype> // toupper
#include <cstdlib> // atoi, atof
#include "LesData3.h" // Prototypene for denne filens innhold
/**
* Leser og returnerer ett (upcaset) tegn.
*
* @param t - Ledetekst til brukeren nr ber om ett tegn
*
* @return Ett (upcaset) tegn.
*/
char lesChar(const char* t) {
char tegn;
std::cout << t;
std::cin >> tegn; std::cin.ignore(MAXCHAR, '\n');
return (toupper(tegn));
}
/**
* Leser og returnerer et flyttall mellom to gitte grenser.
*
* @param t - Ledetekst til brukeren nr ber om input/et tall
* @param min - Minimum for innlest og godtatt tallverdi
* @param max - Maksimum for innlest og godtatt tallverdi
*
* @return Godtatt verdi i intervallet 'min' - 'max'
*/
float lesFloat (const char* t, const float min, const float max) {
char buffer[MAXCHAR] = "";
float tall = 0.0F;
bool feil = false;
do {
feil = false;
std::cout << std::fixed << std::showpoint << std::setprecision(2);
std::cout << t << " (" << min << " - " << max << "): ";
std::cin.getline(buffer, MAXCHAR);
tall = static_cast <float> (atof(buffer));
if (tall == 0 && buffer[0] != '0')
{ feil = true; std::cout << "\nERROR: Not a float\n\n"; }
} while (feil || tall < min || tall > max);
return tall;
}
/**
* Leser og returnerer et heltall mellom to gitte grenser.
*
* @param t - Ledetekst til brukeren nr ber om input/et tall
* @param min - Minimum for innlest og godtatt tallverdi
* @param max - Maksimum for innlest og godtatt tallverdi
*
* @return Godtatt verdi i intervallet 'min' - 'max'
*/
int lesInt(const char* t, const int min, const int max) {
char buffer[MAXCHAR] = "";
int tall = 0;
bool feil = false;
do {
feil = false;
std::cout << t << " (" << min << " - " << max << "): ";
std::cin.getline(buffer, MAXCHAR);
tall = atoi(buffer);
if (tall == 0 && buffer[0] != '0')
{ feil = true; std::cout << "\nERROR: Not an integer\n\n"; }
} while (feil || tall < min || tall > max);
return tall;
}
/**
* Enkel verktykasse for lese: tegn og tall.
*
* FULLSTENDIG IDENTISK til 'LesData2.h' bare at adskilt
* funksjonenes DEKLARASJON (som er p DENNE filen) og DEFINISJON.
*
* @file LesData3.H
* @author Frode Haug, NTNU
*/
#ifndef __LESDATA3_H
#define __LESDATA3_H
const int MAXCHAR = 200; // Max.tegn i input-buffer.
char lesChar(const char* t);
float lesFloat(const char* t, const float min, const float max);
int lesInt(const char* t, const int min, const int max);
#endif
\ No newline at end of file
/**
* The minimal viable product of Group 4's product
* @file MVP.cpp
*
* @copyright Copyright (c) 2022
*
*/
/*
#include <iostream> // cout, cin
#include <string> // string
#include <vector> // vector
#include <iomanip> // setw
#include <fstream> // ifstream, ofstream
#include "LesData3.h"
using namespace std;
class Tournament
{
private:
public:
void theTournament();
};
class Player : public Tournament
{
private:
string name;
int rating;
int playerID;
string club;
int wins,
draws;
float score;
public:
// sets all data as empty or 0
Player() {name = club = ""; rating = playerID = wins = draws = score = 0;};
void newPlayer(const string nme);
void writePlayer() const;
string returnName();
float returnScore();
void editSelectedPlayer();
void addWin();
void addDraw();
void calculateScore();
};
class Match : public Tournament {
private:
vector <int *> tableNr;
};
// Global values
const int STRLEN = 100;
int gRounds = 0;
int gTables = 0;
// Vectors
vector<Player *> gPlayers;
vector <Player *> player1;
vector <Player *> player2;
// global functions
void addPlayer();
void writeMenu();
void editPlayer();
void viewPlayers();
Player *findPlayer(const string name);
void newMatch();
void printScore();
int main() {
char command;
//readFromFile();
//readFromFileTournament();
//amntTables();
writeMenu();
command = lesChar("\nCommand");
while (command != 'Q')
{
switch (command)
{
case 'A': addPlayer(); break;
case 'W': viewPlayers(); break;
case 'E': editPlayer(); break;
case 'M': newMatch(); break;
case 'V': printScore(); break;
default: writeMenu(); break;
}
command = lesChar("\nCommand");
}
//writeToFileTournament();
//writeToFile();
return 0;
}
void Player::newPlayer(const string nme) {
name = nme;
rating = lesInt("\n\tPlayers rating: ", 1, 3000);
playerID = lesInt("\n\tPlayers ID: ", 1, 10000);
cout << "\n\tPlayers club: "; getline(cin, club);
}
*/
/**
* edits selected player from list
*
*/
/*
void Player::editSelectedPlayer() {
cout << "\n\tPlayers Name: "; getline(cin, name);
rating = lesInt("\n\tPlayers rating: ", 1, 3000);
playerID = lesInt("\n\tPlayers ID: ", 1, 10000);
cout << "\n\tPlayers club: "; getline(cin, club);
}
*/
/*
* Writes out all info on player
*
*/
/*
void Player::writePlayer() const
{
cout << "\n"
<< "Player name: " << name << '\n'
<< "Player rating: " << rating << '\n'
<< "PlayerID: " << playerID << '\n'
<< "Players club: " << club << '\n'
<< "Amount of draws: " << draws << '\n'
<< "Amount of wins: " << wins << "\n\n";
}
*/
/*
* Finds player based on name and returns pointer
*
* @param name
* @return Player*
*//*
Player *findPlayer(const string name)
{
for (size_t i = 0; i < gPlayers.size(); i++)
{
if (gPlayers[i]->returnName() == name)
return gPlayers[i];
}
return nullptr;
}
string Player::returnName(){
return name;
}
float Player::returnScore(){
return score;
}
void Player::addWin() {
wins++;
}
void Player::addDraw() {
draws++;
}
void Player::calculateScore()
{
score = draws*0.5 + wins*1;
}
void Tournament::theTournament() {
int matches;
matches = lesInt("\n\tHow many matches wil be played: ", 2, (gPlayers.size() / 2));
}
//=============================================================================
//
// NON CLASS FUNCTIONS
//
// ============================================================================
*/
/**
* Add a player
*
* @see - Player::newPlayer()
*/
/*
void addPlayer()
{
Player *newPlayer;
string name;
newPlayer = new Player;
cout << "\n\tnew players name: "; getline(cin, name);
newPlayer->newPlayer(name);
gPlayers.push_back(newPlayer);
cout << "\n\nPlayer added!\n\n";
}
*/
/**
* Writes out all players and their stats
*
* @see writePlayer() const
*//*
void viewPlayers() {
for(size_t i = 0; i < gPlayers.size(); i++){
cout << "\n\tPlayer " << i+1;
gPlayers[i]->writePlayer();
}
}
*/
/**
* find player to edit
*
* @see viewPlayers()
* @see editSelectedPlayer()
* @see returnName()
*//*
void editPlayer() {
int choise,
playerNr;
string name;
choise = lesInt("\n\t1. Enter info 2. List", 1, 2);
// searches after player by name
if(choise == 1) {
cout << "\n\tPlayers name: "; getline(cin, name);
for(size_t i = 0; i < gPlayers.size(); i++) {
if(gPlayers[i]->returnName() == name) {
gPlayers[i]->editSelectedPlayer();
}
}
// pickes player out from list
} else if(choise == 2) {
viewPlayers();
playerNr = lesInt("\n\tPlayer: ", 1, gPlayers.size());
for(size_t i = 0; i < gPlayers.size(); i++) {
if(gPlayers[playerNr-1]->returnName() == gPlayers[i]->returnName())
{
gPlayers[i]->editSelectedPlayer();
}
}
}
}
*/
/**
* Sets up a new match
*
*//*
void newMatch() {
string name;
int WorD,
who;
viewPlayers();
cout << "\n\tPlayer1 name: "; getline(cin, name);
for(size_t i = 0; i < gPlayers.size(); i++) {
if(gPlayers[i]->returnName() == name) {
player1.push_back(gPlayers[i]);
}
}
cout << "\n\tPlayer2 name: "; getline(cin, name);
for(size_t i = 0; i < gPlayers.size(); i++) {
if(gPlayers[i]->returnName() == name) {
player2.push_back(gPlayers[i]);
}
}
WorD = lesInt("\n\tWas there a 1. win or 2. draw", 1, 2);
if(WorD == 2){
player1[0]->addDraw();
player2[0]->addDraw();
cout << "\n\tMatch was a draw! both players rewarded 0.5 points\n\n";
}else if(WorD == 1) {
cout << "\n\tWho won 1. " << player1[0]->returnName() << " or 2. " << player2[0]->returnName(); who = lesInt(": ",1,2);
if(who == 1) {
player1[0]->addWin();
} else {
player2[0]->addWin();
}
}
}
void printScore(){
cout<<"\nResults:\n"
<<"\tName: \tScore:\n";
for(size_t i = 0; i < gPlayers.size(); i++){
gPlayers[i]->calculateScore();
cout<<"\t" <<gPlayers[i]->returnName() <<"\t" <<gPlayers[i]->returnScore() <<"\n";
}
}
*/
/**
* writes the programs menu options
*//*
void writeMenu()
{
cout << "\nMenu: \n"
<< "\tA - Add player\n"
<< "\tW - view players\n"
<< "\tE - Edit player\n"
<< "\tM - New match\n"
<< "\tV - View results\n"
<< "\tQ - Quit\n\n";
}
*/
\ No newline at end of file
firstname
lastname
rating
playerid
club
white
results liste ->
playedAgainst liste ->
//
// Created by sigru on 26.04.2022.
//
#include "Tournament.h"
#include <iostream> // cout, cin
#include <string> // string
#include <vector> // vector
#include <iomanip> // setw
#include <fstream> // ifstream, ofstream
#include "LesData3.h"
#include "player.h"
#include "functions.h"
using namespace std;
extern vector<Tournament *> gTournaments;
extern vector<Player*> playerList;
extern vector<Player*> sortedRankings;
void Tournament::enterResults()
{
if (playerList.size()==0)
{
cout << endl << "No player's exist.";
return;
}
cout << endl << "Enter results for round " << currentRound;
for (int i = 0; i < nrOfTables; ++i)
{
playerList[searchByID(white[i],playerList)]->playedAgainst.push_back( playerList[searchByID(black[i],playerList)]->playerID);
playerList[searchByID(black[i],playerList)]->playedAgainst.push_back(playerList[searchByID(white[i],playerList)]->playerID);
playerList[searchByID(white[i],playerList)]->white++;
int answer;
cout << endl << "Enter results for table " << i+1 << " with "
<< playerList[searchByID(white[i],playerList)]->lastName
<< " as white and "
<< playerList[searchByID(black[i],playerList)]->lastName
<< " as black."
<< endl << "1: white victory"
<< endl << "2: black victory"
<< endl << "3: draw";
answer = lesInt("\nResult: ",1,3);
switch (answer)
{
case 1:
playerList[searchByID(white[i],playerList)]->results.push_back(2);
playerList[searchByID(black[i],playerList)]->results.push_back(0);
break;
case 2:
playerList[searchByID(white[i],playerList)]->results.push_back(0);
playerList[searchByID(black[i],playerList)]->results.push_back(2);
break;
case 3:
playerList[searchByID(white[i],playerList)]->results.push_back(1);
playerList[searchByID(black[i],playerList)]->results.push_back(1);
break;
default:
cout << "Error!";
break;
}
}
sortRanking();
if(currentRound == nrOfRounds)
{
finished=true;
endTournament();
}
else
{
printTables();
}
}
void Tournament::printTables()
{
if (playerList.size()==0)
{
cout << endl << "No player's exist.";
return;
}
if(canStartRound() == false)
{
cout << endl << "There number of tables does not correspond to the amount of players,"
<< " please add or remove players so that there is exactly two players per table." << endl;
return;
}
white.clear();
black.clear();
currentRound++;
if (currentRound == 1)
{
vector<Player*> playersPlaying = playerList;
for (int i = 0; i < nrOfTables; ++i)
{
white.push_back(playersPlaying[playersPlaying.size()-1]->playerID);
playersPlaying.pop_back();
black.push_back(playersPlaying[playersPlaying.size()-1]->playerID);
playersPlaying.pop_back();
}
}
else
{
createMatchups();
}
cout << endl << "Table \t\t White Player \t\t Black Player";
for (int i = 0; i < nrOfTables; ++i)
{
cout << endl << i+1 << "\t\t"
<< playerList[searchByID(white[i],playerList)]->lastName
<< "\t\t" << playerList[searchByID(black[i],playerList)]->lastName;
}
}
void Tournament::createMatchups()
{
Player* currentPlayer;
Player* opponent;
vector<Player*> playersPlaying = sortedRankings;
sortRanking();
for (int i = 0; i < nrOfTables; i++)
{
currentPlayer = playersPlaying[0];
for (int j = 1; j < playersPlaying.size(); j++) //finds the players opponent
{
if (!currentPlayer->hasPlayedAgainst(playersPlaying[j]->playerID))
{
opponent=playersPlaying[j];
for (int k = j; k < playersPlaying.size(); ++k)
{
playersPlaying[k]=playersPlaying[k+1];
}
playersPlaying.pop_back();
for (int k = 0; k < playersPlaying.size(); ++k)
{
playersPlaying[k]=playersPlaying[k+1];
}
playersPlaying.pop_back();
j=playersPlaying.size(); //removes the opponents from the list and exists the loop
}
}
if (currentPlayer->white > opponent->white || currentPlayer->white == opponent->white)
{
white.push_back(opponent->playerID);
black.push_back(currentPlayer->playerID);
}
else if (currentPlayer->white < opponent->white)
{
white.push_back(currentPlayer->playerID);
black.push_back(opponent->playerID);
}
}
}
bool Tournament::canStartRound()
{
if (playerList.size() == nrOfTables*2)
{
return true;
}
else
{
return false;
}
}
void Tournament::endTournament()
{
cout << endl << endl << "The tournament is finished! Here are the final rankings:";
printRanking();
}
/**
* Writes all data about the tournament rounds to file
*/
void Tournament::writeToFileTournaments(ofstream &out)
{
out << nrOfTables << '\n';
out << currentRound << '\n';
out << nrOfRounds << '\n';
for(int i = 0; i < white.size(); i++)
out << white[i];
for(int i = 0; i < black.size(); i++)
out << black[i];
out << finished;
}
/**
* Reads all data about the tournament rounds from file
*/
void Tournament::readFromFileTournaments(ifstream &in)
{
int ID;
in >> nrOfTables; in.ignore();
in >> currentRound; in.ignore();
in >> nrOfRounds; in.ignore();
for(int i = 0; i < nrOfTables; i++)
in >> ID;
white.push_back(ID);
in.ignore();
for(int i = 0; i < nrOfTables; i++)
in >> ID;
black.push_back(ID);
}
//
// Created by sigru on 26.04.2022.
//
#ifndef CHECKMATE_TOURNAMENT_H
#define CHECKMATE_TOURNAMENT_H
#include <iostream> // cout, cin
#include <string> // string
#include <vector> // vector
#include <iomanip> // setw
#include <fstream> // ifstream, ofstream
#include "LesData3.h"
#include "player.h"
using namespace std;
extern vector<Player*> playerList;
extern vector<Player*> sortedRankings;
class Tournament
{
public :
int nrOfTables; //amount of tables in the tournament
int currentRound; //which round we are currently in, is updated in Tournament::printTables
int nrOfRounds; //amount of rounds the tournament has total
vector<int>white; //id of the players with white pieces in the current round, so white[0] is the id of the player with white pieces on table 1
vector<int>black; //id of the players with black pieces in the current round, so black[0] is the id of the player with black pieces on table 1
bool finished;
//white and black are reset each round, but still needs to be saved
virtual void enterResults(); //Enters the results after a round, goes through each table and asks the result before allowing the next round to start
virtual void endTournament(); //Goes if currentround is the final round, prints the final results
virtual void printTables(); //Calls createMatchups and prints the tables
virtual void createMatchups();//Figures out who should play against each other
bool canStartRound();//Controls that the amount of tables and players correspond
void writeToFileTournaments(ofstream &out); //-
void readFromFileTournaments(ifstream &in); //-
};
#endif //CHECKMATE_TOURNAMENT_H
#include <iostream> // cout, cin
#include <string> // string
#include <vector> // vector
#include <iomanip> // setw
#include <fstream> // ifstream, ofstream
#include "LesData3.h"
#include "player.h"
#include "Tournament.h"
#include "functions.h"
using namespace std;
vector<Player*> playerList;
vector<Player*> sortedRankings;
vector<Tournament *> gTournaments;
string menuToPrint;
string menuStart = "\nS: Start a new tournament\nQ: Quit the program";
string menuSetUp = "\nV: View player list\nE: Edit a player\nA: Add player\nD: Delete player\nF: Start the first round\nS: Start a new tournament\nQ: Quit the program";
string menuDuring = "\nV: View player list\nR: View rankings\nE: Edit a player\nN: Enter results and advance to the next round\nS: Start a new tournament\nQ: Quit the program";
string menuEnd = "\nS: Start a new tournament\nR: View rankings\nV: View player list\nQ: Quit the program";
int main()
{
char command;
readFromFile();
readFromFileTournament();
cout << endl << "Welcome to checkmate, a program for organizing chess tournaments. "
<< "Please use a single character as shown in the text when interacting with the program. "
<< "If you encounter any issues, please take contact with either the leader of the team, "
<< "Loke Svelland at lokens@stud.ntnu.no, "
<< "or the lead programmer, Sigrun Hogstedt at sigruah@stud.ntnu.no." << endl;
do
{
if (gTournaments.empty())
{
menuToPrint=menuStart;
}
else if (gTournaments[0]->currentRound==0)
{
menuToPrint=menuSetUp;
}
else if (gTournaments[0]->finished)
{
menuToPrint=menuEnd;
}
else
{
menuToPrint=menuDuring;
}
cout << endl << menuToPrint;
command = lesChar("\nCommand: ");
// cout << command;
if (menuToPrint==menuStart)
{
switch (command)
{
case 'S':
startNewTournament();
break;
case 'Q':
cout << endl << "Closing the program";
break;
default:
cout << endl << "Please enter a valid command.";
break;
}
}
else if (menuToPrint==menuSetUp)
{
switch (command)
{
case 'S':
startNewTournament();
break;
case 'V':
viewPlayers();
break;
case 'E':
editPlayer();
break;
case 'A':
if (playerList.size() == gTournaments[0]->nrOfTables*2)
{
cout << endl << "There are already enough players, either reset tournament and add more tables"
<< " or delete an existing player to add more.";
}
else
{
int id;
id = lesInt("\nWhat is the new player's ID?\n",0,10000);
newPlayer(id);
}
break;
case 'D':
deletePlayer();
break;
case 'Q':
cout << endl << "Closing the program";
break;
case 'F':
gTournaments[0]->printTables();
break;
default:
cout << endl << "Please enter a valid command.";
break;
}
}
else if (menuToPrint==menuDuring)
{
switch (command)
{
case 'S':
startNewTournament();
break;
case 'V':
viewPlayers();
break;
case 'E':
editPlayer();
break;
case 'R':
printRanking();
break;
case 'N':
gTournaments[0]->enterResults();
break;
case 'Q':
cout << endl << "Closing the program";
break;
default:
cout << endl << "Please enter a valid command.";
break;
}
}
else if (menuToPrint==menuEnd)
{
switch (command)
{
case 'S':
startNewTournament();
break;
case 'V':
viewPlayers();
break;
case 'R':
printRanking();
break;
case 'Q':
cout << endl << "Closing the program";
break;
default:
cout << endl << "Please enter a valid command.";
break;
}
}
else
{
cout << endl << "A serious error has occured.";
}
}while (command != 'Q');
writeToFileTournament();
writeToFile();
return 0;
}
#include <iostream> // cout, cin
#include <string> // STRING-KLASSEN
#include "player.h"
#include "LesData3.h"
#include "functions.h"
using namespace std;
extern vector<Player*>playerList;
void deletePlayer()
{
if (playerList.size()==0)
{
cout << endl << "No player's exist.";
return;
}
int choice = 0;
int id;
int index = -1;
string search;
Player* playerToDelete= nullptr;
do {
cout << endl << "Choose what you wish to search by";
choice = lesInt("\n1: search by name \nor \n2: search by playerID\n", 1, 2);
switch (choice)
{
case 1:
do
{
cout << "\nPlease type either the player's first or last name\n";
getline(cin, search);
index = searchByName(search);
if (index == -1)
{
cout << endl << "Name not found, please try again.";
}
} while(index == -1);
playerToDelete = playerList[index];
break;
case 2:
do
{
id = lesInt("\nPlease type the ID of the player you wish to delete\n",0,10000);
index = searchByID(id,playerList);
if (index == -1)
{
cout << endl << "No player with this ID exists, please try again!" << endl;
}
}while(searchByID(id,playerList)==-1);
playerToDelete = playerList[index];
break;
default:
cout << endl << "Invalid input, please try again";
break;
}
}while(playerToDelete == nullptr);
bool toDelete = true;
do {
choice = 0;
cout << endl << "This is the player the program has found: ";
playerToDelete->printPlayer();
choice = lesInt("Do you wish to delete this player?\n1: yes\n2: no\n",1,2);
switch (choice)
{
case 1:
playerList[index]=playerList[playerList.size()-1];
playerList.pop_back();
toDelete=false;
break;
case 2:
toDelete=false;
break;
default:
cout << endl << "Invalid input!";
break;
}
} while (toDelete);
choice = lesInt("\nDo you wish to delete another player?\n1: yes\n2: no",1,2);
switch (choice)
{
case 1:
deletePlayer();
break;
case 2:
return;
break;
default:
cout << endl << "Invalid input, returning to menu.";
return;
break;
}
}
\ No newline at end of file