Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 9th, 2008, 7:29 PM   #11
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,722
Rep Power: 5 Sane is on a distinguished road
Re: intermediate programming ideas

He would be spending the majority of his time reading and researching mathematical algorithms, such as Newton's method of approximation. There's not very much value there as a programming excercise. The making-your-own-language part is too advanced for most people here, forget an intermediate programmer.
Sane is offline   Reply With Quote
Old Feb 10th, 2008, 12:59 AM   #12
slicksk8te
Newbie
 
Join Date: Feb 2008
Posts: 13
Rep Power: 0 slicksk8te is on a distinguished road
Re: intermediate programming ideas

it seems that that could be a nice goal because I just recently perfected a line drawing alogorithim in one of my programs, but I think that I will probrably try to do the tic tac toe game first. I am thinking about tetris as well but I am not sure how to implement the basic gameplay of the game.
slicksk8te is offline   Reply With Quote
Old Feb 12th, 2008, 5:55 AM   #13
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 864
Rep Power: 3 lectricpharaoh is on a distinguished road
Re: intermediate programming ideas

Quote:
Originally Posted by slicksk8te
I am thinking about tetris as well but I am not sure how to implement the basic gameplay of the game.
Tetris gameplay is actually quite simple. If you're familiar with basic OOP techniques, it's really quite easy, depending on your approach. I'll describe how I did it.

The heart of the game was a 'block' class, an instance of which described a single block (ie a piece within a 4x4 grid, not a single square). The data members described which piece it was (there being seven in standard Tetris, with each being represented with some data structure, like a bitmap or 4x4 array to hold the 'shape'), what the rotation was (0-3, corresponding to rotations of 0, 90, 180, and 270 degrees), and the location within the larger play grid. This larger grid is a simple 2D array representing the squares of the play field, and what they are filled with (if anything).

Of course, you also need methods to do things, like rotate and move the blocks, as well as check for collisions. When a block attempts to move, I created a new block using a private constructor that allowed me to control all aspects of its creation (ie, position, rotation, and shape, rather than just shape, with a default 'top of the play field' positioning). The new block is created with the positioning and rotation that the existing block is trying to achieve. Thus, if I'm trying to move a block left, I create a new block that's identical to the existing one, except one square to the left. I then call the collision-detection method, which 'superimposes' the block over the play field, and detects if there is a collision. This boils down to a range check for the side and bottom edges of the field, and an iterative check for each square occupied by the piece. If a collision results from a sideways move or a rotation, the operation is prevented. If it results from a downwards move, the piece 'lands', which sets the appropriate squares of the play field, and also clears the 'moving' flag for the piece. In the main game loop, pieces are spawned, and live in an inner loop that handles user input. It looks like this in pseudocode:
while(!game_over)
{
  Block piece;
  if(piece.collide())
    game_over = true;  // if a newly-created piece collides, the game is over
  while(piece.isMoving() && !game_over)
    handle_user_input_and_update_pieces();
}
When a piece lands, you check to see if any lines were completed (you only need to check the ones that the piece could possibly complete, meaning four lines at most, depending on the piece and its rotation). If any were completed, you remove them and update the game field, along with any secondary effects (scoring, playing sounds, etc).

You can handle the drawing of the pieces in numerous ways. One way is for the block class to be entirely responsible; since it knows about the game grid and such, this makes sense. Of course, a more flexible way is to let the block class do it indirectly through a function pointer. This would be a static member of the block class, and you fill it in upon initializing the game. The benefit of this approach is that it lets you customize the drawing with ease. Parameters passed to this function would include things such as a single square's position (relative to the game grid), and the 'fill type' of the square (empty, solid, what color, etc). You could do all your updating with this function, but as it might flicker for larger updates (such as when lines were completed and removed, and the game field adjusted by the lines above 'falling down'), you could write optimized routines for those situations as well. They'd probably equate to a blit from one area of the game field to another.

There were also some members to control the speed of various things, like how fast the piece could fall, rotate, or move; these were used in conjunction with an incrementing timer to limit the speeds involved. For example, if you want to limit rotation to once every x 'ticks' of the timer, you set the rotation ticker to x plus the current time 'ticks' value inside the rotation methods upon a successful rotate. You also check to see if the current 'ticks' value is less than the rotation ticker before allowing a rotation.

Anyways, hopefully this has been enough to give you some ideas, if you're interested in writing a Tetris clone. Note that this isn't the only, or even necessarily the best, way to do it. It's just how I happened to do it, and I figured I'd share.
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp
lectricpharaoh is offline   Reply With Quote
Old Feb 12th, 2008, 2:23 PM   #14
slicksk8te
Newbie
 
Join Date: Feb 2008
Posts: 13
Rep Power: 0 slicksk8te is on a distinguished road
Re: intermediate programming ideas

Thanks for the help. This is a good next step for puting my new shape drawing functions to use. This is very helpful and I hope to get started on it within the next few days.
slicksk8te is offline   Reply With Quote
Old Feb 14th, 2008, 12:48 PM   #15
hunter123
Programming God
 
hunter123's Avatar
 
Join Date: Feb 2008
Location: Indiana
Posts: 4
Rep Power: 0 hunter123 is on a distinguished road
Re: intermediate programming ideas

read
hunter123 is offline   Reply With Quote
Old Feb 14th, 2008, 12:49 PM   #16
hunter123
Programming God
 
hunter123's Avatar
 
Join Date: Feb 2008
Location: Indiana
Posts: 4
Rep Power: 0 hunter123 is on a distinguished road
Re: intermediate programming ideas

read read and read
hunter123 is offline   Reply With Quote
Old Feb 14th, 2008, 11:11 PM   #17
slicksk8te
Newbie
 
Join Date: Feb 2008
Posts: 13
Rep Power: 0 slicksk8te is on a distinguished road
Re: intermediate programming ideas

ok that is not helpful at all.
slicksk8te is offline   Reply With Quote
Old Feb 15th, 2008, 8:07 AM   #18
hunter123
Programming God
 
hunter123's Avatar
 
Join Date: Feb 2008
Location: Indiana
Posts: 4
Rep Power: 0 hunter123 is on a distinguished road
Re: intermediate programming ideas

im sorrry wrong thread to comment to.
hunter123 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming Forums - New Management big_k105 Community Announcements and Feedback 14 Oct 13th, 2007 11:02 PM
Ideas for software/Systems Programming bigguy Coder's Corner Lounge 1 May 28th, 2006 6:17 PM
Does Programming Make You Smarter? Sane Coder's Corner Lounge 43 Oct 2nd, 2005 6:12 AM
MIT's Metaphor For Software Programming Infinite Recursion Other Programming Languages 2 Jun 12th, 2005 6:42 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:35 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC