Question

You will implement 2 games, the Prisoner's Dilemma, The Stag Hunt and compare their results for...

You will implement 2 games, the Prisoner's Dilemma, The Stag Hunt and compare their results for your report. After you finish implementing everything else, you will only need to change one function to make it a prisoner's dilemma or a stag hunt game

1) main.cpp: This is the driver program that sets up the game and plays it. This is where you will call functions to print the statistics. The required statistics is percentage of players using strategy 1 (cooperate or hunt stag) after each round of game play.

2) player.h, player.cc: These files have the definition and implementation of the player class which is a node in the game board. Each player has 4 neighbors (left, right, top, bottom). You will need to implement the functions in the player class. Instructions are in the files. For example, you need to implement the function that the player uses to set his next strategy. You will need to calculate the reward in each round and then add up the reward with the lifetime total.

3) GameBoard.h and GameBoard.cc: This class uses the player class to set up the game board. The setBoard function sets up a rectangle while setBoardTorus creates a donut shaped game board. We will just use the rectangle for this analysis. Most of the functions in this class are done using arrays. You need to change it to linked list based implementation. I suggest you do so after you have finished the Player class and it is working to your satisfaction.

-Board size and shape. -- try various board sizes and shapes (rectangle, square, line are easy to do with the given code). Plot the percentage of players for each strategy with respect to the number of times the game is played

-Starting value/percentage of Cooperating players/Stag hunters. Vary the initial number of players that use the Cooperate/Stag hunt strategy and see how the number changes for consecutive games. plot the strategy vs game number.

________________________________________main.cpp________________________________________________________

#include <iostream>
#include "GameBoard.h"
/*Driver program to test the game and to find some statistics*/

int main(int argc, char** argv) {
   GameBoard g(10,10);
//Set some percentage of strategies as COOPERATE
   g.setStrategy(90);
  
   g.displayBoard();
std::cout << std::endl;
  
   g.displayStrategy();
std::cout << std::endl;
//Play the game a lot of times
   for (int i = 0; i < 100000; ++i)
   {
       g.playGame();
   //   g.displayLastRewards();
   //   std::cout << std::endl;

   // g.displayStrategy();
// std::cout << std::endl;
g.setNextStrategy();

//ToDo: Print the percentage of players using COOPERATE strategy after each game.   
    }
   g.displayLastRewards();
    g.displayStrategy();
  
   return 0;
}

________________________________________________________________________________________________________

________________________________________player.h_________________________________________________________

#ifndef PLAYER_H
#define PLAYER_H
#include <cstdlib>

enum STRATEGY {COOPERATE, DEFECT};

class Player
{
  
private:
double reward;
double game_reward;
STRATEGY strategy;
int type;
Player *left, *right, *top, *bottom;

void setReward(double r){reward = r;}
void calculateRewards();
Player* findBestNbr();

double getReward(STRATEGY s, STRATEGY nbr);
void setLastReward(double r){game_reward = r;}

public:
Player(){ reward = 0; game_reward = 0; strategy = COOPERATE; type = 1; left = NULL; right = NULL; top = NULL; bottom = NULL;}
~Player(){};

double getReward(){ return reward;}
double getLastReward(){return game_reward;}
     
   STRATEGY getStrategy() { return strategy;}
double getType() {return type;}

Player *getLeft(){return left;}
   Player *getRight(){return right;}
Player *getTop(){ return top;}
Player *getBottom(){ return bottom;}

void setStrategy(STRATEGY s){strategy = s;}

void setLeft(Player *l){left = l;}
void setRight(Player *r){right = r;}
void setTop(Player *t){top = t;}
void setBottom(Player *b){bottom = b;}


   void setNextStrategy();

   void playGame();
void setType();
};
#endif

________________________________________________________________________________________________________

__________________________________________player.cc______________________________________________________

#include "player.h"

//Play the game, it is essentially calculating the reward after you have already given yourself a strategy and your neighbors have done the same
//You probably do not need to change anything in this function
void
Player::playGame()
{
   calculateRewards();  
}

//Given your strategy and your neighbors strategy, what will be your reward?
double
Player::getReward(STRATEGY s, STRATEGY nbr)
{
   double r = 0;
  
   return r;       
}  

//Which one of your neighbors received the best payoff in the last game. This function will be quite long
Player *
Player::findBestNbr()
{
   Player *p = NULL;
   return p;
}

//Set your next strategy based by observing your neighbors and your payoff. Perhaps use the strategy which gave the best payoff
//It might be your own strategy or one of your neighbors
void
Player::setNextStrategy()
{
     
   return;
}

//Calculate your reward for the last game and total over all past games.
//You should add up the rewards from games with each neighbor and divide by the number of neighbors you have.
//If you are a corner or border node, you have fewer neighbors
void
Player::calculateRewards()
{
     
   return;
}

//You can set your type by counting how many neighbors you have i.e., how many neighbors are not NULL. This number can be 2,3 or 4   
void
Player::setType()
{
  
   return;          
}

________________________________________________________________________________________________________

________________________________________GameBoard.h____________________________________________________

