Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
M
Multiplayer Lobby Simulator
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
CI / CD
CI / CD
Pipelines
Schedules
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
Allen B. Doelue
Multiplayer Lobby Simulator
Commits
59183b41
Commit
59183b41
authored
May 14, 2020
by
Cameron Mahan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added Waitlist
parent
11dd2b74
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
264 additions
and
5 deletions
+264
-5
Queue.cpp
Queue.cpp
+132
-0
Queue.h
Queue.h
+124
-0
main.cpp
main.cpp
+8
-5
No files found.
Queue.cpp
0 → 100644
View file @
59183b41
#include <new>
using
namespace
std
;
#include "Queue.h"
//--- Definition of Queue constructor
Queue
::
Queue
()
:
myFront
(
0
),
myBack
(
0
)
{}
//--- Definition of Queue copy constructor
Queue
::
Queue
(
const
Queue
&
original
)
{
myFront
=
myBack
=
0
;
if
(
!
original
.
empty
())
{
// Copy first node
myFront
=
myBack
=
new
Queue
::
Node
(
original
.
front
());
// Set pointer to run through original's linked list
Queue
::
NodePointer
origPtr
=
original
.
myFront
->
next
;
while
(
origPtr
!=
0
)
{
myBack
->
next
=
new
Queue
::
Node
(
origPtr
->
data
);
myBack
=
myBack
->
next
;
origPtr
=
origPtr
->
next
;
}
}
}
//--- Definition of Queue destructor
Queue
::~
Queue
()
{
// Set pointer to run through the queue
Queue
::
NodePointer
prev
=
myFront
,
ptr
;
while
(
prev
!=
0
)
{
ptr
=
prev
->
next
;
delete
prev
;
prev
=
ptr
;
}
}
//--- Definition of assignment operator
const
Queue
&
Queue
::
operator
=
(
const
Queue
&
rightHandSide
)
{
if
(
this
!=
&
rightHandSide
)
// check that not q = q
{
this
->~
Queue
();
// destroy current linked list
if
(
rightHandSide
.
empty
())
// empty queue
myFront
=
myBack
=
0
;
else
{
// copy rightHandSide's list
// Copy first node
myFront
=
myBack
=
new
Queue
::
Node
(
rightHandSide
.
front
());
// Set pointer to run through rightHandSide's linked list
Queue
::
NodePointer
rhsPtr
=
rightHandSide
.
myFront
->
next
;
while
(
rhsPtr
!=
0
)
{
myBack
->
next
=
new
Queue
::
Node
(
rhsPtr
->
data
);
myBack
=
myBack
->
next
;
rhsPtr
=
rhsPtr
->
next
;
}
}
}
return
*
this
;
}
//--- Definition of empty()
bool
Queue
::
empty
()
const
{
return
(
myFront
==
0
);
}
//--- Definition of enqueue()
void
Queue
::
enqueue
(
const
QueueElement
&
value
)
{
Queue
::
NodePointer
newptr
=
new
Queue
::
Node
(
value
);
if
(
empty
())
myFront
=
myBack
=
newptr
;
else
{
myBack
->
next
=
newptr
;
myBack
=
newptr
;
}
}
//--- Definition of display()
void
Queue
::
display
(
ostream
&
out
)
const
{
Queue
::
NodePointer
ptr
;
for
(
ptr
=
myFront
;
ptr
!=
0
;
ptr
=
ptr
->
next
)
{
ptr
->
data
.
displayName
();
out
<<
ptr
->
data
.
getRank
()
<<
endl
;
}
out
<<
endl
;
}
//--- Definition of front()
QueueElement
Queue
::
front
()
const
{
if
(
!
empty
())
return
(
myFront
->
data
);
else
{
cerr
<<
"*** Queue is empty "
" -- returning garbage ***
\n
"
;
QueueElement
*
temp
=
new
(
QueueElement
);
QueueElement
garbage
=
*
temp
;
// "Garbage" value
delete
temp
;
return
garbage
;
}
}
//--- Definition of dequeue()
void
Queue
::
dequeue
()
{
if
(
!
empty
())
{
Queue
::
NodePointer
ptr
=
myFront
;
myFront
=
myFront
->
next
;
delete
ptr
;
if
(
myFront
==
0
)
// queue is now empty
myBack
=
0
;
}
else
cerr
<<
"*** Queue is empty -- can't remove a value ***
\n
"
;
}
Queue.h
0 → 100644
View file @
59183b41
#include <iostream>
#include "Player.h"
#ifndef QUEUE_H
#define QUEUE_H
typedef
Player
QueueElement
;
class
Queue
{
public
:
/***** Function Members *****/
/***** Constructors *****/
Queue
();
/*-----------------------------------------------------------------------
Construct a Queue object.
Precondition: None.
Postcondition: An empty Queue object has been constructed.
(myFront and myBack are initialized to null pointers).
-----------------------------------------------------------------------*/
Queue
(
const
Queue
&
original
);
/*-----------------------------------------------------------------------
Copy Constructor
Precondition: original is the queue to be copied and is received
as a const reference parameter.
Postcondition: A copy of original has been constructed.
-----------------------------------------------------------------------*/
/***** Destructor *****/
~
Queue
();
/*-----------------------------------------------------------------------
Class destructor
Precondition: None.
Postcondition: The linked list in the queue has been deallocated.
-----------------------------------------------------------------------*/
/***** Assignment *****/
const
Queue
&
operator
=
(
const
Queue
&
rightHandSide
);
/*-----------------------------------------------------------------------
Assignment Operator
Precondition: rightHandSide is the queue to be assigned and is
received as a const reference parameter.
Postcondition: The current queue becomes a copy of rightHandSide
and a reference to it is returned.
-----------------------------------------------------------------------*/
bool
empty
()
const
;
/*-----------------------------------------------------------------------
Check if queue is empty.
Precondition: None.
Postcondition: Returns true if queue is empty and false otherwise.
-----------------------------------------------------------------------*/
void
enqueue
(
const
QueueElement
&
value
);
/*-----------------------------------------------------------------------
Add a value to a queue.
Precondition: value is to be added to this queue.
Postcondition: value is added at back of queue.
-----------------------------------------------------------------------*/
void
display
(
ostream
&
out
)
const
;
/*-----------------------------------------------------------------------
Display values stored in the queue.
Precondition: ostream out is open.
Postcondition: Queue's contents, from front to back, have been
output to out.
-----------------------------------------------------------------------*/
QueueElement
front
()
const
;
/*-----------------------------------------------------------------------
Retrieve value at front of queue (if any).
Precondition: Queue is nonempty.
Postcondition: Value at front of queue is returned, unless the queue
is empty; in that case, an error message is displayed and a
"garbage value" is returned.
-----------------------------------------------------------------------*/
void
dequeue
();
/*-----------------------------------------------------------------------
Remove value at front of queue (if any).
Precondition: Queue is nonempty.
Postcondition: Value at front of queue has been removed, unless
queue is empty; in that case, an error message is displayed
and execution allowed to proceed.
-----------------------------------------------------------------------*/
private
:
/*** Node class ***/
class
Node
{
public
:
QueueElement
data
;
Node
*
next
;
//--- Node constructor
Node
(
QueueElement
value
,
Node
*
link
=
0
)
/*-------------------------------------------------------------------
Precondition: value and link are received
Postcondition: A Node has been constructed with value in its
data part and its next part set to link (default 0).
------------------------------------------------------------------*/
{
data
=
value
;
next
=
link
;
}
};
typedef
Node
*
NodePointer
;
/***** Data Members *****/
NodePointer
myFront
,
// pointer to front of queue
myBack
;
// pointer to back of queue
};
// end of class declaration
#endif
main.cpp
View file @
59183b41
...
@@ -3,11 +3,15 @@ using namespace std;
...
@@ -3,11 +3,15 @@ using namespace std;
#include "Player.h"
#include "Player.h"
#include "Lobby.h"
#include "Lobby.h"
#include "Queue.h"
#include <fstream>
#include <fstream>
#include <sstream>
#include <sstream>
int
main
()
int
main
()
{
{
//Create waitlist queue
Queue
waitlist
;
//Create lobbies and add to array
//Create lobbies and add to array
const
unsigned
maxLobbies
=
4
;
const
unsigned
maxLobbies
=
4
;
const
unsigned
maxPlayers
=
6
;
const
unsigned
maxPlayers
=
6
;
...
@@ -59,14 +63,13 @@ int main()
...
@@ -59,14 +63,13 @@ int main()
}
}
if
(
sorted
==
false
)
{
if
(
sorted
==
false
)
{
//Place player in waiting queue
//Place player in waiting queue
waitlist
.
enqueue
(
p
);
cout
<<
"Player placed in wait queue"
<<
endl
;
cout
<<
"Player placed in wait queue"
<<
endl
;
}
}
//Display lobbies
for
(
unsigned
i
=
0
;
i
>
maxLobbies
&&
sorted
==
false
;
i
++
)
{
cout
<<
"Displaying Lobby "
<<
i
+
1
<<
":"
<<
endl
;
lobbies
[
i
].
display
();
}
}
}
//Display waitlist
cout
<<
"Players in waitlist"
<<
endl
;
waitlist
.
display
(
cout
);
}
}
else
cout
<<
"file cannot open"
;
else
cout
<<
"file cannot open"
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment