Docsity

"Programming Assignment for Customized Board Game Functions" - Prof. Daniel W. Watson, Assignments of Computer Science

Toggle sidebar
bg1
CS 24020 – Fall 2006
Assignment 10 – 20 points
Due December 4, 2005
RISK
This homework is worth 2 points.
RISK
Background
In this programming assignment, you will write a set of functions that act as a player in
the classic Hasbro board game called RISK.
If you don't know how to play RISK, I recommend that you do a little web snooping –
there are many sites that will supply you with the official rules to the game, such as
http://boardgames.about.com/od/riskrules/index_a.htm
(the 40th anniversary set rules will suffice for us)
For our game, we make a few changes to the classic rules in order to simplify your task:
There are not three different types of card – there is only one type of card: the
wildcard. If you earn a card during your turn, it is a wildcard. Turn in any
three wildcards at the beginning of your turn to receive more armies.
The first set of cards turned in gets 5 armies. The turn-in value increases by 5
each time a set of cards is turned in.
Free moves are restricted to one per turn, ending the turn, between two adjacent
territories owned by the player.
The maximum allowable number of dice is always used for both the defender
and the attacker.
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download "Programming Assignment for Customized Board Game Functions" - Prof. Daniel W. Watson and more Assignments Computer Science in PDF only on Docsity!

CS 24020 – Fall 2006

Assignment 10 – 20 points

Due December 4, 2005

RISK

This homework is worth 2 points.

RISK

Background

In this programming assignment, you will write a set of functions that act as a player in

the classic Hasbro board game called RISK.

If you don't know how to play RISK, I recommend that you do a little web snooping –

there are many sites that will supply you with the official rules to the game, such as

http://boardgames.about.com/od/riskrules/index_a.htm

(the 40th^ anniversary set rules will suffice for us)

For our game, we make a few changes to the classic rules in order to simplify your task:

 There are not three different types of card – there is only one type of card: the

wildcard. If you earn a card during your turn, it is a wildcard. Turn in any

three wildcards at the beginning of your turn to receive more armies.

 The first set of cards turned in gets 5 armies. The turn-in value increases by 5

each time a set of cards is turned in.

 Free moves are restricted to one per turn, ending the turn, between two adjacent

territories owned by the player.

 The maximum allowable number of dice is always used for both the defender

and the attacker.

Our game of RISK is not restricted to the classic game board. In fact, the board supplied

with your assignment is not the classic version.

Supplied Code

You are given, in this assignment, the code to run the game (risk.cpp), a description of

the board (riskworld.h), and a .h file for each potential player in the game (p xxxx .h)

The world in our game of RISK is defined in a header file called "riskworld.h". Here is

an example of a simple RISK game board:

const int num_territories = 5; const int adj[num_territories][num_territories]= {0,1,0,0,0, 1,0,1,1,0, 0,1,0,0,0, 0,1,0,0,1, 0,0,0,1,0}; const char *name[num_territories]={ "brighamCity", "logan", "paradise", "smithfield", "preston"}; const int num_continents =3; const int continent[num_territories]={0,1,1,1,2}; const char *continent_name[num_territories]={"Flatland","Cache","Potatoland"}; const int continent_worth[num_territories] = {1,4,1}; int next_turn_in_value = 5; int num_players = 0; const int max_num_players = 6; int territory_owner[num_territories] = {-1,-1,-1,-1,-1}; int num_armies[num_territories] = {0,0,0,0,0}; int num_cards[max_num_players] = {0,0,0,0,0,0};

In this world, there are five territories (brighamCity, logan, paradise, smithfield, and

preston) and three continents (Flatland, Cache, and Potatoland). As designated by the

continent array, brighamCity is in Flatland, preston is in Potatoland, and the rest of the

territories are in Cache. The "army worth" of Flatland is 1, Cache is 4, and Potatoland is

1. Initially, there are 0 players, each territory is unowned, there are no armies on any

territory, and nobody has any cards. The initial turn-in value (i.e. the number of armies

you get when you turn in cards) is 5.

If this sounds complicated at this point, don't despair. There are really only three

functions that you need to write to make your player in the game. A set of generic

functions is provided to you to get you started; all you really need to do is to modify the

three functions you are given.

Let's go over the headers for each of those functions:

The first function is called place_army. It is called by the game whenever your player

needs to put an army down on the board. Here is the generic version that you'll get to

start with:

// This function gets called at the times when you are allowed to place // an army on the board. There may be unassigned territories -- if so, // you must pick one of those territories. If there are no unassigned // territories, then you must pick a territory that you currently own. // The return type is the territory you have chosen. Failure to correctly // choose a territory results in forfeiture of your turn. // // The return value is the territory where you wish to place your army. // int place_army_0001( int player_num, // your player number int card_earned, // true if you've earned a card this turn int * from, // not used int * to, // not used int * num) // not used { int i; int min=1000000; int choice; //if there are unclaimed territories, pick one of those for(i=0;i<num_territories;++i) if(territory_owner[i]==-1) return i; //else find my weakest territory and put it there for(i=0;i<num_territories;++i){ if(territory_owner[i]==player_num && num_armies[i]<min) { min = num_armies[i]; choice = i; } } return choice; }

There are a few arguments that aren't used in this function. They are there only so that

the argument lists for all three of your functions are identical, and (hopefully) less

confusing. All this function has to do is return the number of the territory on which you

wish to place your next army.

Note that the name of this function is place_army_0001. The 0001 part of that is your

Eagle PIN. The zip file accompanying this assignment already has your functions in

place. All you need do is to modify the functions in p0001.h, again where 0001 is your

Eagle PIN.

The generic function place_army_0001 looks for any unoccupied territory, or failing that,

the weakest territory. You should change that to make it better. Don't change the

// move to that territory. The num must be a minimum of 1 and a // maximum of the number of armies in the from territory minus 1. // int action_0001( int player_num, // your player number int card_earned, // true if you've earned a card this turn int * from, // the "from" territory int * to, // the "to" territory int * num ){ // how many armies int i; int j; int action_type; action_type = 0; //try to attack if(action_type == 0){ for(i=0; i<num_territories;++i){ for(j=0;j<num_territories;++j){ if(adj[i][j] && territory_owner[i]==player_num && territory_owner[j]!=player_num && num_armies[i] > num_armies[j]){ //neighbor is hostile *from =i; *to = j; *num=num_armies[i]-1; return action_type; } } } } action_type = 1; for(i=0;i<num_territories;++i) if(territory_owner[i] == player_num && num_armies[i]>1){ for(j=0;j<num_territories;++j ){ if(adj[i][j] && territory_owner[j] == player_num){ *from = i; *to = j; *num=num_armies[i]-1; return action_type; } } } //no possible move action_type = 1; *num = 0; return action_type;

This function returns a 0 if your player wishes to attack another territory. It returns a

non-zero value if your player wishes to do a free move, ending your turn.

If you are attacking, the from variable indicates where you are attacking from. The to

variable indicates the territory you are attacking. The num variable tells how many

armies to move, if you conquer the territory on that roll. Your function should set these

variables to the desired values before returning a 0.

If you are performing a free move, the from, to, and num are used for the function to

communicate the territory to take armies from, the territory to send them to, and the

number of armies to send. Your function should set these variables to the desired value

before returning a 1. If you set num to 0, then no armies are moved, and the from and to

variables are not checked.

The function action given simply looks for numerical advantages for attacking. You'll

need to make that more sophisticated. Just don't change the header, or it won't compile

when we put it in with the others.

What To Do

Modify p xxxx .h (where xxxx is your Eagle PIN ) to make it better. Compile risk.cpp.

Run. Improve. Enjoy.

If you want your name to show up in the on-screen menu when you are doing your

individual testing, be sure to modify the check_playername function and main so

that the id associated with your name calls the right function.

What To Turn In

Submit your modified p xxxx .h via Eagle. No report is required.

How It Will Be Graded

15 of the 20 possible points will be determined by your code: neatness, innovation, and

performance.

5 of the 20 possible points will be determined by your code's performance in a RISK

tournament held December 7, in class, according to the following chart: