Wednesday, April 25, 2012

How to use matrix to make an 8bits game map

      My friend, who is learning programming now asked me how to use C++ to create a video game; well a video game need videos(just kidding), I don't know about how to make a huge game like StarCraft, or Call of Duty or Halo, but I can make a 8bits video game by some idea from my mind.
     8bits games are most from old computers or SFC, those video games are very basic programs, and  the very important point in those games is MAP.
    How to make a 8bits map is a very important part of the video games, you can make a Lunatic map, or a very basic map, but the whole game mainly effected by the map; to make a map, what we should use is Matrix, such as:
int Map[20][560]
But how is that possible to make a map for a video game?
we can see this ex. :
int Map [][5]
{{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,5,5,0,0,0,0,0,0,1,0,0,2,2,0,0,0,0,3,3,0,3,3,0,0,0,0,0},
  {0,0,0,0,0,0,0,0,1,1,0,0,2,2,0,0,0,0,3,6,3,0,0,18,0,0,0,0},
  {0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,18,0,0,0,0},
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}}
See, this looks kinda like a map, as the result, we can use pics to change those number, we can use lots of 8*8 pics to cover those numbers up, and than a 8 bits map done.

Sunday, April 22, 2012

Using C++ to make a Pascal Triangle

/*My friend asked me how to make a Pascal triangle in C++, I helped him but got a little debug problem,  but I fix it finally.*/
#include "stdafx.h"
#include <iostream>
using namespace std;
int xnumber(int input)
{
 int a=1;
 while( input>=2 )/*The number can't be 0, I dont know why, but I this is the number that fix the whole program*/
 {
  a = input * a ;
  //cout<<a<<" ";
  input--;
 }
 return a;
}
 int C(int n,int r)
 {
  int a = n-r;
  a = xnumber(n)/(xnumber(r) * xnumber(a));
  return a;
 }
 int _tmain(int argc, _TCHAR* argv[])
{
 int a,line=1,lineneeded;
 cout<<"Plz write down how many lines U need"<<endl;
 cin>>lineneeded;
 cout<<"1 "<<endl ;
while(line <= lineneeded)
 {
  a=1;
  cout<<"1 ";
  //cout<<line;
  while(a <= line)
  {
   cout<<C(line,a)<<" ";
   a++;
  }
  cout<<endl;
  line++;
 }
 cout<<endl;
 system("pause");
 return 0;
}
=====================================END===============================
The out put of this program is going to be a pascal triangle, but the input can't higher than 12.
out put:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
       .
       .
       .

Friday, April 20, 2012

Efficient Sorting Methods


The Bubble Sort
Implementation: Beginner
DescriptionThe bubble sort is one of the simplest methods you can use. This method works well for simple data structures or if the data set to be sorted is more or less sorted. The bubble sort is very inefficient for a general data set. In the bubble sort algorithm, successive sweeps are made through the records to be sorted. During each sweep, the algorithm compares the key to the elements and swaps the elements if they are not in the desired order. Swapping occurs only among consecutive elements of the data structure. As a result, only one element is placed in its sorted place after each sweep. The sorted elements are not needed for comparison in successive sweeps.
PseudocodeFor iteration = 0 to (n-1)
Begin
For I = 0 to (n-1-iteration)
Begin
If array[I] > array[I+1] then
Swap array[I] and array[I+1]
End
End
Codevoid CSort::BubbleSort( void )
{
for ( int iteration = 0 ; iteration < Count - 1 ; iteration++ ) {
for ( int i = 0 ; i < Count - 1 - iteration ; i++ ) {
if ( Array[ i ] > Array[ i + 1 ] )
Swap( i, i+1 ) ;
}
}
}
Analysis
  • After each iteration, only one data element is placed in the proper-sorted position.
  • Depends on the comparing and swapping of consecutive data elements. “Bubbling” the biggest element to the end.
  • Simple implementation.
  • The best-case scenario is when the data elements are almost sorted in the correct order.
  • The worst-case scenario is when the data elements are in reverse order.

