2 Replies - 672 Views - Last Post: 02 November 2009 - 07:47 PM Rate Topic: -----

#1 mgrande  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-October 09

How to use structures in C++

Posted 02 November 2009 - 02:44 PM

Alright now. I'm having a problem using structures in C++. My problem is, I don't know anything about them and how to use them. My professor tried to teach this stuff in class, but failed (I personally think he's a lousy teacher and it's a fact that he's a very lazy teacher). Anyways, I'm supposed to use structures this program I wrote. The program's like battleship except it's with robots. It's pretty fun to play, but if you want to play it, I would recommend taking out the part of the code that prints out the 2d array :P. Here it is:

Main code:
#include "robot.h"

int main()
{
	srand (time(NULL));

	board_creation (board);  //creates the playing board
	deploy(locX, locY, board); // deploys robots

	for (int i = 0; i < 5; i++)
	{
		for (int h = 0; h < 5; h++)
		{
			cout << board[i][h] << "|";
		}
		cout << endl;
	}

	fire (fireX, fireY);
	hit (fireX, fireY, locX, locY, board, robots);

	for (int z = 0; z < 1; z--)
	{
		operational (robots);

	for (int i = 0; i < 5; i++)
	{
		for (int h = 0; h < 5; h++)
		{
			cout << board[i][h] << "|";
		}
		cout << endl;
	}

		fire (fireX, fireY);
		hit (fireX, fireY, locX, locY, board, robots);
	}
}

void board_creation (char board[5][5])
{
	for (int a = 0; a < 5; a++)
	{
		for (int b = 0; b < 5; b++)
			board[a][b] = 'O';
	}
}

void deploy (int locX, int locY, char board[5][5])
{
	for (int a = 0; a < 5; a++)
	{
		pick(locX, locY);
		match(locX, locY, board);
	}
}

void pick (int &locX, int &locY)
{

	locX = rand() % 5;
	locY = rand() % 5;
}

void match (int locX, int locY, char board[5][5])
{
	while (board[locX][locY] == 'X')
		pick(locX, locY);
	if (board[locX][locY] != 'X')
		board[locX][locY] = 'X';
}

void fire (int &fireX, int &fireY)
{
	cout << "Please enter your X-axis firing coordinates (1 - 5): ";
	cin >> fireX;
	fireX = fireX - 1;
	cout << "Please enter your Y-axis firing coordinates (1 - 5): ";
	cin >> fireY;
	fireY = fireY - 1;
}

void operational (int robots)
{
	if (robots != 0)
		cout << "There's still more robots, sir." << endl;
	else
	{
		cout << "You destroyed all the robots, sir." << endl;
		exit (1);
	}
}

void unprotect (int locX, int locY, char board[5][5], int robots)
{
	pick (locX, locY);
	collision (locX, locY, board, robots);
}

void collision (int locX, int locY, char board[5][5], int &robots)
{
	if (board[locX][locY] == 'X' || board[locX][locY] == 'Y')
	{
		board[locX][locY] = 'O';
		robots--;
		robots--;
	}
	else
		board[locX][locY] = 'Y';
}

void hit (int fireX, int fireY, int locX, int locY, char board[5][5], int &robots)
{
	if (board[fireX][fireY] == 'O')
		cout << "That was a miss, sir." << endl;
	else if (board[fireX][fireY] == 'X')
	{
		cout << "That was a hit, sir." << endl;
		cout << "The robot is now unprotected and jumped to a new area." << endl;
		board[fireX][fireY] = 'O';
		unprotect (locX, locY, board, robots);
	}
	else if (board[fireX][fireY] == 'Y')
	{
		cout << "That was a hit, sir." << endl;
		cout << "The robot was destroyed." << endl;
		board[fireX][fireY] = 'O';
		robots--;
	}
}



Here's the .h code
/* Robot world game (like battleship).
 * Mike Grande
 * 10/29/09
 * CS23021
 * Lab 08 Robot World
 */

#ifndef ROBOT_H_
#define ROBOT_H_

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int robots = 5; // Number of robots
char board[5][5]; // Playing board size
int locX, locY; // Location chosen before robot is placed
int fireX, fireY; // Location chosen to shoot

//
// Initialization functions
//

void board_creation (char board[5][5]); // Creates playing board
void deploy(int locX, int locY, char board[5][5]); // Randomly places 5 robots on playing board
void pick(int &locX, int &locY); // Chooses a random location on playing board
void match(int locX, int locY, char board[5][5]); // Checks to see if a robot occupies random location chosen

//
// Battle functions
//

void operational(int robots);  // Exits if all robots are destroyed
void fire(int &fireX, int &fireY); // Asks for firing coordinates
void unprotect (int locX, int locY, char board[5][5], int robots); // Robot loses its protection and jumps to a new location
void collision (int locX, int locY, char board[5][5], int &robots); // If a robot jumps to an occupied location, both robots are destroyed
void hit (int fireX, int fireY, int locX, int locY, char board[5][5], int &robots); // Checks to see if a robot was hit and destroyed

#endif



I don't want this done for me...I want help with it so I can do it myself! If anybody has a tutorial or something like that then that would be fantastic. Thanks in advance for your help guys.

Mike

Is This A Good Question/Topic? 0
  • +

Replies To: How to use structures in C++

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1051
  • View blog
  • Posts: 4,458
  • Joined: 09-June 09

Re: How to use structures in C++

Posted 02 November 2009 - 02:57 PM

Google structs and classes ..

but a struct and a class have the same functionality;

struct test
{
//By default all members in a struct are public to main
// type private: to make members only usable within class functions
// declare your functions
int add(int, int);

};

//Define your function
int test::add(int a, int b)
{
  return a+b;
}

int main()
{
 test call;
 cout<<call.add(2,2)<<endl;

 return 0;
}


Was This Post Helpful? 1
  • +
  • -

#3 mgrande  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-October 09

Re: How to use structures in C++

Posted 02 November 2009 - 07:47 PM

I finally figured it out :). It's really not difficult at all haha. Time for a well deserved break. Thanks a lot for the help ImaSexy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1