Shadowrun: Awakened 29 September 2011 - Build 871
Game_of_life.cpp
Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2010 Intel Corporation.  All Rights Reserved.
00003 
00004     This file is part of Threading Building Blocks.
00005 
00006     Threading Building Blocks is free software; you can redistribute it
00007     and/or modify it under the terms of the GNU General Public License
00008     version 2 as published by the Free Software Foundation.
00009 
00010     Threading Building Blocks is distributed in the hope that it will be
00011     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00012     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with Threading Building Blocks; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019     As a special exception, you may use this file as part of a free software
00020     library without restriction.  Specifically, if other files instantiate
00021     templates or use macros or inline functions from this file, or you compile
00022     this file and link it with other files to produce an executable, this
00023     file does not by itself cause the resulting executable to be covered by
00024     the GNU General Public License.  This exception does not however
00025     invalidate any other reasons why the executable file might be covered by
00026     the GNU General Public License.
00027 */
00028 
00029 /* 
00030     Game_of_life.cpp : 
00031                       main project file.
00032 */
00033 #include "Board.h"
00034 #include "Form1.h"
00035 
00036 #define WIN32_LEAN_AND_MEAN
00037 
00038 #ifndef _CONSOLE
00039 #include <windows.h>
00040 #else
00041 #include <iostream>
00042 #include <sstream>
00043 #include <time.h>
00044 #include "Evolution.h"
00045 
00046 #define BOARD_SQUARE_SIZE 2
00047 
00048 int low;                            
00049 int high;                           
00050 double execution_time;              
00051 #endif
00052 
00053 Board::Board(int width, int height, int squareSize, LabelPtr counter)
00054 : m_width(width), m_height(height), m_squareSize(squareSize), m_counter(counter)
00055 {
00056 #ifndef _CONSOLE
00057     InitializeComponent();
00058     DoubleBuffered = true;
00059 
00060     this->Width = m_squareSize*width;
00061     this->Height = m_squareSize*height;
00062 #endif
00063     m_matrix = new Matrix();
00064     m_matrix->width = width;
00065     m_matrix->height = height;
00066     m_matrix->data = new char[width*height];
00067     memset(m_matrix->data, 0, width*height);
00068 #ifndef _CONSOLE
00069     m_occupiedBrush = gcnew SolidBrush(Color::Black);
00070     m_freeBrush = gcnew SolidBrush(Color::LightGray);
00071     
00072     m_graphics = CreateGraphics();
00073     m_bmp = gcnew Bitmap(Width, Height);
00074     m_mem_dc = Graphics::FromImage(m_bmp);
00075 #endif
00076 }
00077 
00078 Board::~Board()
00079 {
00080 #ifndef _CONSOLE
00081     if (components)
00082     {
00083         delete components;
00084     }
00085 #endif
00086     delete[] m_matrix->data;
00087     delete m_matrix;
00088 }
00089 
00090 void Board::seed(int s)
00091 {        
00092     srand(s);
00093     for (int j=0; j<m_height; j++)
00094     {
00095         for (int i=0; i<m_width; i++)
00096         {        
00097             int x = rand()/(int)(((unsigned)RAND_MAX + 1) / 100);
00098             m_matrix->data[i+j*m_width] = x>75? 1: 0;               // 25% occupied
00099         }
00100     }
00101 #ifndef _CONSOLE
00102     Invalidate();
00103 #endif
00104 }
00105 
00106 void Board::seed( const BoardPtr src )
00107 {        
00108             memcpy(m_matrix->data, src->m_matrix->data, m_height*m_width);
00109 #ifndef _CONSOLE
00110     Invalidate();
00111 #endif
00112 }
00113 
00114 #ifndef _CONSOLE
00115 void Board::draw(Graphics^ g)
00116 {
00117     m_mem_dc->FillRectangle(m_freeBrush, Drawing::Rectangle(0, 0, m_width*m_squareSize, m_height*m_squareSize));
00118     for (int j=0; j<m_height; j++)
00119     {
00120         for (int i=0; i<m_width; i++)
00121         {    
00122             if ( m_matrix->data[i+j*m_width] )
00123             {
00124                 m_mem_dc->FillRectangle(m_occupiedBrush, Drawing::Rectangle(i*m_squareSize, j*m_squareSize, m_squareSize, m_squareSize));
00125             }
00126         }
00127     }
00128     g->DrawImage(m_bmp, 0, 0);
00129 }
00130 
00131 void Board::OnPaint(PaintEventArgs^ e)
00132 {
00133     draw(e->Graphics);
00134 }
00135 
00136 [STAThreadAttribute]
00137 int main(array<System::String ^> ^args)
00138 {
00139     // Enabling Windows XP visual effects before any controls are created
00140     Application::EnableVisualStyles();
00141     Application::SetCompatibleTextRenderingDefault(false); 
00142 
00143     // Create the main window and run it
00144     Application::Run(gcnew Form1());
00145     return 0;
00146 }
00147 #else
00148 
00150 void PrintUsage() 
00151 {
00152     printf("Usage: gol [M[:N] -t execution_time]\nM and N are a range of numbers of threads to be used.\nexecution_time is a time (in sec) for execution game_of_life iterations\n");
00153     printf("Default values:\nM:\t\tautomatic\nN:\t\tM\nexecution_time:\t10\n");
00154 }
00155 
00157 bool ParseCommandLine(int argc, char * argv []) 
00158 {
00159     char* s = argv[1];
00160     char* end;
00162     if(argc == 1)
00163     {
00164         low = tbb::task_scheduler_init::automatic;
00165         high = low;
00166         execution_time = 5;
00167         return true;
00168     }
00170     if(argc != 4)
00171     {
00172         PrintUsage();
00173         return false;
00174     }
00175     if(std::string("-t") != argv[argc-2])
00177     high = strtol(s,&end,0);
00178     low = strtol(s,&end,0);
00179     switch( *end ) 
00180     {
00181         case ':': 
00182             high = strtol(end+1,0,0); 
00183             break;
00184         case '\0':
00185             break;
00186         default:
00187             PrintUsage();
00188             return false;
00189     }
00190     if (high < low)
00191     {
00192         std::cout << "Set correct range. Current range: " << low << ":" << high << std::endl;
00193         PrintUsage();
00194         return false;
00195 
00196     }
00198     execution_time = strtol(argv[argc-1],&end,0);
00199     return true;
00200 }
00201 
00202 int main( int argc, char* argv[] ) 
00203 {
00204     if(!ParseCommandLine( argc, argv ))
00205         return 1;
00206     SequentialEvolution* m_seq;
00207     ParallelEvolution* m_par;
00208     Board* m_board1;
00209     Board* m_board2; 
00210     int* count = NULL;
00211    
00212     int boardWidth = 300;
00213     int boardHeight = 300;
00214 
00215     m_board1 = new Board(boardWidth, boardHeight, BOARD_SQUARE_SIZE, count);
00216     m_board2 = new Board(boardWidth, boardHeight, BOARD_SQUARE_SIZE, count);
00217 
00218     time_t now = time(NULL);
00219     printf("Generate Game of life board\n");
00220     m_board1->seed((int)now);
00221     m_board2->seed(m_board1);
00222 
00223     m_seq = new SequentialEvolution(m_board1->m_matrix, m_board1);
00224     m_seq->Run(execution_time, 1);
00225     delete m_seq;
00226 
00227     m_par = new ParallelEvolution(m_board2->m_matrix, m_board2);
00228     for( int p = low; p <= high; ++p ) 
00229     {
00230         m_par->Run(execution_time, p);
00231     }
00232     delete m_par;
00233 
00234     delete m_board1;
00235     delete m_board2;
00236     return 0;
00237 }
00238 #endif

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net