<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2enclosuresfull.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:media="http://search.yahoo.com/mrss/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>Coding Mix</title><link>http://www.codingmix.com/search/label/Cplusplus%20projects</link><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/codingmix/Cplusplus/projects" /><description></description><language>en</language><managingEditor>noreply@blogger.com (Csabi)</managingEditor><lastBuildDate>Thu, 23 Feb 2012 19:15:09 PST</lastBuildDate><generator>Blogger http://www.blogger.com</generator><openSearch:totalResults xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">7</openSearch:totalResults><openSearch:startIndex xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">1</openSearch:startIndex><openSearch:itemsPerPage xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">25</openSearch:itemsPerPage><feedburner:info uri="codingmix/cplusplus/projects" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>codingmix/Cplusplus/projects</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item><title>My first 2D game in C++ using Direct3D</title><link>http://feedproxy.google.com/~r/codingmix/Cplusplus/projects/~3/nzddpsqhEUU/2d-game-directx-snake-cplusplus.html</link><category>Cplusplus projects</category><category>Cplusplus</category><author>noreply@blogger.com (Csabi)</author><pubDate>Fri, 10 Dec 2010 12:54:38 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-5682776130825686229.post-5494779758440019457</guid><description>This is my first 2D game. It`s written in C++ using DirectX9 (Direct3D and DirectInput). This is a very simple Snake game. At the beginning the snake is only 1 square long and when it "eats" the target it`s length is encreased by 1 square. The snake can have a maximum of 20 squares long (each square is 20px), after it reaches this maximum length the snake is moving faster and faster. If the head of the snake hit`s it`s  tail the game is over:&lt;div id="full-post"&gt;&lt;object width="540" height="328"&gt;&lt;param name="movie" value="http://www.youtube.com/v/K8_UpmEfEnU?fs=1&amp;amp;hl=en_US&amp;amp;rel=0&amp;amp;color1=0x234900&amp;amp;color2=0x4e9e00"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowscriptaccess" value="always"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/K8_UpmEfEnU?fs=1&amp;amp;hl=en_US&amp;amp;rel=0&amp;amp;color1=0x234900&amp;amp;color2=0x4e9e00" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="540" height="328"&gt;&lt;/embed&gt;&lt;/object&gt;And here is the source-code:&lt;div class="code"&gt;#include &amp;lt;Windows.h&amp;gt;&lt;br /&gt;#include &amp;lt;WindowsX.h&amp;gt;&lt;br /&gt;#include &amp;lt;d3d9.h&amp;gt;&lt;br /&gt;#include &amp;lt;d3dx9.h&amp;gt;&lt;br /&gt;#include &amp;lt;dinput.h&amp;gt;&lt;br /&gt;#include &amp;lt;ctime&amp;gt;&lt;br /&gt;&lt;br /&gt;#pragma comment(lib,&amp;quot;d3d9.lib&amp;quot;);&lt;br /&gt;#pragma comment(lib,&amp;quot;d3dx9.lib&amp;quot;);&lt;br /&gt;#pragma comment(lib,&amp;quot;dinput8.lib&amp;quot;);&lt;br /&gt;#pragma comment(lib,&amp;quot;dxguid.lib&amp;quot;);&lt;br /&gt;&lt;br /&gt;LPDIRECT3D9 d3d;&lt;br /&gt;LPDIRECT3DDEVICE9 d3ddev;&lt;br /&gt;LPDIRECT3DVERTEXBUFFER9 buffer=NULL;&lt;br /&gt;LPDIRECTINPUT8 dinput;&lt;br /&gt;LPDIRECTINPUTDEVICE8 keyboard;&lt;br /&gt;BYTE buttons[256];&lt;br /&gt;ID3DXFont *m_font;&lt;br /&gt;&lt;br /&gt;LRESULT CALLBACK WindowProc(HWND hWnd,&lt;br /&gt;                                                             UINT message,&lt;br /&gt;                                                             WPARAM wParam,&lt;br /&gt;                                                             LPARAM lParam);&lt;br /&gt;void initialized3d(HWND hWnd);&lt;br /&gt;void renderframe(void);&lt;br /&gt;void cleand3d(void);&lt;br /&gt;void graphics(void);&lt;br /&gt;void initializedinput(HINSTANCE hInstance,&lt;br /&gt;                             HWND hWnd);&lt;br /&gt;void getkey(void);&lt;br /&gt;void cleandinput(void);&lt;br /&gt;&lt;br /&gt;struct VERTEX {&lt;br /&gt;   FLOAT X, Y, Z, RHW; &lt;br /&gt;   DWORD COLOR;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;#define FVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)&lt;br /&gt;&lt;br /&gt;int WINAPI WinMain(HINSTANCE hInstance, &lt;br /&gt;                                   HINSTANCE hPrevInstance, &lt;br /&gt;                                   LPSTR lpCmdLine,&lt;br /&gt;                                   int nCmdShow){&lt;br /&gt;   WNDCLASSEX windowclass;&lt;br /&gt;   ZeroMemory(&amp;amp;windowclass,sizeof(WNDCLASSEX));&lt;br /&gt;&lt;br /&gt;   windowclass.cbSize = sizeof(WNDCLASSEX);&lt;br /&gt;   windowclass.style = CS_HREDRAW | CS_VREDRAW;&lt;br /&gt;   windowclass.lpfnWndProc = WindowProc;&lt;br /&gt;   windowclass.hInstance = hInstance;&lt;br /&gt;   windowclass.hCursor = LoadCursor(NULL,IDC_ARROW);&lt;br /&gt;   windowclass.hbrBackground = (HBRUSH)COLOR_WINDOW;&lt;br /&gt;   windowclass.lpszClassName = L&amp;quot;WindowClass&amp;quot;;&lt;br /&gt;&lt;br /&gt;   RegisterClassEx(&amp;amp;windowclass);&lt;br /&gt;   HWND hWnd = CreateWindowEx(NULL,&lt;br /&gt;                                                          L&amp;quot;WindowClass&amp;quot;,&lt;br /&gt;                                                          L&amp;quot;Snake - www.codingmix.com&amp;quot;,&lt;br /&gt;                                                          WS_OVERLAPPEDWINDOW,&lt;br /&gt;                                                          600,&lt;br /&gt;                                                          200,&lt;br /&gt;                                                          636,&lt;br /&gt;                                                          478,&lt;br /&gt;                                                          NULL,&lt;br /&gt;                                                          NULL,&lt;br /&gt;                                                          hInstance,&lt;br /&gt;                                                          NULL);&lt;br /&gt;   ShowWindow(hWnd,&lt;br /&gt;                          nCmdShow);&lt;br /&gt;   initialized3d(hWnd);&lt;br /&gt;   initializedinput(hInstance,&lt;br /&gt;                         hWnd);&lt;br /&gt;   MSG message;&lt;br /&gt;   while(TRUE){&lt;br /&gt;      while(PeekMessage(&amp;amp;message,&lt;br /&gt;                                      NULL,&lt;br /&gt;                                      0,&lt;br /&gt;                                      0,&lt;br /&gt;                                      PM_REMOVE)){&lt;br /&gt;         TranslateMessage(&amp;amp;message);&lt;br /&gt;         DispatchMessage(&amp;amp;message);&lt;br /&gt;      }&lt;br /&gt;      if(message.message == WM_QUIT){&lt;br /&gt;         break;&lt;br /&gt;      }&lt;br /&gt;      getkey();&lt;br /&gt;      graphics();&lt;br /&gt;      renderframe();&lt;br /&gt;   }  &lt;br /&gt;   cleand3d();&lt;br /&gt;   cleandinput();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void initialized3d(HWND hWnd){&lt;br /&gt;   d3d = Direct3DCreate9(D3D_SDK_VERSION);&lt;br /&gt;   D3DPRESENT_PARAMETERS d3dpp;&lt;br /&gt;   ZeroMemory(&amp;amp;d3dpp,sizeof(d3dpp));&lt;br /&gt;&lt;br /&gt;   d3dpp.Windowed = TRUE;&lt;br /&gt;   d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;&lt;br /&gt;   d3dpp.hDeviceWindow = hWnd;&lt;br /&gt;&lt;br /&gt;   d3d-&amp;gt;CreateDevice(D3DADAPTER_DEFAULT,&lt;br /&gt;                                   D3DDEVTYPE_HAL,&lt;br /&gt;                                   hWnd,&lt;br /&gt;                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING,&lt;br /&gt;                                   &amp;amp;d3dpp,&lt;br /&gt;                                   &amp;amp;d3ddev);&lt;br /&gt;   D3DXCreateFont(d3ddev, &lt;br /&gt;                                50, &lt;br /&gt;                                0, &lt;br /&gt;                                FW_BOLD, &lt;br /&gt;                                0, &lt;br /&gt;                                FALSE, &lt;br /&gt;                                DEFAULT_CHARSET, &lt;br /&gt;                                OUT_DEFAULT_PRECIS, &lt;br /&gt;                                DEFAULT_QUALITY, &lt;br /&gt;                                DEFAULT_PITCH | FF_DONTCARE, &lt;br /&gt;                                TEXT(&amp;quot;Arial&amp;quot;), &lt;br /&gt;                                &amp;amp;m_font );&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void initializedinput(HINSTANCE hInstance,&lt;br /&gt;                              HWND hWnd){&lt;br /&gt;   DirectInput8Create(hInstance,&lt;br /&gt;                                 DIRECTINPUT_VERSION,&lt;br /&gt;                                 IID_IDirectInput8,&lt;br /&gt;                                 (void**)&amp;amp;dinput,&lt;br /&gt;                                 NULL);&lt;br /&gt;   dinput-&amp;gt;CreateDevice(GUID_SysKeyboard,&lt;br /&gt;                                      &amp;amp;keyboard,&lt;br /&gt;                                      NULL);&lt;br /&gt;   keyboard-&amp;gt;SetDataFormat(&amp;amp;c_dfDIKeyboard);&lt;br /&gt;   keyboard-&amp;gt;SetCooperativeLevel(hWnd,&lt;br /&gt;                                                      DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int verticesnr = 132;&lt;br /&gt;int trianglesnr = 44;&lt;br /&gt;bool left = false;&lt;br /&gt;bool right = false;&lt;br /&gt;bool up = true;&lt;br /&gt;bool down = false;&lt;br /&gt;float posy = 220.0f;&lt;br /&gt;float posx = 280.0f;&lt;br /&gt;bool target = false;&lt;br /&gt;float random1;&lt;br /&gt;float random2;&lt;br /&gt;float newpos[][2] = {&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;   {-20.0f, -20.0f},&lt;br /&gt;};&lt;br /&gt;int squares = 0;&lt;br /&gt;bool gameover = false;&lt;br /&gt;int col1 = 0;&lt;br /&gt;int col2 = 255;&lt;br /&gt;int currentframe = 0;&lt;br /&gt;int speed = 30;&lt;br /&gt;void graphics(void){&lt;br /&gt;   static int framenr = 0;&lt;br /&gt;   framenr++;&lt;br /&gt;   if(framenr % speed == 0 &amp;amp;&amp;amp; !gameover){&lt;br /&gt;      if(squares &amp;gt; 0){&lt;br /&gt;         for(int i=squares-1;i&amp;gt;0;i--){&lt;br /&gt;            newpos[i][0] = newpos[i-1][0];&lt;br /&gt;            newpos[i][1] = newpos[i-1][1];&lt;br /&gt;         } &lt;br /&gt;         newpos[0][0] = posx;&lt;br /&gt;         newpos[0][1] = posy;&lt;br /&gt;      }&lt;br /&gt;      if(up){&lt;br /&gt;         posy -= 20.0f;&lt;br /&gt;      }&lt;br /&gt;      else if(down){&lt;br /&gt;         posy += 20.0f;&lt;br /&gt;      }&lt;br /&gt;      else if(left){&lt;br /&gt;         posx -= 20.0f;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         posx += 20.0f;&lt;br /&gt;      }&lt;br /&gt;      for(int i=0;i&amp;lt;=squares;i++){&lt;br /&gt;         for(int y=0;y&amp;lt;=squares;y++){&lt;br /&gt;            if(newpos[i][0] == newpos[y][0] &amp;amp;&amp;amp; newpos[i][1] == newpos[y][1] &amp;amp;&amp;amp; i != y){&lt;br /&gt;               gameover = true;&lt;br /&gt;            }&lt;br /&gt;         }&lt;br /&gt;         if(posx == newpos[i][0] &amp;amp;&amp;amp; posy == newpos[i][1]){&lt;br /&gt;            gameover = true;&lt;br /&gt;         }&lt;br /&gt;      }&lt;br /&gt;      if(posx == random1 &amp;amp;&amp;amp; posy == random2){&lt;br /&gt;         target = false;&lt;br /&gt;         if(squares &amp;gt; 19 &amp;amp;&amp;amp; squares &amp;gt; 0){&lt;br /&gt;            speed--;&lt;br /&gt;         }&lt;br /&gt;         else{&lt;br /&gt;            if(squares == 0){&lt;br /&gt;               newpos[0][0] = posx;&lt;br /&gt;               newpos[0][1] = posy;&lt;br /&gt;            }&lt;br /&gt;            else{&lt;br /&gt;               newpos[squares][0] = newpos[squares-1][0];&lt;br /&gt;               newpos[squares][1] = newpos[squares-1][1];&lt;br /&gt;            }&lt;br /&gt;            squares++;&lt;br /&gt;         }&lt;br /&gt;      }&lt;br /&gt;      for(int i=0;i&amp;lt;squares;i++){&lt;br /&gt;         if(newpos[i][0] &amp;gt; 600){&lt;br /&gt;            newpos[i][0]-=620;&lt;br /&gt;         }&lt;br /&gt;         else if(newpos[i][0] &amp;lt; 0){&lt;br /&gt;            newpos[i][0]+=620;&lt;br /&gt;         }&lt;br /&gt;         else if(newpos[i][1] &amp;gt; 420){&lt;br /&gt;            newpos[i][1]-=440;&lt;br /&gt;         }&lt;br /&gt;         else if(newpos[i][1] &amp;lt; 0){&lt;br /&gt;            newpos[i][1]+=440;&lt;br /&gt;         }&lt;br /&gt;      }&lt;br /&gt;      if(posx &amp;gt; 600){&lt;br /&gt;         posx-=620;&lt;br /&gt;      }&lt;br /&gt;      else if(posx &amp;lt; 0){&lt;br /&gt;         posx+=620;&lt;br /&gt;      }&lt;br /&gt;      else if(posy &amp;gt; 420){&lt;br /&gt;         posy-=440;&lt;br /&gt;      }&lt;br /&gt;      else if(posy &amp;lt; 0){&lt;br /&gt;         posy+=440;&lt;br /&gt;      }&lt;br /&gt;      currentframe = 0;&lt;br /&gt;   }&lt;br /&gt;   else if(framenr % 20 == 0 &amp;amp;&amp;amp; gameover){&lt;br /&gt;      if(col1 == 255){&lt;br /&gt;         col1 = 0;&lt;br /&gt;         col2 = 255;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         col1 = 255;&lt;br /&gt;         col2 = 0;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;   if(buttons[DIK_LEFT] &amp;amp; 0x80 &amp;amp;&amp;amp; right == false &amp;amp;&amp;amp; currentframe == 0){&lt;br /&gt;      left = true;&lt;br /&gt;      up = false;&lt;br /&gt;      down = false;&lt;br /&gt;      currentframe = 1;&lt;br /&gt;   }&lt;br /&gt;   else if(buttons[DIK_RIGHT] &amp;amp; 0x80 &amp;amp;&amp;amp; left == false &amp;amp;&amp;amp; currentframe == 0){&lt;br /&gt;      right = true;&lt;br /&gt;      up = false;&lt;br /&gt;      down = false;&lt;br /&gt;      currentframe = 1;&lt;br /&gt;   }&lt;br /&gt;   else if(buttons[DIK_UP] &amp;amp; 0x80 &amp;amp;&amp;amp; down == false &amp;amp;&amp;amp; currentframe == 0){&lt;br /&gt;      up = true;&lt;br /&gt;      left = false;&lt;br /&gt;      right = false;&lt;br /&gt;      currentframe = 1;&lt;br /&gt;   }&lt;br /&gt;   else if(buttons[DIK_DOWN] &amp;amp; 0x80 &amp;amp;&amp;amp; up == false &amp;amp;&amp;amp; currentframe == 0){&lt;br /&gt;      down = true;&lt;br /&gt;      left = false;&lt;br /&gt;      right = false;&lt;br /&gt;      currentframe = 1;&lt;br /&gt;   }&lt;br /&gt;   if(!target){&lt;br /&gt;      srand((unsigned)time(0));&lt;br /&gt;      random1 = (rand() % 30) * 20.0f; &lt;br /&gt;      random2 = (rand() % 22) * 20.0f;&lt;br /&gt;      target = true;&lt;br /&gt;   }&lt;br /&gt;   VERTEX vertices[] = {&lt;br /&gt;      //Target&lt;br /&gt;      {random1 + 10.0f, random2, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),},&lt;br /&gt;      {random1 + 20.0f, random2 + 10.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),},&lt;br /&gt;      {random1, random2 + 10.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),},&lt;br /&gt;      {random1, random2 + 10.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),},&lt;br /&gt;      {random1 + 20.0f, random2 + 10.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),},&lt;br /&gt;      {random1 + 10.0f, random2 + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),},&lt;br /&gt;      //Head&lt;br /&gt;      {posx , posy, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {posx + 20.0f, posy, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {posx, posy + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {posx, posy + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {posx + 20.0f, posy, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {posx + 20.0f, posy + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 1&lt;br /&gt;      {newpos[0][0] , newpos[0][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[0][0] + 20.0f, newpos[0][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[0][0], newpos[0][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[0][0], newpos[0][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[0][0] + 20.0f, newpos[0][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[0][0] + 20.0f, newpos[0][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 2&lt;br /&gt;      {newpos[1][0] , newpos[1][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[1][0] + 20.0f, newpos[1][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[1][0], newpos[1][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[1][0], newpos[1][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[1][0] + 20.0f, newpos[1][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[1][0] + 20.0f, newpos[1][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 3&lt;br /&gt;      {newpos[2][0] , newpos[2][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[2][0] + 20.0f, newpos[2][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[2][0], newpos[2][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[2][0], newpos[2][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[2][0] + 20.0f, newpos[2][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[2][0] + 20.0f, newpos[2][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 4&lt;br /&gt;      {newpos[3][0] , newpos[3][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[3][0] + 20.0f, newpos[3][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[3][0], newpos[3][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[3][0], newpos[3][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[3][0] + 20.0f, newpos[3][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[3][0] + 20.0f, newpos[3][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 5&lt;br /&gt;      {newpos[4][0] , newpos[4][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[4][0] + 20.0f, newpos[4][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[4][0], newpos[4][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[4][0], newpos[4][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[4][0] + 20.0f, newpos[4][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[4][0] + 20.0f, newpos[4][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 6&lt;br /&gt;      {newpos[5][0] , newpos[5][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[5][0] + 20.0f, newpos[5][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[5][0], newpos[5][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[5][0], newpos[5][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[5][0] + 20.0f, newpos[5][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[5][0] + 20.0f, newpos[5][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 7&lt;br /&gt;      {newpos[6][0] , newpos[6][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[6][0] + 20.0f, newpos[6][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[6][0], newpos[6][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[6][0], newpos[6][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[6][0] + 20.0f, newpos[6][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[6][0] + 20.0f, newpos[6][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 8&lt;br /&gt;      {newpos[7][0] , newpos[7][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[7][0] + 20.0f, newpos[7][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[7][0], newpos[7][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[7][0], newpos[7][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[7][0] + 20.0f, newpos[7][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[7][0] + 20.0f, newpos[7][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 9&lt;br /&gt;      {newpos[8][0] , newpos[8][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[8][0] + 20.0f, newpos[8][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[8][0], newpos[8][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[8][0], newpos[8][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[8][0] + 20.0f, newpos[8][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[8][0] + 20.0f, newpos[8][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 10&lt;br /&gt;      {newpos[9][0] , newpos[9][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[9][0] + 20.0f, newpos[9][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[9][0], newpos[9][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[9][0], newpos[9][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[9][0] + 20.0f, newpos[9][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[9][0] + 20.0f, newpos[9][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 11&lt;br /&gt;      {newpos[10][0] , newpos[10][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[10][0] + 20.0f, newpos[10][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[10][0], newpos[10][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[10][0], newpos[10][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[10][0] + 20.0f, newpos[10][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[10][0] + 20.0f, newpos[10][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 12&lt;br /&gt;      {newpos[11][0] , newpos[11][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[11][0] + 20.0f, newpos[11][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[11][0], newpos[11][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[11][0], newpos[11][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[11][0] + 20.0f, newpos[11][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[11][0] + 20.0f, newpos[11][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 13&lt;br /&gt;      {newpos[12][0] , newpos[12][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[12][0] + 20.0f, newpos[12][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[12][0], newpos[12][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[12][0], newpos[12][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[12][0] + 20.0f, newpos[12][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[12][0] + 20.0f, newpos[12][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 14&lt;br /&gt;      {newpos[13][0] , newpos[13][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[13][0] + 20.0f, newpos[13][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[13][0], newpos[13][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[13][0], newpos[13][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[13][0] + 20.0f, newpos[13][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[13][0] + 20.0f, newpos[13][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 15&lt;br /&gt;      {newpos[14][0] , newpos[14][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[14][0] + 20.0f, newpos[14][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[14][0], newpos[14][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[14][0], newpos[14][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[14][0] + 20.0f, newpos[14][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[14][0] + 20.0f, newpos[14][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 16&lt;br /&gt;      {newpos[15][0] , newpos[15][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[15][0] + 20.0f, newpos[15][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[15][0], newpos[15][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[15][0], newpos[15][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[15][0] + 20.0f, newpos[15][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[15][0] + 20.0f, newpos[15][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 17&lt;br /&gt;      {newpos[16][0] , newpos[16][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[16][0] + 20.0f, newpos[16][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[16][0], newpos[16][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[16][0], newpos[16][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[16][0] + 20.0f, newpos[16][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[16][0] + 20.0f, newpos[16][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 18&lt;br /&gt;      {newpos[17][0] , newpos[17][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[17][0] + 20.0f, newpos[17][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[17][0], newpos[17][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[17][0], newpos[17][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[17][0] + 20.0f, newpos[17][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[17][0] + 20.0f, newpos[17][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 19&lt;br /&gt;      {newpos[18][0] , newpos[18][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[18][0] + 20.0f, newpos[18][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[18][0], newpos[18][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[18][0], newpos[18][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[18][0] + 20.0f, newpos[18][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[18][0] + 20.0f, newpos[18][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      //Square 20&lt;br /&gt;      {newpos[19][0] , newpos[19][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[19][0] + 20.0f, newpos[19][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[19][0], newpos[19][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[19][0], newpos[19][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[19][0] + 20.0f, newpos[19][1], 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;      {newpos[19][0] + 20.0f, newpos[19][1] + 20.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(col1, col2, 0),},&lt;br /&gt;   };&lt;br /&gt;   d3ddev-&amp;gt;CreateVertexBuffer(verticesnr*sizeof(VERTEX),&lt;br /&gt;                                                 0,&lt;br /&gt;                                                 FVF,&lt;br /&gt;                                                 D3DPOOL_MANAGED,&lt;br /&gt;                                                 &amp;amp;buffer,&lt;br /&gt;                                                 NULL);&lt;br /&gt;   VOID* pVoid;&lt;br /&gt;   buffer-&amp;gt;Lock(0,&lt;br /&gt;                         0,&lt;br /&gt;                         (void**)&amp;amp;pVoid,&lt;br /&gt;                         0);&lt;br /&gt;   memcpy(pVoid,&lt;br /&gt;                 vertices,&lt;br /&gt;                 sizeof(vertices));&lt;br /&gt;   buffer-&amp;gt;Unlock();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void getkey(void){&lt;br /&gt;   keyboard-&amp;gt;Acquire();&lt;br /&gt;   keyboard-&amp;gt;GetDeviceState(256,&lt;br /&gt;                                              (LPVOID)buttons);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void renderframe(void){&lt;br /&gt;   d3ddev-&amp;gt;Clear(0,&lt;br /&gt;                            NULL,&lt;br /&gt;                            D3DCLEAR_TARGET,&lt;br /&gt;                            D3DCOLOR_XRGB(255,222,173),&lt;br /&gt;                            1.0f,&lt;br /&gt;                            0);&lt;br /&gt;   d3ddev-&amp;gt;BeginScene();&lt;br /&gt;&lt;br /&gt;   D3DCOLOR fontColor = D3DCOLOR_ARGB(255,255,0,0); &lt;br /&gt;   RECT rct;&lt;br /&gt;   rct.left=200;&lt;br /&gt;   rct.right=480;&lt;br /&gt;   rct.top=150;&lt;br /&gt;   rct.bottom=rct.top+50;&lt;br /&gt;   if(gameover){&lt;br /&gt;      m_font-&amp;gt;DrawText(NULL,&lt;br /&gt;                                     (LPCWSTR)L&amp;quot;Game over&amp;quot;,&lt;br /&gt;                                     -1,&lt;br /&gt;                                     &amp;amp;rct,&lt;br /&gt;                                     0,&lt;br /&gt;                                     fontColor);&lt;br /&gt;   }&lt;br /&gt;   d3ddev-&amp;gt;SetFVF(FVF);&lt;br /&gt;   d3ddev-&amp;gt;SetStreamSource(0,&lt;br /&gt;                                              buffer,&lt;br /&gt;                                              0,&lt;br /&gt;                                              sizeof(VERTEX));&lt;br /&gt;   d3ddev-&amp;gt;DrawPrimitive(D3DPT_TRIANGLELIST,&lt;br /&gt;                                         0,&lt;br /&gt;                                         trianglesnr);&lt;br /&gt;   d3ddev-&amp;gt;EndScene();&lt;br /&gt;   d3ddev-&amp;gt;Present(NULL,&lt;br /&gt;                               NULL,&lt;br /&gt;                               NULL,&lt;br /&gt;                               NULL);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void cleand3d(void){&lt;br /&gt;   buffer-&amp;gt;Release();&lt;br /&gt;   d3ddev-&amp;gt;Release();&lt;br /&gt;   d3d-&amp;gt;Release();&lt;br /&gt;   m_font-&amp;gt;Release();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void cleandinput(void){&lt;br /&gt;   keyboard-&amp;gt;Unacquire(); &lt;br /&gt;   dinput-&amp;gt;Release(); &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;LRESULT CALLBACK WindowProc(HWND hWnd,&lt;br /&gt;                                                             UINT message,&lt;br /&gt;                                                             WPARAM wParam,&lt;br /&gt;                                                             LPARAM lParam){&lt;br /&gt;   if(message == WM_DESTROY){&lt;br /&gt;      PostQuitMessage(0);&lt;br /&gt;      return 0;&lt;br /&gt;   }&lt;br /&gt;   return DefWindowProc(hWnd,&lt;br /&gt;                                        message,&lt;br /&gt;                                        wParam,&lt;br /&gt;                                        lParam);&lt;br /&gt;}&lt;/div&gt;You can ask anything about how it works but I`m just a beginner in DirectX programming...&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5682776130825686229-5494779758440019457?l=www.codingmix.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/lOhiimVtiGAfGsfntOh3tODRW5g/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/lOhiimVtiGAfGsfntOh3tODRW5g/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/lOhiimVtiGAfGsfntOh3tODRW5g/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/lOhiimVtiGAfGsfntOh3tODRW5g/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/codingmix/Cplusplus/projects/~4/nzddpsqhEUU" height="1" width="1"/&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">12</thr:total><enclosure url="http://www.youtube.com/v/K8_UpmEfEnU?fs=1&amp;amp;hl=en_US&amp;amp;rel=0&amp;amp;color1=0x234900&amp;amp;color2=0x4e9e00" length="1079" type="application/x-shockwave-flash" /><media:content url="http://www.youtube.com/v/K8_UpmEfEnU?fs=1&amp;amp;hl=en_US&amp;amp;rel=0&amp;amp;color1=0x234900&amp;amp;color2=0x4e9e00" fileSize="1079" type="application/x-shockwave-flash" /><feedburner:origLink>http://www.codingmix.com/2010/12/2d-game-directx-snake-cplusplus.html</feedburner:origLink></item><item><title>Addition with extremely huge numbers in C++</title><link>http://feedproxy.google.com/~r/codingmix/Cplusplus/projects/~3/oqQeMw4Op6M/addition-with-extremely-huge-numbers-in.html</link><category>Cplusplus projects</category><category>Cplusplus</category><author>noreply@blogger.com (Csabi)</author><pubDate>Fri, 03 Dec 2010 11:40:56 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-5682776130825686229.post-7400027607781102421</guid><description>In C++ you the data type with the largest range is the &lt;i&gt;long long&lt;/i&gt;, with a maximum limit of ~19 digits. This means that you can`t make additions with numbers that result a larger number. I know this is a crazy thing, why would you need to calculate with such huge numbers ? I think never..., but the method is very useful and ingenious. &lt;div id="full-post"&gt;Here is my code:&lt;div class="code"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;#include &amp;lt;string&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;   string nr; &lt;br /&gt;   bool validnr = false;&lt;br /&gt;   while(!validnr){&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;Enter the first number: &amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      cin &amp;gt;&amp;gt; nr;&lt;br /&gt;      validnr = true;&lt;br /&gt;      &lt;span style="color:green;"&gt;//Check the number for invalid characters&lt;/span&gt;&lt;br /&gt;      for(int i=0;i&amp;lt;nr.length();i++){&lt;br /&gt;         if((int) nr[i] &amp;lt; 48 || (int) nr[i] &amp;gt; 57){&lt;br /&gt;            &lt;span style="color:green;"&gt;//48 is the ASCII code of number 0&lt;/span&gt;&lt;br /&gt;            &lt;span style="color:green;"&gt;//57 is the ASCII code of number 9&lt;/span&gt;&lt;br /&gt;            validnr = false; &lt;br /&gt;         }&lt;br /&gt;      }&lt;br /&gt;      if(!validnr){&lt;br /&gt;         cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;Please enter a valid number!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;   string nr2;&lt;br /&gt;   validnr = false;&lt;br /&gt;   &lt;span style="color:green;"&gt;//Do the same for the second number&lt;/span&gt;&lt;br /&gt;   while(!validnr){&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;Enter the second number: &amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      cin &amp;gt;&amp;gt; nr2;&lt;br /&gt;      validnr = true;&lt;br /&gt;      for(int i=0;i&amp;lt;nr2.length();i++){&lt;br /&gt;         if((int) nr2[i] &amp;lt; 48 || (int) nr2[i] &amp;gt; 57){&lt;br /&gt;            validnr = false; &lt;br /&gt;         }&lt;br /&gt;      }&lt;br /&gt;      if(!validnr){&lt;br /&gt;         cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;Please enter a valid number!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;   int digitsnr;&lt;br /&gt;   &lt;span style="color:green;"&gt;//Make the two digits to have the same size&lt;/span&gt;&lt;br /&gt;   &lt;span style="color:green;"&gt;//by adding 0`s in front of it&lt;/span&gt;&lt;br /&gt;   &lt;span style="color:green;"&gt;//This is important because we can`t add a digit with nothing&lt;/span&gt;&lt;br /&gt;   if(nr.length() &amp;gt; nr2.length()){&lt;br /&gt;      digitsnr = nr.length() - 1;&lt;br /&gt;      while(nr.length() != nr2.length()){&lt;br /&gt;         nr2 = &amp;quot;0&amp;quot; + nr2;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;   else{&lt;br /&gt;      digitsnr = nr2.length() - 1;&lt;br /&gt;      while(nr.length() != nr2.length()){&lt;br /&gt;         nr = &amp;quot;0&amp;quot; + nr;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;   string result;&lt;br /&gt;   char digit;&lt;br /&gt;   int remainder = 0; &lt;span style="color:green;"&gt;//If the result of two digits is more than 10&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:green;"&gt;//we need to add 1 to the following digit(e.g: 5 + 7 = 12, 12 is made from 2 digits&lt;/span&gt; &lt;br /&gt;   for(int i=digitsnr;i&amp;gt;=0;i--){ &lt;span style="color:green;"&gt;//Loop through each digit&lt;/span&gt;&lt;br /&gt;      if(remainder + nr[i] + nr2[i] - 96 &amp;gt; 9){&lt;br /&gt;         digit = remainder + nr[i] + nr2[i] - 48 - 10; &lt;span style="color:green;"&gt;//We are working with ASCII codes&lt;/span&gt;&lt;br /&gt;         result = digit + result;&lt;br /&gt;         remainder = 1; &lt;span style="color:green;"&gt;//We have to increase the following digit&lt;/span&gt;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         digit = remainder + nr[i] + nr2[i] - 48;&lt;br /&gt;         remainder = 0;&lt;br /&gt;         result = digit + result;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;   if(remainder){&lt;br /&gt;      result = &amp;quot;1&amp;quot; + result; &lt;br /&gt;     &lt;span style="color:green;"&gt;//Add the last remaining if required&lt;/span&gt;&lt;br /&gt;   }&lt;br /&gt;   cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;Result = &amp;quot; &amp;lt;&amp;lt; result &amp;lt;&amp;lt; endl;&lt;br /&gt;   system(&amp;quot;pause&amp;quot;);&lt;br /&gt;}&lt;/div&gt;The numbers are stored in strings, each character of the strings is checked to avoid wrong characters. Each character is checked using it`s ASCII code, (for example 0 has the 47 code number, 1 has the 49 code). After the numbers are validated the two numbers are added digit by digit starting from the last number:&lt;div class="code"&gt;123 + 56 = &lt;br /&gt;9&lt;br /&gt;79&lt;br /&gt;179&lt;/div&gt;Is that simple, but if something it`s not clear please don`t hesitate, ASK ME!&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5682776130825686229-7400027607781102421?l=www.codingmix.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/oRMerc0U-v4b5SAadXFZI8Q8F3o/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/oRMerc0U-v4b5SAadXFZI8Q8F3o/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/oRMerc0U-v4b5SAadXFZI8Q8F3o/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/oRMerc0U-v4b5SAadXFZI8Q8F3o/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/codingmix/Cplusplus/projects/~4/oqQeMw4Op6M" height="1" width="1"/&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">7</thr:total><feedburner:origLink>http://www.codingmix.com/2010/12/addition-with-extremely-huge-numbers-in.html</feedburner:origLink></item><item><title>A very simple C++ Tic Tac Toe game (v2)</title><link>http://feedproxy.google.com/~r/codingmix/Cplusplus/projects/~3/19jp-lgHtyc/very-simple-cplusplus-tic-tac-toe-game.html</link><category>Cplusplus projects</category><category>Cplusplus</category><author>noreply@blogger.com (Csabi)</author><pubDate>Fri, 01 Oct 2010 13:45:13 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-5682776130825686229.post-1501933207832143526</guid><description>This is my second C++ Tic Tac Toe game. This one has a shorter source-code because instead of using a variable for each square I`we used a two-dimensional array. With arrays is more simple to check if a square is available or to check for an eventual winner. &lt;div id="full-post"&gt;The rest of the game is almost the same as the &lt;a href="http://www.codingmix.com/2010/07/simples-cplusplus-tic-tac-toe-game-ever_24.html"&gt;first version&lt;/a&gt;, the only difference is that I`we used &lt;i&gt;char&lt;/i&gt;s instead of &lt;i&gt;int&lt;/i&gt;s to show the state of the squares.&lt;br /&gt;&lt;br /&gt;Required knowledge: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-4-variables.html"&gt;variables&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-5-inputoutput.html"&gt;input/output&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-6-ifelseelse-if_18.html"&gt;if/else/else if statements&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/07/cplusplus-lesson-8-while-loop-and-for.html"&gt;loop&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/09/cplusplus-lesson-9-arrays.html"&gt;arrays&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Here is my code:&lt;div class="code"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;   &lt;span style="color:green;"&gt;//This array of &lt;i&gt;char&lt;/i&gt;s represents the game board, and it holds the content of each square (x,o or empty)&lt;/span&gt;&lt;br /&gt;   char square[3][3] = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};&lt;br /&gt;   &lt;span style="color:green;"&gt;//The current player (x or o)&lt;/span&gt;&lt;br /&gt;   char turn = 'X';&lt;br /&gt;   &lt;span style="color:green;"&gt;//These variables will hold the number of the row and column selected by the players.&lt;/span&gt;&lt;br /&gt;   int row;&lt;br /&gt;   int column;&lt;br /&gt;   &lt;span style="color:green;"&gt;//The winner (x, o, t(if it`s a tie) or empty)&lt;/span&gt;&lt;br /&gt;   char winner = ' ';&lt;br /&gt;   &lt;span style="color:green;"&gt;//Required variables to check if a move is valid or not&lt;/span&gt;&lt;br /&gt;   bool validmove;&lt;br /&gt;   bool validrow;&lt;br /&gt;   bool validcolumn;&lt;br /&gt;   &lt;span style="color:green;"&gt;//The game loop`s again and again until the game is over&lt;/span&gt;&lt;br /&gt;   while(winner == ' '){&lt;br /&gt;      &lt;span style="color:green;"&gt;//Draw the board&lt;/span&gt;&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot; 1 2 3&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot; +---+---+---+&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      for(int i=1;i&amp;lt;4;i++){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot; | &amp;quot; &amp;lt;&amp;lt; square[i-1][0] &amp;lt;&amp;lt; &amp;quot; | &amp;quot; &amp;lt;&amp;lt; square[i-1][1] &amp;lt;&amp;lt; &amp;quot; | &amp;quot; &amp;lt;&amp;lt; square[i-1][2] &amp;lt;&amp;lt; &amp;quot; |&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot; +---+---+---+&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//Check for an eventual winner of for a tie&lt;/span&gt;&lt;br /&gt;      for(int i=0;i&amp;lt;3;i++){&lt;br /&gt;         if(square[i][0] == square[i][1] &amp;amp;&amp;amp; square[i][1] == square[i][2] &amp;amp;&amp;amp; square[i][0] != ' '){&lt;br /&gt;            winner = square[i][0];&lt;br /&gt;         }&lt;br /&gt;      }&lt;br /&gt;      for(int i=0;i&amp;lt;3;i++){&lt;br /&gt;         if(square[0][i] == square[1][i] &amp;amp;&amp;amp; square[1][i] == square[2][i] &amp;amp;&amp;amp; square[0][i] != ' '){&lt;br /&gt;            winner = square[0][i];&lt;br /&gt;         }&lt;br /&gt;      }&lt;br /&gt;      if(square[0][0] == square[1][1] &amp;amp;&amp;amp; square[1][1] == square[2][2] &amp;amp;&amp;amp; square[0][0] != ' '){&lt;br /&gt;         winner = square[0][0];&lt;br /&gt;      }&lt;br /&gt;      if(square[0][2] == square[1][1] &amp;amp;&amp;amp; square[1][1] == square[2][0] &amp;amp;&amp;amp; square[0][2] != ' '){&lt;br /&gt;         winner = square[0][2];&lt;br /&gt;      }&lt;br /&gt;      if(square[0][0] == square[0][1] &amp;amp;&amp;amp; square[0][1] == square[0][2] &amp;amp;&amp;amp; square[0][2] == square[1][0] &amp;amp;&amp;amp; square[1][0] == square[1][1] &amp;amp;&amp;amp; square[1][1] == square[1][2] &amp;amp;&amp;amp; square[1][2] == square[2][0] &amp;amp;&amp;amp; square[2][0] == square[2][1] &amp;amp;&amp;amp; square[2][1] == square[2][2] &amp;amp;&amp;amp; square[0][0] != ' '){&lt;br /&gt;         winner = 't';&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//If somebody won&lt;/span&gt;&lt;br /&gt;      if(winner == 'X' || winner == 'O'){&lt;br /&gt;         &lt;span style="color:green;"&gt;//Display congratulations message&lt;/span&gt;&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;Congratulations! Player &amp;quot;;&lt;br /&gt;         if(winner == 'X'){&lt;br /&gt;            cout &amp;lt;&amp;lt; 1;&lt;br /&gt;         }&lt;br /&gt;         else{&lt;br /&gt;            cout &amp;lt;&amp;lt; 2;&lt;br /&gt;         }&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; is the winner!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;         break;&lt;br /&gt;      }&lt;br /&gt;      else if(winner == 't'){&lt;br /&gt;         &lt;span style="color:green;"&gt;//Display a message if it`s tie&lt;/span&gt;&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;Tie!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;         break;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//If the game is not yet over show who`s the next player to move&lt;/span&gt;&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;Player &amp;quot;;&lt;br /&gt;      if(turn == 'X'){&lt;br /&gt;         cout &amp;lt;&amp;lt; 1;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; 2;&lt;br /&gt;      } &lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;'s turn:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      &lt;span style="color:green;"&gt;//Loop until the player selects a valid square&lt;/span&gt;&lt;br /&gt;      validmove = false;&lt;br /&gt;      while(!validmove){&lt;br /&gt;         validrow = false;&lt;br /&gt;         &lt;span style="color:green;"&gt;//Loop until the player selects a valid row&lt;/span&gt;&lt;br /&gt;         while(!validrow){&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot;Row: &amp;quot;;&lt;br /&gt;            cin &amp;gt;&amp;gt; row;&lt;br /&gt;            if(row == 1 || row == 2 || row == 3){&lt;br /&gt;               validrow = true;&lt;br /&gt;            }&lt;br /&gt;            else{&lt;br /&gt;               cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;Invalid row!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;            }&lt;br /&gt;         }&lt;br /&gt;         validcolumn = false;&lt;br /&gt;         &lt;span style="color:green;"&gt;//Loop until the player selects a valid column&lt;/span&gt;&lt;br /&gt;         while(!validcolumn){&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot;Column: &amp;quot;;&lt;br /&gt;            cin &amp;gt;&amp;gt; column;&lt;br /&gt;            if(column == 1 || column == 2 || column == 3){&lt;br /&gt;               validcolumn = true;&lt;br /&gt;            }&lt;br /&gt;            else{&lt;br /&gt;               cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;Invalid column!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;            }&lt;br /&gt;         }&lt;br /&gt;         &lt;span style="color:green;"&gt;//Change the turn to the next player&lt;/span&gt;&lt;br /&gt;         if(square[row-1][column-1] == ' '){&lt;br /&gt;            square[row-1][column-1] = turn;&lt;br /&gt;            validmove = true;&lt;br /&gt;            if(turn == 'X'){&lt;br /&gt;               turn = 'O';&lt;br /&gt;            }&lt;br /&gt;            else{&lt;br /&gt;               turn = 'X';&lt;br /&gt;            }&lt;br /&gt;         }&lt;br /&gt;         &lt;span style="color:green;"&gt;//If the selected square is occupied display a message and loop again&lt;/span&gt;&lt;br /&gt;         else{&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot;The selected square is occupied!&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;Select again:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;         }&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;   system(&amp;quot;pause&amp;quot;);&lt;br /&gt;}&lt;/div&gt;If something it`s not clear or you have any question just ask!&lt;br /&gt;If this is too advanced for you feel free to read my lessons or view the previous, simpler version of the game here: &lt;a href="http://www.codingmix.com/2010/07/simples-cplusplus-tic-tac-toe-game-ever_24.html"&gt;The simplest C++ Tic Tac Toe game ever&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5682776130825686229-1501933207832143526?l=www.codingmix.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/dRxuNrXP6v6E5wlLG4kP-XWWSVU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/dRxuNrXP6v6E5wlLG4kP-XWWSVU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/dRxuNrXP6v6E5wlLG4kP-XWWSVU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/dRxuNrXP6v6E5wlLG4kP-XWWSVU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/codingmix/Cplusplus/projects/~4/19jp-lgHtyc" height="1" width="1"/&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">3</thr:total><feedburner:origLink>http://www.codingmix.com/2010/10/very-simple-cplusplus-tic-tac-toe-game.html</feedburner:origLink></item><item><title>C++ Console Calculator v3</title><link>http://feedproxy.google.com/~r/codingmix/Cplusplus/projects/~3/k6ppczSGwP8/cplusplus-console-calculator-v3.html</link><category>Cplusplus projects</category><category>Cplusplus</category><author>noreply@blogger.com (Csabi)</author><pubDate>Sat, 16 Oct 2010 07:13:34 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-5682776130825686229.post-232818944587829691</guid><description>This is a tutorial about creating the third version of my C++ Calculator (console application). Here are the first and second versions: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-console-calculator-v1.html"&gt;Calculator v1&lt;/a&gt; and &lt;a href="http://www.codingmix.com/2010/06/cplusplus-console-calculator-v2.html"&gt;Calculator v2&lt;/a&gt;. The previous versions of this applications are closed after displaying a warning message when an invalid operation is selected. In this version this was improved: the programs shows an error message and it asks again and again until a valid operation is selected. This version is also a bit shorter because in some places I`we used arrays and loops.&lt;div id="full-post"&gt;&lt;br /&gt;Required knowledge: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-4-variables.html"&gt;variables&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-5-inputoutput.html"&gt;input/output&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-6-ifelseelse-if_18.html"&gt;if/else/else if statements&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-7-switchcase-statement.html"&gt;switch/case statements&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/07/cplusplus-lesson-8-while-loop-and-for.html"&gt;loop&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/09/cplusplus-lesson-9-arrays.html"&gt;arrays&lt;/a&gt;.&lt;br /&gt;Here is my code: &lt;div class="code"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;   cout &amp;lt;&amp;lt; &amp;quot;Enter the first number:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   int numone;&lt;br /&gt;   cin &amp;gt;&amp;gt; numone;&lt;br /&gt;   bool validselection = false;&lt;br /&gt;   char operation[] = {'*','/','+','-'};&lt;br /&gt;   int selectedoperation;&lt;br /&gt;   while(!validselection){&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;Select an operation:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      for(int i=0;i&amp;lt;4;i++){&lt;br /&gt;         cout &amp;lt;&amp;lt; i+1 &amp;lt;&amp;lt; &amp;quot;. &amp;quot; &amp;lt;&amp;lt; operation[i] &amp;lt;&amp;lt; endl;&lt;br /&gt;      }&lt;br /&gt;      cin &amp;gt;&amp;gt; selectedoperation;&lt;br /&gt;      if(selectedoperation == 1 || selectedoperation == 2 || selectedoperation == 3 || selectedoperation == 4){&lt;br /&gt;         validselection = true;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;Invalid selection!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;   cout &amp;lt;&amp;lt; &amp;quot;Enter the second number:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   int numtwo;&lt;br /&gt;   cin &amp;gt;&amp;gt; numtwo;&lt;br /&gt;   float result;&lt;br /&gt;   bool dividebyzero = false;&lt;br /&gt;   switch(selectedoperation){&lt;br /&gt;   case 1:&lt;br /&gt;      result = numone * numtwo;&lt;br /&gt;      break;&lt;br /&gt;   case 2:&lt;br /&gt;      if(numtwo == 0){&lt;br /&gt;         dividebyzero = true;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         result = (float)numone / numtwo;&lt;br /&gt;      }&lt;br /&gt;      break;&lt;br /&gt;   case 3:&lt;br /&gt;      result = numone + numtwo;&lt;br /&gt;      break;&lt;br /&gt;   case 4:&lt;br /&gt;      result = numone - numtwo;&lt;br /&gt;      break;&lt;br /&gt;   }&lt;br /&gt;   if(!dividebyzero){&lt;br /&gt;      cout &amp;lt;&amp;lt; numone &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; operation[selectedoperation - 1] &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; numtwo &amp;lt;&amp;lt; &amp;quot; = &amp;quot; &amp;lt;&amp;lt; result &amp;lt;&amp;lt; endl;&lt;br /&gt;   }&lt;br /&gt;   else{&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;Cannot divide by zero!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   }&lt;br /&gt;   system(&amp;quot;pause&amp;quot;);&lt;br /&gt;}&lt;/div&gt;This works almost the same way as the &lt;a href="http://www.codingmix.com/2010/06/cplusplus-console-calculator-v2.html"&gt;Calculator v2&lt;/a&gt;, the difference is that this version uses a loop and an array to display the available operations instead of multiple &lt;i&gt;cout&lt;/i&gt;`s and this whole part of code where the operation is asked is inside a loop and it loops until a valid operation is selected.&lt;br /&gt;This was the second version, but there will be more advanced ones, too.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5682776130825686229-232818944587829691?l=www.codingmix.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/TYmrEM6nzzXoa-oue8hQH_ILRHI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/TYmrEM6nzzXoa-oue8hQH_ILRHI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/TYmrEM6nzzXoa-oue8hQH_ILRHI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/TYmrEM6nzzXoa-oue8hQH_ILRHI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/codingmix/Cplusplus/projects/~4/k6ppczSGwP8" height="1" width="1"/&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total><feedburner:origLink>http://www.codingmix.com/2010/09/cplusplus-console-calculator-v3.html</feedburner:origLink></item><item><title>The simplest C++ Tic Tac Toe game ever</title><link>http://feedproxy.google.com/~r/codingmix/Cplusplus/projects/~3/JAtW6iOuxJQ/simples-cplusplus-tic-tac-toe-game-ever_24.html</link><category>Cplusplus projects</category><category>Cplusplus</category><author>noreply@blogger.com (Csabi)</author><pubDate>Sat, 02 Oct 2010 03:56:13 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-5682776130825686229.post-2061702966661096784</guid><description>This is the simplest C++ Tic Tac Toe game ever, it`s made using only the most basic statements: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-5-inputoutput.html"&gt;cout&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-5-inputoutput.html"&gt;cin&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-4-variables.html"&gt;variables&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-6-ifelseelse-if_18.html"&gt;if/else/else if&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-7-switchcase-statement.html"&gt;switch/case&lt;/a&gt; and &lt;a href="http://www.codingmix.com/2010/07/cplusplus-lesson-8-while-loop-and-for.html"&gt;loops&lt;/a&gt;&lt;div id="full-post"&gt;&lt;br /&gt;The game uses 9 variables, one for each square of the Tic Tac Toe game board, each of them can have 3 different value:&lt;div class="code"&gt;0 - free square&lt;br /&gt;1 - occupied by an x&lt;br /&gt;2 - occupied by a 0&lt;/div&gt;The board is redrawn after each move in a loop. The whole action of the game happens in this loop. First the board is checked for an eventual winner or for a tie. If the game is not over yet than it`s time to move. First the move is checked, if it`s not a valid move a message is displayed and the player needs to pick an another square, if the move is valid than the loop runs again, but this time is the other player`s turn.&lt;div class="code"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;   &lt;span style="color:green;"&gt;//These variables are holding the value of each square (0 = free, 1 = x, 2 = o)&lt;/span&gt;&lt;br /&gt;   int one = 0;&lt;br /&gt;   int two = 0;&lt;br /&gt;   int three = 0;&lt;br /&gt;   int four = 0;&lt;br /&gt;   int five = 0;&lt;br /&gt;   int six = 0;&lt;br /&gt;   int seven = 0;&lt;br /&gt;   int eight = 0;&lt;br /&gt;   int nine = 0;&lt;br /&gt;   &lt;span style="color:green;"&gt;//This holds the number of the current player (1 = player1(x), 2 = player2(o)&lt;/span&gt;&lt;br /&gt;   int turn = 1;&lt;br /&gt;   &lt;span style="color:green;"&gt;//These variables are holding the row and column number inputted by a player&lt;/span&gt;&lt;br /&gt;   int row;&lt;br /&gt;   int column;&lt;br /&gt;   &lt;span style="color:green;"&gt;//These variables will be used later to check if a move is valid or not&lt;/span&gt;&lt;br /&gt;   bool validmove;&lt;br /&gt;   bool validrow;&lt;br /&gt;   bool validcolumn;&lt;br /&gt;   &lt;span style="color:green;"&gt;//The bellow variable holds the number of the winner, when the value is not 0 means the game is over&lt;/span&gt;&lt;br /&gt;   int winner = 0;&lt;br /&gt;   &lt;span style="color:green;"&gt;//The game loops again and again until somebody wins or it`s tie (winner = 3)&lt;/span&gt;&lt;br /&gt;   while(winner == 0){&lt;br /&gt;      &lt;span style="color:green;"&gt;//Draw the board from simple +,- and | characters&lt;/span&gt;&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;1 2 3&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;+---+---+---+&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;1 | &amp;quot;;&lt;br /&gt;      if(one == 0){&lt;br /&gt;         &lt;span style="color:green;"&gt;//For each square the variable of that square is checked, and based on it`s value a character is written (0 - nothing, 1 = x, 2 = o)&lt;/span&gt;&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;|&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else if(one == 1){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;x |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;o |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      if(two == 0){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else if(two == 1){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; x |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; o |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      if(three == 0){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else if(three == 1){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; x |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; o |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;+---+---+---+&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;2 | &amp;quot;;&lt;br /&gt;      if(four == 0){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;|&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else if(four == 1){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;x |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;o |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      if(five == 0){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else if(five == 1){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; x |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; o |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      if(six == 0){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else if(six == 1){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; x |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; o |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;+---+---+---+&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;3 | &amp;quot;;&lt;br /&gt;      if(seven == 0){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;|&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else if(seven == 1){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;x |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;o |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      if(eight == 0){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else if(eight == 1){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; x |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; o |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      if(nine == 0){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else if(nine == 1){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; x |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot; o |&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;      cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;+---+---+---+&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; endl;&lt;br /&gt;      &lt;span style="color:green;"&gt;//The following several lines will check if the game is over or not&lt;/span&gt;&lt;br /&gt;      &lt;span style="color:green;"&gt;//Check`s the first line of the board for a winner combination (xxx or ooo)&lt;/span&gt;&lt;br /&gt;      if(one == two &amp;amp;&amp;amp; two == three &amp;amp;&amp;amp; one != 0){&lt;br /&gt;         &lt;span style="color:green;"&gt;//If the three squares have the same value and they are not free, then the winner is the player with the number from one of those squares&lt;/span&gt;&lt;br /&gt;         winner = one;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//Checks the second line&lt;/span&gt;&lt;br /&gt;      if(four == five &amp;amp;&amp;amp; five == six &amp;amp;&amp;amp; four != 0){&lt;br /&gt;         winner = four;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//Checks the third line&lt;/span&gt;&lt;br /&gt;      if(seven == eight &amp;amp;&amp;amp; eight == nine &amp;amp;&amp;amp; seven != 0){&lt;br /&gt;         winner = seven;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//Checks the first column&lt;/span&gt;&lt;br /&gt;      if(one == four &amp;amp;&amp;amp; four == seven &amp;amp;&amp;amp; one != 0){&lt;br /&gt;         winner = one;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//Checks the second column&lt;/span&gt;&lt;br /&gt;      if(two == five &amp;amp;&amp;amp; five == eight &amp;amp;&amp;amp; two != 0){&lt;br /&gt;         winner = two;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//Checks the third column&lt;/span&gt;&lt;br /&gt;      if(three == six &amp;amp;&amp;amp; six == nine &amp;amp;&amp;amp; three != 0){&lt;br /&gt;         winner = three;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//Checks the diagonal from top left to bottom right&lt;/span&gt;&lt;br /&gt;      if(one == five &amp;amp;&amp;amp; five == nine &amp;amp;&amp;amp; one != 0){&lt;br /&gt;         winner = one;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//Checks the diagonal from bottom left to top right&lt;/span&gt;&lt;br /&gt;      if(three == five &amp;amp;&amp;amp; five == seven &amp;amp;&amp;amp; three != 0){&lt;br /&gt;         winner = three;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//If nobody wins this turn it checks to see if the board is full or not&lt;/span&gt;&lt;br /&gt;      if(one != 0 &amp;amp;&amp;amp; two != 0 &amp;amp;&amp;amp; three != 0 &amp;amp;&amp;amp; four != 0 &amp;amp;&amp;amp; five != 0 &amp;amp;&amp;amp; six != 0 &amp;amp;&amp;amp; seven != 0 &amp;amp;&amp;amp; eight != 0 &amp;amp;&amp;amp; nine != 0 &amp;amp;&amp;amp; winner == 0){&lt;br /&gt;         &lt;span style="color:green;"&gt;//If the table is full, then it`s tie&lt;/span&gt;&lt;br /&gt;         winner = 3;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;//If somebody won or it`s a tie shows a message and break out from the loop - the game is over&lt;/span&gt;&lt;br /&gt;      if(winner == 1 || winner == 2){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;Congratulations! Player &amp;quot; &amp;lt;&amp;lt; winner &amp;lt;&amp;lt; &amp;quot; is the winner!&amp;quot;;&lt;br /&gt;         break;&lt;br /&gt;      }&lt;br /&gt;      else if(winner == 3){&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;Tie!&amp;quot;;&lt;br /&gt;         break;&lt;br /&gt;      }&lt;br /&gt;      &lt;span style="color:green;"&gt;If the game is nor over yet than it writes a "Player x`s turn:" - where x is the number of the player (1 or 2)&lt;/span&gt;&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;Player &amp;quot; &amp;lt;&amp;lt; turn &amp;lt;&amp;lt; &amp;quot;'s turn:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      validmove = false;&lt;br /&gt;      &lt;span style="color:green;"&gt;//Loops until the player makes a valid move&lt;/span&gt;&lt;br /&gt;      while(!validmove){&lt;br /&gt;         validrow = false;&lt;br /&gt;         &lt;span style="color:green;"&gt;//Loops until the player chooses a valid row&lt;/span&gt;&lt;br /&gt;         while(!validrow){&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot;Row: &amp;quot;;&lt;br /&gt;            cin &amp;gt;&amp;gt; row;&lt;br /&gt;            if(row == 1 || row == 2 || row == 3){&lt;br /&gt;               validrow = true;&lt;br /&gt;            }&lt;br /&gt;            else{&lt;br /&gt;               cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;Invalid row!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;            }&lt;br /&gt;         }&lt;br /&gt;         validcolumn = false;&lt;br /&gt;         &lt;span style="color:green;"&gt;//Loops until the player chooses a valid column&lt;/span&gt;&lt;br /&gt;         while(!validcolumn){&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot;Column: &amp;quot;;&lt;br /&gt;            cin &amp;gt;&amp;gt; column;&lt;br /&gt;            if(column == 1 || column == 2 || column == 3){&lt;br /&gt;               validcolumn = true;&lt;br /&gt;            }&lt;br /&gt;            else{&lt;br /&gt;               cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;Invalid column!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;            }&lt;br /&gt;         }&lt;br /&gt;         &lt;span style="color:green;"&gt;//After the user selects a valid row and column number the changes are applied to the game board and the variable of the selected square if it`s not occupied and it changes the value of the &lt;i&gt;validmove&lt;/i&gt; variable from &lt;i&gt;false&lt;/i&gt; to &lt;i&gt;true&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;         switch(row){&lt;br /&gt;         case 1:&lt;br /&gt;            if(column == 1){&lt;br /&gt;               if(one == 0){&lt;br /&gt;                  one = turn;&lt;br /&gt;                  validmove = true;&lt;br /&gt;               }&lt;br /&gt;            }&lt;br /&gt;            else if(column == 2){&lt;br /&gt;               if(two == 0){&lt;br /&gt;                  two = turn;&lt;br /&gt;                  validmove = true;&lt;br /&gt;               }&lt;br /&gt;            }&lt;br /&gt;            else{&lt;br /&gt;               if(three == 0){&lt;br /&gt;                  three = turn;&lt;br /&gt;                  validmove = true;&lt;br /&gt;               }&lt;br /&gt;            }&lt;br /&gt;            break;&lt;br /&gt;         case 2:&lt;br /&gt;            if(column == 1){&lt;br /&gt;               if(four == 0){&lt;br /&gt;                  four = turn;&lt;br /&gt;                  validmove = true;&lt;br /&gt;               }&lt;br /&gt;            }&lt;br /&gt;            else if(column == 2){&lt;br /&gt;               if(five == 0){&lt;br /&gt;                  five = turn;&lt;br /&gt;                  validmove = true;&lt;br /&gt;               }&lt;br /&gt;            }&lt;br /&gt;            else{&lt;br /&gt;               if(six == 0){&lt;br /&gt;                  six = turn;&lt;br /&gt;                  validmove = true;&lt;br /&gt;               }&lt;br /&gt;            }&lt;br /&gt;            break;&lt;br /&gt;         case 3:&lt;br /&gt;            if(column == 1){&lt;br /&gt;               if(seven == 0){&lt;br /&gt;                  seven = turn;&lt;br /&gt;                  validmove = true;&lt;br /&gt;               }&lt;br /&gt;            }&lt;br /&gt;            else if(column == 2){&lt;br /&gt;               if(eight == 0){&lt;br /&gt;                  eight = turn;&lt;br /&gt;                  validmove = true;&lt;br /&gt;               }&lt;br /&gt;            }&lt;br /&gt;            else{&lt;br /&gt;               if(nine == 0){&lt;br /&gt;                  nine = turn;&lt;br /&gt;                  validmove = true;&lt;br /&gt;               }&lt;br /&gt;            }&lt;br /&gt;         }&lt;br /&gt;         &lt;span style="color:green;"&gt;//If &lt;i&gt;validmove&lt;/i&gt; is still &lt;i&gt;false&lt;/i&gt;, then the selected square is occupied and a message is displayed&lt;/span&gt;&lt;br /&gt;         if(!validmove){&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot;The sellected square is occupied!&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;Select again:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;         }&lt;br /&gt;         else{&lt;br /&gt;            &lt;span style="color:green;"&gt;//Else if it was a valid move, then it`s the other player`s turn&lt;/span&gt;&lt;br /&gt;            if(turn == 1){&lt;br /&gt;               turn = 2;&lt;br /&gt;            }&lt;br /&gt;            else{&lt;br /&gt;               turn = 1;&lt;br /&gt;            }&lt;br /&gt;         }&lt;br /&gt;         cout &amp;lt;&amp;lt; endl;&lt;br /&gt;      }&lt;br /&gt;   &lt;span style="color:green;"&gt;//The loop start`s over&lt;/span&gt;&lt;br /&gt;   }   &lt;br /&gt;   system(&amp;quot;pause&amp;quot;);&lt;br /&gt;}&lt;/div&gt;The second version of this Tic Tac Toe game is ready with minor changes in the code, using a wee bit harder but more effective techniques: &lt;a href="http://www.codingmix.com/2010/10/very-simple-cplusplus-tic-tac-toe-game.html"&gt;Tic Tac Toe v2&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5682776130825686229-2061702966661096784?l=www.codingmix.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/6o0VkdxB6u7ulmtu5tB8HXJt3ZQ/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/6o0VkdxB6u7ulmtu5tB8HXJt3ZQ/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/6o0VkdxB6u7ulmtu5tB8HXJt3ZQ/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/6o0VkdxB6u7ulmtu5tB8HXJt3ZQ/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/codingmix/Cplusplus/projects/~4/JAtW6iOuxJQ" height="1" width="1"/&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">3</thr:total><feedburner:origLink>http://www.codingmix.com/2010/07/simples-cplusplus-tic-tac-toe-game-ever_24.html</feedburner:origLink></item><item><title>C++ Console Calculator v2</title><link>http://feedproxy.google.com/~r/codingmix/Cplusplus/projects/~3/BYJtJ-cY0wc/cplusplus-console-calculator-v2.html</link><category>Cplusplus projects</category><category>Cplusplus</category><author>noreply@blogger.com (Csabi)</author><pubDate>Sat, 02 Oct 2010 03:50:06 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-5682776130825686229.post-8527173715013262902</guid><description>This is a tutorial about creating the second version of my C++ Calculator (console application). Here is the first version: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-console-calculator-v1.html"&gt;Calculator v1&lt;/a&gt;. The only difference between the first and second versions is that this one uses switch/case statement in some places instead of multiple if else statements.&lt;div id="full-post"&gt;&lt;br /&gt;Required knowledge: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-4-variables.html"&gt;variables&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-5-inputoutput.html"&gt;input/output&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-6-ifelseelse-if_18.html"&gt;if/else/else if statements&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-7-switchcase-statement.html"&gt;switch/case statements&lt;/a&gt;.&lt;br /&gt;Here is my code: &lt;div class="code"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;   cout &amp;lt;&amp;lt; &amp;quot;Enter the first number:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   int numone;&lt;br /&gt;   cin &amp;gt;&amp;gt; numone;&lt;br /&gt;   cout &amp;lt;&amp;lt; &amp;quot;Select an operation:&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;1. *&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;2. /&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;3. +&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;4. -&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   int operation;&lt;br /&gt;   cin &amp;gt;&amp;gt; operation;&lt;br /&gt;   if(operation == 1 || operation == 2 || operation == 3 || operation == 4){&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;Enter the second number:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      int numtwo;&lt;br /&gt;      cin &amp;gt;&amp;gt; numtwo;&lt;br /&gt;      float result;&lt;br /&gt;      bool dividebyzero = false;&lt;br /&gt;      switch(operation){&lt;br /&gt;      case 1:&lt;br /&gt;         result = numone * numtwo;&lt;br /&gt;         break;&lt;br /&gt;      case 2:&lt;br /&gt;         if(numtwo == 0){&lt;br /&gt;            dividebyzero = true;&lt;br /&gt;         }&lt;br /&gt;         else{&lt;br /&gt;            result = (float) numone / numtwo;&lt;br /&gt;         }&lt;br /&gt;         break;&lt;br /&gt;      case 3:&lt;br /&gt;         result = numone + numtwo;&lt;br /&gt;         break;&lt;br /&gt;      case 4:&lt;br /&gt;         result = numone - numtwo;&lt;br /&gt;         break;&lt;br /&gt;      }&lt;br /&gt;      if(!dividebyzero){&lt;br /&gt;         cout &amp;lt;&amp;lt; numone;&lt;br /&gt;         switch(operation){&lt;br /&gt;         case 1:&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot; * &amp;quot;;&lt;br /&gt;            break;&lt;br /&gt;         case 2:&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot; / &amp;quot;;&lt;br /&gt;            break;&lt;br /&gt;         case 3:&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot; + &amp;quot;;&lt;br /&gt;            break;&lt;br /&gt;         case 4:&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot; - &amp;quot;;&lt;br /&gt;         }&lt;br /&gt;         cout &amp;lt;&amp;lt; numtwo &amp;lt;&amp;lt; &amp;quot; = &amp;quot; &amp;lt;&amp;lt; result &amp;lt;&amp;lt; endl;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;Cannot divide by zero!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;   else{&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;Invalid selection!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   }&lt;br /&gt;   system(&amp;quot;pause&amp;quot;);&lt;br /&gt;}&lt;/div&gt;After the program reads the first number it check if the selection is valid. If not it will display and error message. If will ask for a second number and based on the operation value (using switch/case statement) it will make the mathematical operation. If the selected operation is 2 (division), the program will check the value of the second number. If the second number is 0 it will set the dividebezero boolean to true. If dividebyzero is true at the end, the program will show and error message instead of the result.&lt;br /&gt;&lt;br /&gt;This was the second version,here is the third one: &lt;a href="http://www.codingmix.com/2010/09/cplusplus-console-calculator-v3.html"&gt;Calculator v3&lt;/a&gt;. Here is a link to the first version: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-console-calculator-v1.html"&gt;Calculator v1&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5682776130825686229-8527173715013262902?l=www.codingmix.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/oUNPQwrwlWSwvdgj1UxFtL3STOc/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/oUNPQwrwlWSwvdgj1UxFtL3STOc/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/oUNPQwrwlWSwvdgj1UxFtL3STOc/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/oUNPQwrwlWSwvdgj1UxFtL3STOc/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/codingmix/Cplusplus/projects/~4/BYJtJ-cY0wc" height="1" width="1"/&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total><feedburner:origLink>http://www.codingmix.com/2010/06/cplusplus-console-calculator-v2.html</feedburner:origLink></item><item><title>C++ Console Calculator v1</title><link>http://feedproxy.google.com/~r/codingmix/Cplusplus/projects/~3/9Ts5nCTeLwE/cplusplus-console-calculator-v1.html</link><category>Cplusplus projects</category><category>Cplusplus</category><author>noreply@blogger.com (Csabi)</author><pubDate>Thu, 02 Sep 2010 11:02:07 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-5682776130825686229.post-1419408886631161130</guid><description>This is a tutorial about how to make a C++ console Calculator (version 1) which first asks for a number, than shows a list of mathematical operations, than asks for an another number, than writes out the result. Here is an image of how it will look:&lt;div id="full-post"&gt;&lt;a href="http://3.bp.blogspot.com/_Ok7mVUxPcsk/TByRI5zpt_I/AAAAAAAAAPs/u8ndtCz_Kio/s1600/Calculator+v1.jpg"&gt;&lt;img style="display:block; margin:5px auto 5px; text-align:center;width: 545px; height: 275px;" src="http://3.bp.blogspot.com/_Ok7mVUxPcsk/TByRI5zpt_I/AAAAAAAAAPs/u8ndtCz_Kio/s1600/Calculator+v1.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5484418028372867058" /&gt;&lt;/a&gt;Required knowledge: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-4-variables.html"&gt;variables&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-5-inputoutput.html"&gt;input/output&lt;/a&gt;, &lt;a href="http://www.codingmix.com/2010/06/cplusplus-lesson-6-ifelseelse-if_18.html"&gt;if/else/else if statements&lt;/a&gt;.&lt;br /&gt;Here is my code:&lt;div class="code"&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main(){&lt;br /&gt;   cout &amp;lt;&amp;lt; &amp;quot;Enter the first number:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   int numone;&lt;br /&gt;   cin &amp;gt;&amp;gt; numone;&lt;br /&gt;   cout &amp;lt;&amp;lt; &amp;quot;Select an operation:&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;1. *&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;2. /&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;3. +&amp;quot; &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;4. -&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   int operation;&lt;br /&gt;   cin &amp;gt;&amp;gt; operation;&lt;br /&gt;   if(operation == 1 || operation == 2 || operation == 3 || operation == 4){&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;Enter the second number:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;      int numtwo;&lt;br /&gt;      cin &amp;gt;&amp;gt; numtwo;&lt;br /&gt;      float result;&lt;br /&gt;      bool dividebyzero = false;&lt;br /&gt;      if(operation == 1){&lt;br /&gt;         result = numone * numtwo;&lt;br /&gt;      }&lt;br /&gt;      else if(operation == 2){&lt;br /&gt;         if(numtwo != 0){&lt;br /&gt;            result = (float) numone / numtwo;&lt;br /&gt;         }&lt;br /&gt;         else{&lt;br /&gt;            dividebyzero = true;&lt;br /&gt;         }&lt;br /&gt;      }&lt;br /&gt;      else if(operation == 3){&lt;br /&gt;         result = numone + numtwo;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         result = numone - numtwo;&lt;br /&gt;      }&lt;br /&gt;      if(!dividebyzero){&lt;br /&gt;         cout &amp;lt;&amp;lt; numone;&lt;br /&gt;         if(operation == 1){&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot; * &amp;quot;;&lt;br /&gt;         }&lt;br /&gt;         else if(operation == 2){&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot; / &amp;quot;;&lt;br /&gt;         }&lt;br /&gt;         else if(operation == 3){&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot; + &amp;quot;;&lt;br /&gt;         }&lt;br /&gt;         else{&lt;br /&gt;            cout &amp;lt;&amp;lt; &amp;quot; - &amp;quot;;&lt;br /&gt;         }&lt;br /&gt;         cout &amp;lt;&amp;lt; numtwo &amp;lt;&amp;lt; &amp;quot; = &amp;quot; &amp;lt;&amp;lt; result &amp;lt;&amp;lt; endl;&lt;br /&gt;      }&lt;br /&gt;      else{&lt;br /&gt;         cout &amp;lt;&amp;lt; &amp;quot;Cannot divide by zero!&amp;quot;;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;   else{&lt;br /&gt;      cout &amp;lt;&amp;lt; &amp;quot;Invalid selection!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;   }&lt;br /&gt;   system(&amp;quot;pause&amp;quot;);&lt;br /&gt;}&lt;/div&gt;After the program reads the first number and the operation number it checks if the operator value is valid (1,2,3 or 4), if it`s an invalid number selection (else than 1,2,3,4) it will display an error message. If the operation number is valid then the program asks for the second number, and calculates the result based on the operation. If the selected operation is 2 (division) than it checks the second numbers value. If the second number is 0 than it sets the dividebyzero boolean to true. Finally the program checks for the value of dividebyzero. If it`s true than it shows and error message, else it writes the first number, than the operation (based on the operation number) , than the second number , than an equal sign (=) and the result.&lt;br /&gt;&lt;br /&gt;This is a very simple program, it uses only the most basic statements of C++. The second version is ready, check it out: &lt;a href="http://www.codingmix.com/2010/06/cplusplus-console-calculator-v2.html"&gt;Calculator v2&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5682776130825686229-1419408886631161130?l=www.codingmix.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/KTBYrq_juqhklHOHkjabqRfL8ck/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/KTBYrq_juqhklHOHkjabqRfL8ck/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/KTBYrq_juqhklHOHkjabqRfL8ck/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/KTBYrq_juqhklHOHkjabqRfL8ck/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/codingmix/Cplusplus/projects/~4/9Ts5nCTeLwE" height="1" width="1"/&gt;</description><media:thumbnail url="http://3.bp.blogspot.com/_Ok7mVUxPcsk/TByRI5zpt_I/AAAAAAAAAPs/u8ndtCz_Kio/s72-c/Calculator+v1.jpg" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total><feedburner:origLink>http://www.codingmix.com/2010/06/cplusplus-console-calculator-v1.html</feedburner:origLink></item><media:rating>nonadult</media:rating></channel></rss>

