473,513 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

game help

8 New Member
Hi, iam new to C++, but i thought i'd try making a small game. It is a table, with A-Z across the top and 1-26 down the side. I used arrays in for loops for this. Now i need to work out how to get coordinates (eg, B-14) for random number genorator and user input etc. I cannot work this out, could you please help!!?
Dec 8 '06 #1
19 1532
DemonPiggies
8 New Member
use your random number generator twice for each random move
case:
array[generator()][generator()];

How is you generator constructed?
Dec 8 '06 #2
shadowEod
8 New Member
Thanks, but its getting the coordinates in the first place that my problem even before generating random ones. this is my code for the table

Expand|Select|Wrap|Line Numbers
  1.     for(int i=0; i<=size; i++)            
  2.         cout << setw(1) << top[i] << " ";
  3.  
  4.     cout << endl;                
  5.  
  6.     for(k=0; k<=27; k++)
  7.         cout << "__";                 
  8.  
  9.  
  10.     for(int j=0; j<=size2; j++)
  11.     {
  12.         cout << endl << setw(3) << side[j] << " |";
  13.  
  14.         for(k=1; k<=26; k++)
  15.             cout << setw(1) << "*" << " ";
  16.     }
  17.  
Dec 8 '06 #3
willakawill
1,646 Top Contributor
Hi, if you have constructed your array as arGame[26][26] then you have a matrix that you can instantly access without loops. If you have a single dimension array you can do the same with a little math.

So, for the above array, B-14 is arGame[1][13] in a single dimension use the following guide:

Expand|Select|Wrap|Line Numbers
  1. array[col][row] is array[col + (numcols * row)]
which gives:
Expand|Select|Wrap|Line Numbers
  1. int arGames[26 * 26];
  2.  
  3. //and B-14
  4. guess = arGame[1 + (26 * 12)];
Dec 8 '06 #4
shadowEod
8 New Member
i had to use two arrays, because ones char and the others int - dont know how to do it any other way :(
Dec 8 '06 #5
DemonPiggies
8 New Member
Are you having problems getting the user's input for cord.'s? or just gernerating then?
is this code for graphically showing the table?
(I added some '{' for better reading )
for(int i=0; i<=size; i++)
{cout << setw(1) << top << " ";}

cout << endl;

for(k=0; k<=27; k++)
{ cout << "__"; }


for(int j=0; j<=size2; j++)
{
cout << endl << setw(3) << side[j] << " |";

for(k=1; k<=26; k++)
{cout << setw(1) << "*" << " ";}
}

so if this is the table itself just being outputted to the screen where's the array or arrays I can't realy help without knowing the real scope of the program...
Dec 8 '06 #6
willakawill
1,646 Top Contributor
another thing that might be useful to you is converting the B character to a numeric value for your loop.

First you can use the toupper() function to convert the character to upper case and then subtract 65 (the ascii value of 'A').

If the input is 'D' this will give you the value 3 etc.
Dec 8 '06 #7
DemonPiggies
8 New Member
use char[ letters ][ numbers ]
you can always cast the numbers as char using some one line function
char convert( int number )
{
switch( number )
case 1 etc.
}

or
use an enumerator (probably the easiest)
or submit and use an int array[][] and whenever you need a letter use something like some function
Dec 8 '06 #8
DemonPiggies
8 New Member
that does not work for every system
getting anscii values depends (A) on hardware and (B) on the enviroment in which your programming

...there is probably some table with those values you can go against....
some enviroments have a unction for that but that'll depend on the enviroment...
Dec 8 '06 #9
willakawill
1,646 Top Contributor
that does not work for every system
getting anscii values depends (A) on hardware and (B) on the enviroment in which your programming

...there is probably some table with those values you can go against....
some enviroments have a unction for that but that'll depend on the enviroment...
How interesting. Which environment does not have ascii values for characters?
And which hardware?
Dec 8 '06 #10
shadowEod
8 New Member
My god, your heads might explode when you see my arrays:

Expand|Select|Wrap|Line Numbers
  1.     int size = 25;                        
  2.     int size2 = 25;        
  3.  
  4.     char top [26]={65,66,67,68,69,70,71,72,73,74,75,76,77,
  5.         78,79,80,81,82,83,84,85,86,87,88,89,90};
  6.  
  7.     int side [26]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  8.                  16,17,18,19,20,21,22,23,24,25,26};    
  9.  
  10.     int k = 0;    
  11.  
Dec 8 '06 #11
willakawill
1,646 Top Contributor
My god, your heads might explode when you see my arrays:

Expand|Select|Wrap|Line Numbers
  1.     int size = 25;                        
  2.     int size2 = 25;        
  3.  
  4.     char top [26]={65,66,67,68,69,70,71,72,73,74,75,76,77,
  5.         78,79,80,81,82,83,84,85,86,87,88,89,90};
  6.  
  7.     int side [26]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
  8.                  16,17,18,19,20,21,22,23,24,25,26};    
  9.  
  10.     int k = 0;    
  11.  
