#include \"GameBoard.h\" #include <cstdlib> #include <ctime> #include <iostream>
ID: 3697199 • Letter: #
Question
#include "GameBoard.h"
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>
using namespace std;
//Set the strategies of all players you have.
//Traverse the list across each row until you are done and set the strategies randomly
void GameBoard::setStrategy(int PERC_COOP)
{
//This sample implementation uses arrays, your task is to rewrite the body of this function so that you use linked list instead of array
srand(time(0));
int random = 0;
for(int i=0; i < length*width; ++i)
{
if (rand()%100 < PERC_COOP)
players[i].setStrategy(COOPERATE);
else
players[i].setStrategy(DEFECT);
}
}
void GameBoard::setNextStrategy()
{
//This sample implementation uses arrays, your task is to rewrite the body of this function so that you use linked list instead of array
for(int i=0; i < length*width; ++i)
{
players[i].setNextStrategy();
}
}
void GameBoard::playGame()
{
//This sample implementation uses arrays, your task is to rewrite the body of this function so that you use linked list instead of array
for(int i=0; i < length*width; ++i)
{
players[i].playGame();
}
}
void GameBoard::displayRewards()
{
//This sample implementation uses arrays, your task is to rewrite the body of this function so that you use linked list instead of array
for(int i=0; i < length*width; ++i)
{
cout << "| " << setw(7) << setprecision(3) << players[i].getReward() << " |" ;
if ((i+1)%length == 0)
cout << endl;
}
}
void GameBoard::displayLastRewards()
{
//This sample implementation uses arrays, your task is to rewrite the body of this function so that you use linked list instead of array
for(int i=0; i < length*width; ++i)
{
cout << "| " << setw(7) << setprecision(3) << players[i].getLastReward() << " |" ;
if ((i+1)%length == 0)
cout << endl;
}
}
void
GameBoard::displayBoard()
{
//This sample implementation uses arrays, your task is to rewrite the body of this function so that you use linked list instead of array
for(int i=0; i < length*width; ++i)
{
if (i%length ==0)
cout << endl;
cout << "| " << players[i].getType() << " |";
}
}
void
GameBoard::displayStrategy()
{
//This sample implementation uses arrays, your task is to rewrite the body of this function so that you use linked list instead of array
for(int i=0; i < length*width; ++i)
{
if (i%length ==0)
cout << endl;
cout << "| " << players[i].getStrategy() << " |";
}
}
//This function sets up the rectangular board. Leave it the way it is.
void GameBoard::setBoard()
{
for(int i=0; i < length*width; ++i)
{
if(i< length)
{
players[i].setTop(NULL);
}
else
players[i].setTop(&players[i-length]);
if(i% length == 0)
{
players[i].setLeft(NULL);
}
else
players[i].setLeft (&players[i-1]);
if((i + 1)%length == 0)
players[i].setRight(NULL);
else
players[i].setRight(&players[i+1]);
if(i>length*(width-1)-1)
players[i].setBottom (NULL);
else
players[i].setBottom(&players[i+length]);
players[i].setType();
}
}
//This function sets up the rectangular board. Leave it the way it is.
void GameBoard::setBoardTorus()
{
for(int i=0; i < length*width; ++i)
{
if(i< length)
{
players[i].setTop(&players[i+length*(width-1)]);
}
else
players[i].setTop(&players[i-length]);
if(i% length == 0)
{
players[i].setLeft(&players[i+length-1]);
}
else
players[i].setLeft (&players[i-1]);
if((i + 1)%length == 0)
players[i].setRight(&players[i-length+1]);
else
players[i].setRight(&players[i+1]);
if(i>length*(width-1)-1)
players[i].setBottom (&players[i-length*(width-1)]);
else
players[i].setBottom(&players[i+length]);
players[i].setType();
}
}
Explanation / Answer
void GameBoard::setStrategy(int PERC_COOP)
{
Srand(time(0));
int random=0;
struct Node *players;
for(players=0;players>length*wide;players=players->next)
{
if(rand()%100<PERC_LOOP)
players->next.setStrategy(COOPERATE);
else
players->next.setStrategy(DEFECT);
}
}
void GameBoard::setNextStrategy()
{
struct Node *players;
for(players=0;players>length*wide;players=players->next)
{
players->next.setNextStrategy();
}
}
void GameBoard::playGame()
{
struct Node *players;
for(players=0;players>length*wide;players=players->next)
{
players->next.playGame();
}
}
void GameBoard::displayRewards()
{
struct Node *players;
for(players=0;players>length*wide;players=players->next) {
cout << "| " << setw(7) << setprecision(3) << players->next.getReward() << " |" ;
if ((i+1)%length == 0)
cout << endl;
}
}
void GameBoard::displayLastRewards()
{
struct Node *players;
for(players=0;players>length*wide;players=players->next)
{
cout << "| " << setw(7) << setprecision(3) << players->next.getLastReward() << " |" ;
if ((i+1)%length == 0)
cout << endl;
}
}
void
GameBoard::displayBoard()
{
struct Node *players;
for(players=0;players>length*wide;players=players->next)
{
if (i%length ==0)
cout << endl;
cout << "| " << players->next.getType() << " |";
}
}
void
GameBoard::displayStrategy()
{
struct Node *players;
for(players=0;players>length*wide;players=players->next)
{
if (i%length ==0)
cout << endl;
cout << "| " << players->next.getStrategy() << " |";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.