Click here to Skip to main content
15,887,683 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How to add additional character in multiple places of an array string Pin
Richard MacCutchan7-May-15 6:06
mveRichard MacCutchan7-May-15 6:06 
AnswerRe: How to add additional character in multiple places of an array string Pin
CPallini7-May-15 6:08
mveCPallini7-May-15 6:08 
QuestionEnumerating windows on desktop (VC++) Pin
Krishnakumartg4-May-15 0:19
Krishnakumartg4-May-15 0:19 
SuggestionRe: Enumerating windows on desktop (VC++) Pin
David Crow4-May-15 3:28
David Crow4-May-15 3:28 
GeneralRe: Enumerating windows on desktop (VC++) Pin
Krishnakumartg4-May-15 19:59
Krishnakumartg4-May-15 19:59 
QuestionHow to get Browser AddOns list using C++.? Pin
mbatra313-May-15 20:24
mbatra313-May-15 20:24 
AnswerRe: How to get Browser AddOns list using C++.? Pin
Richard MacCutchan3-May-15 21:27
mveRichard MacCutchan3-May-15 21:27 
Questionconvert this c++ code to c programing Pin
Member 116584881-May-15 21:53
Member 116584881-May-15 21:53 
#include <time.h>
#include <iostream>
#include <string>
#include <iomanip>
/*http://rosettacode.org/wiki/2048*/



typedef unsigned int uint;
using namespace std;
enum movDir { UP, DOWN, LEFT, RIGHT };




class tile
{
public:
tile() : val( 0 ), blocked( false ) {}
uint val;
bool blocked;
};




class g2048
{
public:
g2048() : done( false ), win( false ), moved( true ), score( 0 ) {}
void loop()
{
addTile();
while( true )
{
if( moved ) addTile();
drawBoard();
if( done ) break;
waitKey();
}
string s = "Game Over!";
if( win ) s = "You've made it!";
cout << s << endl << endl;
}
private:




void drawBoard()

{
system( "cls" );
cout << "SCORE: " << score << endl << endl;
for( int y = 0; y < 4; y++ )
{
cout << "+------+------+------+------+" << endl << "| ";
for( int x = 0; x < 4; x++ )
{
if( !board[x][y].val ) cout << setw( 4 ) << " ";
else cout << setw( 4 ) << board[x][y].val;
cout << " | ";
}
cout << endl;
}
cout << "+------+------+------+------+" << endl << endl;
}





void waitKey()

{
moved = false; char c;
cout << "(W)Up (S)Down (A)Left (D)Right "; cin >> c; c &= 0x5F;
switch( c )
{
case 'W': move( UP );break;
case 'A': move( LEFT ); break;
case 'S': move( DOWN ); break;
case 'D': move( RIGHT );
}
for( int y = 0; y < 4; y++ )
for( int x = 0; x < 4; x++ )
board[x][y].blocked = false;
}
void addTile()
{
for( int y = 0; y < 4; y++ )
for( int x = 0; x < 4; x++ )
if( !board[x][y].val )
{
uint a, b;
do
{ a = rand() % 4; b = rand() % 4; }
while( board[a][b].val );

int s = rand() % 100;
if( s > 89 ) board[a][b].val = 4;
else board[a][b].val = 2;
if( canMove() ) return;
}
done = true;
}





bool canMove()
{
for( int y = 0; y < 4; y++ )
for( int x = 0; x < 4; x++ )
if( !board[x][y].val ) return true;

for( int y = 0; y < 4; y++ )
for( int x = 0; x < 4; x++ )
{
if( testAdd( x + 1, y, board[x][y].val ) ) return true;
if( testAdd( x - 1, y, board[x][y].val ) ) return true;
if( testAdd( x, y + 1, board[x][y].val ) ) return true;
if( testAdd( x, y - 1, board[x][y].val ) ) return true;
}
return false;
}





bool testAdd( int x, int y, uint v )
{
if( x < 0 || x > 3 || y < 0 || y > 3 ) return false;
return board[x][y].val == v;
}




void moveVert( int x, int y, int d )
{
if( board[x][y + d].val && board[x][y + d].val == board[x][y].val && !board[x][y].blocked && !board[x][y + d].blocked )
{
board[x][y].val = 0;
board[x][y + d].val *= 2;
score += board[x][y + d].val;
board[x][y + d].blocked = true;
moved = true;
}
else if( !board[x][y + d].val && board[x][y].val )
{
board[x][y + d].val = board[x][y].val;
board[x][y].val = 0;
moved = true;
}
if( d > 0 ) { if( y + d < 3 ) moveVert( x, y + d, 1 ); }
else { if( y + d > 0 ) moveVert( x, y + d, -1 ); }
}




void moveHori( int x, int y, int d )
{
if( board[x + d][y].val && board[x + d][y].val == board[x][y].val && !board[x][y].blocked && !board[x + d][y].blocked )
{
board[x][y].val = 0;
board[x + d][y].val *= 2;
score += board[x + d][y].val;
board[x + d][y].blocked = true;
moved = true;
}
else if( !board[x + d][y].val && board[x][y].val )
{
board[x + d][y].val = board[x][y].val;
board[x][y].val = 0;
moved = true;
}
if( d > 0 ) { if( x + d < 3 ) moveHori( x + d, y, 1 ); }
else { if( x + d > 0 ) moveHori( x + d, y, -1 ); }
}




void move( movDir d )
{
switch( d )
{
case UP:
for( int x = 0; x < 4; x++ )
{
int y = 1;
while( y < 4 )
{ if( board[x][y].val ) moveVert( x, y, -1 ); y++;}
}
break;
case DOWN:
for( int x = 0; x < 4; x++ )
{
int y = 2;
while( y >= 0 )
{ if( board[x][y].val ) moveVert( x, y, 1 ); y--;}
}
break;
case LEFT:
for( int y = 0; y < 4; y++ )
{
int x = 1;
while( x < 4 )
{ if( board[x][y].val ) moveHori( x, y, -1 ); x++;}
}
break;
case RIGHT:
for( int y = 0; y < 4; y++ )
{
int x = 2;
while( x >= 0 )
{ if( board[x][y].val ) moveHori( x, y, 1 ); x--;}
}
}
}


tile board[4][4];
bool win, done, moved;
uint score;
};




int main( int argc, char* argv[] )
{
srand( static_cast<uint>( time( NULL ) ) );
g2048 g; g.loop();
return system( "pause" );
}
AnswerRe: convert this c++ code to c programing Pin
Richard MacCutchan1-May-15 22:45
mveRichard MacCutchan1-May-15 22:45 
AnswerRe: convert this c++ code to c programing Pin
David Crow2-May-15 16:47
David Crow2-May-15 16:47 
AnswerRe: convert this c++ code to c programing Pin
Munchies_Matt5-May-15 2:18
Munchies_Matt5-May-15 2:18 
QuestionStrange Compiler behavior Pin
ForNow1-May-15 12:08
ForNow1-May-15 12:08 
QuestionRe: Strange Compiler behavior Pin
Richard MacCutchan1-May-15 22:43
mveRichard MacCutchan1-May-15 22:43 
AnswerRe: Strange Compiler behavior Pin
ForNow2-May-15 15:12
ForNow2-May-15 15:12 
GeneralRe: Strange Compiler behavior Pin
Richard MacCutchan2-May-15 20:50
mveRichard MacCutchan2-May-15 20:50 
GeneralRe: Strange Compiler behavior Pin
ForNow2-May-15 21:05
ForNow2-May-15 21:05 
QuestionCWinThread::CAsyncSocket::Create issues Pin
ForNow28-Apr-15 4:52
ForNow28-Apr-15 4:52 
AnswerCode works in CWInThread constructer ? Pin
ForNow29-Apr-15 6:03
ForNow29-Apr-15 6:03 
GeneralRe: Code works in CWInThread constructer ? Pin
Albert Holguin1-May-15 4:40
professionalAlbert Holguin1-May-15 4:40 
GeneralRe: Code works in CWInThread constructer ? Pin
ForNow1-May-15 6:16
ForNow1-May-15 6:16 
Questionswscanf_s White Space Help Pin
Jesuaw28-Apr-15 4:09
Jesuaw28-Apr-15 4:09 
AnswerRe: swscanf_s White Space Help Pin
k505428-Apr-15 5:41
mvek505428-Apr-15 5:41 
GeneralRe: swscanf_s White Space Help Pin
Jesuaw28-Apr-15 6:50
Jesuaw28-Apr-15 6:50 
GeneralRe: swscanf_s White Space Help Pin
k505428-Apr-15 7:05
mvek505428-Apr-15 7:05 
GeneralRe: swscanf_s White Space Help Pin
Jesuaw28-Apr-15 7:49
Jesuaw28-Apr-15 7:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.