Jump to content
  • Advertisement
Sign in to follow this  
tnutty

C++ game no graphics.

This topic is 3321 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Any opinions? Its supposed to be a copy of this game : http://www.ocvts.org/instructors/htm/mclarke/compsci05/numberGame.jpg
#include<iostream>
#include<windows.h>
#include<ctime>
#include<cstdlib>
#include<string>//not used
#include<vector>

using namespace std;

void Display_gameBoard(vector<int>& vecBrd, int move);
void scrambleVect( vector<int>& gameBrd);
void getMove(int& move);
bool moveCheck(vector<int> gameVec, int move );

int main()
{
system("color 1f");

vector<int> gameBrd(16);//The game board for the puzzle;

char play;
int num_of_Tries(0);//counts the number of tries token to solve the puzzle;
int move(-1); //Determines where the user wants to move;
srand(time(NULL));//seeding random numbers;
int gameOver = 0;

for(int i=0;i<(int)gameBrd.size();i++)//Populate the vector with numbers 1-15 and 00 for space;
{
	gameBrd = i+1;
	if(i==15)
		gameBrd = 0;
}

//Intro to the game / prompt user to play;
cout << "\nThe object of this game is to get the tiles like the following picture : \n\n";
Display_gameBoard(gameBrd,-1);
cout<<"\n\nYou have to move 1 tile next to the blank space (00) just like a puzzle game.\n\n";
Sleep(2500);
cout<<"So for example, you can either move the 15 into the blank space(00) or,";
cout<<"\n\nyou can move 12 into the blank space(00) ( as in this case )\n\n";
Sleep(2500);
cout<<"\n\nAs shown in the tiles, you have to position the numbers 1-15 in order from left to right\n\n";
Sleep(2500);
cout << "\n\nThe computer will scramble up the tiles when the game begins.\n";
Sleep(2500);
cout << "\nSo do you want to play this game (y/n)  :  ";
cin >> play;

if(play == 'n' || play == 'N' )
{
	system("cls");
		cout << "Good bye ...\n ";
	Sleep(1000);
}

else while(play == 'y' || play == 'Y')
{

	if(num_of_Tries == 0)//Scramble the board when the game begins;
	{	system("cls");
		scrambleVect(gameBrd);
		Display_gameBoard(gameBrd,move);
		
	}
	

	//Get move form player;
	cout << "\n\n\nYou could only move one tile at a time.\n\n";
	cout << "The tile you want to move must be next to 00/blank space.\n\nWhat will be your move : ";
	cin >> move;
	cout << endl << endl;

	while(move<=0 || move >15)
	{
		cout<<"Invalid move. Try again :  ";
		cin >>  move;
		cout<<endl<<endl;
	}


	while(moveCheck(gameBrd,move) == false ) 
	{
		cout << "\n\nThats cheating! Invalid move\n" << "\nRemember, only up, left, right, or down(if possible) are valid. Try again :p : ";
		cin >> move;
	}

	
	system("cls");
	Display_gameBoard(gameBrd,move);
	num_of_Tries++; 

	cout<<"	   The number of moves you have made is :  "<<num_of_Tries<<endl;


	//Check if game is over;
	for(int i=0, j = 1 ; i < (int)gameBrd.size()-1 ; i++)
	{
		if(gameBrd != j)
		{
			gameOver = 0;
				break;
		}
		else
		{
			j++;
			gameOver++;
			
		}
		
	}

	if(gameOver == gameBrd.size()-1  )
	{
		cout << "\n\nCongratulation. You have defeated the game.\n\nIt took you "<<num_of_Tries<<" tries to finish this game\n";
		cout << "Thanks for playing. \n";
		exit(1);
	}
		


}

	return 0;

}//End of main();


void Display_gameBoard(vector<int>& vecBrd, int move)
{
	
	int post(-100);

	cout<<"\n\n"<<endl;

	for(int i=0 ; i < (int)vecBrd.size() ; i++)
	{	

		if(vecBrd == 0 && move >0)
			{	post = i;
				vecBrd = move;
			}
		if(vecBrd == move && i!= post)
			vecBrd =0;

		if(i == 4 || i == 8 || i == 12 || i==16)
			cout<<endl<<endl;

		if( vecBrd <10 )
			cout<<" |[0"<<vecBrd<<"] ";
		
		else cout<<" |["<<vecBrd<<"] ";
		
		if(i ==  vecBrd.size())
			cout<<"\n\n"<<endl;

	}

	
}


void scrambleVect( vector<int>& gameBrd)
{
	int change;
	int x(0);
	
	
for(int i=0 ; i < (int)gameBrd.size() ; i++)
	gameBrd = -1;

while(x < 5)
{
		for(int j = 0 ; j < (int)gameBrd.size() ; j++)
		{
			change = rand() % 16;
				
				for(int k=0 ; k<=j ; k++)
				{
					if(gameBrd[k] == change )
					{	
						j = j-1;
						break;
					}
			
					else if(k == j)
						gameBrd[k] = change;
					
				}
		}//end of for loop;
x++;
}//end of while loop;

}//end of function;


bool moveCheck(vector<int> gameVec, int move )
{
	int moveLocation;
	int blank; 
	
	for(int i=0 ; i < (int)gameVec.size() ; i++)
	{
		if(gameVec == move)
				moveLocation = i;
		if(gameVec == 0 )
				blank = i;
	}

	
		if(blank == moveLocation-1 || blank == moveLocation+1 || blank == moveLocation - 4 ||blank == moveLocation+4)	
		return true;
	
	else return false;

}
		
	

Share this post


Link to post
Share on other sites
Advertisement
You could easily make your game multiplatform with a few minor changes, if interested.

First, cls doesn't usually exist in linux, so it just prints an error. This, however, keeps the game playable. as most terminals are 25-lines, you could replace
system("cls");
with
for (int i = 0; i < 25; i++) cout << endl;

Then, Sleep and windows.h doesn't exist in windows. On most platforms, (Linux, Mac, and I think every other Unix) there is another function called usleep, defined in unistd.h, with microsecond precision, so you can do:


#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h>
#define Sleep(n) usleep(n * 1000)
#endif


Or you can follow the inverse path, defining you own usleep on windows
http://blogs.msdn.com/cellfish/archive/2008/09/17/sleep-less-than-one-millisecond.aspx

Hope this helps.

Share this post


Link to post
Share on other sites
Quote:
Original post by ravengangrel
First, cls doesn't usually exist in linux, so it just prints an error. This, however, keeps the game playable. as most terminals are 25-lines...

So you are replacing platform dependency with assumptions. My terminal is 80 characters wide and 26 characters high. What do you think would annoy people more, the fact that they can only play it on Windows, or that it doesn't work well on any system.

@tnutty: You use a loop else while(play == 'y' || play == 'Y') ... but don't set play at the end of the loop and you exit() once the game is over. Either remove the while loop or ask the player at the end if they want to play again.

Share this post


Link to post
Share on other sites
Right, it's not the best way to do it.

However, as far as I know you can't (easily, at least) change the console size on windows, so it would still work on windows and also on most other platforms.

We can replace the 25 with a 200, then it will be work on any terminal with <= 200 lines. It seems good enough from the user's view. Then we are again assuming the user's terminal won't have more than 200 lines.

We can also make good ol'
#ifdef WIN32
#define CLEAR_SCREEN system("cls");
#else
#define CLEAR_SCREEN system("clear");
#endif
But again we are assuming every platform different from windows will have the "clear" command.

We can also check every operating system out there, and add an #elif for each. But then we are ignoring future platforms.

Even more, the OP is assuming the user has a terminal, which could not be true.

Hey, I just wanted to show the OP a quick and easy way to go multiplatform, without giving boring details, because most people aren't interested in anything but windows, and if the OP decided not to ignore my post, he can investigate further on his own or just keep asking here.

Don't take me wrong, please. You made a good point. But you must make some assumptions if you want to program.

Share this post


Link to post
Share on other sites
Probably the best way to do console stuff that is non-standard would be to create a wrapper around all the functions (using something like #ifdef WIN32 as you mentioned) or use some third-party library that is cross-platform. But i really don't think the OP needs to worry about that for just a simple little game unless they decide to actually make it cross-platform.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!