Could someone explain why I would create an Entity base class that all my entities would derive from. Wouldn't this limit the inheritance of the classes that derive from the base class?
I can understand why it would be a good idea, but what about inheritance for the other classes that may need their own child classes? (I don't think multiple inheritance exists in C#)
I'm having a bit of a math issue.
I've got a player, right? And he's on a tile (32x32). There are 8 tiles around him. I want to find the tile that he's aiming at--that is, he's aiming at the mouse, and I want to find which of the 8 tiles he is aiming at.
How do I do this?
He can't be aiming at one of the corner tiles because the only part of those that are "exposed" are the corner points, and points are infinitesimal and therefore negligible. So the player is aiming at the tile either above, below, left, or right.
If the player's position is always considered to be at the center of the tile he's in, then it's pretty simple. Subtract the player's position from the mouse position and look at the X and Y components of the resulting vector. If the abs(Y) > abs(x), the mouse is pointing on something steeper than a 45-degree angle, so look at the sign of Y to decide whether it's the top or bottom tile. Otherwise, look at the sign of X to decide whether it's the left or right tile.
[QUOTE=Yogurt;32952322]I'm having a bit of a math issue.
I've got a player, right? And he's on a tile (32x32). There are 8 tiles around him. I want to find the tile that he's aiming at--that is, he's aiming at the mouse, and I want to find which of the 8 tiles he is aiming at.
How do I do this?[/QUOTE]
You'd want to find the difference between the point location of your player (in pixels, as rendered on the screen) and the point location of the cursor. (basically mouselocation - playerlocation) That will give you an x- and a y-value. Do some trigonmetry on that to produce an angle, I'm not entirely certain but I am pretty sure that the atan2 function is commonly used for this purpose. From there, you can just check which range the angle falls into, and generate the tile the player is aiming at.
Using this approach you might have issues with aiming at corner tiles, so you'd need to make the range for the angle wider in those cases. When I say the 'range for the angle', you'd use some sort of code like
[code]
if angle is between 0 and 25:
top tile is aimed at
else if angle is between 25 and 50:
top right tile is aimed at
etc
[/code]
This is probably how I would go about it off the top of my head.
[editline]25th October 2011[/editline]
If you didn't lock the option to the 8 adjacent tiles, you could just convert the location of the mouse back into a tile using the reverse of the approach you use to convert the tile the player's in to a location on the screen.
What do you use in java to lets say check if an input contains something? In my case it's :
if input contains . (dot) then it says something. How do I do that?
Say I have a list of numbers and I want to list them in random order.
Listing them in random order is not the problem, the problem is making sure the list isn't containing the chosen number before adding it.
Can someone help me with that problem?
[editline]25th October 2011[/editline]
I suppose you could say I want to shuffle the contents of an array.
Anyone any good with arrays and java? Trying to make Blackjack (or Pontoon or 21 depending where you live) and I can't work out how to solve this:
[IMG]http://i53.tinypic.com/15yamup.png[/IMG]
Here's the relevant code:
[CODE] //Prepare variables and arrays
int[][] deck;
int cardCount, suitCount;
String suitName = "";
//Start Game, Sort cards into deck
suitCount = 1;
cardCount = 1;
deck = new int [4] [13];
while (suitCount <= 4) {
while (cardCount <= 13) {
deck [suitCount][cardCount] = cardCount;
cardCount ++;
}
suitCount ++;
} [/CODE]
Its my first project in Java really apart from some messing around, and my first time using arrays in Java
[QUOTE=NotoriousSpy;32952302]Could someone explain why I would create an Entity base class that all my entities would derive from. Wouldn't this limit the inheritance of the classes that derive from the base class?
I can understand why it would be a good idea, but what about inheritance for the other classes that may need their own child classes? (I don't think multiple inheritance exists in C#)[/QUOTE]
Not entirely sure what you are trying to ask here, but you can have multiple inheritance in C#. They're called interfaces.
What is the best way to display an ASCII[U] file[/U] in a console screen?
Currently I get a space between every line (like there is a \n in the loop or something.
This is my code: (C)
[CODE]
#include <stdio.h>
#include <stdlib.h>
/*****************************************************************************/
int main (void)
{
int ch ;
char String [255];
char file[100];
FILE *BEW1;
while (2>1)
{
printf ("Opening what?\n\n");
scanf ("%s",&file);
BEW1 = fopen (file,"r");
if(BEW1 == NULL)
{
printf ("Cannot open file\n");
break;
}
else
printf ("Open\n");
{
while ( (ch = getc(BEW1)) != EOF )
{
fgets (String , 255 , BEW1) ;
puts (String);
if (String == EOF) break;
}
}
}
}[/CODE]
[QUOTE=fylth;32956524]Anyone any good with arrays and java? Trying to make Blackjack (or Pontoon or 21 depending where you live) and I can't work out how to solve this:
[IMG]http://i53.tinypic.com/15yamup.png[/IMG]
Here's the relevant code:
[CODE] //Prepare variables and arrays
int[][] deck;
int cardCount, suitCount;
String suitName = "";
//Start Game, Sort cards into deck
suitCount = 1;
cardCount = 1;
deck = new int [4] [13];
while (suitCount <= 4) {
while (cardCount <= 13) {
deck [suitCount][cardCount] = cardCount;
cardCount ++;
}
suitCount ++;
} [/CODE]
Its my first project in Java really apart from some messing around, and my first time using arrays in Java[/QUOTE]
In an n-length array, the indices would be 0 to (n-1), so your loops should be 0-3 and 0-12, instead of 1-4 and 1-13
I think that worked, however now i'm getting this:
[IMG]http://i54.tinypic.com/5pwk3.png[/IMG]
(ignore the "Card one" bit, now it just says "The card"
What im trying to do is output each card from the array so I can tell if it's working
Again, relevant code (line 29 - 50):
[CODE] //--Display cards test--
int deckCount = 1;
suitCount = 1;
cardCount = 1;
while (deckCount <= 52) {
switch (suitCount) {
case 1: suitName = "Hearts"; break;
case 2: suitName = "Diamonds"; break;
case 3: suitName = "Clubs"; break;
case 4: suitName = "Spades"; break;
}
System.out.println("Card is the " + deck[cardCount] + " of " + suitName);
deckCount ++;
cardCount ++;
if (cardCount == 1) {
cardCount = 1;
suitCount ++;
}
}[/CODE]
NEVERMIND - solved that problem, got a new one now but it looks more solvable at least
[C++]so i made a program that uses an iteration to let me input 5 numbers.
i have a problem though, im supposed to make it tell me all the 5 numbers at the end of the program.
I cant cout it, since all of the numbers are "j". help?
How would I go about drawing 2D perlin noise in OpenGL?
[QUOTE=farmatyr;32959637]How would I go about drawing 2D perlin noise in OpenGL?[/QUOTE]
Draw to a texture and render it on a quad?
[QUOTE=esalaka;32959842]Draw to a texture and render it on a quad?[/QUOTE]
Thank you, so much to learn! Will post results later.
[QUOTE=Clio;32959601][C++]so i made a program that uses an iteration to let me input 5 numbers.
i have a problem though, im supposed to make it tell me all the 5 numbers at the end of the program.
I cant cout it, since all of the numbers are "j". help?[/QUOTE]
The numbers are "j"?
..
Can you post your code? (Use [cpp]-tags)
can i post it tomorrow? its really late and im really tired and i probably wont understand anything you tell me.
besides its on the laptop and my brother took it :S
also j is an int. thats pretty much all i remember from the program right now :S
Sure. I and other people will still be around, provided this forum doesn't get closed down til then (which it probably won't).
Sounds like you tried to output "j" instead of just j (without quotes) btw.
Yet another problem with this game, I honestly can't work out how or why im getting this problem.
[CODE] int count = 1;
while (count <= 3) {
Random r = new Random();
int cardRand = r.nextInt(12) + 1;
int suitRand = r.nextInt(3) + 1;
switch (suitRand) {
case 1: suitName = "Hearts"; break;
case 2: suitName = "Diamonds"; break;
case 3: suitName = "Clubs"; break;
case 4: suitName = "Spades"; break;
}
switch (cardRand) {
case 1: computerCards = computerCards + " Ace of " + suitName; break;
case 11: computerCards = computerCards + " Jack of " + suitName; break;
case 12: computerCards = computerCards + " Queen of " + suitName; break;
case 13: computerCards = computerCards + " King of " + suitName; break;
default : computerCards = computerCards + Integer.toString(deck[suitRand][cardRand]) + " of " + suitName; break;
}
System.out.print(computerCards);
count ++;
} [/CODE]
This is the code which picks 3 cards from the deck of cards (it doesn't remove them yet from the deck)and then should display them, for some reason the output looks like:
2 of Clubs2 of Clubs Jack of Clubs2 of Clubs Jack of Club9 of Hearts
Quick maths question, how would I know if 2 entities are going to collide at the bottom or the top if I know their Y velocities and positions?
[QUOTE=ZeekyHBomb;32961312]Sure. I and other people will still be around, provided this forum doesn't get closed down til then (which it probably won't).
Sounds like you tried to output "j" instead of just j (without quotes) btw.[/QUOTE]
I think his problem is probably something like this:
[code]j = 0;
for (x = 0; x < 5; x++)
{
j = input();
}
print(j);
[/code]
and he wants to know how he can output all of the inputted numbers.
He could either use an array or one of the stl containers.
[QUOTE=fylth;32964654]Yet another problem with this game, I honestly can't work out how or why im getting this problem.
[CODE]
int count = 1;
while (count <= 3) {
Random r = new Random();
int cardRand = r.nextInt(12) + 1;
int suitRand = r.nextInt(3) + 1;
switch (suitRand) {
case 1: suitName = "Hearts"; break;
case 2: suitName = "Diamonds"; break;
case 3: suitName = "Clubs"; break;
case 4: suitName = "Spades"; break;
}
switch (cardRand) {
case 1: computerCards = computerCards + " Ace of " + suitName; break;
case 11: computerCards = computerCards + " Jack of " + suitName; break;
case 12: computerCards = computerCards + " Queen of " + suitName; break;
case 13: computerCards = computerCards + " King of " + suitName; break;
default : computerCards = computerCards + Integer.toString(deck[suitRand][cardRand]) + " of " + suitName; break;
}
System.out.print(computerCards);
count ++;
}
[/CODE]
This is the code which picks 3 cards from the deck of cards (it doesn't remove them yet from the deck)and then should display them, for some reason the output looks like:
2 of Clubs2 of Clubs Jack of Clubs2 of Clubs Jack of Club9 of Hearts[/QUOTE]
You're using "System.out.print". I'd suggest System.out.println which will print each thing on a new line instead so it will become more clear.
The actual issue with your code, however, is that you're not resetting the value of computerCards each time to the empty string (so = ""). Because you aren't doing this, each time the while block operates it simply adds more stuff to the end of your string. So first you'll have "Apple" then "AppleOrange" then "AppleOrangePotato" instead of "Apple", "Orange", "Potato". If you added
[code]
computerCards = "";
[/code]
after the "count ++;" line I think this problem should be resolved.
[QUOTE=fylth;32964654]Yet another problem with this game, I honestly can't work out how or why im getting this problem.
This is the code which picks 3 cards from the deck of cards (it doesn't remove them yet from the deck)and then should display them, for some reason the output looks like:
2 of Clubs2 of Clubs Jack of Clubs2 of Clubs Jack of Club9 of Hearts[/QUOTE]
To add to what mechanarchy said, you'll want to create that Random outside the while loop. It may be different in java but Random probably gets seeded by the current time, which will likely be the same every iteration.
[editline]26th October 2011[/editline]
[QUOTE=Within;32956342]Say I have a list of numbers and I want to list them in random order.
Listing them in random order is not the problem, the problem is making sure the list isn't containing the chosen number before adding it.
Can someone help me with that problem?
[editline]25th October 2011[/editline]
I suppose you could say I want to shuffle the contents of an array.[/QUOTE]
Here's a generic shuffle method for C#
[code]
public static void Shuffle<T>(IList<T> list)
{
if (list.Count > 1)
{
for (int i = list.Count - 1; i >= 0; i--)
{
T tmp = list[i];
int randomIndex = rand.Next(i + 1);
list[i] = list[randomIndex];
list[randomIndex] = tmp;
}
}
}
[/code]
C++
I'm trying to read an entire file into a "char" dynamic array using ifstream. It seemed to be working fine, for a while. Now it's reading in 90% of this file as 0s:
Outputting the buffer in hex (only the first part of the file), and actual file contents:
[img_thumb]http://dl.dropbox.com/u/11093974/Junk/file.png[/img_thumb][img_thumb]http://dl.dropbox.com/u/11093974/Junk/file2.png[/img_thumb]
The code:
[cpp]std::ifstream file;
file.open(fileName.c_str());
if (file.is_open())
{
// get the size of the file
long begin, end;
begin = (long)file.tellg();
file.seekg (0, std::ios::end);
end = (long)file.tellg();
file.seekg(0, std::ios::beg);
unsigned int size = (unsigned int)(end - begin);
// create a buffer to hold the data
char* buff = new char[size];
// read from the file
file.read(buff, size);
// output the buffer (for testing)
for (unsigned int i = 0; i < size; i++)
{
cout << "[" << setw(3) << hex << (int)(unsigned char)buff[i] << "]";
Sleep(10);
}
}[/cpp]
I have also confirmed that it is getting the size of the file correctly. So I think there's some problem with file.read(buff, size). I've never used ifstream before to read an entire file but I'm hoping for something cross-platform. Anyone see what I'm doing wrong?
Is there another cross-platform (standard) way to read in an entire file into a buffer?
[del]edit: I must be doing something else wrong. [url=http://www.cplusplus.com/reference/clibrary/cstdio/fread/]fread[/url] is doing it too.[/del]
edit2: Nevermind, no it's not. fread works!
Check if any state-flag was set on file after the read-operation.
You should also set the binary-flag when opening the file if you don't want it to convert the new-line characters from anything platform specific to '\n'.
[QUOTE=ZeekyHBomb;32970312]You should also set the binary-flag when opening the file...[/QUOTE]
Ohh! Duh. That fixed it.
Regarding my edits: fread works too, but I'd have to go in and change everywhere I'm reading in files. Adding the binary flag is much easier. Thanks. :)
So I need a list (or similar data structure) that always holds the current value for a given variable, once it has been added. This is what currently occurs (in simpler/pseudo code):
[CODE]intValue = 5;
intList.Add(intValue);
Print intList[0].toString();[/CODE]
Prints "5"
[CODE]intValue++;
Print intList[0].toString();[/CODE]
Still Prints "5" when I want it to print intValue's new value, "6".
Basically the list needs to store a reference to intValue (I think that's the correct terminology) and not it's actual value. Thanks for your time.
You need to store references in there.
If you are using a managed language, use a boxed type.
Otherwise you need to store pointers in that list.
Lets say I have a pointer declared like this :
[code]mapData = new int[RoomWith * RoomHeight];[/code]
And I want to get lets say the first RoomWidth amount of ints from the pointer as characters, what would be the neatest / quickest way to do so ?
So something like char* row = GetRange(data, from , to);
Which in this case would be char * row = GetRange(mapData, 0, RoomWidth);
Edit :
Trying some things by myself :
[code]
std::string rowString;
for(int j = 0; j < RoomWidth; j++)
{
char smallBuffer [10];
sprintf(smallBuffer, "%i", mapData[i * RoomWidth + j]);
rowString.append(smallBuffer);
}
[/code]
I've been having a lot of trouble with this assignment which I have to finish until Friday:
[quote]There are from 1 to x offices in the building, a tablet is nailed on each office door with it's number, each number must have a separate tablet. So, if there are 10 offices in the building, then 11 tablets will be required, if 100 then 192. Write a program which calculates how many tablets will be required for x offices.[/quote]
Anyone got any suggestions?
Sorry, you need to Log In to post a reply to this thread.