The Selection Sort
Implementation: Beginner
DescriptionThe selection sort algorithm is based on using the data elements as keys for comparison such that, at the end of each scan, only one data element is placed in the desired sorted position. This algorithm is simple but very inefficient because it does not consider partial or fully sorted lists. In other words, if you have a partial or fully pre-sorted list, the selection sort does the same number of comparisons as it would on a completely random list and does not use any intelligence (unlike the bubble sort) to improve the performance. As a result the, the selection sort does not really lend itself to a best-case scenario.
PseudocodeFor iteration = 0 to (n-1)
Begin
Lowest = iteration
For I = iteration+1 to (n-1)
Begin
If ( array[Lowest] > array[I] )
Lowest = I
End
Swap array[iteration] and array[lowest]
End
Codevoid CSort::SelectionSort( void )
{
for ( int iteration = 0 ; iteration < Count - 1 ; iteration++ ) {
int lowest = iteration ;
for ( int i = iteration + 1 ; i < Count ; i++ ) {
if ( Array[ i ] < Array[ lowest ] )
lowest = i ;
}
Swap( iteration, lowest ) ;
}
}
Analysis
  • After each iteration, only one data element is placed in the proper-sorted position.
  • The selection sort only swaps once for every iteration.
  • The selection sort has a simple implementation.
  • The best-case scenario and the worst-case scenario is the same because the selection sort does not consider any partial sorting that might exist in the input.

The Quick Sort
Implementation: Expert
DescriptionThe quick sort is the most efficient internal sort algorithm. Its preformance is largely influence by the choice of the pivot. The quick sort makes use of three strategies:
  1. Split the array into smaller sub arrays
  2. Sort the sub arrays
  3. Merge the sorted sub arrays
A quick sort can be implemented in several ways, but the goal of each approach is to select a data element and place it in its proper position (which is referred to as the pivot) so that all the elemets on the left sided of the pivot are less than (or come before) the pivot and all the elements on the right side of the pivot are greater than (or come after) the pivot. The choice of the pivot and the method used to split the array has a big influence on the overall preformance.
Pseudocode
  1. Select a data element and position it as a pivot so that it divides the array into a left sub array and a right sub array
  2. Apply a quick sort to the left sub array
  3. Apply a quick sort to the right sub array
Codevoid CSort::QuickSort( int first, int last )
{
int Lo, Hi, Mid, T;
Lo = first;
Hi = last;
Mid = Array[(Lo+Hi)/2];
do
{
while (Array[Lo] < Mid)
Lo++;
while (Array[Hi] > Mid)
Hi–;
if (Lo <= Hi)
{
Swap(Lo, Hi);
Lo++;
Hi--;
}
}
while (Lo <= Hi);
if (Hi > first)
QuickSort(first, Hi);
if (Lo < last)
QuickSort(Lo, last);
}
Analysis
  • The split phase is very complex and the merge is simple
  • In the best case, the work done is on the order of nlog2n
  • In the worst case, the work is equal to the selectio sort, resulting in O(n2)
  • The choice of the pivot is very important for the performance of the quick sort.
  • The worst-case scenario is when the data elements are in reverse order.

Software comparison:
Bubble SortSelection SortQuick SortItems
39453029633410
981655960955920
1493669282995530
2046331441351268140

Summary
The key point to remember is that the efficiency of any sorting method strongly depends on the implementation of the method and the actual data the method is sorting. The best way to choose the right sorting method for you needs is to test each method again various types of inputs. In particular, against a sample of the kind of data you intend to sort.
Downloads
Download the following code to test the sorting methods:

Wednesday, April 18, 2012

Introduction

       C++ is a computer programming language; the father of C++ called C, C is a a general-purpose compute programming language, It created in between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use of Unix. 
       Back to C++, C had been enhanced in 1983 by Bjarne Stroustrup, it be came C Classes(now called C++), which we known as C++,the different between C and C++ is, C++ adds a new tool, which called Classes;
       this is a web that I love,
www.cplusplus.com
I usually use this web for library search.