Great. And what do these arrays represent?
Dec 8 '06 #12
shadowEod
8 New Member
the top row of the table - A - Z (obviously the 'char top') and the verticle one - 1-26 (int side). its bad i know, but its only supposed to be rough (and just for me)
Dec 8 '06 #13
willakawill
1,646 Top Contributor
the top row of the table - A - Z (obviously the 'char top') and the verticle one - 1-26 (int side). its bad i know, but its only supposed to be rough (and just for me)
It's not bad at all and that is not what I am asking you.
What do the arrays represent in your game. What do you want to use them for?
Dec 8 '06 #14
shadowEod
8 New Member
oh sorry - the game is a hidden coordinate that the user has to guess by inputting the coordinates (obviously) - those arrays are for the top and side - is that what your asking?
Dec 8 '06 #15
shadowEod
8 New Member
This is the entire code so far

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     int size = 25;                        
  9.     int size2 = 25;            
  10.  
  11.     char top [26]={65,66,67,68,69,70,71,72,73,74,75,76,77,                  78,79,80,81,82,83,84,85,86,87,88,89,90};
  12.  
  13.     int side [26]={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};        
  14.  
  15.     int k = 0;                
  16.  
  17.     cout << endl << "\t\t\tHIDDEN SQUARES GAME!" << endl << endl << "     ";
  18.  
  19.  
  20.     for(int i=0; i<=size; i++)    
  21.         cout << setw(1) << top[i] << " ";
  22.  
  23.     cout << endl;                    
  24.  
  25.     for(k=0; k<=27; k++)
  26.         cout << "__";             
  27.  
  28.  
  29.     for(int j=0; j<=size2; j++)
  30.     {
  31.         cout << endl << setw(3) << side[j] << " |";
  32.  
  33.         for(k=1; k<=26; k++)
  34.             cout << setw(1) << "*" << " ";
  35.     }
  36.  
  37.     cout << endl;
  38.  
  39.     return 0;
  40. }
  41.  
Dec 8 '06 #16
DemonPiggies
8 New Member
I know my toshiba satelite using metrowerks in java don't like anscii it uses some other default as it's base I can't remeber which...
I believe C++ and Java have a function where you can format your char's into anscii values instead of it's default
Dec 8 '06 #17
willakawill
1,646 Top Contributor
oh sorry - the game is a hidden coordinate that the user has to guess by inputting the coordinates (obviously) - those arrays are for the top and side - is that what your asking?
I understand. So you do not need to initialise your arrays this way.

if you generate 2 random numbers between 0 and 25, this will be the coordinates of your hidden value. Store the value at these coordinates and let the user guess the coordinates until they are correct or give up.

fistly declare your array like this:

Expand|Select|Wrap|Line Numbers
  1. char arGame[26][26] = {0};
Then you generate 2 random numbers and store your prize at those coordinates as indicated previously by DemonPiggles using a variation of this code to generate the numbers:

Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib> 
  2. #include <ctime> 
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int generator() 
  8.     srand((unsigned)time(0)); 
  9.     int random_integer; 
  10.    random_integer = (rand()%26); 
  11.    return random_integer
  12. }
so you can use

Expand|Select|Wrap|Line Numbers
  1. arGame[generator()][generator()] = 1;
so you now have a hidden square with the value 1, while all the other squares are 0.

Take the input of the guess as a character using a variation of below

Expand|Select|Wrap|Line Numbers
  1. //Program creates char d, sets it equal to lowercase letter
  2. //Converts it to uppercase and outputs it
  3. #include <cctype>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     char d='a';  
  11.     d=toupper(d);
  12.     cout<<d;
  13. }
Don't forget to subtract 25 from the result

Expand|Select|Wrap|Line Numbers
  1. d -= 25;
and you have the value for the left part of your array.
next take the number input and subtract 1. You now have the value for the right part of your array.

Expand|Select|Wrap|Line Numbers
  1. if (arGame[InCh][InNum] == 1)
  2.    cout << "Congratulations! You have just wiped the hard drive";
have fun
Dec 8 '06 #18
shadowEod
8 New Member
Thank you very much :D
Dec 8 '06 #19
willakawill
1,646 Top Contributor
Thank you very much :D
Welcome very much :)
Dec 8 '06 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

138
6406
by: theodp | last post by:
--> From http://www.techdirt.com/articles/20040406/1349225.shtml Microsoft Patents Saving The Name Of A Game Contributed by Mike on Tuesday, April 6th, 2004 @ 01:49PM from the...
1
1548
by: Claude Vernier | last post by:
Hello, I'm a C# programmer. I have two projects. First, write an action scrolling game in C#, (portable to Windows Mobile eventually...), This game will look like The Adventure of Link from...
7
2823
by: Gasten | last post by:
Hello. The last weeks I've been coding a roguelike (you know, like nethack) in python using the nCurses library. Some week ago I ran into a problem: When I made the object for messagebar-output, I...
1
4391
hpbutterbeer
by: hpbutterbeer | last post by:
We have a Machine Project and my brain is currently in a clouded state. Sorry, I'm just a beginner in C Programming... Text twist is a windows game whose main objective is to form words out of the...
0
1695
by: raypjr | last post by:
Hi everyone. I need a little help with some parts of a word guessing game I'm working on. I have some parts done but unsure about others and could use a little advice. Any help is very much...
0
1827
by: Jeff Rush | last post by:
At PyCon this year we're going to have a multi-day game programming clinic and challenge. This is a first-time event and an experiment to find those in the Python community who enjoy playing and...
0
1013
by: smartx | last post by:
Hi, I would like to share my knowledge as a game programmer, really game programming is hard, I don't want to disappoint you, the important point to become a game programmer is to love game...
2
5298
by: LilMeechc20 | last post by:
Hello, I have a group assignment that I have to do. We have to write a Tic Tac Toe game. One person in my group has managed to write the code for a multiplayer (human -vs- human) game and I...
0
7166
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7543
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7106
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7534
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5689
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5094
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3226
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
459
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.