Game board code

This question is pretty complicated.
Write a program to simulate a simplified board game called Survival. Two clans are stranded on opposite ends of an island in the Pacific Ocean. It has been about one week since they both arrived at the island mysteriously. Through some fortune both clans have discovered a plot of land in the middle of the island overflowing with resources. The land is a 10 x 10 region, where each element has one type of hidden resource.

The hidden resources are (1) food, (2) weapons, (3) medical supplies and (4) clean water. Each clan will send one representative to play this game.

The simplified game of Survival is a two-player game played on a 10 x 10 game board. The objective of the game is for each clan to gather as many resources to determine who will survive the longest on the island. The winner is the player who has the most number of resources, in terms of points. This modified version of Survival will be simulated using two computer players.

No user input is required for this game. The 10x10 board will be simulated using a 10 x 10 2D array. At the start, the game must randomly decide which computer player (1 or 2) will go first.


This game will include the following pieces:
• “food” piece – represents one food resource
• “weapons” piece – represents one weapons resource
• “medical supplies” piece – represents one medical supplies resource • “clean water” piece – represents one clean water resource

For this game the program should randomly hide 15 pieces of each resource across the 10 x 10 grid. Assume only one resource can be placed inside each box of the grid. The game begins by generating a random number between 25 and 100, inclusive which indicates the total number of turns (i.e. player 1 and player 2 combined) to take.

Then, the program should simulate the game play as follows:

• At each turn, the current state of the game board should be displayed and which resources have been taken by a computer player (player 1 or
2);
• At each turn the program must also generate two random numbers, each between 1 and 10, inclusive that represent the x and y position in the 2D array.
• The rules for placing an item on the game board are as follows:
o If the two random numbers total up to 0, 10 or 18, then the player has the choice to denote the area around that [x][y]. The choice depends on another random number between 1 and 2, inclusive. If the random number is 1, then the detonation will happen. Otherwise, no detonation occurs, the resource can be assigned to the player’s inventory and the game board is updated.

The area around the [x][y] for detonation is the immediate 8 neighbours of the [x][y] position. Denotation means that all resources in those 8 immediate neighbours will be destroyed such that no computer player can acquire them now or in the future.


o For any other dice value, the program should check the [x][y] position. If there is a resource available there it should be assigned to the computer player’s inventory and the game board is updated.

• If the [x][y] position on the game board has no resource, the computer player skips its turn.

Overall, each position (or slot) in the displayed 10x10 2D game board can contain any of the following:
• E  empty space
• F  food resource
• W  weapons resource
• M  medical supplies resource
• C  clean water resource

Every time a resource is assigned to the inventory of one of the computer player’s points are assigned as follows:

Resource Points
Food 45
Weapons 25
Medical Supplies 25
Clean Water 50

The game ends when the following condition is satisfied:

• When there are no more remaining resources to find on the game board. At this point, your program should go through the game board identify the winner.

How to win ( in order of priority)
One player has 25% more clean water resources than the other player
One player has 35% more medical supplies resources than the other player
The player with most number of points

• The program should print a “Tie” if both computer players end up with the same number of points based on their inventory.

Your program must, at least, include the following methods/functions with the correct input parameters and output type:

i. initializeGameBoard, which will take as input the player’s 2D array and assign empty slots containing E;
ii. getResource, which will take as input the 2D array and updates the game board based on the indicated rules;
iii. computeResourcePoints, which will take a resource and return the points awarded to it;
iv. computeWinner, which will take as input the 2D array and display the winner.

Your program also needs to track and print the following:
• The total number of resources belonging to each computer player
• The total number of hidden resources destroyed through detonation
• The total number of hidden resources by type (e.g. food, weapons, etc.) destroyed through detonation


I did a list showing the steps.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  Start- Create 10 x 10 board
- Generate 15 random [x,y]
- Insert food
- Generate 15 random [x,y]
- Insert 15 random [x,y]
- Insert weapons
- Generate 15 random [x,y]
- Insert medical supplies
- Generate 15 random [x,y]
- Insert water
- Generate P|15 =< P >= 100
- Generate random 1/2
- If else statement for player turn
- Generate 15 random [x,y]
- Send player to square
- Generate random 1/2
- If statement( if is 1), detonate 3x3 area.
- To board status
- Player scores
- If statement for (is no of turns >p?)
- Yes - Player Won
- No - start value +1 
- if statement for if there is item 
- Yes - Collect
- No - To board Status
- Player scores
- If statement for (is no of turns >p?)
- Yes - Player Won
- No - Start value +1
- Stop


This are the code I did so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

int main()
{
	int array[10][10];
	int food = 15;
	int weapon = 15;
	int medicalsupplies = 15;
	int water = 15;

	for (int x = 0; x < 10; x++) {
		for (int y = 0; y < 10; y++)
			array[x][y] = rand() % 900 + 100;
		}
	for (int i = 0; i < 10; ++i)
	{
		for (int j = 0; j < 10; ++j)
		{
			cout << array[i][j];
		}
		cout << '\n';
	}

	cout << endl;


	for (int y = 0; y < 10; y++) {
		for (int x = 0; x < 10; x++)
			food = (rand() % food) + 1;
		}
	
	for (int y = 0; y < 10; y++) {
		for (int x = 0; x < 10; x++)
			weapon = (rand() % weapon) + 1;
	}
	

			return 0;
		}
	


I think I did something wrong somewhere where I'm supposed to hide 15 of each resources across the board but I couldn't figure out the problem.
Last edited on
First, get your indentation correctly(although that's probably more of a copy-paste problem, I believe).
Secondly, I suggest abandoning rand(). We are in C++. See: https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful"]Rand considered harmful

Other then that, I can't help you for now(I'm not going to read through your homework assignment), but if you take random value and assign it to some variable, I'd guess one time would do(line 30 and 35). Doing this tens of times really doesn't make much sense.

I'd suggest you removing description of homework, trying to solve it yourself, and when you encounter problem - describe the moment which is problematic for you(what it should do, what it does, show your code), but remember - we want short description of the problem, not your homework ! - and then we will be glad to help.
"rand()" is fine for this trivial use case, the focus should be on completing the assignment first not over complicating things. If OP is concerned about it then he can prototype with "rand()" and then learn to use find & replace.

@ OP: First of all, your teacher is an idiot for telling you to do this on the command line. Secondly, you want to indicate the 'pieces' with letter, so 'array' should be a char array, not an array of ints. Third, you need to initialize your array, that is set the contents all to zero before you do anything: http://en.cppreference.com/w/cpp/language/array . Fourth, your code is more or less executed left to right, top to bottom, so you need to move the part where you output your array to someplace after you populate the items locations. In order to help you further you'll need to include some amount of comments. Note: You should replace all of the instances of 10 with a const int variable and initialize that at the top of your code, this allows you to change everything from one place instead of hunting through your code.
Hi,

I think it is awesome that you have written some pseudo code to help plan you code - good work !! :+)

I have a few pointers to help that process to work better.

First up be wary that the text of the assignment may not be written in the exact order that you need to do things in. For example, two thirds of the way through we discover that the board should store these chars : E, F, W, M, C. So your job is to break the whole problem into manageable pieces
and organise them into a logical order. So this is a Divide & Conquer approach.

For this assignment, they are several things you need to keep track of:

> The Board, and the state it is in;
> Initialisation
> updating for each turn
> The Players and the information they need to keep track of, points etc;
> The Game Play
> who's turn is it?
> what happens when a player has their turn?
> what are the consequences of that turn?
> The Win state
> what constitutes a win?
> keep track of that for each turn

With a more complicated assignment like this one, it might be better to first go through the text of the assignment and highlight the important parts. You could do this on paper with a highlighting pen, or in a word processor that has that capability. Here I have just used the bold function of the formatting options on this page, but I have only done some of it:


Write a program to simulate a simplified board game called Survival. Two clans are stranded on opposite ends of an island in the Pacific Ocean. It has been about one week since they both arrived at the island mysteriously. Through some fortune both clans have discovered a plot of land in the middle of the island overflowing with resources. The land is a 10 x 10 region, where each element has one type of hidden resource.

The hidden resources are (1) food, (2) weapons, (3) medical supplies and (4) clean water. Each clan will send one representative to play this game.

The simplified game of Survival is a two-player game played on a 10 x 10 game board. The objective of the game is for each clan to gather as many resources to determine who will survive the longest on the island. The winner is the player who has the most number of resources, in terms of points. This modified version of Survival will be simulated using two computer players.

No user input is required for this game. The 10x10 board will be simulated using a 10 x 10 2D array. At the start, the game must randomly decide which computer player (1 or 2) will go first.


So you need to identify things like:
> what order do things happen;
> What variables and their types do we need
> What are some meaningful names we can use for our variables & functions
> Are there any variables that are constant
> What code can we put into functions - the assignment listed functions you should have - I would have more than that


One needs to make careful decisions about the types of variables you are going to store - e.g. char, int, double etc. So your board could be char Board[Rows][Cols]; , or if using enums, you can use unsigned int.


Write you pseudo code as actual comments in a .cpp file, and indent them to show what forms part of a process of something more general. It can be helpful to have the comments in front of you on the screen rather than elsewhere. Leave the comments in, so others can see what you did, or at least see what your thought process was. So to start I might have this, showing how I started with general things, then went back and refined them, the indentation shows further refinement:

1
2
// Create a Board object
// Initialise the board with a function - required 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Create a Board object 10 Rows by 10 Columns
     // 10 by 10 array of char - 10 is const and must be positive
      const unsigned int Rows = 10;
      const unsigned int Cols = 10;

     char Board[Rows][Cols];

// Initialise the board with a function - required
     //Function Name InitialiseBoard
     // Return Type void - Board is in main() scope for now
     // arguments 
        // the Board
        // Number of rows const unsigned - used to control loops
        // Number of Columns const unsigned

     // function declaration put before main() function
     void InitialiseBoard( char Board[][Cols] ,    // one can spread the parameters 1 per line - easier to read
                                  const unsigned int NumberRows ,
                                  const unsigned int NumberCols  
     );  

    // Function Body - put after main()
        // Outer loop through Rows
        // Inner loop through Columns
             // initialise each element to 'E' for empty 


You can see that I have gone into lots of detail here for demonstration purposes, once you have some experience you will be able to do less detail, but remember it's still a good idea to at least write out a skeleton of what you want to do, so things aren't missed.

The other thing is that the functions have declarations and definitions, and a place where they are called (and these 3 are separated in the file), so the above pseudo code could be a little confusing. First think about the order in which things need to happen, what functions we need to achieve that, what the function needs in terms of return type and arguments, then what will the actual function look like. Remember that all these 3 things for functions need to match up in terms of their return type, and the number and type of their arguments / parameters.

With the pseudo code you posted, I can see you have skipped over a lot of important things at the start, so this has had an effect on the C++ code. So I am having trouble seeing why you initialised your array on line 14 like you did.

Any way, it's not all bad - I am sure you can work through all this. Hopefully I have managed to show a methodical way of tackling these problems. Good Luck :+D
Topic archived. No new replies allowed.