Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PROG1004_2022_group4
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Magnus Westerheim Johannessen
PROG1004_2022_group4
Commits
b3cdc68d
Commit
b3cdc68d
authored
3 years ago
by
lokesvelland
Browse files
Options
Downloads
Patches
Plain Diff
fikset på players og slikt
parent
7ef3b109
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/kode/checkmate.cpp
+358
-290
358 additions, 290 deletions
src/kode/checkmate.cpp
with
358 additions
and
290 deletions
src/kode/checkmate.cpp
+
358
−
290
View file @
b3cdc68d
...
...
@@ -7,8 +7,8 @@
#include
"LesData2.h"
using
namespace
std
;
class
Player
{
class
Player
{
private:
string
name
;
int
rating
;
...
...
@@ -19,13 +19,25 @@ private:
float
score
;
public:
void
newPlayer
();
bool
playerName
(
const
string
pname
)
{
if
(
pname
==
name
)
{
void
newPlayer
(
Player
*
player
);
bool
playerName
(
const
string
pname
)
{
if
(
pname
==
name
)
{
return
true
;
}
else
return
false
;
};
bool
playerId
(
const
int
ID
)
{
if
(
ID
==
playerID
)
{
}
else
return
false
;
};
bool
playerId
(
const
int
ID
)
{
if
(
ID
==
playerID
)
{
return
true
;
}
else
return
false
;
};
}
else
return
false
;
};
void
writePlayer
()
const
;
void
updatePlayer
();
int
playerWin
()
{
wins
=
0
;
};
...
...
@@ -36,11 +48,13 @@ public:
void
readFromFilePlayer
(
ifstream
&
in
);
};
class
Tournament
{
class
Tournament
{
private:
int
tableNr
[
gTables
];
// these will only work if gTables is a const?
string
player1
[
gTables
],
player2
[
gTables
];
public:
void
writeTournament
();
void
fillTableArray
();
...
...
@@ -74,8 +88,8 @@ void skrivMeny();
void
writeToFile
();
void
readFromFile
();
int
main
()
{
int
main
()
{
char
command
;
// readFromFile();
...
...
@@ -85,8 +99,10 @@ char command;
skrivMeny
();
command
=
lesChar
(
"
\n
Kommando"
);
while
(
command
!=
'Q'
)
{
switch
(
command
)
{
while
(
command
!=
'Q'
)
{
switch
(
command
)
{
case
1
:
newTournament
();
break
;
case
2
:
addPlayer
();
break
;
case
3
:
enterResults
();
break
;
...
...
@@ -106,29 +122,33 @@ char command;
/**
* Reads new player
*/
void
Player
::
newPlayer
()
{
cout
<<
"
\n\t
Full name: "
;
getline
(
cin
,
player
->
name
);
void
Player
::
newPlayer
(
Player
*
player
)
{
cout
<<
"
\n\t
Full name: "
;
getline
(
cin
,
player
->
name
);
player
->
rating
=
lesInt
(
"
\n\t
Rating: "
,
1
,
10
);
player
->
playerID
=
lesInt
(
"
\n\t
Player ID: "
,
1
,
1000
);
cout
<<
"
\n\t
Club: "
;
getline
(
cin
,
player
->
club
);
cout
<<
"
\n\t
Club: "
;
getline
(
cin
,
player
->
club
);
}
/**
* Writes out all info on player
*
*/
void
Player
::
writePlayer
()
const
{
void
Player
::
writePlayer
()
const
{
cout
<<
"
\n
"
<<
name
<<
'\n'
cout
<<
"
\n
"
<<
name
<<
'\n'
<<
rating
<<
'\n'
<<
playerID
<<
'\n'
<<
club
<<
'\n'
<<
wins
<<
"
\n\n
"
;
}
/**
...
...
@@ -137,11 +157,16 @@ void Player::writePlayer() const {
* @param name
* @return Player*
*/
Player
*
findPlayer
(
const
string
name
)
{
for
(
int
i
=
0
;
i
<
gPlayers
.
size
();
i
++
)
{
if
(
gPlayers
[
i
]
->
playerName
(
name
)
==
true
)
{
Player
*
findPlayer
(
const
string
name
)
{
for
(
int
i
=
0
;
i
<
gPlayers
.
size
();
i
++
)
{
if
(
gPlayers
[
i
]
->
playerName
(
name
)
==
true
)
{
return
gPlayers
[
i
];
}
else
return
nullptr
;
}
else
return
nullptr
;
}
}
...
...
@@ -151,29 +176,34 @@ Player* findPlayer(const string name) {
* @param ID
* @return Player*
*/
Player
*
findPlayerID
(
const
int
ID
)
{
for
(
int
i
=
0
;
i
<
gPlayers
.
size
();
i
++
)
{
if
(
gPlayers
[
i
]
->
playerId
(
ID
)
==
true
)
{
Player
*
findPlayerID
(
const
int
ID
)
{
for
(
int
i
=
0
;
i
<
gPlayers
.
size
();
i
++
)
{
if
(
gPlayers
[
i
]
->
playerId
(
ID
)
==
true
)
{
return
gPlayers
[
i
];
}
else
return
nullptr
;
}
else
return
nullptr
;
}
}
/**
* Calculates the score for a single player
*
*/
void
Player
::
calculateScore
(){
void
Player
::
calculateScore
()
{
for
(
int
i
=
1
;
i
<
wins
;
i
++
){
for
(
int
i
=
1
;
i
<
wins
;
i
++
)
{
score
+=
1
;
}
for
(
int
i
=
1
;
i
<
draws
;
i
++
){
for
(
int
i
=
1
;
i
<
draws
;
i
++
)
{
score
+=
0.5
;
}
}
/**
...
...
@@ -181,13 +211,15 @@ for(int i=1; i<draws; i++){
*
* @param in - the file being read from
*/
void
Player
::
readFromFilePlayer
(
ifstream
&
in
){
void
Player
::
readFromFilePlayer
(
ifstream
&
in
)
{
getline
(
in
,
name
);
in
>>
rating
;
in
.
ignore
();
in
>>
playerID
;
in
.
ignore
();
in
>>
rating
;
in
.
ignore
();
in
>>
playerID
;
in
.
ignore
();
getline
(
in
,
club
);
}
/**
...
...
@@ -195,7 +227,8 @@ getline(in, club);
*
* @param out - the file being written to
*/
void
Player
::
writeToFilePlayer
(
ofstream
&
out
){
void
Player
::
writeToFilePlayer
(
ofstream
&
out
)
{
out
<<
setw
(
STRLEN
)
<<
name
<<
setw
(
STRLEN
)
<<
rating
...
...
@@ -208,11 +241,13 @@ void Player::writeToFilePlayer(ofstream & out){
* Writes out all data for one tournament round
*
*/
void
Tournament
::
writeTournament
(){
void
Tournament
::
writeTournament
()
{
cout
<<
setw
(
STRLEN
)
<<
"White player"
<<
setw
(
STRLEN
)
<<
"Black player"
<<
setw
(
STRLEN
)
<<
"Table number
\n
"
;
for
(
int
i
=
0
;
i
>
gTables
;
i
++
){
for
(
int
i
=
0
;
i
>
gTables
;
i
++
)
{
cout
<<
setw
(
STRLEN
)
<<
player1
[
i
]
<<
setw
(
STRLEN
)
<<
player2
[
i
]
<<
setw
(
STRLEN
)
<<
tableNr
[
i
];
...
...
@@ -223,9 +258,11 @@ void Tournament::writeTournament(){
* Fills the tableNr array with numbers [1,2,3,...,gTables]
*
*/
void
Tournament
::
fillTableArray
(){
void
Tournament
::
fillTableArray
()
{
tableNr
[
0
]
=
1
;
for
(
int
i
=
1
;
i
<
gTables
;
i
++
){
for
(
int
i
=
1
;
i
<
gTables
;
i
++
)
{
tableNr
[
i
]
=
tableNr
[
i
-
1
]
+
1
;
}
}
...
...
@@ -235,9 +272,11 @@ void Tournament::fillTableArray(){
*
* @param out - the file being written to
*/
void
Tournament
::
writeToFileTournament
(
ofstream
&
out
){
void
Tournament
::
writeToFileTournament
(
ofstream
&
out
)
{
for
(
int
i
=
0
;
i
<
gTables
;
i
++
){
for
(
int
i
=
0
;
i
<
gTables
;
i
++
)
{
out
<<
setw
(
STRLEN
)
<<
player1
[
i
]
<<
setw
(
STRLEN
)
<<
player2
[
i
]
<<
setw
(
STRLEN
)
<<
tableNr
[
i
]
...
...
@@ -250,14 +289,18 @@ for(int i=0; i<gTables; i++){
*
* @param in - file being read from
*/
void
Tournament
::
readFromFileTournament
(
ifstream
&
in
){
void
Tournament
::
readFromFileTournament
(
ifstream
&
in
)
{
for
(
int
i
=
0
;
i
<
gTables
;
i
++
){
in
>>
player1
[
i
];
in
.
ignore
();
//Might be getting some
in
>>
player2
[
i
];
in
.
ignore
();
//Difficulties here because of spacing between names
in
>>
tableNr
[
i
];
in
.
ignore
();
//Can't use getline() because player1 and player2 are arrays
for
(
int
i
=
0
;
i
<
gTables
;
i
++
)
{
in
>>
player1
[
i
];
in
.
ignore
();
// Might be getting some
in
>>
player2
[
i
];
in
.
ignore
();
// Difficulties here because of spacing between names
in
>>
tableNr
[
i
];
in
.
ignore
();
// Can't use getline() because player1 and player2 are arrays
}
}
/**
...
...
@@ -270,7 +313,8 @@ for(int i=0; i<gTables; i++){
* Enter results for a specific round
*
*/
void
enterResults
()
{
void
enterResults
()
{
int
round
;
int
table
;
...
...
@@ -286,28 +330,27 @@ table = lesInt("Table:",0,(gTables+1));
cout
<<
"
\n
Enter result for "
<<
/*name of player*/
"(W(in), L(ose), D(raw)):"
;
result
=
lesChar
(
"Result:"
);
if
(
result
==
'W'
){
if
(
result
==
'W'
)
{
//+1 to wins int of player ]
}
else
if
(
result
==
'D'
){
else
if
(
result
==
'D'
)
{
//+1 to draws int of player
}
}
/**
* Calculates and prints out scores for all players in descending order
*
* @see - calculateScore()
*/
void
viewResults
(){
void
viewResults
()
{
for
(
int
i
=
0
;
i
<
gPlayers
.
size
();
i
++
){
for
(
int
i
=
0
;
i
<
gPlayers
.
size
();
i
++
)
{
gPlayers
[
i
]
->
calculateScore
();
}
}
/**
...
...
@@ -315,7 +358,8 @@ for(int i=0; i < gPlayers.size();i++){
* @see writeTournament()
* @see fillTableArray()
*/
void
newTournament
()
{
void
newTournament
()
{
gRounds
++
;
}
...
...
@@ -325,11 +369,12 @@ void newTournament() {
*
* @see - Player::newPlayer()
*/
void
addPlayer
(){
void
addPlayer
()
{
Player
*
newPlayer
;
newPlayer
=
new
Player
;
newPlayer
->
newPlayer
();
newPlayer
->
newPlayer
(
newPlayer
);
gPlayers
.
push_back
(
newPlayer
);
cout
<<
"
\n\n
Player added!
\n\n
"
;
...
...
@@ -340,52 +385,69 @@ void addPlayer(){
*
* @see - Player::updatePlayer
*/
void
editPlayer
(){
int
choise
;
void
editPlayer
()
{
int
choise
,
ID
;
string
search
;
// choose wether to enter info or look through list
choise
=
lesInt
(
"
\n\t
1. Enter info or 2. List"
,
1
,
2
);
cout
<<
"
\n\n
"
;
if
(
choise
==
1
){
choise
=
0
;
// enter info
if
(
choise
==
1
)
{
choise
=
0
;
// resets variable
// choose search by name or playerID
choise
=
lesInt
(
"
\n\t
1. search by name or 2. search by playerID"
,
1
,
2
);
if
(
choise
==
1
){
cout
<<
"
\n\t
Name: "
;
getline
(
cin
,
search
);
if
(
findPlayer
(
search
)
==
nullptr
)
{
if
(
choise
==
1
)
// if name
{
// user enters name to search for
cout
<<
"
\n\t
Name: "
;
getline
(
cin
,
search
);
// findPlayer searches through all players in vector
// if none has the name
if
(
findPlayer
(
search
)
==
nullptr
)
{
cout
<<
"
\n\t
Name not recognized!
\n
"
;
}
else
{
}
else
{
// if player found, write out info
cout
<<
"
\n\t
Player found!
\n
"
;
findPlayer
(
search
)
->
writePlayer
();
}
}
else
if
(
choise
==
2
){
findPlayer
(
search
)
->
ID
=
lesInt
(
"
\n\t
PlayerID: "
,
1
,
1000
);
if
(
findPlayerID
(
ID
)
==
nullptr
)
{
}
// if choose playerID
else
if
(
choise
==
2
)
{
ID
=
lesInt
(
"
\n\t
PlayerID: "
,
1
,
1000
);
if
(
findPlayerID
(
ID
)
==
nullptr
)
{
cout
<<
"
\n\t
PlayerID not recognized!"
;
}
else
{
}
else
{
cout
<<
"
\n\t
Player found!
\n
"
;
findPlayerID
(
ID
)
->
writePlayer
();
}
}
cout
<<
"
\n\n
"
;
}
else
if
(
choise
==
2
)
{
else
if
(
choise
==
2
)
{
viewPlayers
();
}
}
/**
*
*
*/
void
viewPlayers
()
{
void
viewPlayers
()
{
for
(
int
i
=
0
;
i
<
gPlayers
.
size
();
i
++
)
{
for
(
int
i
=
0
;
i
<
gPlayers
.
size
();
i
++
)
{
cout
<<
gPlayers
[
i
]
<<
"
\n\n
"
;
}
}
/**
...
...
@@ -394,14 +456,15 @@ void viewPlayers() {
* @return true
* @return false
*/
bool
checkPlayers
()
{
bool
checkPlayers
()
{
}
/**
* writes the programs menu options
*/
void
skrivMeny
()
{
void
skrivMeny
()
{
cout
<<
"
\n
Menu:
\n
"
<<
"
\t
1 - Generate tournament set-up
\n
"
...
...
@@ -411,17 +474,18 @@ cout << "\nMenu: \n"
<<
"
\t
5 - view player list
\n
"
<<
"
\t
6 - view results
\n
"
<<
"
\t
Q - Quit
\n\n
"
;
}
/**
* Writes all data belonging to the Player class to the file
*/
void
writeToFile
(){
void
writeToFile
()
{
ofstream
outfile
(
"PlayerList.dta"
);
if
(
outfile
){
if
(
outfile
)
{
for
(
int
i
=
0
;
i
<
gPlayers
.
size
();
i
++
)
gPlayers
[
i
]
->
writeToFilePlayer
(
outfile
);
...
...
@@ -429,19 +493,21 @@ if(outfile){
}
else
cout
<<
"
\n
Couldn't find PlayerList.dta
\n
"
;
// maybe unneccesary, considering it's also checked when reading data from file
}
/**
* Reads all data belonging to the Player class from the file
*/
void
readFromFile
(){
void
readFromFile
()
{
Player
*
playerNew
;
ifstream
infile
(
"PlayerList.dta"
);
if
(
infile
){
while
(
!
infile
.
eof
()){
if
(
infile
)
{
while
(
!
infile
.
eof
())
{
playerNew
=
new
Player
();
playerNew
->
readFromFilePlayer
(
infile
);
gPlayers
.
push_back
(
playerNew
);
...
...
@@ -451,17 +517,18 @@ if(infile){
}
else
cout
<<
"
\n
Couldn't find PlayerList.dta!
\n
"
;
}
/**
* Writes all data about the tournament rounds to file
*/
void
writeToFileTournament
(){
void
writeToFileTournament
()
{
ofstream
outfile
(
"tournamentData.dta"
);
if
(
outfile
){
if
(
outfile
)
{
for
(
int
i
=
0
;
i
<
gTournaments
.
size
();
i
++
)
gTournaments
[
i
]
->
writeToFileTournament
(
outfile
);
...
...
@@ -469,19 +536,21 @@ if(outfile){
}
else
cout
<<
"
\n
Couldn't find tournamentData.dta
\n
"
;
// maybe unneccesary, considering it's also checked when reading data from file
}
/**
* Reads all data about the tournament rounds from file
*/
void
readFromFileTournament
(){
void
readFromFileTournament
()
{
Tournament
*
tournamentNew
;
ifstream
infile
(
"tournamentData.dta"
);
if
(
infile
){
while
(
!
infile
.
eof
()){
if
(
infile
)
{
while
(
!
infile
.
eof
())
{
tournamentNew
=
new
Tournament
();
tournamentNew
->
readFromFileTournament
(
infile
);
gTournaments
.
push_back
(
tournamentNew
);
...
...
@@ -491,5 +560,4 @@ if(infile){
}
else
cout
<<
"
\n
Couldn't find tournamentData.dta!
\n
"
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment