I am taking a high school course of C++. I have made a Win32 Console Application of "Game of Life". For a final project I want to change it from Win32 Console Application to Win32 Console (GUI style). Because of the differences in the two I do not know where to begin. If someone could simply tell me how to out put a matrix that would be very much apresheated. I know I have really bad spelling. Could someone please post the right spelling of the bluntly wrong spelled word. Thank you.
Code for the
CODE
// Scott Mullen, 4/3/08 life program.cpp
//
#include "stdafx.h"
#include <iostream.h>
#include <lvp\vector.h>
#include <lvp\matrix.h>
#include <lvp\string.h>
#include <lvp\bool.h>
#include <lvp\utility.h>
#include <windows.h>
#include <scrnsave.h>
//lvp files came from www.lpdatafiles.com
/*
If the cell is alive on the previous day
Then if the number of neibors was 2 or 3
the cell remains alive
otherwise the cell dies (of either loneliness or overcrowding)
If the cell is not alive on the previous day
Then if the number of neibors was exactly 3
the cell becomes alive
otherwise it remains dead
*/
typedef matrix<char> TTTLife;
void DisplayBoard(const TTTLife &Life)//creates board
{
for(int Row=0; Row<Life.numrows(); Row++){
//display row
for(int Col=0; Col<Life.numcols();Col++)
cout<<Life[Row][Col]<<" ";
cout<<endl;
}
}
//----------------------------------------------------------------------------------------------------------------
void GetStart(int &Row, int &Col, TTTLife &Life)//gets the starting cells
{
int cellnum=1;
int numcells;
cout<<"How many cells would you like to start with?: ";//user inputs number of cells wanted
cin>>numcells;
do{
cout<<"Choose a row for cell "<<cellnum<<" to start in: ";
cin>>Row;
cout<<"Choose a column for cell "<<cellnum<<" to start in: ";
cin>>Col;
cellnum++;//
Life[Row][Col]='X';
}while(cellnum<numcells+1);//loops until number of cells equals the desired number of cells
}
//----------------------------------------------------------------------------------------------------------------
void GetStart2(int &Row, int &Col, TTTLife &Life, int Size, int Size2)//gets the starting cells
//this is the method being used. more practical for board size
{
int cellnum=1;
int numcells;
cout<<"How many cells would you like to start with?: ";//user inputs number of cells wanted
cin>>numcells;
do{
Row=random(Size2);
Col=random(Size);
cellnum++;
Life[Row][Col]='X';
}while(cellnum<numcells+1);//loops until number of cells equals the desired number of cells
}
//------------------------------------------------------------------------------------------------------------------
char FindNeighbor(int Row, int Col, const TTTLife &Life)
{
int CurrNeighbor=0;
if(Col>0)//check left
if(Life[Row][Col-1]=='X')
CurrNeighbor++;
if(Row<Life.numrows()-1)//check down
if(Life[Row+1][Col]=='X')
CurrNeighbor++;
if(Row>0)//check up
if(Life[Row-1][Col]=='X')
CurrNeighbor++;
if(Col<Life.numcols()-1)//check right
if(Life[Row][Col+1]=='X')
CurrNeighbor++;
if(Row>0 && Col<Life.numcols()-1)//check diagonal up right
if(Life[Row-1][Col+1]=='X')
CurrNeighbor++;
if(Row<Life.numrows()-1 && Col<Life.numcols()-1)//check diagonal down right
if(Life[Row+1][Col+1]=='X')
CurrNeighbor++;
if(Col>0 && Row>0)//check diagonal up left
if(Life[Row-1][Col-1]=='X')
CurrNeighbor++;
if(Row<Life.numrows()-1 && Col>0)//check diagonal down left
if(Life[Row+1][Col-1]=='X')
CurrNeighbor++;
return(CurrNeighbor);
}
//-------------------------------------------------------------------------------------------------
void AddCell(int &Row, int &Col, TTTLife &Life, TTTLife &Temp)
{
int numneighbor=FindNeighbor(Row,Col,Life);
if(numneighbor==3)
Temp[Row][Col]='X';//adds cell if neighbors are correct
if(numneighbor>3)
Temp[Row][Col]=' ';//kills cell if over crowded
if(numneighbor<2)
Temp[Row][Col]=' ';//kills cell if lonely
}
//-------------------------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
randomize();
int x=1;
const int Size=35;//collumns in board
const int Size2=55;//rows in board
TTTLife Life(Size2, Size, ' ');
TTTLife Temp(Size2, Size, ' ');//Temporary board
int Row;
int Col;
int years=0;
int days=0;
int cellnum=1;
int numcells=500;
GetStart2(Row, Col, Life, Size, 55);
Temp=Life;//Turns current board into Temp board
do{
setcolor(x);//changes the color every day
x++;
if(x==16)
x=1;
cout<<endl;
DisplayBoard(Life);//display board
for(int Row=0; Row<Life.numrows(); Row++){
for(int Col=0; Col<Life.numcols();Col++)
AddCell(Row,Col,Life,Temp);//adds the cells
}
Life=Temp;//sets real board to Temp board when done
Sleep(200);//one day every .2 seconds.
//Pause();
days++; //counts days
if(days%365==0){
do{//adds a random amount of cells every year
Row=random(Size2);
Col=random(Size);
cellnum++;
Life[Row][Col]='T';
}while(cellnum<numcells+1);
cellnum=1;
}
if(days==365){//turns days into years on counter
years++;
days=0;
}
cout<<"Years: "<<years<<" Days: "<<days;//display counter
clrscr();
}while(1);
return 0;
}
this is the code for the lvp/utility
CODE
/* Utility Library
void Pause()
int GetInt(int Lo, int Hi) */
#include "stdafx.h"
#include <iostream.h>
#include <conio.h>
#include <windows.h>
#include <lvp\random.h>
# include <stdlib.h>
# include <iomanip.h>
# include <math.h>
#include <lvp/string.h>
//--------------------------------------------------------------------------------------------------------------------------------------------------------
void Pause()
/* Displays a message and waits for the user to hit a key
Post: Key pressed */
{
cout<<"Press a key to continue"<<endl;
getch(); // Pause for user to kit a key
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
int GetInt(int Lo, int Hi)
/* Obtains and returns an integer from the user in the range Lo..Hi.
Pre: Lo<Hi
Post: An integer between Lo and Hi inclusive returned */
{
int Entry;
cin>>Entry;
while((Entry<Lo) || (Entry>Hi)){
cout<<"Value must be between "<<Lo<<" and "<<Hi<<endl;
cout<<"Please re-enter: ";
cin>>Entry;
}
return(Entry);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
void Signature()
/* Displays the writers name*/
{
cout<<"This program was written by Scott Mullen."<<endl;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
int randominrange(int Lo, int Hi)
/* Randomly gives a number within the speified range */
{
int range=Hi-Lo;
return(Lo + random(range+1));
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
void clrscr()
{
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord = {0, 0};
DWORD count;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hStdOut, &csbi);
FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
//note that you can use this function to
//position the cursor in the console.
SetConsoleCursorPosition(hStdOut, coord);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
void setcolor(unsigned int color)
// Shatter, shatters the screen!!
// by: Wolf Coder (1605)
{
/*****
Use this function to set the color of text output to the screen.
This is for windows platform only.
Required header <windows.h>
COLOR CODES:
1 BLUE
2 GREEN
3 CYAN
4 RED
5 MAGENTA
6 BROWN
7 LIGHTGRAY
8 DARKGRAY
9 LIGHTBLUE
10 LIGHTGREEN
11 LIGHTCYAN
12 LIGHTRED
13 LIGHTMAGENTA
14 YELLOW
15 WHITE
*****/
if (color >15 || color <=0)
{
cout <<"Error" <<endl;
}
else
{
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon,color);
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
void Shatter(int R, int G, int B)
{
typedef struct
{
#define DIVI 16
#define DMEM (DIVI*DIVI)
double x,y,mx,my,dx,dy;
HBITMAP hbitmap;
}piece;
/*int WINAPI Win(HINSTANCE hinstance,HINSTANCE hprevinstance,PSTR szcmdline,int icmdshow)
{*/
HDC hdc,xdc,mdc;
HWND hwnd;
int cx,cy,counter;
piece particle[DMEM];
HBITMAP hbitmap; // For frame locking
HPEN hpen = CreatePen(PS_SOLID,0,RGB(R,G,B));//boarder around screen*****
HBRUSH hbrush = (HBRUSH)GetStockObject(BLACK_BRUSH);
if(LockWindowUpdate(hwnd = GetDesktopWindow()))
{
hdc = GetDCEx(hwnd,NULL,DCX_CACHE | DCX_LOCKWINDOWUPDATE);
xdc = CreateCompatibleDC(hdc);
mdc = CreateCompatibleDC(hdc);
// Now get the size of each rectangle
cx = GetSystemMetrics(SM_CXSCREEN)/DIVI;
cy = GetSystemMetrics(SM_CYSCREEN)/DIVI;
// Now create all the pieces...
srand((int)GetCurrentTime()); // Initialize random seed
hbitmap = CreateCompatibleBitmap(hdc,cx*DIVI,cy*DIVI); // The size of the whole screen
for(int index = 0;index < DMEM;index++)
{
particle[index].hbitmap = CreateCompatibleBitmap(hdc,cx,cy); // The size of a piece is (cx,cy)
particle[index].mx = (double)((rand()%16)-8);
particle[index].my = (double)((rand()%16)-8);
}
// Now the pieces need to be grabbed
counter = 0; // Reset counter
for(int x = 0;x < cx*DIVI;x += cx)
{
for(int y = 0;y < cy*DIVI;y += cy)
{
SelectObject(xdc,particle[counter].hbitmap);
particle[counter].x = (double)x;
particle[counter].y = (double)y;
particle[counter].dx = x;
particle[counter].dy = y;
BitBlt(xdc,0,0,cx,cy,hdc,x,y,SRCCOPY);
counter++;
}
}
// Run for 8000 frames
for(int frame = 0;frame < 325;frame++)
{
SelectObject(xdc,hbitmap);
SelectObject(xdc,hpen);
SelectObject(xdc,hbrush); // Select an empty shader for the result bitmap
Rectangle(xdc,0,0,cx*DIVI,cy*DIVI); // Fill with zero value
for(int index = 0;index < DMEM;index++)
{
SelectObject(mdc,particle[index].hbitmap); // Get that bitmap!
BitBlt(xdc,(int)particle[index].x,(int)particle[index].y,cx,cy,mdc,0,0,SRCCOPY); // Draw the particle
particle[index].x += particle[index].mx;
particle[index].y += particle[index].my;
particle[index].my += 0.1;
}
BitBlt(hdc,0,0,cx*DIVI,cy*DIVI,xdc,0,0,SRCCOPY);
}
Sleep(1000); // Sleep for a second
// Now run using a tracking method for recombine!
for(int dframe = 0;dframe < 200;dframe++)
{
SelectObject(xdc,hbitmap);
SelectObject(xdc,hpen);
SelectObject(xdc,hbrush); // Select an empty shader for the result bitmap
Rectangle(xdc,0,0,cx*DIVI,cy*DIVI); // Fill with zero value
for(int index = 0;index < DMEM;index++)
{
if(dframe == 180)
{
particle[index].x = particle[index].dx;
particle[index].y = particle[index].dy;
}
SelectObject(mdc,particle[index].hbitmap); // Get that bitmap!
BitBlt(xdc,(int)particle[index].x,(int)particle[index].y,cx,cy,mdc,0,0,SRCCOPY); // Draw the particle
double dist_x = particle[index].x-particle[index].dx;
double dist_y = particle[index].y-particle[index].dy;
dist_x /= 16;
dist_y /= 16;
particle[index].x -= dist_x;
particle[index].y -= dist_y;
}
BitBlt(hdc,0,0,cx*DIVI,cy*DIVI,xdc,0,0,SRCCOPY);
}
// Now pack everything away
DeleteObject(hbitmap);
for(int dindex = 0;dindex < DMEM;dindex++)
DeleteObject(particle[dindex].hbitmap);
ReleaseDC(hwnd,hdc);
DeleteDC(hdc);
DeleteDC(xdc);
DeleteDC(mdc);
LockWindowUpdate(hwnd);
}
DeleteObject(hpen);
DeleteObject(hbrush);
//MessageBox(hwnd,"That was cool!","Cool",MB_ICONEXCLAMATION);
// return 1;
//}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
// windows.h needs to be included for this...
void sleep(int secs)
{
unsigned long int t=GetTickCount()+(1000L*secs);
while(GetTickCount()<t)
{ /* Wait !!!*/ }
}
/*
void PrimeCheck()
{
int i,p,result_div,check,s=0;
float calc;
clrscr();
do {
cout<<"Enter number :";
cin>>p;
for(i=2;i<p;i++)
{
calc=p/i;
result_div=floor(calc);
check=result_div*i;
if(p==' ' || p<=0)
{
break;
}
else if(check==p)
{
break;
}
else if(check<p || p==2 || p==1)
{
continue;
}
}
if (check==p)
{
cout<<p<<" "<<"No prime number"<<endl;
}
else
{
cout<<p<<" "<<" Prime number"<<endl;
}
}while(s!=1);
getch();
}
*/