#ifndef GAMEBOARD_H
#define GAMEBOARD_H
#include "player.h"
class GameBoard
{
public:
GameBoard(){length = 3; width = 3; players = new Player[length*width]; setBoard(); }
GameBoard(int l, int w){length = l; width = w; players = new Player[length*width]; setBoard(); }
~GameBoard(){delete [] players;}
  
Player *getBoard(){return players;}
  
       void setStrategy(int);
void setNextStrategy();
  
       void playGame();
  
       void displayRewards();
void displayLastRewards();
void displayBoard();
   void displayStrategy();


private:   
    Player *players;
    int length, width;
    void setBoard();
    void setBoardTorus();
};
#endif

________________________________________________________________________________________________________

_________________________________________GameBoard.cc__________________________________________________

#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();

}

}

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
You will implement 2 games, the Prisoner's Dilemma, The Stag Hunt and compare their results for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • I've created a functioning program but I can't figure out how to implement class structure into...

    I've created a functioning program but I can't figure out how to implement class structure into it. I'm currently studying classes and I'm wondering if you could help me recreate my program with classes so I can have a better understanding of how classes work. Thank you. #pragma once #include <iostream> using namespace std;    bool DeclareResults(char ch, char gameboard[][3]);    bool FilledBoard(char gameboard[][3]);    void printGameBoard(char gameboard[][3]); #include "fnt.h"; #include <iostream> using namespace std; int main() {    //declare...

  • Find the errors in the following program. You must use pass by reference. #include <iostream> using...

    Find the errors in the following program. You must use pass by reference. #include <iostream> using namespace std; void area2(int area, int length, int width = 0); int main() { int length, width, area; // for rectangle use two arguments cout << "Enter length and width of a rectangle" << endl; cin >> length >> width; cout << "The area is " << area2(area, length, width) << endl; // for squares use one argument cout << "Enter side of a...

  • The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[...

    The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[SIZE];    int player, choice, win, count;    char mark;    count = 0; // number of boxes marked till now    initBoard(board);       // start the game    player = 1; // default player    mark = 'X'; // default mark    do {        displayBoard(board);        cout << "Player " << player << "(" << mark...

  • can someone help me fix my jeopardy game #include<iostream> #include<stdlib.h> using namespace std; int rollDie() {...

    can someone help me fix my jeopardy game #include<iostream> #include<stdlib.h> using namespace std; int rollDie() { return (rand() % 6+1); } void askYoNs(){ cout<<"Do you want to roll a dice (Y/N)?:"<<endl; } void printScores(int turnTotal,int humanTotal,int compTotal){ int player; int human; if(player==human){ cout<<"Your turn total is "<<turnTotal<<endl; } else{ cout<<"computer turn total is "<<turnTotal<<endl; } cout<<"computer: "<<compTotal<<endl; cout<<"human: "<<humanTotal<<endl; cout<<endl; } int human; int changePlayer(int player){ if(player==human) return 1; return human; } int process(int& turnTotal,int roll,int curr_player,int& humanTotal,int& computerTotal){ if(roll==2...

  • In need of some help with a LCR C++ game. The code will run, but I...

    In need of some help with a LCR C++ game. The code will run, but I need it to cycle through the turns automatically so that the only input from the user is the number of players. It's also showing the winner as the person with 0 chips which is incorrect and I can't figure out why. Any help is greatly appreciated as this is already late. Game rules: Develop a program that follows the rules of Left Center Right...

  • Hello, I am working on my final and I am stuck right near the end of...

    Hello, I am working on my final and I am stuck right near the end of the the program. I am making a Tic-Tac-Toe game and I can't seem to figure out how to make the program ask if you would like to play again, and keep the same names for the players that just played, keep track of the player that wins, and display the number of wins said player has, after accepting to keep playing. I am wanting...

  • Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you...

    Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you must copy and paste to cloud9. Do not change the provided complete functions, or the function stub headers / return values. Currently, if the variables provided in main are commented out, the program will compile. Complete the specifications for each function. As you develop the program, implement one function at a time, and test that function. The provided comments provide hints as to what...

  • Hello, I am working on a C++ pick 5 lottery game that gives you the option...

    Hello, I am working on a C++ pick 5 lottery game that gives you the option to play over and over. I have everything working right except that every time the game runs it generates the same winning numbers. I know this is an srand or rand problem, (at least I think it is), but I can't figure out what my mistake is. I've tried moving srand out of the loop, but I'm still getting the same random numbers every...

  • Problem with C++ program. Visual Studio say when I try to debug "Run-Time Check Failure #2...

    Problem with C++ program. Visual Studio say when I try to debug "Run-Time Check Failure #2 - Stack around the variable 'string4b' was corrupted. I need some help because if the arrays for string4a and string4b have different sizes. Also if I put different sizes in string3a and string4b it will say that those arrays for 3a and 3b are corrupted. But for the assigment I need to put arrays of different sizes so that they can do their work...

  • This is an advanced version of the game tic tac toe, where the player has to...

    This is an advanced version of the game tic tac toe, where the player has to get five in a row to win. The board is a 30 x 20. Add three more functions that will check for a win vertically, diagonally, and backwards diagonally. Add an AI that will play against the player, with random moves. Bound check each move to make sure it is in the array. Use this starter code to complete the assignment in C++. Write...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT