Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <conio.h>
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <time.h>
- #include <windows.h>
- using namespace std;
- int map[16] = {0};
- int step = 0;
- char k;
- void draw(){ //透過重新繪製畫面,表現出數字的移動
- system( "cls" );
- cout << "steps:" << step << "\n";
- for( int i = 0 ; i < 16 ; i++ ){
- if( map[i] == 0 ){
- cout << "\t";
- }
- else{
- cout << map[i] << "\t";
- }
- if( i % 4 == 3 ){
- cout << "\n";
- }
- }
- }
- void random(){ //每一次移動都會產生一個新的數字
- int x = 0;
- while( x < 1 ){
- int index = rand() % 16;
- int number = rand() % 5;
- if( number ==2 || number == 4 ){
- if(map[index] ==0){
- map[ index ] = number;
- x+=1;
- }
- }
- }
- }
- void init(){ //初始化規格跟一開始的2個數字
- int x = 0;
- while( x < 3 ){
- int index = rand() % 16;
- int number = rand() % 5;
- if( number ==2 || number == 4 ){
- map[ index ] = number;
- x+=1;
- }
- }
- }
- //push f、s、e、d : 當玩家執行上 (e)下 (d)左 (s)右 (f)鍵時,數字就會照2048的規則跑
- void push_f(){
- for(int i = 3;i <=15 ; i +=4){
- for(int j = i-1 ; j>=i-3 ; j-- ){
- int k = j;
- while(1){
- //如果右邊的數字為0,就跟它對調。調著調著就會把數字全部往右移
- if(map[k+1]==0){
- int m_f = map[k+1];
- map[k+1] = map[k];
- map[k] = m_f;
- }
- //如果數字一樣就加起來
- else if(map[k+1]==map[k]){
- map[k+1]= map[k+1]*2;
- map[k]=0;
- }
- k=k+1;
- if(k ==i)break;
- }
- }
- }
- }
- push_s(){
- for(int i = 0;i <=12 ; i +=4){
- for(int j = i+1 ; j<=i+3 ; j++ ){
- int k = j;
- while(1){
- //如果左邊的數字為0,就跟它對調。調著調著就會把數字全部往左移
- if(map[k-1]==0){
- int m_s = map[k-1];
- map[k-1] = map[k];
- map[k] = m_s;
- }
- //如果數字一樣就加起來
- else if(map[k-1]==map[k]){
- map[k-1]= map[k-1]*2;
- map[k]=0;
- }
- k=k-1;
- if(k ==i)break;
- }
- }
- }
- }
- void push_e(){
- for(int i = 0;i <=3 ; i ++){
- for(int j = i+4 ; j<=i+12 ; j+=4 ){
- int k = j;
- while(1){
- //如果上面的數字為0,就跟它對調。調著調著就會把數字全部往上移
- if(map[k-4]==0){
- int m_e = map[k-4];
- map[k-4] = map[k];
- map[k] = m_e;
- }
- //如果數字一樣就加起來
- else if(map[k-4]==map[k]){
- map[k-4]= map[k-4]*2;
- map[k]=0;
- }
- k=k-4;
- if(k ==i)break;
- }
- }
- }
- }
- void push_d(){
- for(int i = 12 ; i <=15 ; i ++){
- for(int j = i-4 ; j>=i-12 ; j-=4 ){
- int k = j;
- while(1){
- //如果下面的數字為0,就跟它對調。調著調著就會把數字全部往下移
- if(map[k+4]==0){
- int m_d = map[k+4];
- map[k+4] = map[k];
- map[k] = m_d;
- }
- //如果數字一樣就加起來
- else if(map[k+4]==map[k]){
- map[k+4]= map[k+4]*2;
- map[k]=0;
- }
- k=k+4;
- if(k ==i)break;
- }
- }
- }
- }
- int main(){
- init();
- draw();
- while(1){
- if( kbhit() ){
- k = _getch();
- if( k == 'q' ){
- draw();
- break;
- }
- if( k == 'f' ){
- push_f();
- random();
- step++;
- }
- if( k == 's' ){
- push_s();
- random();
- step++;
- }
- if( k =='e' ){
- push_e();
- random();
- step++;
- }
- if( k == 'd' ){
- push_d();
- random();
- step++;
- }
- bool finished = false;
- int lefted = 16;
- for( int i = 0 ; i <= 15 ; i++ ){
- if( map[i] == 16 ){
- finished = true;
- }
- if( map[i] != 0 ){
- lefted--;
- }
- }
- if( finished ){
- draw();
- cout << "congras, mission completed!";
- break;
- }
- else if( lefted == 0 ){
- draw();
- cout << "sorry, you failed!";
- }
- else{
- draw();
- Sleep(100);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment