Jump to content
J i m b o

Guessing Game [C++]

Recommended Posts

Hey guys, didn't really know I was going to make a guessing game until I was done with the funky ass 2d array but anyways here is your basic, "Guess a number; too high or too low, until you guess right" game using a 2d array of 50 numbers. Then made it look a little nicer, and added the two error handlers. unfortunately I couldn't get row 0 and 1 to line up properly in console since I used a for loop to make the game board.

Well here it is...

[code]#include <iostream>#include <stdlib.h>
#include <time.h>
#include <Windows.h>
using namespace std;






int computerRow, computerCol, userRow, userCol, computerGridNumber, userGridNumber, randomNumber;




int board[10][5] =
{ { 0, 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, 42, 43, 44 }, { 45, 46, 47, 48, 49 } };


void drawGameBoard();
void getComputerNumber();
void getUserNumber();


void SetColor(int value){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), value);
}
//12 is red
//3 is dark teal
//11 is bright teal
//14 is bright yellow


int main()
{






drawGameBoard();


SetColor(10);
cout << "|------------------------------|\n";
cout << "| HELLO! PLEASE ENTER A NUMBER |\n";
cout << "| YOU SEE ON THE GRID. THE GAME|\n";
cout << "| WILL LET YOU KNOW WETHER YOU |\n";
cout << "| ARE CLOSER OR FURTHER AWAY, |\n";
cout << "| -----------------------------|\n";
cout << "| GUESS THE RIGHT NUMBER TO WIN|\n";
cout << "|------------------------------|\n";


getComputerNumber();


do{


getUserNumber();




if (userGridNumber < randomNumber)
{
SetColor(11);
drawGameBoard();
cout << "Too low!" << endl;


cout << "You selected " << userGridNumber << endl;



}
else if (userGridNumber > randomNumber)
{
drawGameBoard();
SetColor(11);
cout << "Too high!" << endl;


cout << "You selected " << userGridNumber << endl;



}
else if (userGridNumber == randomNumber)
{
drawGameBoard();


SetColor(11);
cout << "You win!" << endl;


cout << "You selected " << userGridNumber << endl;
cout << "Computer chose: " << randomNumber << endl;

}




} while (userGridNumber != randomNumber);


cin.get();
return 0;




}








void drawGameBoard()
{
SetColor(15);
cout << " ||ROWS||\n";
SetColor(3);
for (int row = 0; row < 10; row++)
{
for (int col = 0; col < 5; col++)
{



cout << "|" << board[row][col];


}
SetColor(15);
cout << " " << "|" << " ||" << row << "||";
SetColor(3);
cout << "\n-------------------------|";
cout << endl;
}
SetColor(15);
cout << "||0||1||2||3||4| |COLUMNS|" << endl;



}


void getUserNumber()
{


SetColor(13);
cout << "|------------------------------|\n";
cout << "| PLEASE ENTER THE ROW |\n";
cout << "| NUMBER YOU WISH TO USE (UP & |\n";
cout << "| DOWN) USING 0 - 9 |\n";
cout << "|------------------------------|\n";
cin >> userRow;


while (userRow < 0 || userRow > 9)
{
SetColor(12);
cout << "|------------------------------|\n";
cout << "| ||ERROR|| |\n";
cout << "| ---------------------------- |\n";
cout << "| PLEASE USE NUMBERS |\n";
cout << "| 0 - 9 |\n";
cout << "| -----------------------------|\n";
cout << "| LEFT TO RIGHT ROW |\n";
cout << "|------------------------------|\n";
cin >> userRow;


}


SetColor(11);
cout << "|------------------------------|\n";
cout << "| PLEASE ENTER THE COLUMN |\n";
cout << "| NUMBER YOU WISH TO USE (LEFT |\n";
cout << "| TO RIGHT) USING 0 - 4 |\n";
cout << "|------------------------------|\n";
cin >> userCol;


while (userCol < 0 || userCol > 4)
{
SetColor(12);
cout << "|------------------------------|\n";
cout << "| ||ERROR|| |\n";
cout << "| ---------------------------- |\n";
cout << "| PLEASE USE NUMBERS |\n";
cout << "| 0 - 4 |\n";
cout << "| -----------------------------|\n";
cout << "| UP AND DOWN COLUMNS |\n";
cout << "|------------------------------|\n";
cin >> userCol;
}
userGridNumber = board[userRow][userCol];


cin.get();




}


void getComputerNumber()
{

srand(time(NULL));


//Gets the computers row number
computerRow = rand() % 9 + 0;


//Gets the computers col number
computerCol = rand() % 4 + 0;


//Gets the actual grid number to base our guessing off of

computerGridNumber = board[computerRow][computerCol];


randomNumber = computerGridNumber;



}[/code]

Share this post


Link to post
Share on other sites
Looks good, formatting can use some fixing up however. Consider checking out [url=http://www.projectpluto.com/win32a.htm]PDcurses[/url] for manipulating the console; the Windows API is massive and including it only for the purpose of coloring the console is somewhat unnecessary.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×