• What Do You Need Help With? V6
    7,544 replies, posted
If Object Orientation is where you're having problems then you might want to refer to some basic Java tutorials. It's such a good language for learning concepts. [editline]17th May 2014[/editline] [url=http://docs.oracle.com/javase/tutorial/java/concepts/]Here's a good place to start that doesn't require much if any Java knowledge.[/url]
When creating a java command line application, what is the best way to program it so the user can select what they would like to do? as in: Select a number to choose what you would like to do: 1. add user 2. remove user 3. back to menu ect, I know it would be done by creating multiple methods, but I'm not sure how to put it all together and to make it so you can go back?
[QUOTE=Shadow187(FP);44834818]If Object Orientation is where you're having problems then you might want to refer to some basic Java tutorials. It's such a good language for learning concepts. [editline]17th May 2014[/editline] [url=http://docs.oracle.com/javase/tutorial/java/concepts/]Here's a good place to start that doesn't require much if any Java knowledge.[/url][/QUOTE] Could I follow it if I use C#?
[QUOTE=JohanGS;44835003]Could I follow it if I use C#?[/QUOTE] I don't know much about C#. I know it's similar to C++ though, so you should be able to. It's all conceptual, so as long as you can ready basic code you should be fine. [editline]17th May 2014[/editline] [QUOTE=smidge146;44834910]When creating a java command line application, what is the best way to program it so the user can select what they would like to do? as in: Select a number to choose what you would like to do: 1. add user 2. remove user 3. back to menu ect, I know it would be done by creating multiple methods, but I'm not sure how to put it all together and to make it so you can go back?[/QUOTE] Create a Menu class. Each menu Class has some fields. One of them is "parent_menu", which you can assign to another menu class that is its parent. An array of options. [I]String[] menuOptions = new String[] {"1. Add User\n", "2. Remove User\n", "3. Back to " + this.parent_menu.getMenuTitle() + "."};[/I] See how useful it could be?
[QUOTE=Shadow187(FP);44835043]I don't know much about C#. I know it's similar to C++ though, so you should be able to. It's all conceptual, so as long as you can ready basic code you should be fine. [editline]17th May 2014[/editline] Could you give me a rough example? Create a Menu class. Each menu Class has some fields. One of them is "parent_menu", which you can assign to another menu class that is its parent. An array of options. [I]String[] menuOptions = new String[] {"1. Add User\n", "2. Remove User\n", "3. Back to " + this.parent_menu.getMenuTitle() + "."};[/I] See how useful it could be?[/QUOTE]
Sorry about the weird formatting. Something strange is going on with my ability to post. This is untested code but it looks fine. You can add or remove as much or as little extra methods you deem necessary. Usage is as follows. [code] public static void main(String[] args) { Menu mainMenu = new Menu("Main Menu", "Add User", "Remove User", "Exit Program"); mainMenu.displayOptions(); }[/code] Output [code]---Main Menu--- 1. Add User 2. Remove User 3. Exit Program [/code] [I]Menu[/I] class. [code]package menuWork; import java.util.ArrayList; import java.util.List; public class Menu { String menuTitle; List<String> menuOptions = new ArrayList<String>(); Menu parentMenu; public Menu(String menuTitle, Menu parentMenu, String... options) { this.menuTitle = menuTitle; this.parentMenu = parentMenu; int count = 1; for (String s : options) { String option = s.trim(); option = count + ". " + option + "\n"; this.menuOptions.add(option); count++; } } public Menu(String menuTitle, String... options) { this.menuTitle = menuTitle; int count = 1; for (String s : options) { String option = s.trim(); option = count + ". " + option + "\n"; this.menuOptions.add(option); count++; } } public void displayOptions() { System.out.println("---" + this.getTitle() + "---\n"); for (String s : this.menuOptions) { System.out.println(s); } } public Menu getParentMenu() { return this.parentMenu; } public String getTitle() { return this.menuTitle; } public void setTitle(String s) { this.menuTitle = s; } public void setParentMenu(Menu m) { this.parentMenu = m; } public void addOption(String s) { this.menuOptions.add(s); } public void removeOption(String s) { if (this.menuOptions.contains(s)) { this.menuOptions.remove(s); } } public void removeOption(int i) { this.menuOptions.remove(i + 1); } } [/code]
[t]http://puu.sh/8Qh4t.png[/t] So I was playing this game and I began to wonder how this game makes random territories that look like country boarders. What is the best way to generate territories like this from a grid of hexagonal tiles? I tried googling it but I couldn't find the answer.
[QUOTE=Alternative Account;44829581]Could you tell us the programming language you're using, and show us the code responsible for handling input?[/QUOTE] The code is in java and I had to rewrite it because I didn't know what the hell I Was doing [code] private static int sweg = Integer.MAX_VALUE; public static int [] swag(Graph g, int k) { final int [] future = new int[g.size()]; final int[]previous = new int[g.size()]; final boolean[] kay = new boolean[g.size()]; for(int i = 0; i <future.length;i++) { future[i] = sweg; } future[k] = 0; for (int i =0; i< future.length; i++) { final int n = shortestPath(future, kay); kay[n] = true; final int [] p = g.adjacentSides(n); for(int j = 0; j < p.length; j++) { final int c = p[j]; final int d = future[n] + g.getWeight(c, c); if(future[c] > d) { future[c] = d; previous[c] = c; } }[/code] Here is part of i, The way I'm implenting is for scanner is like main class Scanner n = new System.in etc. Any Suggestions?
So my vertices and texture coords are correct but this texture just renders black. Any idea why? [code] using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using OpenTK.Graphics.OpenGL4; using Shared; using GL4 = OpenTK.Graphics.OpenGL4.GL; using PixelFormat = OpenTK.Graphics.OpenGL4.PixelFormat; namespace Client.Render { class CharRenderer { public Font font; private int glTexture; private readonly Vec2 windowSize; public CharRenderer(Vec2 _windowSize) { windowSize = _windowSize; } public void LoadFont(string fileName) { font = new Font(fileName); font.Load(fileName); } public void DrawChar(char c, float x, float y) { CharInfo charD = font.GetChar(c); //TODO: make a Vec2f class. /* float[] vertices = { x/windowSize.x, y/windowSize.y, //Position (float)charD.Position.x/font.FontAtlas.Width, (float)charD.Position.y/font.FontAtlas.Height, //Tex Coords x/windowSize.x + (float)charD.Size.x/windowSize.x, y/windowSize.y, (float)charD.Position.x/font.FontAtlas.Width + (float)charD.Size.x/font.FontAtlas.Width, (float)charD.Position.y/font.FontAtlas.Height, x/windowSize.x + (float)charD.Size.x/windowSize.x, y/windowSize.y + (float)charD.Size.y/windowSize.y, (float)charD.Position.x/font.FontAtlas.Width + (float)charD.Size.x/font.FontAtlas.Width, (float)charD.Position.y/font.FontAtlas.Height + (float)charD.Size.y/font.FontAtlas.Height, x/windowSize.x, y/windowSize.y + (float)charD.Size.y/windowSize.y, (float)charD.Position.x/font.FontAtlas.Width, (float)charD.Position.y/font.FontAtlas.Height + (float)charD.Size.y/font.FontAtlas.Height, };*/ float[] vertices = { //Test by drawing whole atlas. -1,-1,-1,-1,1,-1,1,-1,1,1,1,1,-1,1,-1,1 }; uint[] VBO = new uint[1]; GL4.GenBuffers(1, VBO); GL4.BindBuffer(BufferTarget.ArrayBuffer, VBO[0]); GL4.BufferData(BufferTarget.ArrayBuffer, new IntPtr(vertices.Length * sizeof(float)), vertices, BufferUsageHint.StaticDraw); //Shaders int vertShaderHandle = GL.CreateShader(ShaderType.VertexShader); int fragShaderHandle = GL.CreateShader(ShaderType.FragmentShader); string vertShaderSource = File.ReadAllText("Resources/Shaders/Vert.vert"); string fragShaderSource = File.ReadAllText("Resources/Shaders/Frag.frag"); GL4.ShaderSource(vertShaderHandle, vertShaderSource); GL4.ShaderSource(fragShaderHandle, fragShaderSource); GL4.CompileShader(vertShaderHandle); GL4.CompileShader(fragShaderHandle); int programHandle = GL4.CreateProgram(); GL4.AttachShader(programHandle, vertShaderHandle); GL4.AttachShader(programHandle, fragShaderHandle); string log; GL4.GetProgramInfoLog(programHandle, out log); Console.WriteLine(log); GL4.LinkProgram(programHandle); GL4.UseProgram(programHandle); GL4.EnableVertexAttribArray(0); GL4.BindAttribLocation(programHandle,0,"position"); GL4.VertexAttribPointer(0, 4, VertexAttribPointerType.Float, false, 0, 0); GL4.EnableVertexAttribArray(1); GL4.BindAttribLocation(programHandle,1,"texcoord"); GL4.VertexAttribPointer(1, 4, VertexAttribPointerType.Float, false, sizeof(float)*2, 0); BitmapData bmpData = font.FontAtlas.LockBits(new Rectangle(0, 0, font.FontAtlas.Width, font.FontAtlas.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); glTexture = GL4.GenTexture(); GL4.BindTexture(TextureTarget.Texture2D, glTexture); GL4.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, font.FontAtlas.Width, font.FontAtlas.Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, bmpData.Scan0); font.FontAtlas.UnlockBits(bmpData); GL4.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear); GL4.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Linear); GL4.Uniform1(GL4.GetUniformLocation(programHandle,"tex"),1); } } } [/code] Nevermind got it, the texture uniform was bound to 1 not 0.
[QUOTE=pyschomc;44839219][...] Any Suggestions?[/QUOTE] [code][noparse][code][/code][/noparse][/code] tags
[QUOTE=toaster468;44835337][t]http://puu.sh/8Qh4t.png[/t] So I was playing this game and I began to wonder how this game makes random territories that look like country boarders. What is the best way to generate territories like this from a grid of hexagonal tiles? I tried googling it but I couldn't find the answer.[/QUOTE] There is no answer to your question, because there is no best way to do it. The problem has little significance to anything else except that particular program, and the problem is very generic ("how do I split a grid into random regions"). Due to to this, the solution is uniquely tailored to the program.
[QUOTE=Tamschi;44839377][code][noparse][code][/code][/noparse][/code] tags[/QUOTE] Okay Fixed! I'm having trouble because although I understand the problem, I'm really confused on how to input a Adjacency list (like the one i stated few posts before), and output exactly in the same way.
I have a question, gents. I want to start doing [I]something real[/I] and interesting: next year's Java homework isn't very satisfying. That, and I want to expand my experience with C++. These facts combined with my love for Tower Defense brings me to the conclusion that I should try to make a simple TD game, and then another one to follow that's more complex. But alas I'm left with nowhere to start. I've done some small research and I've mostly decided to start with SFML. Is this a good choice? [editline]asd[/editline] I do have some programming experience - I've been using Java for 2 years and C++ for a few months.
Hey all. So I am torturing myself with OpenGL / Graphics again, because I hate myself that much, and using it to build a simple top-down 2D game. [del]For right now, I need help refactoring some drawing code. Using C#'s "OpenTK" library, I have this method that is called within each individual object's "draw" method: [code] public static void drawRectSimple(float xpos, float ypos, float width, float height, float rot, Color color) { //GL.Rotate(rot, Vector3d.UnitZ); GL.Begin(PrimitiveType.Quads); GL.Color3(color); GL.Vertex2(xpos, ypos); GL.Vertex2(xpos+width, ypos); GL.Vertex2(xpos+width, ypos+height); GL.Vertex2(xpos, ypos+height); GL.End(); }[/code] As the commented-out line suggests, I want to add rotating. However, I know that OpenGL rotation does rotation about the origin, which plays hell with the fact that I am drawing the vertices relative to a non-origin point. I also know the correct way to do this is to use GL.Translate to move the coordinate system, and to draw the points relative to the origin. I attempted this with the following code: [code] public static void drawRectSimple(float xpos, float ypos, float width, float height, float rot, Color color) { //GL.Rotate(rot, Vector3d.UnitZ); GL.Translate(xpos, ypos, 0); GL.Begin(PrimitiveType.Quads); GL.Color3(color); GL.Vertex2(0, 0); GL.Vertex2(width, 0); GL.Vertex2(width, height); GL.Vertex2(0, height); GL.End(); }[/code] But it doesn't work properly. I have two objects being drawn: a rectangle the size of the screen, and a rectangle representing the player. With the first method, it all works fine, but with the second method, only the background draws. Note that I have them in a List and I draw them sequentially, so the player rectangle is drawing after the background. Also note that if I disable the background, so only the player square draws, it draws correctly. I am thinking it may be the case that doing Translate for drawing the background is throwing everything else off, but I am not certain. I would appreciate if someone could provide some assistance here.[/del] [editline]fish[/editline] Apparently I just needed to modify the code slightly: [code] public static void drawRectSimple(float xpos, float ypos, float width, float height, float rot, Color color) { GL.Translate(xpos, ypos, 0); GL.Rotate(rot, Vector3d.UnitZ); GL.Begin(PrimitiveType.Quads); GL.Color3(color); GL.Vertex2(-width/2, -height/2); GL.Vertex2(width/2, -height/2); GL.Vertex2(width/2, height/2); GL.Vertex2(-width/2, height/2); GL.End(); }[/code] And then modify the position of my full-screen rectangle. It now draws correctly. I suppose my next question, then, is how to properly compute the angle between the box's center and the cursor position into an angle theta (in degrees) to rotate about the Z-axis, for getting the box to point toward my cursor. I'm using [code]float rotation = (float)Math.Atan2(mouse.Y - this.pos.Y, mouse.X - this.pos.X) * 360;[/code] With different multipliers for the degrees and variants on the differences, but it doesn't work properly for all four quadrants. I'll take help on this, too. :v:
Mine's probably a simple question. Trying to make a 10x10 grid minesweeper game. No idea how to make the grid. I thought having a list called Row and a List called column would be the way to go. So that I could put those lists into a list called grid, which is essentially, the grid. How do I make it work work though? So that I can eventually be like "Pick 15 random places from grid and place mines". I'm really confused and desperately need help. I'm using scribble (a version of scratch). Any help would be so appreciated.
[QUOTE=Shadow187(FP);44843953]ffp stuff[/quote] Don't use the fixed function pipeline! It's kind of redundant. OpenTK has support for OpenGL4 in the OpenTK.Graphics.OpenGL4 namespace. I suggest you bind it to an alias because a lot of the stuff is named the same in the other namespaces and it is confusing. I called mine GL4. Overv will show you the way. [URL="http://open.gl"]open.gl[/URL] [editline]19th May 2014[/editline] [QUOTE=Just2Rusty;44846253]Mine's probably a simple question. Trying to make a 10x10 grid minesweeper game. No idea how to make the grid. I thought having a list called Row and a List called column would be the way to go. So that I could put those lists into a list called grid, which is essentially, the grid. How do I make it work work though? So that I can eventually be like "Pick 15 random places from grid and place mines". I'm really confused and desperately need help. I'm using scribble (a version of scratch). Any help would be so appreciated.[/QUOTE] Are you using C#? Look in to 2D arrays for storing the states of your mines. [url]http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx[/url] [editline]19th May 2014[/editline] Also, you may look in to this in the future as it's not completely obvious as to how the clearing works. When a mine is cleared, if there are no surrounding mines it clears all surrounding mines and they do the same until no more mines without mines surrounding them.
So I have a class with this method in it: [CODE] public void SetUpGame(Label blueStartingArmy, Label redStartingArmy, bool blueStarts, bool redStarts) { foreach (Label removeLabel in territoryList) { removeLabel.Visible = true; } blueStartingArmy.Text = "32"; redStartingArmy.Text = "32"; startingPlayer = random.Next(1, 3); if (startingPlayer == 1) { blueStarts = true; } else { redStarts = true; } }[/CODE] And I call it like this: [CODE]GameBoard.SetUpGame(reinforcementsBlue, reinforcementsRed, bluePlayersTurn, redPlayersTurn);[/CODE] How come bluePlayersTurn and redPlayersTurn are always false even if blueStarts or redStarts are true? What am I missing here? Also while I am here, how come when I do random.Next(1, 2) it always comes up 1? If I do random.Next(1, 3) then it will only be either 1 or 2. I thought it was random.Next(minimum, maximum) Thanks guys
Are you trying to set bluestarts and redstarts outside the method? Like [code] GameBoard.SetUpGame(reinforcementsBlue, reinforcementsRed, bluePlayersTurn, redPlayersTurn); if(bluePlayersTurn) dostuff [/code] If so that's not how it works, you could either return the value, set some instance variables or use the out keyword.
[QUOTE=reevezy67;44846449]Are you trying to set bluestarts and redstarts outside the method? Like [code] GameBoard.SetUpGame(reinforcementsBlue, reinforcementsRed, bluePlayersTurn, redPlayersTurn); if(bluePlayersTurn) dostuff [/code] If so that's not how it works, you could either return the value, set some instance variables or use the out keyword.[/QUOTE] Yes [CODE] if (bluePlayersTurn) { BluePlayer.PlayersTurn(); RedPlayer.NotPlayersTurn(); } else { RedPlayer.PlayersTurn(); BluePlayer.NotPlayersTurn(); } }[/CODE] But I am not even getting that far. If blueStarts is true then shouldn't bluePlayersTurn be true? In the method I change blueStartingArmy to "32" then reinforcementsBlue changes to "32" I assumed this would work too.
So the parameter is a temporary value, it disappears after the method is finished. If blue player/red player are defined inside the class ,something like this.. [code] class game { bool blueStarts = false; bool redStarts = false; void method(bool bitchin) { bitchin = true; //This will not leave the method. blueStarts = true; //You can access it from any method. } void SomeOtherMethod() { bool b = false; method(b); b == true; //False because it didn't change in method. } } [/code] Sorry I kind of just threw it together but you should get the gist of it.
[QUOTE=reevezy67;44846523] [code] class game { void method(bool bitchin) { bitchin = true; //This will not leave the method. } } [/code] [/QUOTE] blueStartingArmy and redStartingArmy are exactly like this yet they carry over. Are bools just different?
[QUOTE=Just2Rusty;44822118]How would I go about making Minesweeper? Where would I even start? I'm using [URL="http://monofonik.github.io/scribble/"]Scribble[/URL]. Anyone?[/QUOTE] I made Minesweeper in Javascript last year so I can help you with the logic of the game. First you have to generate a field. I recommend using a 2D array (field[][]), if scribble doesn't have 2D arrays but it has lists, that will work fine as well. Initialize all the cells to 0, meaning no adjacent mines. Next we place mines randomly across the field, I did this using a while loop: [code]mines = 10 // Amount of mines to place width = 10; // Amount of cells wide height = width; // Amount of cells high // Fill the field with 0's for (x = 0; x < width; x++) { for (y = 0; y < height; y++) { field[x][y] = 0; // Mines and mine counts revealed[x][y] = false; // Revealed cells } } // While there are mines to place while (mines > 0) { // Select a random x and y coord in the field x = round(random()) * width; y = round(random()) * height; // if it is empty place a mine and update adjacent cells if (field[x][y] >= 0) { // Set to -1, this means mine field[x][y] = -1; // Take away 1 mine from the mines to place --mines; // Update mine count to adjacent cells // Make sure you check it isn't on an edge // Also check if the cell already a mine // Top Left if (x > 0 AND y > 0 AND field[x - 1][y - 1] >= 0) { field[x - 1][y - 1]++; } // Bottom Left if (x > 0 AND y < height - 1 AND field[x - 1][y + 1] >= 0) { field[x - 1][y + 1]++; } // Top Right if (x < width -1 AND y > 0 AND field[x + 1][y - 1] >= 0) { field[x + 1][y - 1]++; } // Bottom Right if (x < width - 1 AND y < height - 1 AND field[x + 1][y + 1] >= 0) { field[x + 1][y + 1]++; } // Top if (y > 0 AND field[x][y - 1] >= 0) { field[x][y - 1]++; } // Bottom if (y < height - 1 AND field[x][y + 1] >= 0) { field[x][y + 1]++; } // Left if (x > 0 AND field[x - 1][y] >= 0) { field[x - 1][y]++; } // Right if (x < width - 1 AND field[x + 1][y] >= 0) { field[x + 1][y]++; } } } [/code] You now have a field with randomly placed mines and cells will have their number of adjacent mines predefined so we can save on computation by not updating it every step. The next part of it is clicking a cell: [code]// Assume x and y are the x and y of the cell clicked var x, y; // First check whether this cell has been revealed or not, don't want to waste time if it already has. if (revealed[x][y] == false) { revealed[x][y] = true; // reveal the cell // if it's a mine, game over if (field[x][y] == -1) { // Reveal all cells for (x = 0; x < width; x++) { for (y = 0; y < height; y++) { revealed[x][y] = true; } } } else { // If it's not a mine we reveal blank cells // Use the function revealCells() to reveal adjacent cells // Top Left if (x > 0 AND y > 0 AND field[x - 1][y - 1] == 0) { revealCells(x - 1, y - 1); } // Bottom Left if (x > 0 AND y < height - 1 AND field[x - 1][y + 1] == 0) { revealCells(x - 1, y + 1); } // Top Right if (x < width -1 AND y > 0 AND field[x + 1][y - 1] == 0) { revealCells(x + 1, y - 1); } // Bottom Right if (x < width - 1 AND y < height - 1 AND field[x + 1][y + 1] == 0) { revealCells(x + 1, y + 1); } // Top if (y > 0 AND field[x][y - 1] == 0) { revealCells(x, y - 1); } // Bottom if (y < height - 1 AND field[x][y + 1] == 0) { revealCells(x, y + 1); } // Left if (x > 0 AND field[x - 1][y] >= 0) { revealCells(x - 1, y); } // Right if (x < width - 1 AND field[x + 1][y] == 0) { revealCells(x + 1, y); } } } [/code] And the code for the revealCells function would look like this: [code] // Top Left if (x > 0 AND y > 0 AND field[x - 1][y - 1] == 0) { revealed[x - 1][y - 1] = true revealCells(x - 1, y - 1); } // Bottom Left if (x > 0 AND y < height - 1 AND field[x - 1][y + 1] == 0) { revealed[x - 1][y + 1] = true revealCells(x - 1, y + 1); } // Top Right if (x < width -1 AND y > 0 AND field[x + 1][y - 1] == 0) { revealed[x + 1][y - 1] = true revealCells(x + 1, y - 1); } // Bottom Right if (x < width - 1 AND y < height - 1 AND field[x + 1][y + 1] == 0) { revealed[x + 1][y + 1] = true revealCells(x + 1, y + 1); } // Top if (y > 0 AND field[x][y - 1] == 0) { revealed[x][y - 1] = true revealCells(x, y - 1); } // Bottom if (y < height - 1 AND field[x][y + 1] == 0) { revealed[x][y + 1] = true revealCells(x, y + 1); } // Left if (x > 0 AND field[x - 1][y] == 0) { revealed[x - 1][y] = true revealCells(x - 1, y); } // Right if (x < width - 1 AND field[x + 1][y] == 0) { revealed[x + 1][y] = true revealCells(x + 1, y); } [/code] It looks like a lot, but minesweeper is really simple and if you read through this you should be able to make something. [editline]19th May 2014[/editline] Here's the source for my minesweeper, it's in javascript so I don't know if you'll be able to read it properly. [url]https://www.dropbox.com/s/b44bpijapavdre6/Minesweeper%20Test.rar[/url] [editline]19th May 2014[/editline] I've had a look at sketch and while it would be possible, I don't recommend it.
I think I understand now. Thank you very much. [CODE] public bool WhoStarts() { int startingPlayer = 0; startingPlayer = random.Next(1, 3); if (startingPlayer == 1) { return true; } else { return false; } }[/CODE] Then changed it to this [CODE] if (GameBoard.WhoStarts()) { BluePlayer.PlayersTurn(); RedPlayer.NotPlayersTurn(); } else { RedPlayer.PlayersTurn(); BluePlayer.NotPlayersTurn(); }[/CODE]
[QUOTE=Pat.Lithium;44846650]Minesweeper[/QUOTE] Tad long winded isn't it? I did an assignment on minesweeper too, not the best thing I've ever written but it's a bit more concise. Disclaimer:I don't like Java. Surrounding mines. [code] public int getSurroundingMines(int row, int col) { int mineCount = 0; int oRow = row - 1; int oCol = col - 1; for(int r = 0; r < 3; r++) { for(int c = 0; c < 3; c++) { int finalRow = oRow + r; int finalCol = oCol + c; if(isValidCell(finalRow,finalCol) && mineField[finalRow][finalCol] == State.MINED) mineCount++; } } //If middle cell is a mine remove one, only want the surrounding. if(mineField[row][col] == State.MINED && mineCount >= 1) mineCount--; return mineCount; } [/code] Clear cells w/adjacent cells. [code] public void clearCell(int row, int col) { if(mineField[row][col] == State.MINED) { System.out.println("You hit a mine! \n"); gameOver(); return; } else mineField[row][col] = State.CLEARED; //If there are no adjacent mines clear the surrounding cells. if(surroundingMines[row][col] == 0) clearSurroundingCells(row,col); } /** Will keep clearing via recursion till a mine is hit. **/ private void clearSurroundingCells(int row, int col){ for(int r = -1; r < 2; r++) { for(int c = -1; c < 2; c++) { int sRow = row + r; int sCol = col + c; if(isValidCell(sRow, sCol) && canClear(sRow, sCol) && (row != 0 && col != 0) ){ clearCell(sRow,sCol); } } } } [/code]
I an issue understanding something in C++ I'm making a program to render a height map using OpenGL I have a struct called Object: [code]struct Object { Mesh mesh; // Polygon definition Mat4 matrix; // World Transformation Matrix unsigned int vbo; // VBO id };[/code] Then I have an object and a list of objects [code]Object terrain; std::list<Object*> objects[/code] in my pre-process I generate a vertices for each pixel and create a VBO for the terrain [code]// From void Core::createTerrain(int xDiv, int zDiv, Object* _terrain, float* heights) glGenBuffers(1, &_terrain->vbo); glBindBuffer(GL_ARRAY_BUFFER, _terrain->vbo); glBufferData(GL_ARRAY_BUFFER, 14 * sizeof(float) * _terrain->mesh.size(), _terrain->mesh.data(), GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0);[/code] After that I add it to the object list with: [code]objects.push_back(&terrain);[/code] Then when I go to render it I go through the list: [code]for (list<Object*>::iterator it = objects.begin(); it != objects.end(); ++it) { //do stuff }[/code] However I don't think I understand the iterator as it.vbo gives the error that it has no member 'vbo'.
Conceptually, iterators are similar to pointers, you need to dereference them. Also, I'd recommend using a std::list<Object>; anything in a std::list is already using a pointer, so adding another layer of indirection is confusing, unnecessary, and slower. Assuming you use a std::list<Object> instead, you can also use the C++11 ranged-for loop: [cpp] for(auto& obj : objects) { //do stuff //In this case, obj's type is Object& } //Basically, it's equivalent to this old-style loop: for(auto it = objects.begin(); it != objects.end(); ++it) { auto& obj = *it; /do stuff } [/cpp]
Thanks, next question OpenGL related, how do I properly use matrix stacks? In my rendering function my tutor has put some comments in the skeleton code: [code] // Push a new matrix to the GL_MODELVIEW stack. // Multiply the matrix by the object's transformation matrix. // Bind the VBO glBindBuffer(GL_ARRAY_BUFFER, ob->vbo); // Pass vertex information // .. // Pass normal information // .. // Pass texture coordinates information // .. // Pass colour information // .. // Draw object // .. // Pop the matrix from the stack. glPopMatrix();[/code] I've added the bind buffer and popMatrix because I thought they go there. [editline]19th May 2014[/editline] do I use [code]glLoadIdentity(); glMatrixMode(GL_MODELVIEW);[/code] then push it into the stack? [editline]19th May 2014[/editline] Do I push lookAt into the stack first then push in my model transformations, then pop the model transformations when they're done?
I don't actually know anything about the (deprecated, I might add) matrix stack, but you can try looking through the [url=https://www.opengl.org/sdk/docs/man2/]reference pages[/url].
What's the best way to learn C++?
[QUOTE=pyschomc;44856858]What's the best way to learn C++?[/QUOTE] Tutorials. It also helps to have an idea of what you want to do, so it might help to just jump right in. However, C++ is a little bit more complex than, say, C#. Unless what you're wanting to do requires C++, I recommend starting off with C#. Never be afraid to use Google, either.
Sorry, you need to Log In to post a reply to this thread.